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。

feat: add execute mode settings and cursor position handling in SQL editor

t8y2790791f94620
diff --git a/src/App.vue b/src/App.vue
index 675937125c..8aa3a8995f 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -124,6 +124,7 @@ const showUpdateDialog = ref(false);
const dangerSql = ref("");
const pendingDangerSql = ref("");
const selectedSql = ref("");
+const cursorPos = ref(0);
const formatSqlRequestId = ref(0);
const activeOutputView = ref<"result" | "explain">("result");
const showDangerDialog = ref(false);
@@ -455,7 +456,10 @@ const activeTab = computed(() =>
const executableSql = computed(() => {
const tab = activeTab.value;
- return tab ? resolveExecutableSql(tab.sql, selectedSql.value) : "";
+ return tab ? resolveExecutableSql(tab.sql, selectedSql.value, {
+ mode: settingsStore.editorSettings.executeMode,
+ cursorPos: cursorPos.value,
+ }) : "";
});
watch(() => queryStore.activeTabId, () => {
@@ -615,6 +619,10 @@ function onEditorSelectionChange(val: string) {
selectedSql.value = val;
}
+function onEditorCursorChange(pos: number) {
+ cursorPos.value = pos;
+}
+
function formatActiveSql() {
const tab = activeTab.value;
if (!tab || tab.mode !== "query" || !tab.sql.trim()) return;
@@ -1464,6 +1472,7 @@ async function setupFileDrop() {
:format-request-id="formatSqlRequestId"
@update:model-value="onEditorUpdate"
@selection-change="onEditorSelectionChange"
+ @cursor-change="onEditorCursorChange"
@format-error="onFormatSqlError"
@execute="tryExecute"
/>
diff --git a/src/components/editor/EditorSettingsDialog.vue b/src/components/editor/EditorSettingsDialog.vue
index 6c2562001b..85ba8cedae 100644
--- a/src/components/editor/EditorSettingsDialog.vue
+++ b/src/components/editor/EditorSettingsDialog.vue
@@ -32,6 +32,7 @@ const emit = defineEmits<{
const editFontFamily = ref(settingsStore.editorSettings.fontFamily);
const editFontSize = ref(settingsStore.editorSettings.fontSize);
const editTheme = ref(settingsStore.editorSettings.theme);
+const editExecuteMode = ref(settingsStore.editorSettings.executeMode);
// Sync from store when dialog opens
watch(() => props.open, (open) => {
@@ -39,6 +40,7 @@ watch(() => props.open, (open) => {
editFontFamily.value = settingsStore.editorSettings.fontFamily;
editFontSize.value = settingsStore.editorSettings.fontSize;
editTheme.value = settingsStore.editorSettings.theme;
+ editExecuteMode.value = settingsStore.editorSettings.executeMode;
}
});
@@ -46,7 +48,8 @@ function hasChanges(): boolean {
return (
editFontFamily.value !== settingsStore.editorSettings.fontFamily ||
editFontSize.value !== settingsStore.editorSettings.fontSize ||
- editTheme.value !== settingsStore.editorSettings.theme
+ editTheme.value !== settingsStore.editorSettings.theme ||
+ editExecuteMode.value !== settingsStore.editorSettings.executeMode
);
}
@@ -55,6 +58,7 @@ function applySettings() {
fontFamily: editFontFamily.value,
fontSize: editFontSize.value,
theme: editTheme.value,
+ executeMode: editExecuteMode.value,
});
emit("update:open", false);
}
@@ -63,6 +67,11 @@ function resetDefaults() {
editFontFamily.value = DEFAULT_EDITOR_SETTINGS.fontFamily;
editFontSize.value = DEFAULT_EDITOR_SETTINGS.fontSize;
editTheme.value = DEFAULT_EDITOR_SETTINGS.theme;
+ editExecuteMode.value = DEFAULT_EDITOR_SETTINGS.executeMode;
+}
+
+function onExecuteModeChange(v: any) {
+ if (v === "all" || v === "current") editExecuteMode.value = v;
}
function onFontFamilyChange(v: any) {
@@ -160,7 +169,7 @@ watch(() => props.open, (open) => {
<template>
<Dialog :open="open" @update:open="(v: boolean) => emit('update:open', v)">
- <DialogContent class="sm:max-w-[720px] max-h-[calc(100vh-80px)] overflow-y-auto">
+ <DialogContent class="sm:max-w-[720px] max-h-[calc(100vh-80px)] overflow-y-auto overflow-x-hidden">
<DialogHeader>
<DialogTitle class="flex items-center gap-2">
<Settings class="h-4 w-4" />
@@ -243,6 +252,19 @@ watch(() => props.open, (open) => {
</Select>
</div>
+ <div class="space-y-2">
+ <Label>{{ t('settings.executeMode') }}</Label>
+ <Select :model-value="editExecuteMode" @update:model-value="onExecuteModeChange">
+ <SelectTrigger>
+ <SelectValue :placeholder="t('settings.executeMode')" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="all">{{ t('settings.executeModeAll') }}</SelectItem>
+ <SelectItem value="current">{{ t('settings.executeModeCurrent') }}</SelectItem>
+ </SelectContent>
+ </Select>
+ </div>
+
<Separator />
<!-- Live Preview -->
@@ -252,7 +274,7 @@ watch(() => props.open, (open) => {
class="rounded-md border overflow-auto max-w-full"
:class="editTheme === 'vscode-light' || editTheme === 'duotone-light' || editTheme === 'xcode' ? 'border-border' : 'border-border/50'"
>
- <div ref="previewRef" style="height: 160px; min-width: 100%" />
+ <div ref="previewRef" style="min-width: 100%" />
</div>
</div>
</div>
diff --git a/src/components/editor/QueryEditor.vue b/src/components/editor/QueryEditor.vue
index 52f7493ca9..da67a8da87 100644
--- a/src/components/editor/QueryEditor.vue
+++ b/src/components/editor/QueryEditor.vue
@@ -24,6 +24,7 @@ const props = defineProps<{
const emit = defineEmits<{
"update:modelValue": [value: string];
"selectionChange": [value: string];
+ "cursorChange": [pos: number];
"formatError": [message: string];
execute: [sql: string];
}>();
@@ -245,6 +246,7 @@ onMounted(async () => {
}
if (update.selectionSet || update.docChanged) {
emit("selectionChange", selectedSqlFromView(update.view));
+ emit("cursorChange", update.state.selection.main.head);
}
}),
fontThemeComp.of(
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index f2731311a0..366377cec8 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -610,6 +610,9 @@ export default {
selectTheme: "Select theme...",
apply: "Apply",
resetDefaults: "Reset Defaults",
+ executeMode: "Execute Mode (Cmd+Enter)",
+ executeModeAll: "Execute all SQL",
+ executeModeCurrent: "Execute statement at cursor",
preview: "Live Preview",
},
};
diff --git a/src/i18n/locales/zh-CN.ts b/src/i18n/locales/zh-CN.ts
index 2bcfce9ddc..9eed5c5728 100644
--- a/src/i18n/locales/zh-CN.ts
+++ b/src/i18n/locales/zh-CN.ts
@@ -608,6 +608,9 @@ export default {
selectTheme: "选择主题...",
apply: "应用",
resetDefaults: "恢复默认",
+ executeMode: "执行模式 (Cmd+Enter)",
+ executeModeAll: "执行全部 SQL",
+ executeModeCurrent: "执行光标所在语句",
preview: "实时预览",
},
};
diff --git a/src/lib/sqlExecutionTarget.ts b/src/lib/sqlExecutionTarget.ts
index ad5e41a426..6140d54ce6 100644
--- a/src/lib/sqlExecutionTarget.ts
+++ b/src/lib/sqlExecutionTarget.ts
@@ -1,4 +1,18 @@
-export function resolveExecutableSql(fullSql: string, selectedSql: string): string {
+import { findStatementAtCursor } from "./sqlStatementSplit";
+
+export type ExecuteMode = "all" | "current";
+
+export function resolveExecutableSql(
+ fullSql: string,
+ selectedSql: string,
+ options?: { mode?: ExecuteMode; cursorPos?: number },
+): string {
const trimmedSelection = selectedSql.trim();
- return trimmedSelection ? trimmedSelection : fullSql;
+ if (trimmedSelection) return trimmedSelection;
+
+ if (options?.mode === "current" && options.cursorPos !== undefined) {
+ return findStatementAtCursor(fullSql, options.cursorPos);
+ }
+
+ return fullSql;
}
diff --git a/src/lib/sqlStatementSplit.ts b/src/lib/sqlStatementSplit.ts
new file mode 100644
index 0000000000..13871d60f5
--- /dev/null
+++ b/src/lib/sqlStatementSplit.ts
@@ -0,0 +1,100 @@
+export function findStatementAtCursor(sql: string, cursorPos: number): string {
+ const statements = splitStatements(sql);
+ for (const stmt of statements) {
+ if (cursorPos >= stmt.start && cursorPos <= stmt.end) {
+ return stmt.text;
+ }
+ }
+ if (statements.length > 0) {
+ const last = statements[statements.length - 1];
+ if (cursorPos >= last.end) return last.text;
+ }
+ return sql.trim();
+}
+
+interface StatementRange {
+ text: string;
+ start: number;
+ end: number;
+}
+
+function splitStatements(sql: string): StatementRange[] {
+ const results: StatementRange[] = [];
+ let i = 0;
+ let stmtStart = 0;
+
+ while (i < sql.length) {
+ const ch = sql[i];
+
+ if (ch === "-" && sql[i + 1] === "-") {
+ i = skipLineComment(sql, i);
+ continue;
+ }
+
+ if (ch === "/" && sql[i + 1] === "*") {
+ i = skipBlockComment(sql, i);
+ continue;
+ }
+
+ if (ch === "'" || ch === '"') {
+ i = skipQuoted(sql, i, ch);
+ continue;
+ }
+
+ if (ch === "`") {
+ i = skipQuoted(sql, i, "`");
+ continue;
+ }
+
+ if (ch === ";") {
+ const text = sql.slice(stmtStart, i).trim();
+ if (text) {
+ results.push({ text, start: stmtStart, end: i + 1 });
+ }
+ stmtStart = i + 1;
+ }
+
+ i++;
+ }
+
+ const trailing = sql.slice(stmtStart).trim();
+ if (trailing) {
+ results.push({ text: trailing, start: stmtStart, end: sql.length });
+ }
+
+ return results;
+}
+
+function skipLineComment(sql: string, pos: number): number {
+ let i = pos + 2;
+ while (i < sql.length && sql[i] !== "\n") i++;
+ return i + 1;
+}
+
+function skipBlockComment(sql: string, pos: number): number {
+ let i = pos + 2;
+ while (i < sql.length - 1) {
+ if (sql[i] === "*" && sql[i + 1] === "/") return i + 2;
+ i++;
+ }
+ return sql.length;
+}
+
+function skipQuoted(sql: string, pos: number, quote: string): number {
+ let i = pos + 1;
+ while (i < sql.length) {
+ if (sql[i] === quote) {
+ if (sql[i + 1] === quote) {
+ i += 2;
+ continue;
+ }
+ return i + 1;
+ }
+ if (sql[i] === "\\" && quote === "'") {
+ i += 2;
+ continue;
+ }
+ i++;
+ }
+ return sql.length;
+}
diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.ts
index 3d618c4db1..ccdc74011b 100644
--- a/src/stores/settingsStore.ts
+++ b/src/stores/settingsStore.ts
@@ -25,6 +25,7 @@ export interface EditorSettings {
fontFamily: string;
fontSize: number;
theme: EditorTheme;
+ executeMode: "all" | "current";
}
export const EDITOR_THEMES: { value: EditorTheme; label: string; dark: boolean }[] = [
@@ -53,6 +54,7 @@ export const DEFAULT_EDITOR_SETTINGS: EditorSettings = {
fontFamily: "'JetBrains Mono', 'Fira Code', monospace",
fontSize: 13,
theme: "one-dark",
+ executeMode: "all",
};
export const STORAGE_KEY = "dbx-editor-settings";
@@ -68,6 +70,7 @@ function loadEditorSettings(): EditorSettings {
fontFamily: parsed.fontFamily ?? DEFAULT_EDITOR_SETTINGS.fontFamily,
fontSize: parsed.fontSize ?? DEFAULT_EDITOR_SETTINGS.fontSize,
theme: parsed.theme ?? DEFAULT_EDITOR_SETTINGS.theme,
+ executeMode: parsed.executeMode ?? DEFAULT_EDITOR_SETTINGS.executeMode,
};
}
} catch { /* ignore */ }
diff --git a/tests/sqlStatementSplit.test.ts b/tests/sqlStatementSplit.test.ts
new file mode 100644
index 0000000000..1976cdc009
--- /dev/null
+++ b/tests/sqlStatementSplit.test.ts
@@ -0,0 +1,67 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import { findStatementAtCursor } from "../src/lib/sqlStatementSplit.ts";
+
+test("finds single statement", () => {
+ assert.equal(findStatementAtCursor("SELECT 1", 3), "SELECT 1");
+});
+
+test("finds first statement before semicolon", () => {
+ const sql = "SELECT 1; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT 1");
+});
+
+test("finds second statement after semicolon", () => {
+ const sql = "SELECT 1; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 12), "SELECT 2");
+});
+
+test("cursor at semicolon belongs to first statement", () => {
+ const sql = "SELECT 1; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 9), "SELECT 1");
+});
+
+test("cursor at end belongs to last statement", () => {
+ const sql = "SELECT 1; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 18), "SELECT 2");
+});
+
+test("skips semicolons inside single-quoted strings", () => {
+ const sql = "SELECT ';'; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 15), "SELECT 2");
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT ';'");
+});
+
+test("skips semicolons inside double-quoted identifiers", () => {
+ const sql = 'SELECT ";"; SELECT 2';
+ assert.equal(findStatementAtCursor(sql, 3), 'SELECT ";"');
+});
+
+test("skips semicolons inside line comments", () => {
+ const sql = "SELECT 1 -- ; comment\n; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT 1 -- ; comment");
+ assert.equal(findStatementAtCursor(sql, 25), "SELECT 2");
+});
+
+test("skips semicolons inside block comments", () => {
+ const sql = "SELECT 1 /* ; */ ; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT 1 /* ; */");
+ assert.equal(findStatementAtCursor(sql, 22), "SELECT 2");
+});
+
+test("handles multiple statements", () => {
+ const sql = "INSERT INTO t VALUES (1); UPDATE t SET x = 2; DELETE FROM t";
+ assert.equal(findStatementAtCursor(sql, 5), "INSERT INTO t VALUES (1)");
+ assert.equal(findStatementAtCursor(sql, 30), "UPDATE t SET x = 2");
+ assert.equal(findStatementAtCursor(sql, 50), "DELETE FROM t");
+});
+
+test("handles trailing semicolon", () => {
+ const sql = "SELECT 1;";
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT 1");
+});
+
+test("handles backtick-quoted identifiers", () => {
+ const sql = "SELECT `a;b`; SELECT 2";
+ assert.equal(findStatementAtCursor(sql, 3), "SELECT `a;b`");
+});