Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(query): keep result actions during slow execution

zipgeba6185a5fd6
diff --git a/apps/desktop/src/stores/__tests__/queryStore.lobSessionRelease.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.lobSessionRelease.spec.ts
index 02aae274f3..b8bbeb6a82 100644
--- a/apps/desktop/src/stores/__tests__/queryStore.lobSessionRelease.spec.ts
+++ b/apps/desktop/src/stores/__tests__/queryStore.lobSessionRelease.spec.ts
@@ -175,4 +175,25 @@ describe("query Store LOB session release through public close and disconnect",
else expect(connections.connectedIds.has("ob-late")).toBe(false);
await store.flushPendingPersist();
});
+
+ it("releases replaced refs from the previous result when an execution fails", async () => {
+ const { useQueryStore } = await import("@/stores/queryStore");
+ const { useConnectionStore } = await import("@/stores/connectionStore");
+ const { useSettingsStore } = await import("@/stores/settingsStore");
+ const connections = useConnectionStore();
+ useSettingsStore().editorSettings.autoCalculateTotalRows = false;
+ connections.connections = [{ id: "ob-fail", name: "OB", db_type: "oceanbase-oracle", database: "SYS", read_only: false } as ConnectionConfig];
+ connections.connectedIds.add("ob-fail");
+ const store = useQueryStore();
+ const id = store.createTab("ob-fail", "SYS", "LOB", "query", "APP");
+ api.executeMulti.mockResolvedValueOnce([{ ...result("old"), large_value_context: { connectionId: "ob-fail", database: "SYS", clientSessionId: "old-client" }, large_value_refs: ["old-ref"] }]);
+ await store.executeTabSql(id, "SELECT PAYLOAD FROM LOB_FIXTURE");
+ expect(store.tabs.find((entry) => entry.id === id)?.result?.large_value_refs).toContain("old-ref");
+ api.executeMulti.mockRejectedValueOnce(new Error("query failed"));
+ await store.executeTabSql(id, "SELECT PAYLOAD FROM LOB_FIXTURE");
+ const tab = store.tabs.find((entry) => entry.id === id)!;
+ expect(tab.result?.execution_error).toBe(true);
+ await vi.waitFor(() => expect(api.releaseLargeValue).toHaveBeenCalledWith(expect.objectContaining({ connectionId: "ob-fail", database: "SYS", valueRef: "old-ref" })));
+ await store.flushPendingPersist();
+ });
});
diff --git a/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts
index 916d751b3b..2b6540a9b1 100644
--- a/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts
+++ b/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts
@@ -207,6 +207,30 @@ describe("queryStore multi-statement errors", () => {
}
});
+ it("keeps the previous query result mounted while a replacement query is slow", async () => {
+ const pendingExecution = deferred<QueryResult[]>();
+ mocks.executeMulti.mockResolvedValueOnce([{ columns: ["value"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 }]).mockImplementationOnce(() => pendingExecution.promise);
+ const { useQueryStore } = await import("@/stores/queryStore");
+ const store = useQueryStore();
+ const tabId = store.createTab("mysql-1", "app", "Query", "query", "app");
+
+ await store.executeTabSql(tabId, "SELECT 1 AS value");
+ const tab = store.tabs.find((item) => item.id === tabId)!;
+ expect(tab.result?.rows).toEqual([[1]]);
+
+ const execution = store.executeTabSql(tabId, "SELECT 2 AS value");
+ await vi.waitFor(() => expect(mocks.executeMulti).toHaveBeenCalledTimes(2));
+
+ expect(tab.isExecuting).toBe(true);
+ expect(tab.result?.rows).toEqual([[1]]);
+
+ pendingExecution.resolve([{ columns: ["value"], rows: [[2]], affected_rows: 0, execution_time_ms: 1 }]);
+ await execution;
+
+ expect(tab.isExecuting).toBe(false);
+ expect(tab.result?.rows).toEqual([[2]]);
+ });
+
it("opens the first error result from a mixed result batch", async () => {
const structuredError = structuredTimeoutError();
mocks.executeMulti.mockResolvedValue([
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index f8ab78c0d1..01651d2e61 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -2233,6 +2233,17 @@ export const useQueryStore = defineStore("query", () => {
}
}
+ function releaseReplacedResultLargeValues(tab: QueryTab, ...previousResults: Array<QueryResult | undefined>) {
+ const retainedResults = [tab.result, ...(tab.results ?? []), ...(tab.resultRuns ?? []).flatMap((run) => [run.result, ...(run.results ?? [])])];
+ const retainedRefs = new Set(retainedResults.flatMap((result) => resultLargeValueKeys(result)));
+ const discardedResults = previousResults.map((result) => {
+ if (!result) return undefined;
+ const refs = [...(result.large_value_refs ?? []), ...(result.large_value_cells ?? []).flatMap((cell) => (cell.value_ref ? [cell.value_ref] : []))];
+ return { ...result, large_value_cells: [], large_value_refs: refs.filter((valueRef) => !retainedRefs.has(resultLargeValueKey(result, valueRef))) };
+ });
+ void releaseResultLargeValues(...discardedResults);
+ }
+
async function closeResultRunSession(tab: QueryTab, run: NonNullable<QueryTab["resultRuns"]>[number]) {
if (!run.resultSessionId) return;
const target = run.multiDbExecution?.target;
@@ -2483,14 +2494,7 @@ export const useQueryStore = defineStore("query", () => {
};
void persistResultRun(tab, run);
tab.resultRuns[index] = run;
- const retainedResults = [tab.result, ...(tab.results ?? []), ...tab.resultRuns.flatMap((item) => [item.result, ...(item.results ?? [])])];
- const retainedRefs = new Set(retainedResults.flatMap((result) => resultLargeValueKeys(result)));
- const discardedResults = [previousRun.result, ...(previousRun.results ?? [])].map((result) => {
- if (!result) return undefined;
- const refs = [...(result.large_value_refs ?? []), ...(result.large_value_cells ?? []).flatMap((cell) => (cell.value_ref ? [cell.value_ref] : []))];
- return { ...result, large_value_cells: [], large_value_refs: refs.filter((valueRef) => !retainedRefs.has(resultLargeValueKey(result, valueRef))) };
- });
- void releaseResultLargeValues(...discardedResults);
+ releaseReplacedResultLargeValues(tab, previousRun.result, ...(previousRun.results ?? []));
}
function syncDisplayedResultRun(tab: QueryTab, sql: string, captureNewRun = false, options: ResultRunCaptureOptions = {}) {
@@ -6203,9 +6207,13 @@ export const useQueryStore = defineStore("query", () => {
function setErrorResult(id: string, e: any) {
const tab = tabs.value.find((t) => t.id === id);
if (!tab) return;
+ const previousDisplayedResults = [tab.result, ...(tab.results ?? [])];
tab.result = toErrorResult(e);
tab.results = undefined;
tab.activeResultIndex = undefined;
+ if (previousDisplayedResults.some(Boolean)) {
+ releaseReplacedResultLargeValues(tab, ...previousDisplayedResults);
+ }
// An error result is a replacement, not the previous dataset.
publishResultGeneration(tab, "execute");
tab.resultSessionId = undefined;
@@ -7346,7 +7354,11 @@ export const useQueryStore = defineStore("query", () => {
const elapsed = () => `${Math.round(performance.now() - startedAt)}ms`;
const batchResume = options?.batchResume;
const continueOnBatchError = batchResume?.continueOnError ?? settingsStore.editorSettings.continueOnErrorOnBatch;
- const preserveResultDuringExecution = batchResume !== undefined || options?.preserveResultDuringExecution === true || captureAutoSavedResultRun || (tab.mode === "query" && !!tab.activeResultRunId && !tab.resultAutoSave && !captureResultRun);
+ // Keep the last query result mounted while a replacement query is running.
+ // Clearing it first unmounts DataGrid and its toolbar, so a slow query makes
+ // the result actions disappear until the new response arrives (issue #10008).
+ const preserveQueryResultDuringExecution = tab.mode === "query" && !!tab.result && !captureResultRun;
+ const preserveResultDuringExecution = batchResume !== undefined || options?.preserveResultDuringExecution === true || captureAutoSavedResultRun || preserveQueryResultDuringExecution;
const updateActiveResultRun = !!tab.activeResultRunId && preserveResultDuringExecution;
tab.isExecuting = true;
tab.executingResultRunId = !captureResultRun && updateActiveResultRun ? tab.activeResultRunId : null;
@@ -8032,6 +8044,7 @@ export const useQueryStore = defineStore("query", () => {
if (captureResultRun && current.isCancelling && restorePendingResultRun(current, executionId)) return false;
const activeGroupIndex = current.activeResultIndex;
const activeGroupResults = current.results;
+ const previousDisplayedResults = [current.result, ...(current.results ?? [])];
const findPageState = mongoFindPageState;
const shouldAppendResult = !!findPageState && !!options?.appendResult && !!current.result && allResults.length === 1;
const shouldReplaceActiveResultInGroup = options?.replaceActiveResultInGroup === true && allResults.length === 1 && Array.isArray(activeGroupResults) && typeof activeGroupIndex === "number" && activeGroupIndex >= 0 && activeGroupIndex < activeGroupResults.length;
@@ -8062,6 +8075,9 @@ export const useQueryStore = defineStore("query", () => {
current.activeResultIndex = undefined;
current.result = allResults[0];
}
+ if (!captureResultRun && previousDisplayedResults.some(Boolean)) {
+ releaseReplacedResultLargeValues(current, ...previousDisplayedResults);
+ }
publishResultGeneration(current, shouldAppendResult ? "append" : "execute");
producedResult = current.result !== undefined;
touchResult(current);
@@ -8581,6 +8597,7 @@ export const useQueryStore = defineStore("query", () => {
}
const activeGroupIndex = current.activeResultIndex;
const activeGroupResults = current.results;
+ const previousDisplayedResults = [current.result, ...(current.results ?? [])];
const shouldAppendResult = !!options?.appendResult && !!current.result;
const shouldReplaceActiveResultInGroup = options?.replaceActiveResultInGroup === true && results.length === 1 && Array.isArray(activeGroupResults) && typeof activeGroupIndex === "number" && activeGroupIndex >= 0 && activeGroupIndex < activeGroupResults.length;
const retainedPaginationResult = options?.retainDisplayedResult === true && results.length === 1 && !isQueryExecutionErrorResult(results[0]!) ? results[0] : undefined;
@@ -8626,6 +8643,9 @@ export const useQueryStore = defineStore("query", () => {
current.activeResultIndex = undefined;
current.result = results[0];
}
+ if (!captureResultRun && previousDisplayedResults.some(Boolean)) {
+ releaseReplacedResultLargeValues(current, ...previousDisplayedResults);
+ }
// Logical-result identity for the view-snapshot cache. An append extends
// the same dataset; every other branch above replaces it.
publishResultGeneration(current, shouldAppendResult ? "append" : (options?.publicationOrigin ?? "execute"));
@@ -8802,6 +8822,7 @@ export const useQueryStore = defineStore("query", () => {
annotateSingleStatementErrorResult(errorResult, queryBaseSql, errorLocateContext?.databaseType, errorLocateContext?.sourceOffset, errorLocateContext?.parameterOptions, errorLocateContext?.executedSql);
const activeGroupIndex = current.activeResultIndex;
const activeGroupResults = current.results;
+ const previousDisplayedResults = [current.result, ...(current.results ?? [])];
const shouldReplaceActiveResultInGroup = options?.replaceActiveResultInGroup === true && Array.isArray(activeGroupResults) && typeof activeGroupIndex === "number" && activeGroupIndex >= 0 && activeGroupIndex < activeGroupResults.length;
if (shouldReplaceActiveResultInGroup) {
current.results = activeGroupResults.slice();
@@ -8812,6 +8833,9 @@ export const useQueryStore = defineStore("query", () => {
current.results = undefined;
current.activeResultIndex = undefined;
}
+ if (!captureResultRun && previousDisplayedResults.some(Boolean)) {
+ releaseReplacedResultLargeValues(current, ...previousDisplayedResults);
+ }
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryWriteTargets = undefined;
diff --git a/packages/app-tests/queryStore.test.ts b/packages/app-tests/queryStore.test.ts
index 855d26629d..44e23c640a 100644
--- a/packages/app-tests/queryStore.test.ts
+++ b/packages/app-tests/queryStore.test.ts
@@ -4664,7 +4664,7 @@ test("closing a database flushes only that database's released tabs before resto
}
});
-test("starting a new query clears the previous result payload immediately", async () => {
+test("keeps the previous result payload visible while a new query is running", async () => {
const restoreStorage = installMemoryStorage();
setActivePinia(createPinia());
const connectionStore = useConnectionStore();
@@ -4708,7 +4708,7 @@ test("starting a new query clears the previous result payload immediately", asyn
try {
const execution = store.executeTabSql(tabId, "select 1");
- assert.equal(tab.result, undefined);
+ assert.equal(tab.result?.columns[0], "old");
assert.equal(tab.results, undefined);
await execution;
assert.deepEqual(tab.result?.columns, ["new"]);