From e3b8f57326d5e55a00d75339875a2b98e095e7bb Mon Sep 17 00:00:00 2001 From: SuLea-IT <105108570+SuLea-IT@users.noreply.github.com> Date: Thu, 30 Apr 2026 14:38:26 +0800 Subject: [PATCH] feat: execute selected sql (#13) Co-authored-by: SuLe --- src/App.vue | 45 ++++++++++++++++++++------- src/components/editor/QueryEditor.vue | 18 +++++++++-- src/i18n/locales/en.ts | 2 +- src/i18n/locales/zh-CN.ts | 2 +- src/lib/sqlExecutionTarget.ts | 4 +++ src/stores/queryStore.ts | 13 +++++++- src/types/database.ts | 1 + tests/sqlExecutionTarget.test.ts | 19 +++++++++++ 8 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 src/lib/sqlExecutionTarget.ts create mode 100644 tests/sqlExecutionTarget.test.ts diff --git a/src/App.vue b/src/App.vue index 2f7cf8e952..8ff8cf933d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -33,6 +33,7 @@ import { setLocale, currentLocale, type Locale } from "@/i18n"; import { getCurrentWindow, type Theme } from "@tauri-apps/api/window"; import { getCurrentWebview } from "@tauri-apps/api/webview"; import * as api from "@/lib/tauri"; +import { resolveExecutableSql } from "@/lib/sqlExecutionTarget"; const { t } = useI18n(); const connectionStore = useConnectionStore(); @@ -44,6 +45,8 @@ const { message: toastMessage, visible: toastVisible, toast } = useToast(); const showConnectionDialog = ref(false); const showHistory = ref(false); const dangerSql = ref(""); +const pendingDangerSql = ref(""); +const selectedSql = ref(""); const showDangerDialog = ref(false); const databaseOptions = ref>({}); const loadingDatabaseOptions = ref>({}); @@ -66,6 +69,16 @@ const activeTab = computed(() => queryStore.tabs.find((t) => t.id === queryStore.activeTabId) ); +const executableSql = computed(() => { + const tab = activeTab.value; + return tab ? resolveExecutableSql(tab.sql, selectedSql.value) : ""; +}); + +watch(() => queryStore.activeTabId, () => { + selectedSql.value = ""; + pendingDangerSql.value = ""; +}); + const activeConnection = computed(() => { const tab = activeTab.value; return tab ? connectionStore.getConfig(tab.connectionId) : undefined; @@ -191,6 +204,10 @@ function onEditorUpdate(val: string) { } } +function onEditorSelectionChange(val: string) { + selectedSql.value = val; +} + function newQuery() { if (!connectionStore.activeConnectionId) return; const conn = connectionStore.connections.find( @@ -222,24 +239,25 @@ function isDangerousSql(sql: string): boolean { return DANGER_RE.test(stripSqlComments(sql)); } -function tryExecute() { +function tryExecute(sqlOverride?: string) { const tab = activeTab.value; - if (!tab || !tab.sql.trim()) return; - if (isDangerousSql(tab.sql)) { - dangerSql.value = tab.sql; + const sql = sqlOverride ?? executableSql.value; + if (!tab || !sql.trim()) return; + if (isDangerousSql(sql)) { + dangerSql.value = sql; + pendingDangerSql.value = sql; showDangerDialog.value = true; } else { - doExecute(); + doExecute(sql); } } -async function doExecute() { +async function doExecute(sql = executableSql.value) { const tab = activeTab.value; - if (!tab) return; + if (!tab || !sql.trim()) return; const connName = connectionStore.getConfig(tab.connectionId)?.name || ""; - const sql = tab.sql; const start = Date.now(); - await queryStore.executeCurrentTab(); + await queryStore.executeCurrentSql(sql); const elapsed = Date.now() - start; const success = !tab.result?.columns.includes("Error"); historyStore.add({ @@ -253,7 +271,9 @@ async function doExecute() { } function onDangerConfirm() { - doExecute(); + const sql = pendingDangerSql.value || executableSql.value; + pendingDangerSql.value = ""; + doExecute(sql); } function onHistoryRestore(sql: string) { @@ -658,7 +678,8 @@ async function setupFileDrop() { class="flex-1" :model-value="activeTab.sql" @update:model-value="onEditorUpdate" - @execute="tryExecute()" + @selection-change="onEditorSelectionChange" + @execute="tryExecute" /> @@ -670,7 +691,7 @@ async function setupFileDrop() { @replace-sql="replaceActiveSql" @append-sql="appendActiveSql" /> - +
{{ t('editor.pressToExecute') }}
diff --git a/src/components/editor/QueryEditor.vue b/src/components/editor/QueryEditor.vue index 790efef82a..f84c28a22e 100644 --- a/src/components/editor/QueryEditor.vue +++ b/src/components/editor/QueryEditor.vue @@ -1,6 +1,7 @@