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
@@ -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