From 0b523fa6b29fc4b277f296dcccadb0a0f0c372d0 Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Sun, 15 Nov 2026 07:02:59 +0300 Subject: [PATCH] feat(frontend): CodeMirror 6 SQL editor with Channel execute Embed CM6 SQL mode, Ctrl/Cmd+Enter / Run to stream db_execute_query events into a queryResult store, with a temporary table preview until Glide. --- apps/tauri/package.json | 6 + apps/tauri/pnpm-lock.yaml | 155 +++++++++++++++++++ apps/tauri/src/App.vue | 10 +- apps/tauri/src/api/query.ts | 32 ++++ apps/tauri/src/stores/queryResult.ts | 118 +++++++++++++++ apps/tauri/src/stores/workspace.ts | 41 ++++- apps/tauri/src/ui/SqlEditor.vue | 145 ++++++++++++++++++ apps/tauri/src/ui/SqlWorkspace.vue | 219 +++++++++++++++++++++++++++ 8 files changed, 718 insertions(+), 8 deletions(-) create mode 100644 apps/tauri/src/api/query.ts create mode 100644 apps/tauri/src/stores/queryResult.ts create mode 100644 apps/tauri/src/ui/SqlEditor.vue create mode 100644 apps/tauri/src/ui/SqlWorkspace.vue diff --git a/apps/tauri/package.json b/apps/tauri/package.json index 0fed140c..77a32238 100644 --- a/apps/tauri/package.json +++ b/apps/tauri/package.json @@ -11,9 +11,15 @@ "typecheck": "vue-tsc --noEmit" }, "dependencies": { + "@codemirror/commands": "^6.11.1", + "@codemirror/lang-sql": "^6.10.0", + "@codemirror/language": "^6.13.1", + "@codemirror/state": "^6.7.6", + "@codemirror/view": "^6.43.15", "@tauri-apps/api": "^2", "@tauri-apps/plugin-dialog": "^2.8.4", "@tauri-apps/plugin-opener": "^2", + "codemirror": "^6.0.2", "pinia": "^3.0.3", "vue": "^3.5.13" }, diff --git a/apps/tauri/pnpm-lock.yaml b/apps/tauri/pnpm-lock.yaml index b3cf45c3..5df39e89 100644 --- a/apps/tauri/pnpm-lock.yaml +++ b/apps/tauri/pnpm-lock.yaml @@ -8,6 +8,21 @@ importers: .: dependencies: + '@codemirror/commands': + specifier: ^6.11.1 + version: 6.11.1 + '@codemirror/lang-sql': + specifier: ^6.10.0 + version: 6.10.0 + '@codemirror/language': + specifier: ^6.13.1 + version: 6.13.1 + '@codemirror/state': + specifier: ^6.7.6 + version: 6.7.6 + '@codemirror/view': + specifier: ^6.43.15 + version: 6.43.15 '@tauri-apps/api': specifier: ^2 version: 2.12.4 @@ -17,6 +32,9 @@ importers: '@tauri-apps/plugin-opener': specifier: ^2 version: 2.7.3 + codemirror: + specifier: ^6.0.2 + version: 6.0.2 pinia: specifier: ^3.0.3 version: 3.0.4(typescript@5.8.3)(vue@3.5.45(typescript@5.8.3)) @@ -59,6 +77,33 @@ packages: resolution: {integrity: sha512-UjI/dKmwJwT9pOszJitq2G6EObZPnsZUfdCr92z2TlF3EZK9J3EdfOKUrCzHl6wFthbzwhzl1Soc/UIwxRiq6w==} engines: {node: '>=6.9.0'} + '@codemirror/autocomplete@6.20.4': + resolution: {integrity: sha512-yF8RhOf2SXlg00+qtupzLhrglcas61boqaONiNcbPIebHCpJocpqTBA6dyrtUrQO47hqyTdeob/3XnYilT7TaQ==} + + '@codemirror/commands@6.11.1': + resolution: {integrity: sha512-O/4hG3SC1YwcmQ0d2UVNDs+AsaNWd1iHVxbTeEBuqH+6bExAiPK3iS/BvpY6rZGURALv4ZD3sIgcCmRvw3ehBg==} + + '@codemirror/lang-sql@6.10.0': + resolution: {integrity: sha512-6ayPkEd/yRw0XKBx5uAiToSgGECo/GY2NoJIHXIIQh1EVwLuKoU8BP/qK0qH5NLXAbtJRLuT73hx7P9X34iO4w==} + + '@codemirror/language@6.13.1': + resolution: {integrity: sha512-zfvU2r3TyylQLMj/te+5CnQ0rSqZi8B1oTCBbQkagcLW4Xc6imab6qN+pEJhsuNOLaJqiRyBXp9UhWA1DhVvDw==} + + '@codemirror/lint@6.9.8': + resolution: {integrity: sha512-qNvtcE73xUto/D1RvtgSRI4A1Hg8ekxlvtZpjHpdpisLBnhyu82EUEJMEgaxkjuQCA8vqnrylENWKOsEDCOHcw==} + + '@codemirror/search@6.7.2': + resolution: {integrity: sha512-gUYkYhT2+n/+VGZ+8EzE5WFkYZUZYm1VOKDudIsNqh42uRVQJ0a6Yss9sdKT3MeOYfuL1N6AZA57oza0Oyr0LA==} + + '@codemirror/state@6.7.6': + resolution: {integrity: sha512-kAz+AncRtKuIknedxT1bq4XwXv4UowhbkHU1myPrtVb/jZtImWuV5BXzv5vK6i3kYACsdiZiQKFQQ5Mq7elW8w==} + + '@codemirror/streamparser@6.0.0': + resolution: {integrity: sha512-JTFPF6xrfjt0mnfGPgzcLiMwYmvIyNaoizp8BYzv8FE8KrT6GBGDZ190rtt+O7Wke3gG/MFHyiY9YGNeimof4w==} + + '@codemirror/view@6.43.15': + resolution: {integrity: sha512-HHsfpqFeHyXLs983pSRe38Xajs0vFWZQ2BuX0m7Ki8w3jZ5iD8cuR3lBz2/5/X53Dhf32SBHKh6rtSTB6tKeGw==} + '@esbuild/aix-ppc64@0.25.12': resolution: {integrity: sha512-Hhmwd6CInZ3dwpuGTF8fJG6yoWmsToE+vYgD4nytZVxcu1ulHpUQRAB1UJ8+N1Am3Mz4+xOByoQoSZf4D+CpkA==} engines: {node: '>=18'} @@ -218,6 +263,18 @@ packages: '@jridgewell/sourcemap-codec@1.6.0': resolution: {integrity: sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==} + '@lezer/common@1.5.5': + resolution: {integrity: sha512-Pw7PVCndruF1pb3ebCxpSVpkMHSDjwRCLcTRw+RizUKco5QbWylwq2g4lnG/RYN7yTTQDXE9F77MwtkAyphrjg==} + + '@lezer/highlight@1.2.5': + resolution: {integrity: sha512-O1GMVKgtf5YspFaRzpmqkVgMtIk2HG9uMAbH6wdAIdNh3TVzCsch/+cRi+6l2//UGDKe6NB9BW3cvuR5ezMQ0w==} + + '@lezer/lr@1.4.12': + resolution: {integrity: sha512-On9HU4oMMiHPMkQDgoTnGahjgG5n/MkN38CgG77sMraKLL+3JBbeQNi1FcCXV9QRqwF9/BUXP3caHyJC1fBOTQ==} + + '@marijn/find-cluster-break@1.0.4': + resolution: {integrity: sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==} + '@rolldown/pluginutils@1.1.0': resolution: {integrity: sha512-K+NU2HDCEem4sClgHH4ZZXwdJeNFkJo+hRk479DIwdIviAPN2hFJH1KHHwg8CxHQXvi0M5XY4deTXINfIiSG6w==} @@ -527,10 +584,16 @@ packages: brace-expansion@2.1.8: resolution: {integrity: sha512-BnlDzDYQoplTsZSvPi86CNzmbcb07X8ufR8fk1zivaiEJlXJkiq1MQyDCVmlWcsWbj6aWXqHnrwwevbrm/Saig==} + codemirror@6.0.2: + resolution: {integrity: sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw==} + copy-anything@4.1.5: resolution: {integrity: sha512-vePst2VLTSqPglW7Q98VwFpGiGpOHTyXIo81MO5VyLecA5zMbZw/P/V1vmwD1jksa2iJpn/YkDrJ3XhTfzUyhQ==} engines: {node: '>=18'} + crelt@1.0.7: + resolution: {integrity: sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==} + csstype@3.2.3: resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==} @@ -630,6 +693,9 @@ packages: resolution: {integrity: sha512-1POYv7uv2gXoyGFpBCmpDVSNV74IfsWlDW216UPjbWufNf+bSU6GdbDsxdcxtfwb4xlI3yxzOTKClUosxARYrQ==} engines: {node: '>=0.10.0'} + style-mod@4.1.4: + resolution: {integrity: sha512-XXWIQt633/EpAFx8aZDOTjBzrCaGmhvEQlQo6MVPfa2OzO2cWo+4hV9h+6UkHYlXGfy+ODXKUdP7Pthmcu5ATw==} + superjson@2.2.6: resolution: {integrity: sha512-H+ue8Zo4vJmV2nRjpx86P35lzwDT3nItnIsocgumgr0hHMQ+ZGq5vrERg9kJBo5AWGmxZDhzDo+WVIJqkB0cGA==} engines: {node: '>=16'} @@ -700,6 +766,9 @@ packages: typescript: optional: true + w3c-keyname@2.2.8: + resolution: {integrity: sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==} + snapshots: '@babel/helper-string-parser@7.29.14': {} @@ -715,6 +784,64 @@ snapshots: '@babel/helper-string-parser': 7.29.14 '@babel/helper-validator-identifier': 7.29.7 + '@codemirror/autocomplete@6.20.4': + dependencies: + '@codemirror/language': 6.13.1 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.15 + '@lezer/common': 1.5.5 + + '@codemirror/commands@6.11.1': + dependencies: + '@codemirror/language': 6.13.1 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.15 + '@lezer/common': 1.5.5 + + '@codemirror/lang-sql@6.10.0': + dependencies: + '@codemirror/autocomplete': 6.20.4 + '@codemirror/language': 6.13.1 + '@codemirror/state': 6.7.6 + '@lezer/common': 1.5.5 + '@lezer/highlight': 1.2.5 + '@lezer/lr': 1.4.12 + + '@codemirror/language@6.13.1': + dependencies: + '@codemirror/state': 6.7.6 + '@codemirror/streamparser': 6.0.0 + '@codemirror/view': 6.43.15 + '@lezer/common': 1.5.5 + '@lezer/highlight': 1.2.5 + '@lezer/lr': 1.4.12 + style-mod: 4.1.4 + + '@codemirror/lint@6.9.8': + dependencies: + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.15 + crelt: 1.0.7 + + '@codemirror/search@6.7.2': + dependencies: + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.15 + crelt: 1.0.7 + + '@codemirror/state@6.7.6': + dependencies: + '@marijn/find-cluster-break': 1.0.4 + + '@codemirror/streamparser@6.0.0': {} + + '@codemirror/view@6.43.15': + dependencies: + '@codemirror/state': 6.7.6 + crelt: 1.0.7 + style-mod: 4.1.4 + w3c-keyname: 2.2.8 + '@esbuild/aix-ppc64@0.25.12': optional: true @@ -795,6 +922,18 @@ snapshots: '@jridgewell/sourcemap-codec@1.6.0': {} + '@lezer/common@1.5.5': {} + + '@lezer/highlight@1.2.5': + dependencies: + '@lezer/common': 1.5.5 + + '@lezer/lr@1.4.12': + dependencies: + '@lezer/common': 1.5.5 + + '@marijn/find-cluster-break@1.0.4': {} + '@rolldown/pluginutils@1.1.0': {} '@rollup/rollup-android-arm-eabi@4.68.2': @@ -1052,8 +1191,20 @@ snapshots: dependencies: balanced-match: 1.0.2 + codemirror@6.0.2: + dependencies: + '@codemirror/autocomplete': 6.20.4 + '@codemirror/commands': 6.11.1 + '@codemirror/language': 6.13.1 + '@codemirror/lint': 6.9.8 + '@codemirror/search': 6.7.2 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.15 + copy-anything@4.1.5: {} + crelt@1.0.7: {} + csstype@3.2.3: {} de-indent@1.0.2: {} @@ -1175,6 +1326,8 @@ snapshots: speakingurl@14.0.1: {} + style-mod@4.1.4: {} + superjson@2.2.6: dependencies: copy-anything: 4.1.5 @@ -1214,3 +1367,5 @@ snapshots: '@vue/shared': 3.5.45 optionalDependencies: typescript: 5.8.3 + + w3c-keyname@2.2.8: {} diff --git a/apps/tauri/src/App.vue b/apps/tauri/src/App.vue index b9f39a93..e907f954 100644 --- a/apps/tauri/src/App.vue +++ b/apps/tauri/src/App.vue @@ -15,6 +15,7 @@ import TabStrip from "./ui/TabStrip.vue"; import StatusBar from "./ui/StatusBar.vue"; import WorkspaceEmptyHero from "./ui/WorkspaceEmptyHero.vue"; import QueryaDivider from "./ui/QueryaDivider.vue"; +import SqlWorkspace from "./ui/SqlWorkspace.vue"; const shell = useShellStore(); const status = useShellStatusStore(); @@ -27,6 +28,8 @@ const showEmpty = computed( () => !connections.active && activeTab.value?.kind === "welcome", ); +const showSql = computed(() => activeTab.value?.kind === "sql"); + async function onOpenSqlite() { try { const selected = await open({ @@ -42,7 +45,6 @@ async function onOpenSqlite() { const conn = await connections.connectSqlite(selected); workspace.addTab({ title: conn.name, kind: "sql" }); } catch (err) { - // Status already set by connections store on connect failure. if (String(err).includes("cancel")) return; } } @@ -110,12 +112,12 @@ onUnmounted(() => { @open-sqlite="onOpenSqlite" @open-tour="onOpenTour" /> +

{{ activeTab?.title }}

Connected: {{ connections.active.path }}

-

Kind: {{ activeTab?.kind }} — editor/grid land next in the slice.

@@ -154,7 +156,9 @@ onUnmounted(() => { .workspace-body { flex: 1; min-height: 0; - overflow: auto; + overflow: hidden; + display: flex; + flex-direction: column; } .tab-placeholder { diff --git a/apps/tauri/src/api/query.ts b/apps/tauri/src/api/query.ts new file mode 100644 index 00000000..3ef225ad --- /dev/null +++ b/apps/tauri/src/api/query.ts @@ -0,0 +1,32 @@ +import { Channel, invoke } from "@tauri-apps/api/core"; + +export type QueryEvent = + | { type: "columns"; names: string[] } + | { type: "rows"; rows: unknown[][]; offset: number } + | { + type: "done"; + row_count: number; + truncated: boolean; + affected_rows?: number | null; + elapsed_ms: number; + } + | { type: "error"; message: string }; + +export type QueryEventHandler = (event: QueryEvent) => void; + +/** Stream SQL results over a Tauri Channel. */ +export async function dbExecuteQuery( + id: string, + sql: string, + onEvent: QueryEventHandler, + limit?: number, +): Promise { + const channel = new Channel(); + channel.onmessage = onEvent; + await invoke("db_execute_query", { + id, + sql, + limit: limit ?? null, + onEvent: channel, + }); +} diff --git a/apps/tauri/src/stores/queryResult.ts b/apps/tauri/src/stores/queryResult.ts new file mode 100644 index 00000000..eb60527e --- /dev/null +++ b/apps/tauri/src/stores/queryResult.ts @@ -0,0 +1,118 @@ +import { defineStore } from "pinia"; +import { ref } from "vue"; +import { + dbExecuteQuery, + type QueryEvent, +} from "../api/query"; +import { formatConnectionError } from "../api/connection"; +import { useConnectionsStore } from "./connections"; +import { useShellStatusStore } from "./shellStatus"; + +/** Last streamed query result — grid (#1876) binds here. */ +export const useQueryResultStore = defineStore("queryResult", () => { + const columns = ref([]); + const rows = ref([]); + const running = ref(false); + const truncated = ref(false); + const rowCount = ref(0); + const elapsedMs = ref(null); + const affectedRows = ref(null); + const error = ref(null); + + function reset() { + columns.value = []; + rows.value = []; + truncated.value = false; + rowCount.value = 0; + elapsedMs.value = null; + affectedRows.value = null; + error.value = null; + } + + function onEvent(ev: QueryEvent) { + switch (ev.type) { + case "columns": + columns.value = ev.names; + rows.value = []; + break; + case "rows": + rows.value = rows.value.concat(ev.rows); + break; + case "done": + rowCount.value = ev.row_count; + truncated.value = ev.truncated; + elapsedMs.value = ev.elapsed_ms; + affectedRows.value = + ev.affected_rows === undefined || ev.affected_rows === null + ? null + : ev.affected_rows; + break; + case "error": + error.value = ev.message; + break; + } + } + + async function run(sql: string) { + const connections = useConnectionsStore(); + const status = useShellStatusStore(); + const id = connections.activeId; + if (!id) { + error.value = "No active connection"; + status.setQueryMetrics({ message: error.value, busy: false }); + return; + } + const trimmed = sql.trim(); + if (!trimmed) { + error.value = "SQL is empty"; + status.setQueryMetrics({ message: error.value, busy: false }); + return; + } + + reset(); + running.value = true; + status.setQueryMetrics({ message: "Running…", busy: true }); + try { + await dbExecuteQuery(id, trimmed, onEvent); + if (error.value) { + status.setQueryMetrics({ message: error.value, busy: false }); + } else if (affectedRows.value != null && columns.value.length === 0) { + status.setQueryMetrics({ + message: `${affectedRows.value} rows affected · ${elapsedMs.value ?? 0} ms`, + busy: false, + rows: affectedRows.value, + cols: 0, + durationMs: elapsedMs.value, + }); + } else { + const trunc = truncated.value ? " (truncated)" : ""; + status.setQueryMetrics({ + message: `${rowCount.value} rows · ${columns.value.length} cols · ${elapsedMs.value ?? 0} ms${trunc}`, + busy: false, + rows: rowCount.value, + cols: columns.value.length, + durationMs: elapsedMs.value, + }); + } + } catch (err) { + const msg = formatConnectionError(err); + error.value = error.value ?? msg; + status.setQueryMetrics({ message: error.value, busy: false }); + } finally { + running.value = false; + } + } + + return { + columns, + rows, + running, + truncated, + rowCount, + elapsedMs, + affectedRows, + error, + reset, + run, + }; +}); diff --git a/apps/tauri/src/stores/workspace.ts b/apps/tauri/src/stores/workspace.ts index 8926eee4..a19ae998 100644 --- a/apps/tauri/src/stores/workspace.ts +++ b/apps/tauri/src/stores/workspace.ts @@ -16,12 +16,15 @@ function nextId(): string { return `tab-${seq}`; } -/** Demo workspace tabs — SQL/result content lands in later issues. */ +const DEFAULT_SQL = "SELECT name FROM sqlite_master WHERE type = 'table';\n"; + +/** Workspace tabs + per-tab SQL buffers. */ export const useWorkspaceStore = defineStore("workspace", () => { const tabs = ref([ { id: "tab-1", title: "Welcome", kind: "welcome" }, ]); const activeId = ref("tab-1"); + const sqlBuffers = ref>({}); const activeTab = computed( () => tabs.value.find((t) => t.id === activeId.value) ?? null, @@ -31,6 +34,20 @@ export const useWorkspaceStore = defineStore("workspace", () => { if (tabs.value.some((t) => t.id === id)) activeId.value = id; } + function ensureSql(id: string, seed = DEFAULT_SQL) { + if (!(id in sqlBuffers.value)) { + sqlBuffers.value = { ...sqlBuffers.value, [id]: seed }; + } + } + + function sqlFor(id: string): string { + return sqlBuffers.value[id] ?? DEFAULT_SQL; + } + + function setSql(id: string, sql: string) { + sqlBuffers.value = { ...sqlBuffers.value, [id]: sql }; + } + function addTab(partial?: Partial>) { const tab: WorkspaceTab = { id: nextId(), @@ -38,6 +55,7 @@ export const useWorkspaceStore = defineStore("workspace", () => { kind: partial?.kind ?? "sql", }; tabs.value = [...tabs.value, tab]; + if (tab.kind === "sql") ensureSql(tab.id); activeId.value = tab.id; return tab; } @@ -45,10 +63,12 @@ export const useWorkspaceStore = defineStore("workspace", () => { function closeTab(id: string) { const idx = tabs.value.findIndex((t) => t.id === id); if (idx < 0) return; + const { [id]: _removed, ...rest } = sqlBuffers.value; + sqlBuffers.value = rest; if (tabs.value.length === 1) { - // Keep at least a welcome tab. - tabs.value = [{ id: nextId(), title: "Welcome", kind: "welcome" }]; - activeId.value = tabs.value[0].id; + const welcome = { id: nextId(), title: "Welcome", kind: "welcome" as const }; + tabs.value = [welcome]; + activeId.value = welcome.id; return; } const next = tabs.value.filter((t) => t.id !== id); @@ -59,5 +79,16 @@ export const useWorkspaceStore = defineStore("workspace", () => { } } - return { tabs, activeId, activeTab, activate, addTab, closeTab }; + return { + tabs, + activeId, + activeTab, + sqlBuffers, + activate, + addTab, + closeTab, + ensureSql, + sqlFor, + setSql, + }; }); diff --git a/apps/tauri/src/ui/SqlEditor.vue b/apps/tauri/src/ui/SqlEditor.vue new file mode 100644 index 00000000..77a36663 --- /dev/null +++ b/apps/tauri/src/ui/SqlEditor.vue @@ -0,0 +1,145 @@ + + + + + diff --git a/apps/tauri/src/ui/SqlWorkspace.vue b/apps/tauri/src/ui/SqlWorkspace.vue new file mode 100644 index 00000000..05230978 --- /dev/null +++ b/apps/tauri/src/ui/SqlWorkspace.vue @@ -0,0 +1,219 @@ + + + + +