fix(grid): show comments for joined query results
This commit is contained in:
1 parent
d5f18034f9
commit
64e5e88c27
11 files changed
+867
-9
No files matched your search
+369
@@ -0,0 +1,369 @@
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const executeMulti = vi.fn();
|
||||
const executeQuery = vi.fn();
|
||||
const analyzeEditableQueryEditability = vi.fn();
|
||||
const getColumns = vi.fn();
|
||||
const listIndexes = vi.fn();
|
||||
const listObjects = vi.fn();
|
||||
const getConnectionConfig = vi.fn();
|
||||
const lookupLocalCompletionTables = vi.fn();
|
||||
const buildSortedQuerySql = vi.fn();
|
||||
const buildDataGridCountSql = vi.fn();
|
||||
const prepareQueryPaginationExecutionPlan = vi.fn(async (options) => ({
|
||||
sqlToExecute: options.sql,
|
||||
pageSql: undefined,
|
||||
pageLimit: undefined,
|
||||
pageOffset: undefined,
|
||||
countSql: undefined,
|
||||
useAgentResultSession: false,
|
||||
}));
|
||||
const editorSettings = {
|
||||
pageSize: 100,
|
||||
autoCalculateTotalRows: false,
|
||||
};
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
analyzeEditableQueryEditability,
|
||||
buildDataGridCountSql,
|
||||
buildSortedQuerySql,
|
||||
closeClientConnectionSession: vi.fn().mockResolvedValue(undefined),
|
||||
closeQuerySession: vi.fn().mockResolvedValue(undefined),
|
||||
executeMulti,
|
||||
executeQuery,
|
||||
getColumns,
|
||||
listIndexes,
|
||||
listObjects,
|
||||
prepareQueryPaginationExecutionPlan,
|
||||
saveOpenTabsState: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
useConnectionStore: () => ({
|
||||
ensureConnected: vi.fn().mockResolvedValue(undefined),
|
||||
getConfig: getConnectionConfig,
|
||||
lookupLocalCompletionTables,
|
||||
recordConnectionLostError: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/settingsStore", () => ({
|
||||
useSettingsStore: () => ({
|
||||
editorSettings,
|
||||
}),
|
||||
}));
|
||||
|
||||
function column(name: string, comment: string | null, isPrimaryKey = false) {
|
||||
return { name, data_type: "varchar", is_nullable: true, column_default: null, is_primary_key: isPrimaryKey, extra: null, comment };
|
||||
}
|
||||
|
||||
const ordersColumns = [column("id", "订单ID", true), column("user_id", "下单用户"), column("amount", "订单金额")];
|
||||
const usersColumns = [column("id", "用户ID", true), column("name", "用户名")];
|
||||
|
||||
/** SELECT a.id, a.user_id, b.id, b.name FROM orders a JOIN users b ON a.user_id = b.id */
|
||||
const joinAnalysis = {
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
tableAlias: "a",
|
||||
selectStar: false,
|
||||
columns: [
|
||||
{ sourceName: "id", sourceKey: "a", resultName: "id", expression: "a.id" },
|
||||
{ sourceName: "user_id", sourceKey: "a", resultName: "user_id", expression: "a.user_id" },
|
||||
{ sourceName: "id", sourceKey: "b", resultName: "id_1", expression: "b.id" },
|
||||
{ sourceName: "name", sourceKey: "b", resultName: "name", expression: "b.name" },
|
||||
],
|
||||
sources: [
|
||||
{ key: "a", tableName: "orders", alias: "a" },
|
||||
{ key: "b", tableName: "users", alias: "b" },
|
||||
],
|
||||
multiSource: true,
|
||||
allowInsertDelete: false,
|
||||
},
|
||||
};
|
||||
|
||||
describe("queryStore multi-source result column comments", () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
const { clearTableMetadataCache } = await import("@/lib/metadata/tableMetadataCache");
|
||||
clearTableMetadataCache();
|
||||
setActivePinia(createPinia());
|
||||
getConnectionConfig.mockReturnValue({ id: "mysql-1", name: "MySQL", db_type: "mysql", database: "app", query_timeout_secs: 30 });
|
||||
getColumns.mockImplementation(async (_connectionId: string, _database: string, _schema: string, table: string) => (table === "orders" ? ordersColumns : usersColumns));
|
||||
listIndexes.mockResolvedValue([]);
|
||||
listObjects.mockResolvedValue([]);
|
||||
lookupLocalCompletionTables.mockReturnValue([]);
|
||||
analyzeEditableQueryEditability.mockResolvedValue(joinAnalysis);
|
||||
buildSortedQuerySql.mockResolvedValue({ ok: true, sql: `${"SELECT *"} ORDER BY 1` });
|
||||
buildDataGridCountSql.mockResolvedValue("SELECT COUNT(*) FROM `orders`");
|
||||
executeQuery.mockResolvedValue({
|
||||
columns: ["row_count"],
|
||||
rows: [[0]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
});
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id", "user_id", "id_1", "name"],
|
||||
rows: [[1, 100, 7, "Alice"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
expect(listObjects).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stores per-ordinal comments and source identity for every JOIN source column", async () => {
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
await store.executeTabSql(tabId, "SELECT a.id, a.user_id, b.id, b.name FROM orders a JOIN users b ON a.user_id = b.id");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.resultColumnComments).toBeDefined());
|
||||
|
||||
// Multi-source results stay non-editable: no single tableMeta.
|
||||
expect(tab.tableMeta).toBeUndefined();
|
||||
expect(tab.queryEditabilityReason).toBe("complex-source");
|
||||
expect(tab.querySourceColumns).toBeUndefined();
|
||||
|
||||
// Comments are indexed by result ordinal, so the second `id` (users.id)
|
||||
// keeps its own comment instead of first-source-wins on the name.
|
||||
expect(tab.resultColumnComments).toEqual(["订单ID", "下单用户", "用户ID", "用户名"]);
|
||||
|
||||
// The display mapping carries source identity per ordinal.
|
||||
expect(tab.queryDisplaySourceColumns).toEqual([
|
||||
{ sourceKey: "a", sourceColumn: "id" },
|
||||
{ sourceKey: "a", sourceColumn: "user_id" },
|
||||
{ sourceKey: "b", sourceColumn: "id" },
|
||||
{ sourceKey: "b", sourceColumn: "name" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("resolves a uniquely qualified unqualified alias back to its physical column", async () => {
|
||||
analyzeEditableQueryEditability.mockResolvedValue({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
tableAlias: "a",
|
||||
selectStar: false,
|
||||
columns: [
|
||||
{ sourceName: "id", sourceKey: "a", resultName: "id", expression: "a.id" },
|
||||
{ sourceName: "name", sourceKey: undefined, resultName: "username", expression: "name" },
|
||||
],
|
||||
sources: [
|
||||
{ key: "a", tableName: "orders", alias: "a" },
|
||||
{ key: "b", tableName: "users", alias: "b" },
|
||||
],
|
||||
multiSource: true,
|
||||
allowInsertDelete: false,
|
||||
},
|
||||
});
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id", "username"],
|
||||
rows: [[1, "Alice"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
// `name` exists only in users across the joined sources, so the unqualified
|
||||
// alias resolves back to users.name despite the binder never seeing a key.
|
||||
await store.executeTabSql(tabId, "SELECT a.id, name AS username FROM orders a JOIN users b ON a.user_id = b.id");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.resultColumnComments).toBeDefined());
|
||||
expect(tab.resultColumnComments).toEqual(["订单ID", "用户名"]);
|
||||
expect(tab.queryDisplaySourceColumns).toEqual([
|
||||
{ sourceKey: "a", sourceColumn: "id" },
|
||||
{ sourceKey: "b", sourceColumn: "name" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns no comment for an ambiguous unqualified column shared by several sources", async () => {
|
||||
analyzeEditableQueryEditability.mockResolvedValue({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
tableAlias: "a",
|
||||
selectStar: false,
|
||||
columns: [{ sourceName: "id", sourceKey: undefined, resultName: "id", expression: "id" }],
|
||||
sources: [
|
||||
{ key: "a", tableName: "orders", alias: "a" },
|
||||
{ key: "b", tableName: "users", alias: "b" },
|
||||
],
|
||||
multiSource: true,
|
||||
allowInsertDelete: false,
|
||||
},
|
||||
});
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id"],
|
||||
rows: [[1]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
// Both orders and users expose `id`; the unqualified reference is
|
||||
// ambiguous, so the result column must not claim either table's comment.
|
||||
await store.executeTabSql(tabId, "SELECT id FROM orders a JOIN users b ON a.user_id = b.id");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.resultColumnComments).toBeDefined());
|
||||
expect(tab.resultColumnComments).toEqual([undefined]);
|
||||
expect(tab.queryDisplaySourceColumns).toEqual([undefined]);
|
||||
});
|
||||
|
||||
it("resolves quoted mixed-case identifiers exactly, per the database's rules", async () => {
|
||||
getConnectionConfig.mockReturnValue({ id: "pg-1", name: "PostgreSQL", db_type: "postgres", database: "app", query_timeout_secs: 30 });
|
||||
const quotedOrders = [column("id", "订单ID", true), column("ID", "大写ID"), column("user_id", "下单用户")];
|
||||
const quotedUsers = [column("id", "用户ID", true), column("Name", "大写Name")];
|
||||
getColumns.mockImplementation(async (_connectionId: string, _database: string, _schema: string, table: string) => (table === "orders" ? quotedOrders : quotedUsers));
|
||||
analyzeEditableQueryEditability.mockResolvedValue({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
tableAlias: "a",
|
||||
selectStar: false,
|
||||
columns: [
|
||||
{ sourceName: "id", sourceNameQuoted: false, sourceKey: "a", resultName: "id", expression: "a.id" },
|
||||
{ sourceName: "ID", sourceNameQuoted: true, sourceKey: "a", resultName: "ID", expression: 'a."ID"' },
|
||||
{ sourceName: "Name", sourceNameQuoted: true, sourceKey: "b", resultName: "Name", expression: 'b."Name"' },
|
||||
],
|
||||
sources: [
|
||||
{ key: "a", tableName: "orders", alias: "a" },
|
||||
{ key: "b", tableName: "users", alias: "b" },
|
||||
],
|
||||
multiSource: true,
|
||||
allowInsertDelete: false,
|
||||
},
|
||||
});
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id", "ID", "Name"],
|
||||
rows: [[1, 9, "Alice"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("pg-1", "app", "Query");
|
||||
|
||||
await store.executeTabSql(tabId, 'SELECT a.id, a."ID", b."Name" FROM orders a JOIN users b ON a.user_id = b.id');
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.resultColumnComments).toBeDefined());
|
||||
|
||||
// Quoted `"ID"` stays distinct from unquoted `id`; the global lower-casing
|
||||
// of the previous map would have collapsed them.
|
||||
expect(tab.resultColumnComments).toEqual(["订单ID", "大写ID", "大写Name"]);
|
||||
expect(tab.queryDisplaySourceColumns).toEqual([
|
||||
{ sourceKey: "a", sourceColumn: "id" },
|
||||
{ sourceKey: "a", sourceColumn: "ID" },
|
||||
{ sourceKey: "b", sourceColumn: "Name" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps duplicate result column names resolved in projection order", async () => {
|
||||
analyzeEditableQueryEditability.mockResolvedValue({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
tableAlias: "a",
|
||||
selectStar: false,
|
||||
columns: [
|
||||
{ sourceName: "id", sourceKey: "a", resultName: "id", expression: "a.id" },
|
||||
{ sourceName: "id", sourceKey: "b", resultName: "id", expression: "b.id" },
|
||||
],
|
||||
sources: [
|
||||
{ key: "a", tableName: "orders", alias: "a" },
|
||||
{ key: "b", tableName: "users", alias: "b" },
|
||||
],
|
||||
multiSource: true,
|
||||
allowInsertDelete: false,
|
||||
},
|
||||
});
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id", "id_1"],
|
||||
rows: [[1, 7]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
// The driver renames the second `id` to `id_1`; ordinal mapping still
|
||||
// resolves column #1 to users.id instead of failing to match by name.
|
||||
await store.executeTabSql(tabId, "SELECT a.id, b.id FROM orders a JOIN users b ON a.user_id = b.id");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.resultColumnComments).toBeDefined());
|
||||
expect(tab.resultColumnComments).toEqual(["订单ID", "用户ID"]);
|
||||
expect(tab.queryDisplaySourceColumns).toEqual([
|
||||
{ sourceKey: "a", sourceColumn: "id" },
|
||||
{ sourceKey: "b", sourceColumn: "id" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps single-source results free of multi-source comment fields", async () => {
|
||||
analyzeEditableQueryEditability.mockResolvedValue({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "orders",
|
||||
selectStar: false,
|
||||
columns: [
|
||||
{ sourceName: "id", sourceKey: "orders:0", resultName: "id", expression: "id" },
|
||||
{ sourceName: "amount", sourceKey: "orders:0", resultName: "amount", expression: "amount" },
|
||||
],
|
||||
},
|
||||
});
|
||||
getColumns.mockResolvedValue(ordersColumns);
|
||||
executeMulti.mockResolvedValue([
|
||||
{
|
||||
columns: ["id", "amount"],
|
||||
rows: [[1, 9.99]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
await store.executeTabSql(tabId, "SELECT id, amount FROM orders");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.tableMeta?.tableName).toBe("orders"));
|
||||
expect(tab.resultColumnComments).toBeUndefined();
|
||||
expect(tab.queryDisplaySourceColumns).toBeUndefined();
|
||||
expect(tab.querySourceColumns).toEqual(["id", "amount"]);
|
||||
});
|
||||
});
|
||||
@@ -117,6 +117,20 @@ describe("queryStore switchTab", () => {
|
||||
expect(queryStore.tabs[1].catalog).toBe("paimon_catalog");
|
||||
});
|
||||
|
||||
it("clones result column comments when duplicating a query tab", () => {
|
||||
const queryStore = useQueryStore();
|
||||
const tabId = queryStore.createTab("pg-1", "app", undefined, "query", undefined, "SELECT 1");
|
||||
const original = queryStore.tabs.find((tab) => tab.id === tabId)!;
|
||||
original.resultColumnComments = ["identifier", "display name"];
|
||||
|
||||
queryStore.duplicateTab(tabId);
|
||||
|
||||
const duplicate = queryStore.tabs[1];
|
||||
expect(Array.isArray(duplicate.resultColumnComments)).toBe(true);
|
||||
expect(duplicate.resultColumnComments).toEqual(original.resultColumnComments);
|
||||
expect(duplicate.resultColumnComments).not.toBe(original.resultColumnComments);
|
||||
});
|
||||
|
||||
it("switches catalog and database as one query context", () => {
|
||||
const queryStore = useQueryStore();
|
||||
const tabId = queryStore.createTab("sr-1", "internal_db", undefined, "query");
|
||||
|
||||
@@ -2,12 +2,12 @@ import { defineStore } from "pinia";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
import { computed, markRaw, nextTick, onScopeDispose, reactive, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { BatchSqlExecution, ConnectionConfig, DatabaseType, IndexInfo, ObjectBrowserViewport, QueryResult, QueryTab, TableInfoTab, TableStructureEditorTarget } from "@/types/database";
|
||||
import type { BatchSqlExecution, ConnectionConfig, DatabaseType, IndexInfo, ObjectBrowserViewport, QueryResult, QueryResultSourceColumnRef, QueryTab, TableInfoTab, TableStructureEditorTarget } from "@/types/database";
|
||||
import { orderPinnedFirst } from "@/lib/app/pinnedItems";
|
||||
import { canCancelQueryExecution } from "@/lib/sql/queryExecutionState";
|
||||
import { buildExplainSql, parseExplainResult, parseDamengExplainText, parseOracleExplainText, sqlServerExplainResult, type BuildExplainSqlResult } from "@/lib/diagram/explainPlan";
|
||||
import { mysqlExplainCompatibilityHint } from "@/lib/diagram/mysqlExplainCompatibility";
|
||||
import { allEditableColumnsWriteable, allPrimaryKeysPresent, analyzeEditableQueryEditability, resolveMetadataColumnName, sourceColumnsForResult, type EditableQueryInfo, type EditableQuerySource } from "@/lib/sql/sqlAnalysis";
|
||||
import { allEditableColumnsWriteable, allPrimaryKeysPresent, analyzeEditableQueryEditability, resolveMetadataColumnName, resolveSourceColumnsByOrdinal, sourceColumnsForResult, type EditableQueryInfo, type EditableQuerySource } from "@/lib/sql/sqlAnalysis";
|
||||
import { buildQueryWithHiddenPrimaryKeys, hiddenResultColumnIndexes, type HiddenPrimaryKeyProjection } from "@/lib/sql/editableQueryHiddenKeys";
|
||||
import { ACTIVE_TAB_STORAGE_KEY, OPEN_TABS_STORAGE_KEY, restoreOpenTabsPayload, restoreOpenTabsState, serializeOpenTabs } from "@/lib/app/openTabsPersistence";
|
||||
import {
|
||||
@@ -965,6 +965,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
tab.resultEstimatedBytes = undefined;
|
||||
tab.queryAnalysis = undefined;
|
||||
tab.querySourceColumns = undefined;
|
||||
tab.resultColumnComments = undefined;
|
||||
tab.queryDisplaySourceColumns = undefined;
|
||||
tab.queryEditabilityReason = undefined;
|
||||
tab.mongoEditTarget = undefined;
|
||||
if (tab.mode === "query") tab.tableMeta = undefined;
|
||||
@@ -1016,6 +1018,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
run.resultEstimatedBytes = undefined;
|
||||
run.queryAnalysis = undefined;
|
||||
run.querySourceColumns = undefined;
|
||||
run.resultColumnComments = undefined;
|
||||
run.queryDisplaySourceColumns = undefined;
|
||||
run.queryEditabilityReason = undefined;
|
||||
run.mongoEditTarget = undefined;
|
||||
run.tableMeta = undefined;
|
||||
@@ -1058,6 +1062,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
tab.resultEvicted = run.resultEvicted;
|
||||
tab.queryAnalysis = run.queryAnalysis;
|
||||
tab.querySourceColumns = run.querySourceColumns;
|
||||
tab.resultColumnComments = run.resultColumnComments;
|
||||
tab.queryDisplaySourceColumns = run.queryDisplaySourceColumns;
|
||||
tab.queryEditabilityReason = run.queryEditabilityReason;
|
||||
tab.mongoEditTarget = run.mongoEditTarget;
|
||||
tab.tableMeta = run.tableMeta;
|
||||
@@ -1217,6 +1223,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
activeResultRunId: run.id,
|
||||
queryAnalysis: run.queryAnalysis,
|
||||
querySourceColumns: run.querySourceColumns,
|
||||
resultColumnComments: run.resultColumnComments,
|
||||
queryDisplaySourceColumns: run.queryDisplaySourceColumns,
|
||||
queryEditabilityReason: run.queryEditabilityReason,
|
||||
tableMeta: run.tableMeta,
|
||||
resultPageSql: run.resultPageSql,
|
||||
@@ -1296,6 +1304,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
resultEvicted: tab.resultEvicted,
|
||||
queryAnalysis: tab.queryAnalysis,
|
||||
querySourceColumns: tab.querySourceColumns,
|
||||
resultColumnComments: tab.resultColumnComments,
|
||||
queryDisplaySourceColumns: tab.queryDisplaySourceColumns,
|
||||
queryEditabilityReason: tab.queryEditabilityReason,
|
||||
mongoEditTarget: tab.mongoEditTarget,
|
||||
tableMeta: tab.tableMeta,
|
||||
@@ -1394,6 +1404,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
resultEvicted: tab.resultEvicted,
|
||||
queryAnalysis: tab.queryAnalysis,
|
||||
querySourceColumns: tab.querySourceColumns,
|
||||
resultColumnComments: tab.resultColumnComments,
|
||||
queryDisplaySourceColumns: tab.queryDisplaySourceColumns,
|
||||
queryEditabilityReason: tab.queryEditabilityReason,
|
||||
mongoEditTarget: tab.mongoEditTarget,
|
||||
tableMeta: tab.tableMeta,
|
||||
@@ -1633,7 +1645,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
const tab: QueryTab = {
|
||||
id,
|
||||
title: title || `query_${tabs.value.length + 1}`,
|
||||
customTitle: mode === "query" && !!title ? true : undefined,
|
||||
customTitle: mode === "query" && title ? true : undefined,
|
||||
forceWordWrap: options.forceWordWrap,
|
||||
connectionId,
|
||||
database,
|
||||
@@ -2685,6 +2697,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
tableMeta: original.tableMeta ? { ...original.tableMeta, columns: [...original.tableMeta.columns], primaryKeys: [...original.tableMeta.primaryKeys] } : undefined,
|
||||
queryAnalysis: original.queryAnalysis ? { ...original.queryAnalysis, sources: original.queryAnalysis.sources?.map((source) => ({ ...source })), columns: original.queryAnalysis.columns.map((c) => ({ ...c })) } : undefined,
|
||||
querySourceColumns: original.querySourceColumns ? [...original.querySourceColumns] : undefined,
|
||||
resultColumnComments: original.resultColumnComments ? [...original.resultColumnComments] : undefined,
|
||||
queryDisplaySourceColumns: original.queryDisplaySourceColumns ? [...original.queryDisplaySourceColumns] : undefined,
|
||||
queryEditabilityReason: original.queryEditabilityReason,
|
||||
resultEvicted: undefined,
|
||||
whereInput: original.whereInput,
|
||||
@@ -3380,7 +3394,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
return producedResult;
|
||||
}
|
||||
|
||||
type QueryMetadataPatch = Pick<QueryTab, "queryAnalysis" | "querySourceColumns" | "queryEditabilityReason" | "tableMeta">;
|
||||
type QueryMetadataPatch = Pick<QueryTab, "queryAnalysis" | "querySourceColumns" | "queryEditabilityReason" | "tableMeta" | "resultColumnComments" | "queryDisplaySourceColumns">;
|
||||
|
||||
type LoadedEditableSource = {
|
||||
source: EditableQuerySource;
|
||||
@@ -3395,6 +3409,38 @@ export const useQueryStore = defineStore("query", () => {
|
||||
writeSchema?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve multi-source result columns (by projection ordinal) back to exactly
|
||||
* one base column per source, then surface the resolved column comments and a
|
||||
* display-only result->source mapping. Reuses the same database-aware binder
|
||||
* as the editability analysis, so `name AS username` (uniquely resolvable
|
||||
* unqualified alias) maps back to its physical column and quoted mixed-case
|
||||
* identifiers keep exact casing. Ambiguous or unresolved columns yield
|
||||
* `undefined` (no comment) instead of first-source-wins on a shared name.
|
||||
*/
|
||||
function resolveMultiSourceColumnInfo(dbType: string, analysis: EditableQueryInfo, resultColumns: string[], loadedSources: LoadedEditableSource[]): { comments: Array<string | undefined>; mapping: Array<QueryResultSourceColumnRef | undefined> } {
|
||||
const refs = resolveSourceColumnsByOrdinal(
|
||||
dbType,
|
||||
analysis,
|
||||
loadedSources.map((loaded) => ({ source: loaded.source, columns: loaded.tableMeta.columns })),
|
||||
resultColumns.length,
|
||||
);
|
||||
const comments: Array<string | undefined> = [];
|
||||
const mapping: Array<QueryResultSourceColumnRef | undefined> = [];
|
||||
for (const ref of refs) {
|
||||
if (!ref) {
|
||||
comments.push(undefined);
|
||||
mapping.push(undefined);
|
||||
continue;
|
||||
}
|
||||
const loaded = loadedSources.find((entry) => entry.source.key === ref.sourceKey);
|
||||
const comment = loaded?.tableMeta.columns.find((column) => column.name === ref.sourceColumn)?.comment?.trim();
|
||||
comments.push(comment || undefined);
|
||||
mapping.push(ref);
|
||||
}
|
||||
return { comments, mapping };
|
||||
}
|
||||
|
||||
function canInsertIntoEditableQuerySource(tab: QueryTab, databaseType: DatabaseType | undefined, loaded: LoadedEditableSource, sourceColumns: readonly (string | undefined)[] | undefined): boolean {
|
||||
if (!canInsertTableRows(databaseType) || !sourceColumns?.length || !sourceColumns.every(Boolean)) return false;
|
||||
const knownTableType =
|
||||
@@ -3418,6 +3464,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
tab.queryEditabilityReason = patch.queryEditabilityReason;
|
||||
tab.mongoEditTarget = undefined;
|
||||
tab.tableMeta = patch.tableMeta;
|
||||
tab.resultColumnComments = patch.resultColumnComments;
|
||||
tab.queryDisplaySourceColumns = patch.queryDisplaySourceColumns;
|
||||
}
|
||||
|
||||
function resolveEditableSourceMetadataTarget(tab: QueryTab, analysis: EditableQueryInfo, source: EditableQuerySource, conn: ConnectionConfig | undefined, dbType: string, executionDatabase: string): EditableSourceMetadataTarget {
|
||||
@@ -3733,12 +3781,20 @@ export const useQueryStore = defineStore("query", () => {
|
||||
};
|
||||
}
|
||||
|
||||
// Multi-source results cannot carry a single tableMeta, but every source
|
||||
// table's metadata is already loaded. Surface per-ordinal column comments
|
||||
// and a display-only result->source mapping so the data grid can still
|
||||
// show comments for joined results (fixes #2129 / #6352).
|
||||
const multiSourceInfo = loadedSources.length > 1 ? resolveMultiSourceColumnInfo(dbType, analysis, tab.result.columns, loadedSources) : undefined;
|
||||
|
||||
if (candidates.length === 0) {
|
||||
return {
|
||||
queryAnalysis: undefined,
|
||||
querySourceColumns: undefined,
|
||||
queryEditabilityReason: loadedSources.some((loaded) => loaded.tableMeta.primaryKeys.length > 0) ? "primary-key-not-returned" : "no-primary-key",
|
||||
tableMeta: undefined,
|
||||
resultColumnComments: multiSourceInfo?.comments,
|
||||
queryDisplaySourceColumns: multiSourceInfo?.mapping,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3748,6 +3804,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
querySourceColumns: undefined,
|
||||
queryEditabilityReason: "complex-source",
|
||||
tableMeta: undefined,
|
||||
resultColumnComments: multiSourceInfo?.comments,
|
||||
queryDisplaySourceColumns: multiSourceInfo?.mapping,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3763,6 +3821,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
querySourceColumns: target.sourceColumns,
|
||||
queryEditabilityReason: undefined,
|
||||
tableMeta: target.tableMeta,
|
||||
resultColumnComments: multiSourceInfo?.comments,
|
||||
queryDisplaySourceColumns: multiSourceInfo?.mapping,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("[DBX] ERROR fetching columns for query metadata:", err);
|
||||
@@ -4079,6 +4139,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
touchResult(current);
|
||||
current.queryAnalysis = undefined;
|
||||
current.querySourceColumns = undefined;
|
||||
current.resultColumnComments = undefined;
|
||||
current.queryDisplaySourceColumns = undefined;
|
||||
current.queryEditabilityReason = undefined;
|
||||
current.mongoEditTarget = undefined;
|
||||
current.tableMeta = undefined;
|
||||
@@ -4461,6 +4523,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
touchResult(current);
|
||||
current.queryAnalysis = undefined;
|
||||
current.querySourceColumns = undefined;
|
||||
current.resultColumnComments = undefined;
|
||||
current.queryDisplaySourceColumns = undefined;
|
||||
current.queryEditabilityReason = undefined;
|
||||
current.mongoEditTarget = mongoCommands.length === 1 ? mongoEditTarget : undefined;
|
||||
current.tableMeta = undefined;
|
||||
@@ -4526,6 +4590,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
touchResult(current);
|
||||
current.queryAnalysis = undefined;
|
||||
current.querySourceColumns = undefined;
|
||||
current.resultColumnComments = undefined;
|
||||
current.queryDisplaySourceColumns = undefined;
|
||||
current.queryEditabilityReason = undefined;
|
||||
current.mongoEditTarget = undefined;
|
||||
current.tableMeta = undefined;
|
||||
@@ -4908,6 +4974,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
}
|
||||
current.queryAnalysis = undefined;
|
||||
current.querySourceColumns = undefined;
|
||||
current.resultColumnComments = undefined;
|
||||
current.queryDisplaySourceColumns = undefined;
|
||||
current.queryEditabilityReason = undefined;
|
||||
current.mongoEditTarget = undefined;
|
||||
if (current.mode !== "data") current.tableMeta = undefined;
|
||||
@@ -5388,6 +5456,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
touchResult(tab, Date.now(), { reuseEstimatedBytes: true });
|
||||
tab.queryAnalysis = undefined;
|
||||
tab.querySourceColumns = undefined;
|
||||
tab.resultColumnComments = undefined;
|
||||
tab.queryDisplaySourceColumns = undefined;
|
||||
tab.queryEditabilityReason = undefined;
|
||||
tab.mongoEditTarget = undefined;
|
||||
syncActiveResultRunFromDisplayed(tab);
|
||||
@@ -5511,6 +5581,8 @@ export const useQueryStore = defineStore("query", () => {
|
||||
|
||||
tab.queryAnalysis = snapshot.queryAnalysis;
|
||||
tab.querySourceColumns = snapshot.querySourceColumns;
|
||||
tab.resultColumnComments = snapshot.resultColumnComments;
|
||||
tab.queryDisplaySourceColumns = snapshot.queryDisplaySourceColumns;
|
||||
tab.queryEditabilityReason = snapshot.queryEditabilityReason;
|
||||
tab.mongoEditTarget = snapshot.mongoEditTarget;
|
||||
// Data tab 的结果快照可能早于最近一次结构变更。已持有真实元数据时,
|
||||
|
||||
Reference in new issue
Block a user