feat(query): add batch result actions
This commit is contained in:
18 files changed
+668
-18
No files matched your search
@@ -188,6 +188,60 @@ describe("queryStore table data refresh", () => {
|
||||
expect(store.tabs.find((tab) => tab.id === archiveTabId)?.result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not build batch INSERT metadata while data-tab metadata is pending", async () => {
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("pg-1", "app", "users", "data", "public");
|
||||
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
|
||||
const result = {
|
||||
columns: ["id"],
|
||||
rows: [[1]],
|
||||
sourceStatement: "SELECT * FROM public.users",
|
||||
};
|
||||
tab.tableMeta = {
|
||||
database: "app",
|
||||
schema: "public",
|
||||
tableName: "users",
|
||||
tableType: "TABLE",
|
||||
columns: [],
|
||||
primaryKeys: [],
|
||||
};
|
||||
tab.tableMetaPending = true;
|
||||
tab.result = result;
|
||||
tab.results = [result];
|
||||
|
||||
await expect(store.resolveResultMetadataForBatch(tabId, tab.result!)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("maps active aliased query results to their source columns", async () => {
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("pg-1", "app", "Query", "query");
|
||||
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
|
||||
const result = {
|
||||
columns: ["id"],
|
||||
rows: [["root"]],
|
||||
sourceStatement: "SELECT name AS id FROM users",
|
||||
};
|
||||
tab.tableMeta = {
|
||||
database: "app",
|
||||
tableName: "users",
|
||||
tableType: "TABLE",
|
||||
columns: [
|
||||
{ name: "id", data_type: "integer", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
|
||||
{ name: "name", data_type: "text", is_nullable: false, column_default: null, is_primary_key: false, extra: null },
|
||||
],
|
||||
primaryKeys: ["id"],
|
||||
};
|
||||
tab.result = result;
|
||||
tab.results = [result];
|
||||
|
||||
const metadata = await store.resolveResultMetadataForBatch(tabId, tab.result!);
|
||||
|
||||
expect(metadata?.queryAnalysis?.selectStar).toBe(false);
|
||||
expect(metadata?.querySourceColumns).toEqual(["name"]);
|
||||
});
|
||||
|
||||
it("uses JDBC ResultSet offset pagination for Caché data tabs", async () => {
|
||||
mocks.getConnectionConfig.mockReturnValue({
|
||||
id: "cache-1",
|
||||
|
||||
@@ -8755,6 +8755,61 @@ export const useQueryStore = defineStore("query", () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveResultMetadataForBatch(id: string, result: QueryResult): Promise<QueryMetadataPatch | undefined> {
|
||||
const tab = findExecutionTab(id);
|
||||
if (!tab || !result.sourceStatement) return undefined;
|
||||
// Resolve on a detached context: checking a non-active result must never
|
||||
// replace the visible grid or apply its metadata to another result.
|
||||
const location = queryResultExecutionLocation(tab);
|
||||
const connection = useConnectionStore().getConfig(location.connectionId);
|
||||
const databaseType = effectiveDatabaseTypeForConnection(connection);
|
||||
if (result === tab.result) {
|
||||
if (tab.mode === "data" && (tab.tableMetaPending || !tab.tableMeta?.columns.length)) return undefined;
|
||||
const tableMeta = tab.mode === "data" ? tableMetaForDataTab(tab) : tab.tableMeta;
|
||||
const structure = analyzeSelectStructureForDisplay(result.sourceStatement);
|
||||
const sameTable = structure?.tableName && tableMeta?.tableName && structure.tableName.toLowerCase() === tableMeta.tableName.toLowerCase() && (!structure.schema || !tableMeta.schema || structure.schema.toLowerCase() === tableMeta.schema.toLowerCase());
|
||||
if (!sameTable || !tableMeta?.tableName || !canInsertTableRows(databaseType) || tableMeta.tableType?.toUpperCase().includes("VIEW")) return undefined;
|
||||
const metadataNames = tableMeta.columns.map((column) => column.name);
|
||||
const querySourceColumns = structure.selectStar
|
||||
? result.columns.map((column) => resolveMetadataColumnName(databaseType ?? "", column, undefined, metadataNames))
|
||||
: structure.columns.length === result.columns.length
|
||||
? structure.columns.map((column) => {
|
||||
if (!column.sourceName || (column.sourceQualifier && !column.sourceKey)) return undefined;
|
||||
return resolveMetadataColumnName(databaseType ?? "", column.sourceName, column.sourceNameQuoted, metadataNames);
|
||||
})
|
||||
: undefined;
|
||||
if (!querySourceColumns || querySourceColumns.some((column) => !column)) return undefined;
|
||||
return {
|
||||
queryAnalysis: {
|
||||
schema: tableMeta.schema,
|
||||
tableName: tableMeta.tableName,
|
||||
tableAlias: structure.tableAlias,
|
||||
selectStar: structure.selectStar,
|
||||
columns: structure.columns,
|
||||
allowInsert: true,
|
||||
allowInsertDelete: true,
|
||||
},
|
||||
querySourceColumns,
|
||||
queryEditabilityReason: undefined,
|
||||
tableMeta,
|
||||
};
|
||||
}
|
||||
const statements = splitSqlStatementRanges(tab.resultBaseSql ?? tab.lastExecutedSql ?? tab.sql, databaseType);
|
||||
const resultIndex = Number.isInteger(result.statement_index) && result.statement_index! >= 0 ? result.statement_index! : Math.max(0, tab.results?.indexOf(result) ?? 0);
|
||||
for (const [index, statement] of statements.entries()) {
|
||||
if (index >= resultIndex) break;
|
||||
const statementResult = tab.results?.find((candidate, fallbackIndex) => (candidate.statement_index ?? fallbackIndex) === index);
|
||||
if (statementResult?.execution_error) continue;
|
||||
location.database = useDatabaseFromStatement(statement.sql, databaseType) ?? location.database;
|
||||
}
|
||||
// Batch actions can also be invoked from data/table tabs. Metadata
|
||||
// resolution is read-only and uses the same query parser, so provide the
|
||||
// query-mode context expected by buildQueryMetadataPatch without changing
|
||||
// the live tab mode or its displayed result.
|
||||
const context = { ...tab, ...location, mode: "query" as const, result };
|
||||
return buildQueryMetadataPatch(context, result.sourceStatement, location.database, undefined, undefined, [], connection);
|
||||
}
|
||||
|
||||
function setActiveResultIndex(id: string, index: number) {
|
||||
const tab = findExecutionTab(id);
|
||||
if (!tab?.results || index < 0 || index >= tab.results.length) return;
|
||||
@@ -9573,6 +9628,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
closeQueryResult,
|
||||
clearQueryResults,
|
||||
setActiveResultIndex,
|
||||
resolveResultMetadataForBatch,
|
||||
executeCurrentTab,
|
||||
executeCurrentSql,
|
||||
executeTabSql,
|
||||
|
||||
Reference in new issue
Block a user