From 7d797df4f8efbc88730c7305d196082790fccc8d Mon Sep 17 00:00:00 2001 From: haipengno1 Date: Tue, 16 Jun 2026 00:22:40 +0800 Subject: [PATCH] fix(redis): sync SELECT database to toolbar and tab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复顶部 SQL 编辑器执行 SELECT 后 db 未联动,导致后续命令写入错库 --- apps/desktop/src/stores/queryStore.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 1c44266cb..acf27c31b 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -23,6 +23,7 @@ import { type MongoAggregateSafetyOptions, } from "@/lib/mongoShellCommand"; import { redisCommandResultToQueryResult } from "@/lib/redisQueryResult"; +import { nextRedisCommandDb } from "@/lib/redisCommandSession"; import { supportsDatabaseFeature } from "@/lib/databaseCapabilities"; import { editablePrimaryKeys } from "@/lib/tableEditing"; import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/tableDataExport"; @@ -1157,20 +1158,22 @@ export const useQueryStore = defineStore("query", () => { // Redis command execution — split multi-line input into individual commands if (conn?.db_type === "redis") { await connStore.ensureConnected(tab.connectionId); - const redisDb = Number(tab.database) || 0; + let currentDb = Number(tab.database) || 0; const commands = sql .split("\n") .map((line) => line.trim()) .filter((line) => line.length > 0); if (commands.length === 0) return; - console.info("[DBX][executeTabSql:redis:start]", { traceId, db: redisDb, commandCount: commands.length, sql }); + console.info("[DBX][executeTabSql:redis:start]", { traceId, db: currentDb, commandCount: commands.length, sql }); const allResults: QueryResult[] = []; const skipSafety = options?.skipRedisSafetyCheck; for (const command of commands) { try { - const result = await api.redisExecuteCommand(tab.connectionId, redisDb, command, skipSafety); + const result = await api.redisExecuteCommand(tab.connectionId, currentDb, command, skipSafety); allResults.push(markQueryResultRowsRaw(redisCommandResultToQueryResult(result.value, performance.now() - startedAt, result.command))); + // Track db switches from SELECT N so later commands in the same batch run on the right db. + currentDb = nextRedisCommandDb(currentDb, command, result.value); } catch (e: any) { allResults.push({ columns: ["Error"], rows: [[e?.message ?? String(e)]], affected_rows: 0, execution_time_ms: 0 }); } @@ -1198,6 +1201,11 @@ export const useQueryStore = defineStore("query", () => { current.resultBaseSql = options?.resultBaseSql ?? sql; current.resultSortedSql = options?.resultSortedSql; captureDisplayedResultRun(current, options?.resultBaseSql ?? sql); + // Reflect db switches from SELECT N in the tab so the toolbar dropdown, tab title and + // sidebar stay in sync with the command's effective db. + if (current.database !== String(currentDb)) { + current.database = String(currentDb); + } } return; }