diff --git a/README.md b/README.md index 7bffa73..2447c19 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ ## Features -- **Multi-Database Support**: Connect to PostgreSQL, MySQL, and SQLite. +- **Multi-Database Support**: Connect to PostgreSQL, MySQL, SQLite, and local DuckDB files. +- **DuckDB (read-only preview)**: Open an existing `.duckdb` file, browse tables and views, run one bounded `SELECT` at a time, and profile a bounded table sample. DuckDB connections cannot edit data, import/export, compare databases, or use the relationship map yet; external file access and extension autoloading are disabled. - **AI Assistant**: Generate, explain, and fix SQL with your own Gemini, Groq, Claude, or OpenAI API key. Review the outbound request and proposed SQL before use. - **Modern Sidebar Experience**: - **Items**: Explore your databases, schemas, and tables. @@ -94,7 +95,7 @@ For automated PostgreSQL and MariaDB release checks, use the [cross-engine smoke ## Tech Stack - **Frontend**: React, TypeScript, Tailwind CSS, Monaco Editor, Lucide Icons. -- **Backend**: Rust (using `sqlx` for database interactions). +- **Backend**: Rust (`sqlx` for PostgreSQL, MySQL, and SQLite; `duckdb` for local DuckDB files). - **Framework**: [Tauri](https://tauri.app/). ## Recommended IDE Setup diff --git a/scripts/connection-clarity.test.mjs b/scripts/connection-clarity.test.mjs index e982a1e..76425e6 100644 --- a/scripts/connection-clarity.test.mjs +++ b/scripts/connection-clarity.test.mjs @@ -29,6 +29,10 @@ test('untagged and SQLite connections do not pretend to be local servers', () => assert.equal(connectionIdentity({ name: 'Archive', type: 'Sqlite', database: '/tmp/archive.db' }).target, '/tmp/archive.db'); }); +test('DuckDB connections identify the selected local file', () => { + assert.equal(connectionIdentity({ name: 'Warehouse', type: 'DuckDb', database: '/tmp/warehouse.duckdb' }).target, '/tmp/warehouse.duckdb'); +}); + test('mode descriptions reflect actual SQL approval behavior', () => { assert.match(safetyModeDescription('Alert'), /every SQL query and data change/i); assert.match(safetyModeDescription('Safe'), /read-only SELECT/i); diff --git a/scripts/connection-credentials.test.mjs b/scripts/connection-credentials.test.mjs index 58cff63..f1b04c4 100644 --- a/scripts/connection-credentials.test.mjs +++ b/scripts/connection-credentials.test.mjs @@ -28,4 +28,5 @@ test('remembered connections skip the prompt only when every required secret exi test('unremembered connections still request credentials', () => { assert.equal(needsCredentialPrompt({ type: 'Postgres' }, { database: true, ssh: true }), true); assert.equal(needsCredentialPrompt({ type: 'Sqlite' }, null), false); + assert.equal(needsCredentialPrompt({ type: 'DuckDb' }, null), false); }); diff --git a/scripts/connection-target.test.mjs b/scripts/connection-target.test.mjs index 9c05aeb..32ea659 100644 --- a/scripts/connection-target.test.mjs +++ b/scripts/connection-target.test.mjs @@ -34,6 +34,13 @@ test('SQLite metadata does not depend on hidden server fields', () => { assert.equal(connectionTargetChanged(previous, { ...previous, host: '127.0.0.1', username: '' }), false); }); +test('DuckDB target identity uses only the file path', () => { + const previous = { type: 'DuckDb', database: '/tmp/shop.duckdb' }; + assert.equal(connectionTargetChanged(previous, { ...previous, host: 'unused' }), false); + assert.equal(connectionTargetChanged(previous, { ...previous, database: '/tmp/other.duckdb' }), true); + assert.equal(connectionTargetFingerprint(previous), connectionTargetFingerprint({ ...previous, username: 'unused' })); +}); + test('saved-query target fingerprint ignores labels but detects a new destination', () => { const previous = { type: 'MySql', host: 'db.example.com', port: 3306, username: 'app', database: 'main', ssh_enabled: true, ssh_host: 'bastion-a' }; assert.equal(connectionTargetFingerprint(previous), connectionTargetFingerprint({ ...previous, name: 'Renamed', safetyMode: 'Safe' })); diff --git a/scripts/quick-open.test.mjs b/scripts/quick-open.test.mjs index 376a90c..398c360 100644 --- a/scripts/quick-open.test.mjs +++ b/scripts/quick-open.test.mjs @@ -70,6 +70,28 @@ test('quick open keeps duplicate tab and table names distinguishable by kind and assert.ok(items.find(item => item.id === 'tab:tab-remote').detail.includes('Production')); }); +test('quick open keeps DuckDB tables with the same name in different schemas distinct', () => { + const duckdbState = { + ...state, + activeConnectionId: 'duckdb', + activeDatabase: '/tmp/shop.duckdb', + openConnectionIds: ['duckdb'], + savedConnections: [{ id: 'duckdb', name: 'Shop', type: 'DuckDb', database: '/tmp/shop.duckdb' }], + databases: ['/tmp/shop.duckdb'], + sidebarItems: { duckdb: [ + { name: 'orders', schema: 'main', item_type: 'Table' }, + { name: 'orders', schema: 'archive', item_type: 'Table' }, + ] }, + tabs: [], + savedQueries: [], + queryHistory: [], + }; + const tables = buildQuickOpenItems(duckdbState).filter(item => item.kind === 'table'); + assert.deepEqual(tables.map(item => item.targetId), ['main.orders', 'archive.orders']); + assert.equal(new Set(tables.map(item => item.id)).size, 2); + assert.equal(resolveQuickOpenTarget(tables[1], duckdbState)?.targetId, 'archive.orders'); +}); + test('quick open rejects stale targets rather than using an old connection or database', () => { const items = buildQuickOpenItems(state); const table = items.find(item => item.id === 'table:local:Table:admins'); diff --git a/scripts/sql-identifiers.test.mjs b/scripts/sql-identifiers.test.mjs new file mode 100644 index 0000000..c0932ca --- /dev/null +++ b/scripts/sql-identifiers.test.mjs @@ -0,0 +1,24 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { build } from 'esbuild'; + +const { outputFiles } = await build({ + entryPoints: ['src/utils/sqlIdentifiers.ts'], + bundle: true, + platform: 'node', + format: 'esm', + write: false, +}); +const { quoteTableReference } = await import( + `data:text/javascript;base64,${Buffer.from(outputFiles[0].contents).toString('base64')}` +); + +test('DuckDB table references quote schema and table separately', () => { + assert.equal(quoteTableReference('archive.orders', 'DuckDb'), '"archive"."orders"'); + assert.equal(quoteTableReference('main.orders', 'DuckDb'), '"main"."orders"'); +}); + +test('other database table names retain their existing single-identifier quoting', () => { + assert.equal(quoteTableReference('orders', 'Postgres'), '"orders"'); + assert.equal(quoteTableReference('orders', 'MySql'), '`orders`'); +}); diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index af75bb4..20b4ce8 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -14,7 +14,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" dependencies = [ - "crypto-common", + "crypto-common 0.1.7", "generic-array", ] @@ -26,7 +26,7 @@ checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" dependencies = [ "cfg-if", "cipher", - "cpufeatures", + "cpufeatures 0.2.17", ] [[package]] @@ -61,6 +61,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" dependencies = [ "cfg-if", + "const-random", "getrandom 0.3.4", "once_cell", "version_check", @@ -127,6 +128,183 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "arrow" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d965156f2220e94dcf676e04322b7b0b41c97e85cb3e1c365b8e56d0f70be999" +dependencies = [ + "arrow-arith", + "arrow-array", + "arrow-buffer", + "arrow-cast", + "arrow-data", + "arrow-ord", + "arrow-row", + "arrow-schema", + "arrow-select", + "arrow-string", +] + +[[package]] +name = "arrow-arith" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca16fdd1daeb1eccefb42df52ec65448ef33eec72488598675d46dfa9ce58923" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-data", + "arrow-schema", + "chrono", + "num-traits", +] + +[[package]] +name = "arrow-array" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3d22a41d3df78b71df0b685af219ae3a94188ab98d6fb29c958eb55c3247f4" +dependencies = [ + "ahash 0.8.12", + "arrow-buffer", + "arrow-data", + "arrow-schema", + "chrono", + "half", + "hashbrown 0.17.1", + "libc", + "num-complex", + "num-integer", + "num-traits", +] + +[[package]] +name = "arrow-buffer" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd46a530bc1a8e763102fc36c1dc0f51f3ffa150c78e7f5658ccb54b1458ade" +dependencies = [ + "bytes", + "half", + "num-bigint 0.5.1", + "num-traits", +] + +[[package]] +name = "arrow-cast" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa83a0752814a1922d5ee75032524ca9bbfb7f60ee35a67ed4ab8b275260aaf" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-data", + "arrow-ord", + "arrow-schema", + "arrow-select", + "atoi 3.1.1", + "base64 0.23.1", + "chrono", + "comfy-table", + "half", + "lexical-core", + "num-traits", + "ryu", +] + +[[package]] +name = "arrow-cmp" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1913e57a64fbf2c4502fa6d54b547d6a1ae59e3146cbc15321d47468c937abd1" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-schema", +] + +[[package]] +name = "arrow-data" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "415b7fbc4cf56b080bb5677a4ae74211b5c79e571ed37a1d547633ec07f3b43b" +dependencies = [ + "arrow-buffer", + "arrow-schema", + "half", + "num-integer", + "num-traits", +] + +[[package]] +name = "arrow-ord" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c58f2ae6dd1480fc413f7320a547e39dcc499c23d4f36ca19572b685811e6e4" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-cmp", + "arrow-data", + "arrow-schema", + "arrow-select", +] + +[[package]] +name = "arrow-row" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f9db8345b0638416d3b56d117d262d13070fdef788c6d75b53a180215c477e" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-data", + "arrow-schema", + "half", +] + +[[package]] +name = "arrow-schema" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2c8e714b466404cdc1189e89f892426efea614d938e16c15518c340cd43ba3" +dependencies = [ + "bitflags 2.10.0", +] + +[[package]] +name = "arrow-select" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5af3f6656c5f2c6944f93d9a357a7f204c93d5f349385a44cf46c4a26fbcf30" +dependencies = [ + "ahash 0.8.12", + "arrow-array", + "arrow-buffer", + "arrow-cmp", + "arrow-data", + "arrow-schema", + "num-traits", +] + +[[package]] +name = "arrow-string" +version = "60.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab1be9306b9a413b5b004e2a5a19caac728870d76e8891b351f040d554c93862" +dependencies = [ + "arrow-array", + "arrow-buffer", + "arrow-data", + "arrow-schema", + "arrow-select", + "memchr", + "num-traits", + "regex", + "regex-syntax", +] + [[package]] name = "async-broadcast" version = "0.5.1" @@ -358,6 +536,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atoi" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d12c37257553c19e9aee6cba77814fdc9e07844abfc8b3a2c377cd920ee5a993" +dependencies = [ + "num-traits", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -445,6 +632,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block-padding" version = "0.3.3" @@ -636,6 +832,12 @@ dependencies = [ "toml 0.9.11+spec-1.1.0", ] +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cbc" version = "0.1.2" @@ -652,6 +854,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "755d2fce177175ffca841e9a06afdb2c4ab0f593d53b4dee48147dfaade85932" dependencies = [ "find-msvc-tools", + "jobserver", + "libc", "shlex", ] @@ -714,7 +918,7 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" dependencies = [ - "crypto-common", + "crypto-common 0.1.7", "inout", ] @@ -728,6 +932,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "comfy-table" +version = "8.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b96a46ad395a5e00676eddaca4a45ed78b29c08be5a878d35e4b071c625637dd" +dependencies = [ + "unicode-segmentation", + "unicode-width", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -743,6 +957,32 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + +[[package]] +name = "const-random" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom 0.2.17", + "once_cell", + "tiny-keccak", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -818,6 +1058,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crc" version = "3.4.0" @@ -866,6 +1115,12 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + [[package]] name = "crypto-common" version = "0.1.7" @@ -877,6 +1132,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + [[package]] name = "cssparser" version = "0.29.6" @@ -1015,7 +1279,7 @@ version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" dependencies = [ - "const-oid", + "const-oid 0.9.6", "pem-rfc7468", "zeroize", ] @@ -1092,12 +1356,23 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "const-oid", - "crypto-common", + "block-buffer 0.10.4", + "const-oid 0.9.6", + "crypto-common 0.1.7", "subtle", ] +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid 0.10.2", + "crypto-common 0.2.2", +] + [[package]] name = "dirs" version = "5.0.1" @@ -1194,7 +1469,7 @@ checksum = "521e380c0c8afb8d9a1e83a1822ee03556fc3e3e7dbc1fd30be14e37f9cb3f89" dependencies = [ "bit-set", "cssparser 0.36.0", - "foldhash", + "foldhash 0.2.0", "html5ever 0.38.0", "precomputed-hash", "selectors 0.36.1", @@ -1246,6 +1521,22 @@ version = "0.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f678cf4a922c215c63e0de95eb1ff08a958a81d47e485cf9da1e27bf6305cfa5" +[[package]] +name = "duckdb" +version = "1.20000.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d807ca47d4fa206f102ff568046e018fb84518eb0a0b5e65a165a2eb7bd4a7c2" +dependencies = [ + "arrow", + "cast", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink 0.10.0", + "libduckdb-sys", + "num-integer", + "strum", +] + [[package]] name = "dunce" version = "1.0.5" @@ -1399,6 +1690,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "1.9.0" @@ -1476,6 +1779,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foldhash" version = "0.2.0" @@ -1825,10 +2134,21 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "r-efi", + "r-efi 5.3.0", "wasip2", ] +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + [[package]] name = "ghash" version = "0.5.1" @@ -2006,6 +2326,18 @@ dependencies = [ "tracing", ] +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "num-traits", + "zerocopy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -2025,12 +2357,27 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + [[package]] name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "hashlink" version = "0.8.4" @@ -2040,6 +2387,15 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "hashlink" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +dependencies = [ + "hashbrown 0.15.5", +] + [[package]] name = "heck" version = "0.4.1" @@ -2088,7 +2444,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" dependencies = [ - "digest", + "digest 0.10.7", ] [[package]] @@ -2161,6 +2517,15 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "hybrid-array" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3944cf8cf766b40e2a1a333ee5e9b563f854d5fa49d6a8ca2764e97c6eddb214" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.8.1" @@ -2580,6 +2945,16 @@ dependencies = [ "syn 2.0.114", ] +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + [[package]] name = "js-sys" version = "0.3.85" @@ -2658,6 +3033,63 @@ dependencies = [ "spin", ] +[[package]] +name = "lexical-core" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d8d125a277f807e55a77304455eb7b1cb52f2b18c143b60e766c120bd64a594" +dependencies = [ + "lexical-parse-float", + "lexical-parse-integer", + "lexical-util", + "lexical-write-float", + "lexical-write-integer", +] + +[[package]] +name = "lexical-parse-float" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52a9f232fbd6f550bc0137dcb5f99ab674071ac2d690ac69704593cb4abbea56" +dependencies = [ + "lexical-parse-integer", + "lexical-util", +] + +[[package]] +name = "lexical-parse-integer" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7a039f8fb9c19c996cd7b2fcce303c1b2874fe1aca544edc85c4a5f8489b34" +dependencies = [ + "lexical-util", +] + +[[package]] +name = "lexical-util" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2604dd126bb14f13fb5d1bd6a66155079cb9fa655b37f875b3a742c705dbed17" + +[[package]] +name = "lexical-write-float" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c438c87c013188d415fbabbb1dceb44249ab81664efbd31b14ae55dabb6361" +dependencies = [ + "lexical-util", + "lexical-write-integer", +] + +[[package]] +name = "lexical-write-integer" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "409851a618475d2d5796377cad353802345cba92c867d9fbcde9cf4eac4e14df" +dependencies = [ + "lexical-util", +] + [[package]] name = "libappindicator" version = "0.9.0" @@ -2697,6 +3129,23 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libduckdb-sys" +version = "1.20000.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0140448c472968ee5cf5ba697435deebe558b366ef1e5c080d24d9696b600725" +dependencies = [ + "cc", + "flate2", + "pkg-config", + "serde", + "serde_json", + "sha2 0.11.1", + "tar", + "ureq", + "vcpkg", +] + [[package]] name = "libloading" version = "0.7.4" @@ -2865,7 +3314,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" dependencies = [ "cfg-if", - "digest", + "digest 0.10.7", ] [[package]] @@ -3033,7 +3482,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" dependencies = [ - "num-bigint", + "num-bigint 0.4.6", "num-complex", "num-integer", "num-iter", @@ -3051,6 +3500,16 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-bigint" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e7820bc0a80a0238e650327316f929ba18d5be054b647490a3a6a339f3e7c0" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint-dig" version = "0.8.6" @@ -3108,7 +3567,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "num-bigint", + "num-bigint 0.4.6", "num-integer", "num-traits", ] @@ -3841,7 +4300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.2.17", "opaque-debug", "universal-hash", ] @@ -3988,6 +4447,12 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "radium" version = "0.7.0" @@ -4331,8 +4796,8 @@ version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8573f03f5883dcaebdfcf4725caa1ecb9c15b2ef50c43a07b816e06799bb12d" dependencies = [ - "const-oid", - "digest", + "const-oid 0.9.6", + "digest 0.10.7", "num-bigint-dig", "num-integer", "num-traits", @@ -4433,6 +4898,7 @@ version = "0.23.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b" dependencies = [ + "log", "once_cell", "ring", "rustls-pki-types", @@ -4637,7 +5103,7 @@ dependencies = [ "once_cell", "rand 0.8.5", "serde", - "sha2", + "sha2 0.10.9", "zbus 3.15.2", ] @@ -4910,8 +5376,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", ] [[package]] @@ -4921,8 +5387,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -4947,7 +5424,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" dependencies = [ - "digest", + "digest 0.10.7", "rand_core 0.6.4", ] @@ -5104,6 +5581,7 @@ dependencies = [ "csv", "dirs 5.0.1", "dotenvy", + "duckdb", "futures", "keyring", "reqwest 0.12.28", @@ -5144,7 +5622,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24ba59a9342a3d9bab6c56c118be528b27c9b60e490080e9711a04dccac83ef6" dependencies = [ "ahash 0.8.12", - "atoi", + "atoi 2.0.0", "byteorder", "bytes", "chrono", @@ -5157,7 +5635,7 @@ dependencies = [ "futures-intrusive", "futures-io", "futures-util", - "hashlink", + "hashlink 0.8.4", "hex", "indexmap 2.13.0", "log", @@ -5170,7 +5648,7 @@ dependencies = [ "rustls-pemfile", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "smallvec", "sqlformat", "thiserror 1.0.69", @@ -5179,7 +5657,7 @@ dependencies = [ "tracing", "url", "uuid", - "webpki-roots", + "webpki-roots 0.25.4", ] [[package]] @@ -5210,7 +5688,7 @@ dependencies = [ "quote", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "sqlx-core", "sqlx-mysql", "sqlx-postgres", @@ -5227,14 +5705,14 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ed31390216d20e538e447a7a9b959e06ed9fc51c37b514b46eb758016ecd418" dependencies = [ - "atoi", + "atoi 2.0.0", "base64 0.21.7", "bitflags 2.10.0", "byteorder", "bytes", "chrono", "crc", - "digest", + "digest 0.10.7", "dotenvy", "either", "futures-channel", @@ -5256,7 +5734,7 @@ dependencies = [ "rust_decimal", "serde", "sha1", - "sha2", + "sha2 0.10.9", "smallvec", "sqlx-core", "stringprep", @@ -5272,7 +5750,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c824eb80b894f926f89a0b9da0c7f435d27cdd35b8c655b114e58223918577e" dependencies = [ - "atoi", + "atoi 2.0.0", "base64 0.21.7", "bitflags 2.10.0", "byteorder", @@ -5297,7 +5775,7 @@ dependencies = [ "rust_decimal", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "smallvec", "sqlx-core", "stringprep", @@ -5313,7 +5791,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa" dependencies = [ - "atoi", + "atoi 2.0.0", "chrono", "flume", "futures-channel", @@ -5422,6 +5900,27 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af23d6f6c1a224baef9d3f61e287d2761385a5b88fdab4eb4c6f11aeb54c4bcf" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.114", +] + [[package]] name = "subtle" version = "2.6.1" @@ -5678,7 +6177,7 @@ dependencies = [ "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "syn 2.0.114", "tauri-utils", "thiserror 2.0.17", @@ -6030,6 +6529,15 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinystr" version = "0.8.2" @@ -6425,6 +6933,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +[[package]] +name = "unicode-width" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" + [[package]] name = "unicode_categories" version = "0.1.1" @@ -6437,7 +6951,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ - "crypto-common", + "crypto-common 0.1.7", "subtle", ] @@ -6447,6 +6961,34 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7ac20be9b7726e0bbdbf974c059676d9acb1cd414961f570a4e8231cacd7fc" +dependencies = [ + "base64 0.23.1", + "log", + "percent-encoding", + "rustls 0.23.36", + "rustls-pki-types", + "ureq-proto", + "utf8-zero", + "webpki-roots 1.0.11", +] + +[[package]] +name = "ureq-proto" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841583fc36089d7d59419568ae157f43a5a03a2e5a81ba87c77d1596ef46e922" +dependencies = [ + "base64 0.23.1", + "http", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.8" @@ -6484,6 +7026,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8-zero" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -6745,6 +7293,15 @@ version = "0.25.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" +[[package]] +name = "webpki-roots" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "webview2-com" version = "0.38.2" @@ -6813,7 +7370,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] @@ -7372,7 +7929,7 @@ dependencies = [ "once_cell", "percent-encoding", "raw-window-handle", - "sha2", + "sha2 0.10.9", "soup3", "tao-macros", "thiserror 2.0.17", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index c4253d3..98380da 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -27,6 +27,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["full"] } sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "mysql", "sqlite", "chrono", "uuid", "json", "rust_decimal"] } +duckdb = { version = "=1.20000.1", features = ["bundled"] } keyring = "2" aes-gcm = "0.10" anyhow = "1.0" diff --git a/src-tauri/src/core/connection_manager.rs b/src-tauri/src/core/connection_manager.rs index 4087c9d..79e6b70 100644 --- a/src-tauri/src/core/connection_manager.rs +++ b/src-tauri/src/core/connection_manager.rs @@ -1,4 +1,4 @@ -use crate::core::{execution_policy::SafetyMode, ConnectionConfig, DatabaseType}; +use crate::core::{duckdb_adapter, execution_policy::SafetyMode, ConnectionConfig, DatabaseType}; use anyhow::{anyhow, Result}; use sqlx::mysql::{MySqlConnectOptions, MySqlSslMode}; use sqlx::postgres::{PgConnectOptions, PgSslMode}; @@ -75,7 +75,7 @@ fn revoke_ai_on_production_transition( fn validate_connection_config(config: &ConnectionConfig) -> Result<()> { match config.db_type { - DatabaseType::Sqlite => { + DatabaseType::Sqlite | DatabaseType::DuckDb => { if config .database .as_deref() @@ -83,7 +83,7 @@ fn validate_connection_config(config: &ConnectionConfig) -> Result<()> { .trim() .is_empty() { - return Err(anyhow!("SQLite database file path is required")); + return Err(anyhow!("Database file path is required")); } } DatabaseType::Postgres | DatabaseType::MySql => { @@ -120,7 +120,7 @@ fn validate_connection_config(config: &ConnectionConfig) -> Result<()> { DatabaseType::MySql => { mysql_ssl_mode(config)?; } - DatabaseType::Sqlite => unreachable!(), + DatabaseType::Sqlite | DatabaseType::DuckDb => unreachable!(), } if config.ssh_enabled && config.ssl_mode.as_deref() == Some("verify-full") { return Err(anyhow!("Verify-full over SSH is not supported because the tunnel uses localhost; use verify-ca or connect directly")); @@ -222,6 +222,10 @@ impl ConnectionManager { password: Option, ) -> Result<()> { validate_connection_config(&config)?; + if matches!(config.db_type, DatabaseType::DuckDb) { + config.safety_mode = SafetyMode::Safe; + config.ai_enabled = Some(false); + } let id = config.id; let _guard = self.context_lock(&id).await.write_owned().await; let result = match config.db_type { @@ -231,6 +235,13 @@ impl ConnectionManager { } DatabaseType::MySql => self.connect_mysql(config.clone(), password.clone()).await, DatabaseType::Sqlite => self.connect_sqlite(config.clone()).await, + DatabaseType::DuckDb => { + let path = config + .database + .clone() + .ok_or_else(|| anyhow!("DuckDB file path is required"))?; + duckdb_adapter::run(path, |_| Ok(())).await + } }; result?; match config.db_type { @@ -254,7 +265,7 @@ impl ConnectionManager { pool.close().await; } } - DatabaseType::Sqlite => { + DatabaseType::Sqlite | DatabaseType::DuckDb => { let stale_postgres = self.postgres_pools.lock().await.remove(&id); let stale_mysql = self.mysql_pools.lock().await.remove(&id); if let Some(pool) = stale_postgres { @@ -287,6 +298,7 @@ impl ConnectionManager { DatabaseType::Postgres => "postgres".to_string(), DatabaseType::MySql => String::new(), DatabaseType::Sqlite => String::new(), + DatabaseType::DuckDb => String::new(), }); Ok((database, config.safety_mode)) } @@ -297,6 +309,9 @@ impl ConnectionManager { let config = configs .get_mut(id) .ok_or_else(|| anyhow!("Connection not found"))?; + if matches!(config.db_type, DatabaseType::DuckDb) && mode != SafetyMode::Safe { + return Err(anyhow!("DuckDB connections are read-only")); + } config.safety_mode = mode; Ok(()) } @@ -451,7 +466,7 @@ impl ConnectionManager { let remote_db_port = config.port.unwrap_or(match config.db_type { DatabaseType::Postgres => 5432, DatabaseType::MySql => 3306, - DatabaseType::Sqlite => unreachable!(), + DatabaseType::Sqlite | DatabaseType::DuckDb => unreachable!(), }); let sess_arc = Arc::new(std::sync::Mutex::new(sess)); @@ -748,6 +763,10 @@ impl ConnectionManager { DatabaseType::Sqlite => Err(anyhow!( "SQLite does not support switching databases within the same file connection." )), + DatabaseType::DuckDb if db_name == config.database.as_deref().unwrap_or_default() => { + Ok(()) + } + DatabaseType::DuckDb => Err(anyhow!("DuckDB file connections cannot switch databases")), }; if result.is_ok() { diff --git a/src-tauri/src/core/duckdb_adapter.rs b/src-tauri/src/core/duckdb_adapter.rs new file mode 100644 index 0000000..4183b6a --- /dev/null +++ b/src-tauri/src/core/duckdb_adapter.rs @@ -0,0 +1,524 @@ +use crate::core::{ + read_only_sql::validate_read_only_sql, FilterConfig, QueryResult, SidebarItem, SidebarItemType, + TableColumnStructure, TableStructure, +}; +use anyhow::{anyhow, Result}; +use chrono::{DateTime, Duration, NaiveDate, NaiveTime, Utc}; +use duckdb::{types::ValueRef, AccessMode, Config, Connection}; +use serde_json::Value; +use std::{path::Path, time::Instant}; +use tokio_util::sync::CancellationToken; + +const MAX_RESULT_ROWS: usize = 10_000; + +pub fn open(path: &str) -> Result { + let file = Path::new(path); + if !file.is_file() { + return Err(anyhow!("DuckDB file does not exist: {path}")); + } + let config = Config::default() + .access_mode(AccessMode::ReadOnly)? + .enable_external_access(false)? + .enable_autoload_extension(false)?; + Ok(Connection::open_with_flags(file, config)?) +} + +pub async fn run( + path: String, + work: impl FnOnce(&Connection) -> Result + Send + 'static, +) -> Result { + tokio::task::spawn_blocking(move || { + let connection = open(&path)?; + work(&connection) + }) + .await? +} + +pub async fn run_cancellable( + path: String, + token: CancellationToken, + work: impl FnOnce(&Connection) -> Result + Send + 'static, +) -> Result> { + let (sender, receiver) = tokio::sync::oneshot::channel(); + let mut task = tokio::task::spawn_blocking(move || { + let connection = open(&path)?; + let _ = sender.send(connection.interrupt_handle()); + work(&connection) + }); + let handle = match receiver.await { + Ok(handle) => handle, + Err(_) => return task.await?.map(Some), + }; + tokio::select! { + result = &mut task => result?.map(Some), + _ = token.cancelled() => { + handle.interrupt(); + let _ = task.await; + Ok(None) + } + } +} + +fn json_value(value: ValueRef<'_>) -> Value { + match value { + ValueRef::Null => Value::Null, + ValueRef::Boolean(value) => Value::Bool(value), + ValueRef::TinyInt(value) => Value::from(value), + ValueRef::SmallInt(value) => Value::from(value), + ValueRef::Int(value) => Value::from(value), + ValueRef::BigInt(value) if value.unsigned_abs() > 9_007_199_254_740_991 => { + Value::String(value.to_string()) + } + ValueRef::BigInt(value) => Value::from(value), + ValueRef::UTinyInt(value) => Value::from(value), + ValueRef::USmallInt(value) => Value::from(value), + ValueRef::UInt(value) => Value::from(value), + ValueRef::UBigInt(value) if value > 9_007_199_254_740_991 => { + Value::String(value.to_string()) + } + ValueRef::UBigInt(value) => Value::from(value), + ValueRef::Float(value) => Value::from(value as f64), + ValueRef::Double(value) => Value::from(value), + ValueRef::Text(value) => Value::String(String::from_utf8_lossy(value).into_owned()), + ValueRef::HugeInt(value) => Value::String(value.to_string()), + ValueRef::UHugeInt(value) => Value::String(value.to_string()), + ValueRef::Decimal(value) => Value::String(value.to_string()), + ValueRef::Date32(days) => { + let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(); + epoch + .checked_add_signed(Duration::days(days as i64)) + .map(|date| Value::String(date.to_string())) + .unwrap_or(Value::Null) + } + ValueRef::Timestamp(unit, value) => { + DateTime::::from_timestamp_micros(unit.to_micros(value)) + .map(|time| Value::String(time.naive_utc().to_string())) + .unwrap_or(Value::Null) + } + ValueRef::Time64(unit, value) => { + let micros = unit.to_micros(value); + u32::try_from(micros.div_euclid(1_000_000)) + .ok() + .and_then(|seconds| { + NaiveTime::from_num_seconds_from_midnight_opt( + seconds, + (micros.rem_euclid(1_000_000) * 1_000) as u32, + ) + }) + .map(|time| Value::String(time.to_string())) + .unwrap_or(Value::Null) + } + other => Value::String(format!("{other:?}")), + } +} + +fn quote_ident(name: &str) -> String { + format!("\"{}\"", name.replace('"', "\"\"")) +} + +fn quote_table(name: &str) -> String { + name.split('.') + .map(quote_ident) + .collect::>() + .join(".") +} + +fn literal(value: &Value) -> Result { + match value { + Value::Null => Ok("NULL".into()), + Value::Bool(value) => Ok(value.to_string()), + Value::Number(value) => Ok(value.to_string()), + Value::String(value) => Ok(format!("'{}'", value.replace('\'', "''"))), + _ => Err(anyhow!("Unsupported keyset cursor")), + } +} + +pub fn query( + connection: &Connection, + sql: &str, + limit: usize, + offset: usize, +) -> Result { + validate_read_only_sql(sql).map_err(|error| anyhow!(error))?; + let start = Instant::now(); + let statement = sql.trim().trim_end_matches(';').trim(); + let bounded = format!( + "SELECT * FROM (\n{statement}\n) AS sqlmate_result LIMIT {} OFFSET {offset}", + limit.min(MAX_RESULT_ROWS) + 1 + ); + let mut prepared = connection.prepare(&bounded)?; + let mut cursor = prepared.query([])?; + let columns = cursor + .as_ref() + .ok_or_else(|| anyhow!("DuckDB result metadata is unavailable"))? + .column_names(); + let mut rows = Vec::new(); + let mut has_more = false; + while let Some(row) = cursor.next()? { + if rows.len() == limit.min(MAX_RESULT_ROWS) { + has_more = true; + break; + } + rows.push( + (0..columns.len()) + .map(|index| row.get_ref(index).map(json_value)) + .collect::>>()?, + ); + } + Ok(QueryResult { + columns, + rows, + affected_rows: 0, + execution_time_ms: start.elapsed().as_millis() as u64, + total_count: None, + page: None, + page_size: None, + has_more: Some(has_more), + }) +} + +pub fn sidebar(connection: &Connection) -> Result> { + let mut statement = connection.prepare("SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog') ORDER BY table_schema, table_name")?; + let items = statement.query_map([], |row| { + let schema: String = row.get(0)?; + let name: String = row.get(1)?; + let kind: String = row.get(2)?; + Ok(SidebarItem { + name, + schema: Some(schema), + item_type: if kind == "VIEW" { + SidebarItemType::View + } else { + SidebarItemType::Table + }, + }) + })?; + items + .collect::>>() + .map_err(Into::into) +} + +pub fn structure(connection: &Connection, table: &str) -> Result { + let (schema, name) = table + .rsplit_once('.') + .map_or(("main", table), |(schema, name)| (schema, name)); + let mut statement = connection.prepare("SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position")?; + let primary_keys: Vec = connection.prepare("SELECT k.column_name FROM information_schema.key_column_usage k JOIN information_schema.table_constraints c ON c.constraint_name = k.constraint_name AND c.table_schema = k.table_schema AND c.table_name = k.table_name WHERE c.constraint_type = 'PRIMARY KEY' AND k.table_schema = ? AND k.table_name = ?")? + .query_map([schema, name], |row| row.get(0))?.collect::>>()?; + let columns = statement + .query_map([schema, name], |row| { + let nullable: String = row.get(2)?; + let column_name: String = row.get(0)?; + Ok(TableColumnStructure { + is_primary_key: primary_keys.contains(&column_name), + name: column_name, + data_type: row.get(1)?, + is_nullable: nullable == "YES", + default_value: row.get(3)?, + comment: None, + }) + })? + .collect::>>()?; + if columns.is_empty() { + return Err(anyhow!("Table not found: {table}")); + } + Ok(TableStructure { + columns, + indexes: vec![], + constraints: vec![], + foreign_keys: vec![], + }) +} + +pub fn table_data( + connection: &Connection, + table: &str, + limit: u32, + offset: u32, + filters: Vec, + sort: Option, + direction: Option, + primary_keys: Vec, + seek: Option, +) -> Result { + if filters.iter().any(|filter| filter.enabled) { + return Err(anyhow!("Filters are not yet supported for DuckDB tables")); + } + if seek.is_some() && (sort.is_some() || primary_keys.len() != 1) { + return Err(anyhow!( + "Keyset paging requires one primary key and default sort" + )); + } + let mut order_terms = Vec::new(); + if let Some(column) = sort.as_deref().filter(|column| !column.is_empty()) { + let direction = if direction.as_deref() == Some("DESC") { + "DESC" + } else { + "ASC" + }; + order_terms.push(format!("{} {direction}", quote_ident(column))); + } + for column in &primary_keys { + if sort.as_deref() != Some(column.as_str()) { + order_terms.push(format!("{} ASC", quote_ident(column))); + } + } + let order_sql = if order_terms.is_empty() { + String::new() + } else { + format!(" ORDER BY {}", order_terms.join(", ")) + }; + let seek_sql = match seek { + Some(value) if primary_keys.len() == 1 && direction.as_deref() != Some("DESC") => format!( + " WHERE {} > {}", + quote_ident(&primary_keys[0]), + literal(&value)? + ), + Some(_) => return Err(anyhow!("Unsupported DuckDB keyset cursor")), + None => String::new(), + }; + let sql = format!( + "SELECT * FROM {}{}{}", + quote_table(table), + seek_sql, + order_sql + ); + query( + connection, + &sql, + limit.clamp(1, 1000) as usize, + if seek_sql.is_empty() { + offset as usize + } else { + 0 + }, + ) +} + +pub fn count(connection: &Connection, table: &str) -> Result { + let sql = format!("SELECT COUNT(*) FROM {}", quote_table(table)); + Ok(connection.query_row(&sql, [], |row| row.get::<_, i64>(0))? as u64) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::core::{ + connection_manager::ConnectionManager, execution_policy::SafetyMode, + query_engine::QueryEngine, ConnectionConfig, + }; + use serde_json::json; + use uuid::Uuid; + #[test] + fn opens_existing_database_read_only_and_bounds_results() { + let file = + std::env::temp_dir().join(format!("sqlmate-duckdb-{}.duckdb", uuid::Uuid::new_v4())); + { + let connection = Connection::open(&file).unwrap(); + connection.execute_batch("CREATE TABLE items(id INTEGER PRIMARY KEY, name VARCHAR); INSERT INTO items VALUES (1, 'a'), (2, 'b'), (3, 'c')").unwrap(); + } + let connection = open(file.to_str().unwrap()).unwrap(); + assert_eq!(sidebar(&connection).unwrap().len(), 1); + let columns = structure(&connection, "items").unwrap().columns; + assert_eq!(columns.len(), 2); + assert!(columns[0].is_primary_key); + let result = query(&connection, "SELECT * FROM items ORDER BY id", 2, 0).unwrap(); + assert_eq!(result.rows.len(), 2); + assert_eq!(result.has_more, Some(true)); + assert_eq!( + query(&connection, "SELECT * FROM items -- browse items", 2, 0) + .unwrap() + .rows + .len(), + 2 + ); + assert_eq!( + query(&connection, "SELECT * FROM items ORDER BY id", 2, 2) + .unwrap() + .has_more, + Some(false) + ); + assert_eq!( + table_data(&connection, "items", 2, 0, vec![], None, None, vec![], None) + .unwrap() + .rows + .len(), + 2 + ); + assert!(query(&connection, "DELETE FROM items", 10, 0).is_err()); + assert!(query(&connection, "SELECT * FROM read_csv('/etc/passwd')", 10, 0).is_err()); + assert!(connection + .execute("INSERT INTO items VALUES (4, 'd')", []) + .is_err()); + drop(connection); + std::fs::remove_file(file).unwrap(); + } + + #[test] + fn table_browse_orders_by_every_composite_primary_key_column() { + let connection = Connection::open_in_memory().unwrap(); + connection + .execute_batch( + "CREATE TABLE parts(a INTEGER, b INTEGER, PRIMARY KEY(a, b)); \ + INSERT INTO parts VALUES (1, 2), (1, 1), (2, 1)", + ) + .unwrap(); + let result = table_data( + &connection, + "parts", + 3, + 0, + vec![], + None, + None, + vec!["a".into(), "b".into()], + None, + ) + .unwrap(); + assert_eq!( + result.rows, + vec![ + vec![json!(1), json!(1)], + vec![json!(1), json!(2)], + vec![json!(2), json!(1)] + ] + ); + } + + #[test] + fn schema_qualified_browse_keeps_same_named_tables_separate() { + let connection = Connection::open_in_memory().unwrap(); + connection + .execute_batch( + "CREATE TABLE main.items(id INTEGER PRIMARY KEY, label VARCHAR); \ + INSERT INTO main.items VALUES (1, 'current'); \ + CREATE SCHEMA archive; \ + CREATE TABLE archive.items(id INTEGER PRIMARY KEY, label VARCHAR); \ + INSERT INTO archive.items VALUES (1, 'old')", + ) + .unwrap(); + let items = sidebar(&connection).unwrap(); + assert!(items + .iter() + .any(|item| item.name == "items" && item.schema.as_deref() == Some("main"))); + assert!(items + .iter() + .any(|item| item.name == "items" && item.schema.as_deref() == Some("archive"))); + assert_eq!( + structure(&connection, "archive.items") + .unwrap() + .columns + .len(), + 2 + ); + let rows = table_data( + &connection, + "archive.items", + 10, + 0, + vec![], + None, + None, + vec!["id".into()], + None, + ) + .unwrap() + .rows; + assert_eq!(rows[0][1], json!("old")); + } + + #[tokio::test] + async fn connection_manager_routes_browse_profile_and_select_without_writes() { + let file = + std::env::temp_dir().join(format!("sqlmate-duckdb-route-{}.duckdb", Uuid::new_v4())); + { + let connection = Connection::open(&file).unwrap(); + connection.execute_batch("CREATE TABLE products(id INTEGER PRIMARY KEY, price DECIMAL(8, 2)); INSERT INTO products VALUES (1, 12.50), (2, 9.99)").unwrap(); + } + let id = Uuid::new_v4(); + let config: ConnectionConfig = serde_json::from_value(json!({ + "id": id, "name": "shop", "db_type": "DuckDb", "database": file.to_str().unwrap(), + "host": null, "port": null, "username": null, "ssl_enabled": false, + "ssh_enabled": false, "safety_mode": "Silent", "environment": "local" + })) + .unwrap(); + let manager = ConnectionManager::new(); + manager.connect(config, None).await.unwrap(); + assert_eq!( + manager.execution_context(&id).await.unwrap().1, + SafetyMode::Safe + ); + assert!(manager + .set_safety_mode(&id, SafetyMode::Silent) + .await + .is_err()); + assert_eq!( + QueryEngine::get_sidebar_items(&manager, &id) + .await + .unwrap() + .len(), + 1 + ); + let structure = QueryEngine::get_table_structure(&manager, &id, "products") + .await + .unwrap(); + assert!(structure.columns[0].is_primary_key); + let page = QueryEngine::get_table_data( + &manager, + &id, + "products", + 1, + 0, + vec![], + None, + None, + vec!["id".into()], + None, + ) + .await + .unwrap(); + assert_eq!(page.rows.len(), 1); + assert_eq!(page.has_more, Some(true)); + let next = QueryEngine::get_table_data( + &manager, + &id, + "products", + 1, + 1, + vec![], + None, + None, + vec!["id".into()], + Some(json!(1)), + ) + .await + .unwrap(); + assert_eq!(next.rows[0][0], json!(2)); + let sample = QueryEngine::get_table_data( + &manager, + &id, + "products", + 100, + 0, + vec![], + None, + None, + vec!["id".into()], + None, + ) + .await + .unwrap(); + assert_eq!(sample.rows.len(), 2); + assert!(QueryEngine::execute_read_only_query( + &manager, + &id, + "UPDATE products SET price = 0", + None, + None + ) + .await + .is_err()); + manager.disconnect(&id).await.unwrap(); + std::fs::remove_file(file).unwrap(); + } +} diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 4554588..5c33496 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -4,13 +4,14 @@ pub mod ai_review; pub mod ai_service; pub mod connection_manager; pub mod data_compare; +pub mod duckdb_adapter; pub mod execution_policy; pub mod query_engine; pub mod query_parameters; pub mod read_only_sql; +pub mod relationships; pub mod row_commit; pub mod schema_snapshot; -pub mod relationships; pub mod telemetry; use ai_review::{AiFailedQueryStore, AiProviderTestStore, AiReviewStore}; @@ -25,6 +26,7 @@ pub enum DatabaseType { Postgres, MySql, Sqlite, + DuckDb, } use std::collections::HashMap; @@ -62,6 +64,9 @@ pub struct ConnectionConfig { impl ConnectionConfig { pub fn ai_allowed(&self) -> bool { + if matches!(self.db_type, DatabaseType::DuckDb) { + return false; + } match self.environment.as_deref().map(str::trim) { Some("production") => self.ai_enabled == Some(true), Some("local" | "test" | "dev" | "staging") => self.ai_enabled != Some(false), diff --git a/src-tauri/src/core/query_engine.rs b/src-tauri/src/core/query_engine.rs index 8b3c213..d8a2aae 100644 --- a/src-tauri/src/core/query_engine.rs +++ b/src-tauri/src/core/query_engine.rs @@ -1,5 +1,5 @@ use crate::core::{ - connection_manager::ConnectionManager, query_parameters::ParameterValue, + connection_manager::ConnectionManager, duckdb_adapter, query_parameters::ParameterValue, read_only_sql::validate_read_only_sql, AiSchemaTable, DatabaseType, FilterConfig, ForeignKeyStructure, QueryPlanResult, QueryResult, SidebarItem, SidebarItemType, StreamingBatch, StreamingComplete, StreamingMetadata, TableColumnStructure, @@ -235,6 +235,7 @@ fn build_explain_sql(sql: &str, database_type: &DatabaseType) -> Result DatabaseType::Postgres => "EXPLAIN (FORMAT JSON)", DatabaseType::MySql => "EXPLAIN", DatabaseType::Sqlite => "EXPLAIN QUERY PLAN", + DatabaseType::DuckDb => return Err(anyhow!("EXPLAIN is not yet supported for DuckDB")), }; Ok(format!("{prefix} {statement}")) } @@ -1315,6 +1316,15 @@ async fn cancel_mysql_statement(pool: &sqlx::MySqlPool, connection_id: u64) { pub struct QueryEngine; +async fn duckdb_path(manager: &ConnectionManager, id: &Uuid) -> Result> { + let config = manager.get_config(id).await?; + Ok(if matches!(config.db_type, DatabaseType::DuckDb) { + config.database + } else { + None + }) +} + impl QueryEngine { pub async fn explain_query( manager: &ConnectionManager, @@ -1387,12 +1397,14 @@ impl QueryEngine { } Value::Array(steps) } + DatabaseType::DuckDb => return Err(anyhow!("EXPLAIN is not yet supported for DuckDB")), }; let database_type = match config.db_type { DatabaseType::Postgres => "Postgres", DatabaseType::MySql => "MySql", DatabaseType::Sqlite => "Sqlite", + DatabaseType::DuckDb => "DuckDb", }; Ok(QueryPlanResult { database_type: database_type.to_string(), @@ -1411,6 +1423,57 @@ impl QueryEngine { read_only: bool, parameter_values: Option<&[ParameterValue]>, ) -> Result<()> { + let config = manager.get_config(connection_id).await?; + if matches!(config.db_type, DatabaseType::DuckDb) { + if parameter_values.is_some() { + return Err(anyhow!("Query parameters are not yet supported for DuckDB")); + } + let path = config + .database + .ok_or_else(|| anyhow!("DuckDB file path is missing"))?; + let query_sql = sql.to_string(); + let result = match duckdb_adapter::run_cancellable(path, token.clone(), move |conn| { + duckdb_adapter::query(conn, &query_sql, MAX_QUERY_RESULT_ROWS as usize, 0) + }) + .await? + { + Some(result) => result, + None => return Ok(()), + }; + if token.is_cancelled() { + return Ok(()); + } + window.emit( + "query-metadata", + StreamingMetadata { + query_id, + columns: result.columns, + }, + )?; + for batch in result.rows.chunks(1000) { + if token.is_cancelled() { + return Ok(()); + } + window.emit( + "query-batch", + StreamingBatch { + query_id, + rows: batch.to_vec(), + }, + )?; + } + window.emit( + "query-complete", + StreamingComplete { + query_id, + execution_time_ms: result.execution_time_ms, + total_rows: result.rows.len() as u64, + affected_rows: 0, + truncated: result.has_more.unwrap_or(false), + }, + )?; + return Ok(()); + } let start = Instant::now(); use futures::StreamExt; @@ -1721,6 +1784,17 @@ impl QueryEngine { page: Option, page_size: Option, ) -> Result { + if let Some(path) = duckdb_path(manager, connection_id).await? { + let sql = sql.to_string(); + let limit = page_size + .unwrap_or(MAX_QUERY_RESULT_ROWS as u32) + .clamp(1, MAX_QUERY_RESULT_ROWS as u32) as usize; + let offset = page.unwrap_or(0).saturating_mul(page_size.unwrap_or(0)) as usize; + return duckdb_adapter::run(path, move |conn| { + duckdb_adapter::query(conn, &sql, limit, offset) + }) + .await; + } let start = Instant::now(); let final_sql = match (page, page_size) { (Some(page), Some(size)) if size > 0 => { @@ -1815,6 +1889,10 @@ impl QueryEngine { page: Option, page_size: Option, ) -> Result { + if duckdb_path(manager, connection_id).await?.is_some() { + return Self::execute_read_only_query(manager, connection_id, sql, page, page_size) + .await; + } let start = Instant::now(); let mut total_count = None; let mut final_sql = sql.to_string(); @@ -2064,6 +2142,9 @@ impl QueryEngine { manager: &ConnectionManager, connection_id: &Uuid, ) -> Result> { + if let Some(path) = duckdb_path(manager, connection_id).await? { + return Ok(vec![path]); + } // Check Postgres { let pools = manager.get_postgres_pools().await; @@ -2112,6 +2193,16 @@ impl QueryEngine { manager: &ConnectionManager, connection_id: &Uuid, ) -> Result> { + if let Some(path) = duckdb_path(manager, connection_id).await? { + return duckdb_adapter::run(path, |conn| { + Ok(duckdb_adapter::sidebar(conn)? + .into_iter() + .filter(|item| matches!(item.item_type, SidebarItemType::Table)) + .map(|item| item.name) + .collect()) + }) + .await; + } // Check Postgres { let pools = manager.get_postgres_pools().await; @@ -2169,6 +2260,23 @@ impl QueryEngine { primary_keys: Vec, seek_after: Option, ) -> Result { + if let Some(path) = duckdb_path(manager, connection_id).await? { + let table_name = table_name.to_string(); + return duckdb_adapter::run(path, move |conn| { + duckdb_adapter::table_data( + conn, + &table_name, + limit, + offset, + filters, + sort_column, + sort_direction, + primary_keys, + seek_after, + ) + }) + .await; + } let limit = limit.clamp(1, 1000); if seek_after.is_some() && (sort_column.is_some() || primary_keys.len() != 1) { return Err(anyhow!( @@ -2297,6 +2405,14 @@ impl QueryEngine { table_name: &str, filters: Vec, ) -> Result { + if let Some(path) = duckdb_path(manager, connection_id).await? { + if filters.iter().any(|filter| filter.enabled) { + return Err(anyhow!("Filters are not yet supported for DuckDB tables")); + } + let table = table_name.to_string(); + return duckdb_adapter::run(path, move |conn| duckdb_adapter::count(conn, &table)) + .await; + } // Check Postgres { let pools = manager.get_postgres_pools().await; @@ -2358,6 +2474,14 @@ impl QueryEngine { connection_id: &Uuid, table_name: &str, ) -> Result { + if duckdb_path(manager, connection_id).await?.is_some() { + return Ok(TableMetadata { + total_size: None, + data_size: None, + index_size: None, + comment: Some("Read-only DuckDB file".into()), + }); + } // Check Postgres { let pools = manager.get_postgres_pools().await; @@ -2619,6 +2743,11 @@ impl QueryEngine { connection_id: &Uuid, table_name: &str, ) -> Result { + if let Some(path) = duckdb_path(manager, connection_id).await? { + let table = table_name.to_string(); + return duckdb_adapter::run(path, move |conn| duckdb_adapter::structure(conn, &table)) + .await; + } let db_type = { if manager .get_postgres_pools() @@ -3082,6 +3211,9 @@ impl QueryEngine { manager: &ConnectionManager, connection_id: &Uuid, ) -> Result> { + if let Some(path) = duckdb_path(manager, connection_id).await? { + return duckdb_adapter::run(path, duckdb_adapter::sidebar).await; + } let mut items = Vec::new(); // Check Postgres @@ -3227,6 +3359,20 @@ impl QueryEngine { table_name: &str, schema: Option<&str>, ) -> Result> { + if let Some(path) = duckdb_path(manager, connection_id).await? { + let table = schema.map_or_else( + || table_name.to_string(), + |schema| format!("{schema}.{table_name}"), + ); + return duckdb_adapter::run(path, move |conn| { + Ok(duckdb_adapter::structure(conn, &table)? + .columns + .into_iter() + .map(|column| column.name) + .collect()) + }) + .await; + } if let Some(pool) = manager .get_postgres_pools() .await diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ee8082f..d33c0f9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -11,12 +11,12 @@ use crate::core::ai_credentials::{ self, AiKeyStore, AiProviderId, AiProviderStatus, AiSettings, KeychainAiStore, }; use crate::core::ai_review::{self, AiAction, AiReviewPreview, FailedAiQuery}; +use crate::core::data_compare::{get_comparison_rows as read_comparison_rows, ComparisonRows}; use crate::core::execution_policy::{ExecutionPolicy, Operation, SafetyMode}; use crate::core::query_engine::QueryEngine; use crate::core::query_parameters::{compile_query, DatabaseDialect, QueryParameter}; -use crate::core::schema_snapshot::{get_schema_snapshot as read_schema_snapshot, SchemaSnapshot}; -use crate::core::data_compare::{get_comparison_rows as read_comparison_rows, ComparisonRows}; use crate::core::relationships::{get_relationships as read_relationships, Relationship}; +use crate::core::schema_snapshot::{get_schema_snapshot as read_schema_snapshot, SchemaSnapshot}; use crate::core::{ connection_manager::ConnectionManager, telemetry, AiSchemaCacheEntry, AiSchemaTable, AppState, ConnectionConfig, DatabaseType, FilterConfig, QueryPlanResult, QueryResult, SidebarItem, @@ -398,6 +398,9 @@ async fn execute_query_streaming( DatabaseType::Postgres => DatabaseDialect::Postgres, DatabaseType::MySql => DatabaseDialect::MySql, DatabaseType::Sqlite => DatabaseDialect::Sqlite, + DatabaseType::DuckDb => { + return Err("Query parameters are not yet supported for DuckDB".to_string()) + } }; Some(compile_query(&sql, parameters, dialect)?) } else { @@ -891,9 +894,15 @@ async fn get_comparison_rows( table: String, limit: u32, ) -> Result { - read_comparison_rows(&state.connection_manager, &connection_id, schema, table, limit) - .await - .map_err(|error| error.to_string()) + read_comparison_rows( + &state.connection_manager, + &connection_id, + schema, + table, + limit, + ) + .await + .map_err(|error| error.to_string()) } #[tauri::command] diff --git a/src-tauri/tests/cross_engine_smoke.rs b/src-tauri/tests/cross_engine_smoke.rs index 185b67e..51aa9f5 100644 --- a/src-tauri/tests/cross_engine_smoke.rs +++ b/src-tauri/tests/cross_engine_smoke.rs @@ -266,7 +266,7 @@ async fn run_smoke(database_type: DatabaseType, port: u16) { .unwrap(); assert_eq!(row.0, "original"); } - DatabaseType::Sqlite => unreachable!(), + DatabaseType::Sqlite | DatabaseType::DuckDb => unreachable!(), } assert!(QueryEngine::execute_read_only_query( &manager, diff --git a/src/components/ConnectionModal.tsx b/src/components/ConnectionModal.tsx index c293951..0ad9d83 100644 --- a/src/components/ConnectionModal.tsx +++ b/src/components/ConnectionModal.tsx @@ -15,7 +15,8 @@ import { aiAccessAfterEnvironmentChange, effectiveAiAccess } from '../utils/aiCo const cn = (...inputs: any[]) => twMerge(clsx(inputs)); export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenChange: (open: boolean) => void }) => { - const [selectedType, setSelectedType] = useState<'Postgres' | 'MySql' | 'Sqlite'>('Postgres'); + const [selectedType, setSelectedType] = useState<'Postgres' | 'MySql' | 'Sqlite' | 'DuckDb'>('Postgres'); + const isLocalFile = selectedType === 'Sqlite' || selectedType === 'DuckDb'; const [name, setName] = useState(''); const [host, setHost] = useState('127.0.0.1'); const [port, setPort] = useState('5432'); @@ -150,31 +151,31 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC id: editingId || uuidv4(), name: name || 'Untitled Connection', db_type: selectedType, - host: selectedType === 'Sqlite' ? null : host, - port: selectedType === 'Sqlite' ? null : parseInt(port), - username: selectedType === 'Sqlite' ? null : user, + host: isLocalFile ? null : host, + port: isLocalFile ? null : parseInt(port), + username: isLocalFile ? null : user, database: database, - ssl_enabled: selectedType !== 'Sqlite' && sslEnabled, + ssl_enabled: !isLocalFile && sslEnabled, ssl_mode: sslMode, ssl_ca_path: sslCA || null, ssl_cert_path: sslCert || null, ssl_key_path: sslKey || null, - ssh_enabled: selectedType !== 'Sqlite' && sshEnabled, - ssh_host: selectedType !== 'Sqlite' && sshEnabled ? sshHost || null : null, - ssh_port: selectedType !== 'Sqlite' && sshEnabled ? parseInt(sshPort) : null, - ssh_username: selectedType !== 'Sqlite' && sshEnabled ? sshUser || null : null, + ssh_enabled: !isLocalFile && sshEnabled, + ssh_host: !isLocalFile && sshEnabled ? sshHost || null : null, + ssh_port: !isLocalFile && sshEnabled ? parseInt(sshPort) : null, + ssh_username: !isLocalFile && sshEnabled ? sshUser || null : null, ssh_auth_method: sshAuth, - ssh_password: selectedType !== 'Sqlite' && sshEnabled ? sshPass || null : null, - ssh_private_key_path: selectedType !== 'Sqlite' && sshEnabled ? sshKey || null : null, + ssh_password: !isLocalFile && sshEnabled ? sshPass || null : null, + ssh_private_key_path: !isLocalFile && sshEnabled ? sshKey || null : null, environment: tag, color_tag: color, - safety_mode: safetyMode, - ai_enabled: aiEnabled, + safety_mode: selectedType === 'DuckDb' ? 'Safe' : safetyMode, + ai_enabled: selectedType === 'DuckDb' ? false : aiEnabled, }); const validateForm = (): string | null => { - if (!database.trim()) return selectedType === 'Sqlite' ? 'Choose a database file.' : 'Enter a database name.'; - if (selectedType !== 'Sqlite') { + if (!database.trim()) return isLocalFile ? 'Choose a database file.' : 'Enter a database name.'; + if (!isLocalFile) { if (!host.trim()) return 'Enter a database host.'; if (!Number.isInteger(Number(port)) || Number(port) < 1 || Number(port) > 65535) return 'Enter a valid database port (1-65535).'; if (!user.trim()) return 'Enter a database user.'; @@ -191,16 +192,16 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC const targetCandidate = (previous: typeof savedConnections[number]) => ({ ...previous, type: selectedType, host, port: Number(port), username: user, - database, ssh_enabled: selectedType !== 'Sqlite' && sshEnabled, + database, ssh_enabled: !isLocalFile && sshEnabled, ssh_host: sshHost, ssh_port: Number(sshPort), ssh_username: sshUser, ssh_auth_method: sshAuth, ssh_private_key_path: sshKey, }); const canSaveMetadataOnly = (previous: typeof savedConnections[number] | undefined): boolean => { if (!previous || password || sshPass || connectionTargetChanged(previous, targetCandidate(previous))) return false; - if (Boolean(previous.rememberCredentials) !== (selectedType !== 'Sqlite' && rememberCredentials)) return false; - if (Boolean(previous.ssl_enabled) !== (selectedType !== 'Sqlite' && sslEnabled)) return false; - if (!sslEnabled || selectedType === 'Sqlite') return true; + if (Boolean(previous.rememberCredentials) !== (!isLocalFile && rememberCredentials)) return false; + if (Boolean(previous.ssl_enabled) !== (!isLocalFile && sslEnabled)) return false; + if (!sslEnabled || isLocalFile) return true; return (previous.ssl_mode || 'prefer') === sslMode && (previous.ssl_ca_path || '') === sslCA && (previous.ssl_cert_path || '') === sslCert @@ -239,7 +240,7 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC const fingerprint = testFingerprint; const target = identity.target; try { - const shouldRemember = selectedType !== 'Sqlite' && rememberCredentials; + const shouldRemember = !isLocalFile && rememberCredentials; await invoke('test_connection', { config: getConfig(), password: editingId && shouldRemember && !password ? null : password, rememberCredentials: shouldRemember }); setTestStatus({ result: 'success', fingerprint, target }); } catch (err: any) { @@ -267,7 +268,7 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC setTestStatus(null); try { const config = getConfig(); - const shouldRemember = selectedType !== 'Sqlite' && rememberCredentials; + const shouldRemember = !isLocalFile && rememberCredentials; const savedConnection = { id: config.id, name: config.name, @@ -278,19 +279,19 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC database: config.database || undefined, ssl_enabled: config.ssl_enabled, ssl_mode: config.ssl_mode, - ssl_ca_path: selectedType !== 'Sqlite' ? sslCA || undefined : undefined, - ssl_cert_path: selectedType !== 'Sqlite' ? sslCert || undefined : undefined, - ssl_key_path: selectedType !== 'Sqlite' ? sslKey || undefined : undefined, + ssl_ca_path: !isLocalFile ? sslCA || undefined : undefined, + ssl_cert_path: !isLocalFile ? sslCert || undefined : undefined, + ssl_key_path: !isLocalFile ? sslKey || undefined : undefined, ssh_enabled: config.ssh_enabled, ssh_host: config.ssh_host || undefined, ssh_port: config.ssh_port || undefined, ssh_username: config.ssh_username || undefined, ssh_auth_method: config.ssh_auth_method, - ssh_private_key_path: selectedType !== 'Sqlite' ? sshKey || undefined : undefined, + ssh_private_key_path: !isLocalFile ? sshKey || undefined : undefined, environment: tag as 'local' | 'test' | 'dev' | 'staging' | 'production', color: color, - safetyMode, - aiEnabled, + safetyMode: config.safety_mode, + aiEnabled: config.ai_enabled, rememberCredentials: shouldRemember, }; if (previous && canSaveMetadataOnly(previous)) { @@ -373,13 +374,15 @@ export const ConnectionModal = ({ open, onOpenChange }: { open: boolean, onOpenC { id: 'Postgres', name: 'PostgreSQL', icon: Database }, { id: 'MySql', name: 'MySQL', icon: Database }, { id: 'Sqlite', name: 'SQLite', icon: Terminal }, + { id: 'DuckDb', name: 'DuckDB', icon: Database }, ].map((type) => ( )} {/* Over SSH Section */} - {selectedType !== 'Sqlite' && ( + {!isLocalFile && (
diff --git a/src/components/DatabaseSelectorModal.tsx b/src/components/DatabaseSelectorModal.tsx index 7fc5d63..b32632f 100644 --- a/src/components/DatabaseSelectorModal.tsx +++ b/src/components/DatabaseSelectorModal.tsx @@ -31,7 +31,7 @@ export const DatabaseSelectorModal = () => { const filteredDatabases = filterDatabases(databases, search); const selectedDatabase = filteredDatabases[selectedIndex]; - const canCreateDatabase = savedConnections.find(connection => connection.id === activeConnectionId)?.type !== 'Sqlite'; + const canCreateDatabase = !['Sqlite', 'DuckDb'].includes(savedConnections.find(connection => connection.id === activeConnectionId)?.type || ''); useEffect(() => { if (!showDatabaseSelector || !activeConnectionId) return; diff --git a/src/components/SidebarTree.tsx b/src/components/SidebarTree.tsx index 2507ecf..f5fe8df 100644 --- a/src/components/SidebarTree.tsx +++ b/src/components/SidebarTree.tsx @@ -2,6 +2,7 @@ import { useState, useMemo, useEffect } from 'react'; import { useDatabaseStore, SidebarItem, SidebarItemType } from '../store/databaseStore'; import { Layout, Eye, Code, FileCode, ChevronDown, ChevronRight, Search, Pin, PinOff, Settings } from 'lucide-react'; import type { CSSProperties } from 'react'; +import { sidebarTableName } from '../utils/sqlIdentifiers'; interface TreeItemProps { item: SidebarItem; @@ -106,6 +107,7 @@ export const SidebarTree = () => { const { selectedConnectionId, sidebarItems, + savedConnections, sidebarSettings, toggleSidebarSetting, pinnedItems, @@ -171,6 +173,10 @@ export const SidebarTree = () => { const currentItems = useMemo(() => { if (!selectedConnectionId) return []; let items = sidebarItems[selectedConnectionId] || []; + const databaseType = savedConnections.find(connection => connection.id === selectedConnectionId)?.type; + if (databaseType === 'DuckDb') { + items = items.map(item => ({ ...item, name: sidebarTableName(item.name, item.schema, databaseType) })); + } // Apply settings filters if (currentSettings) { @@ -184,7 +190,7 @@ export const SidebarTree = () => { const term = sidebarSearchTerm.toLowerCase(); return items.filter(i => i.name.toLowerCase().includes(term)); - }, [sidebarItems, selectedConnectionId, sidebarSearchTerm, currentSettings]); + }, [sidebarItems, savedConnections, selectedConnectionId, sidebarSearchTerm, currentSettings]); const sections = useMemo(() => { const grouped = { diff --git a/src/components/TabContentQuery.tsx b/src/components/TabContentQuery.tsx index 1948dc5..19636b2 100644 --- a/src/components/TabContentQuery.tsx +++ b/src/components/TabContentQuery.tsx @@ -214,6 +214,10 @@ export const TabContentQuery = ({ id, initialQuery = '', connectionId }: TabCont const runEditorQuery = async (mode: SqlExecutionMode) => { if (isLoading || planLoading || preparingRef.current || pendingParameters) return; + if (databaseType === 'DuckDb' && mode === 'all') { + setExecutionNotice('DuckDB runs one read-only SELECT at a time. Place the cursor in a query and use Run Current.'); + return; + } if (!databaseType) { setExecutionNotice('Connection type is unavailable. Reconnect before running SQL.'); return; @@ -225,6 +229,10 @@ export const TabContentQuery = ({ id, initialQuery = '', connectionId }: TabCont } const sqlToRun = resolution.target.sql; const names = findQueryParameters(sqlToRun); + if (databaseType === 'DuckDb' && names.length > 0) { + setExecutionNotice('Named query parameters are not yet supported for DuckDB.'); + return; + } if (names.length > 0) { if (mode === 'all') { setExecutionNotice('Parameterized queries run one SELECT at a time. Place the cursor in one statement and use Run Current.'); @@ -242,6 +250,10 @@ export const TabContentQuery = ({ id, initialQuery = '', connectionId }: TabCont }; const explainEditorQuery = async () => { + if (databaseType === 'DuckDb') { + setExecutionNotice('EXPLAIN is not yet supported for DuckDB.'); + return; + } if (isLoading || planLoading || preparingRef.current) return; const resolution = editorRef.current?.resolveExecution('current'); if (!resolution?.target) { @@ -357,13 +369,13 @@ export const TabContentQuery = ({ id, initialQuery = '', connectionId }: TabCont Run {executionScope === 'selection' ? 'Selection' : 'Current'} {shortcutModifier}+Enter - runEditorQuery('all')} className="flex items-center justify-between gap-4 px-3 py-2 rounded outline-none data-[highlighted]:bg-accent/20 cursor-pointer" > Run All {shortcutModifier}+Shift+Enter - + } @@ -372,7 +384,7 @@ export const TabContentQuery = ({ id, initialQuery = '', connectionId }: TabCont + }
- {lastExecutedSql || `SELECT * FROM "${tableName}" LIMIT ${activeTab?.pageSize || 100} OFFSET ${activeTab?.offset || 0};`} + {lastExecutedSql || `SELECT * FROM ${quoteTableReference(tableName, databaseType)} LIMIT ${activeTab?.pageSize || 100} OFFSET ${activeTab?.offset || 0};`}
)} diff --git a/src/components/TableFooter.tsx b/src/components/TableFooter.tsx index 6e7f1af..88b562d 100644 --- a/src/components/TableFooter.tsx +++ b/src/components/TableFooter.tsx @@ -120,7 +120,7 @@ export const TableFooter = ({ Columns - + } {onExport && ( diff --git a/src/components/Toolbar.tsx b/src/components/Toolbar.tsx index 20f4fda..4fe1781 100644 --- a/src/components/Toolbar.tsx +++ b/src/components/Toolbar.tsx @@ -82,7 +82,7 @@ export const Toolbar = ({ } setSwitchError(null); try { - const status = conn.rememberCredentials && conn.type !== 'Sqlite' + const status = conn.rememberCredentials && conn.type !== 'Sqlite' && conn.type !== 'DuckDb' ? await invoke('credential_status', { connectionId: conn.id }) : null; if (!needsCredentialPrompt(conn, status)) await performConnect(conn); else setPasswordPrompt({ visible: true, connection: conn, password: '', sshPassword: '', remember: Boolean(conn.rememberCredentials) }); @@ -168,7 +168,7 @@ export const Toolbar = ({ - + {pendingChangesCount > 0 &&
} @@ -218,7 +218,7 @@ export const Toolbar = ({ {/* Connection-scoped write protection */}