dirsql 0.3.69__tar.gz → 0.3.71__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dirsql-0.3.69 → dirsql-0.3.71}/Cargo.lock +1 -1
- {dirsql-0.3.69 → dirsql-0.3.71}/PKG-INFO +1 -1
- {dirsql-0.3.69/packages/rust → dirsql-0.3.71}/docs/reference/cli.md +42 -1
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/Cargo.toml +1 -1
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/cli.md +42 -1
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/e2e-attestation.json +2 -2
- {dirsql-0.3.69 → dirsql-0.3.71/packages/rust}/docs/reference/cli.md +42 -1
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/bin/dirsql.rs +89 -7
- dirsql-0.3.71/packages/rust/src/cli/execute.rs +314 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/cli/mod.rs +4 -1
- dirsql-0.3.71/packages/rust/src/cli/router.rs +191 -0
- dirsql-0.3.69/packages/rust/src/cli/router.rs +0 -393
- {dirsql-0.3.69 → dirsql-0.3.71}/Cargo.toml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/README.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/_async.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/_dirsql.pyi +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/cli/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/cli/binary_path.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/cli/is_windows.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/cli/main.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/cli/resolve_config_extensions.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/py.typed +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/resolve_config_extensions.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/dirsql/resolve_extension.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/.claude/CLAUDE.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/.vitepress/config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/.vitepress/theme/index.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/.vitepress/theme/lang.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/AGENTS.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/explanation.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/getting-started.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/columns-from-paths.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/define-tables.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/embed.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/extract-from-contents.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/load-extension.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/persist.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/react-to-changes.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/search-by-meaning.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/howto/skip-files.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/index.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/migrations.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/package.json +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/playwright.config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/pnpm-lock.yaml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/pnpm-workspace.yaml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/reference/columns.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/reference/config.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/reference/hooks.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/reference/http-api.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/reference/sdk.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/tests/integration/home.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/tests/integration/language-flag.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/tests/integration/sidebar.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/tests/unit/config.test.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/tests/unit/lang.test.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/docs/vitest.config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/README.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/conftest.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/.claude/CLAUDE.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/.vitepress/config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/.vitepress/theme/index.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/.vitepress/theme/lang.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/AGENTS.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/explanation.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/getting-started.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/columns-from-paths.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/define-tables.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/embed.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/extract-from-contents.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/load-extension.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/persist.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/react-to-changes.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/search-by-meaning.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/howto/skip-files.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/index.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/migrations.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/package.json +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/playwright.config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/pnpm-lock.yaml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/pnpm-workspace.yaml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/columns.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/config.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/hooks.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/http-api.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/reference/sdk.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/tests/integration/home.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/tests/integration/language-flag.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/tests/integration/sidebar.spec.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/tests/unit/config.test.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/tests/unit/lang.test.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/docs/vitest.config.ts +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/src/lib.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/binding/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/conftest.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/e2e/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/integration/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/python/tests/smoke/__init__.py +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/Cargo.toml +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/README.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/benches/db_bench.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/benches/differ_bench.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/benches/matcher_bench.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/benches/scanner_bench.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/explanation.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/getting-started.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/columns-from-paths.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/define-tables.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/embed.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/extract-from-contents.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/load-extension.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/persist.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/react-to-changes.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/search-by-meaning.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/howto/skip-files.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/index.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/migrations.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/reference/columns.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/reference/config.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/reference/hooks.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/reference/http-api.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/docs/reference/sdk.md +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/cli/init.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/cli/serialize.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/cli/server.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/command.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/config.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/db.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/differ.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/lib.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/matcher.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/persist.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/scanner.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/packages/rust/src/watcher.rs +0 -0
- {dirsql-0.3.69 → dirsql-0.3.71}/pyproject.toml +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
# CLI
|
|
2
2
|
|
|
3
|
-
The `dirsql` binary has
|
|
3
|
+
The `dirsql` binary has three modes:
|
|
4
4
|
|
|
5
5
|
| Invocation | Behavior |
|
|
6
6
|
|---|---|
|
|
7
7
|
| `dirsql` (no subcommand) | Start a long-lived HTTP server exposing a SQL view of a directory. See [HTTP API](./http-api.md). |
|
|
8
|
+
| `dirsql query "<sql>"` | Build the index, run one query, print the rows as JSON, exit. No server, no watch. |
|
|
8
9
|
| `dirsql init` | Generate a starter `.dirsql.toml` by running `claude` over a directory. |
|
|
9
10
|
|
|
10
11
|
## Installation
|
|
@@ -97,6 +98,46 @@ with a JSON body describing the failure:
|
|
|
97
98
|
| `0` | Clean shutdown after `SIGINT` / `SIGTERM`. |
|
|
98
99
|
| `1` | Failed to bind `host:port`, or an error during shutdown. |
|
|
99
100
|
|
|
101
|
+
## `dirsql query`
|
|
102
|
+
|
|
103
|
+
Run one SQL query from the shell — for ad-hoc inspection, scripting, and
|
|
104
|
+
docs verification snippets — without booting the server and `curl`ing it:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
dirsql query "SELECT _basename, _size FROM files ORDER BY _size DESC LIMIT 5"
|
|
108
|
+
# [{"_basename":"model.bin","_size":104857600}, …]
|
|
109
|
+
|
|
110
|
+
dirsql query "SELECT COUNT(*) AS n FROM posts" | jq '.[0].n'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The subcommand builds the index, runs the SQL, prints the result rows as a
|
|
114
|
+
JSON array on stdout (byte-identical to the [`POST /query`](./http-api.md)
|
|
115
|
+
response body), and exits `0`.
|
|
116
|
+
|
|
117
|
+
`dirsql query` is a thin adapter over the **same query pipeline the server
|
|
118
|
+
uses**, so behavior is identical to `POST /query` by construction:
|
|
119
|
+
|
|
120
|
+
- **Config discovery** honors `--config` (default `./.dirsql.toml`),
|
|
121
|
+
[zero-config mode](#zero-config-mode), and `--extension` overrides,
|
|
122
|
+
exactly as server mode does.
|
|
123
|
+
- **Hooks** ([`pre-query`](./hooks.md#pre-query) /
|
|
124
|
+
[`post-query`](./hooks.md#post-query)) and the
|
|
125
|
+
[`[dirsql].hook-timeout`](./config.md#dirsql-keys) apply identically.
|
|
126
|
+
- The **30-second query timeout**, the **read-only rule**, and the
|
|
127
|
+
`_dirsql_*` **internal-table denial** apply identically. A rejected read
|
|
128
|
+
is an error, not empty output.
|
|
129
|
+
|
|
130
|
+
Errors print the same diagnostic the HTTP `{"error": …}` body carries —
|
|
131
|
+
config failures, SQL errors, rejected reads, hook failures, timeouts — to
|
|
132
|
+
stderr, with exit code `1`.
|
|
133
|
+
|
|
134
|
+
### Exit codes
|
|
135
|
+
|
|
136
|
+
| Code | Meaning |
|
|
137
|
+
|---|---|
|
|
138
|
+
| `0` | Query succeeded; rows printed on stdout. |
|
|
139
|
+
| `1` | Any failure: config, SQL, rejected read, hook, or timeout. The diagnostic is on stderr. |
|
|
140
|
+
|
|
100
141
|
## `dirsql init`
|
|
101
142
|
|
|
102
143
|
Generates a `.dirsql.toml` by running the `claude` CLI over the target
|
|
@@ -4,7 +4,7 @@ name = "dirsql-py-ext"
|
|
|
4
4
|
# pypi/maturin handler can rewrite it via `write-version` before
|
|
5
5
|
# `maturin build`. `pyproject.toml` declares `dynamic = ["version"]`
|
|
6
6
|
# and maturin reads this field. Mirrors `packages/rust/Cargo.toml`.
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.71"
|
|
8
8
|
edition.workspace = true
|
|
9
9
|
publish = false
|
|
10
10
|
readme = "README.md"
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
# CLI
|
|
2
2
|
|
|
3
|
-
The `dirsql` binary has
|
|
3
|
+
The `dirsql` binary has three modes:
|
|
4
4
|
|
|
5
5
|
| Invocation | Behavior |
|
|
6
6
|
|---|---|
|
|
7
7
|
| `dirsql` (no subcommand) | Start a long-lived HTTP server exposing a SQL view of a directory. See [HTTP API](./http-api.md). |
|
|
8
|
+
| `dirsql query "<sql>"` | Build the index, run one query, print the rows as JSON, exit. No server, no watch. |
|
|
8
9
|
| `dirsql init` | Generate a starter `.dirsql.toml` by running `claude` over a directory. |
|
|
9
10
|
|
|
10
11
|
## Installation
|
|
@@ -97,6 +98,46 @@ with a JSON body describing the failure:
|
|
|
97
98
|
| `0` | Clean shutdown after `SIGINT` / `SIGTERM`. |
|
|
98
99
|
| `1` | Failed to bind `host:port`, or an error during shutdown. |
|
|
99
100
|
|
|
101
|
+
## `dirsql query`
|
|
102
|
+
|
|
103
|
+
Run one SQL query from the shell — for ad-hoc inspection, scripting, and
|
|
104
|
+
docs verification snippets — without booting the server and `curl`ing it:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
dirsql query "SELECT _basename, _size FROM files ORDER BY _size DESC LIMIT 5"
|
|
108
|
+
# [{"_basename":"model.bin","_size":104857600}, …]
|
|
109
|
+
|
|
110
|
+
dirsql query "SELECT COUNT(*) AS n FROM posts" | jq '.[0].n'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The subcommand builds the index, runs the SQL, prints the result rows as a
|
|
114
|
+
JSON array on stdout (byte-identical to the [`POST /query`](./http-api.md)
|
|
115
|
+
response body), and exits `0`.
|
|
116
|
+
|
|
117
|
+
`dirsql query` is a thin adapter over the **same query pipeline the server
|
|
118
|
+
uses**, so behavior is identical to `POST /query` by construction:
|
|
119
|
+
|
|
120
|
+
- **Config discovery** honors `--config` (default `./.dirsql.toml`),
|
|
121
|
+
[zero-config mode](#zero-config-mode), and `--extension` overrides,
|
|
122
|
+
exactly as server mode does.
|
|
123
|
+
- **Hooks** ([`pre-query`](./hooks.md#pre-query) /
|
|
124
|
+
[`post-query`](./hooks.md#post-query)) and the
|
|
125
|
+
[`[dirsql].hook-timeout`](./config.md#dirsql-keys) apply identically.
|
|
126
|
+
- The **30-second query timeout**, the **read-only rule**, and the
|
|
127
|
+
`_dirsql_*` **internal-table denial** apply identically. A rejected read
|
|
128
|
+
is an error, not empty output.
|
|
129
|
+
|
|
130
|
+
Errors print the same diagnostic the HTTP `{"error": …}` body carries —
|
|
131
|
+
config failures, SQL errors, rejected reads, hook failures, timeouts — to
|
|
132
|
+
stderr, with exit code `1`.
|
|
133
|
+
|
|
134
|
+
### Exit codes
|
|
135
|
+
|
|
136
|
+
| Code | Meaning |
|
|
137
|
+
|---|---|
|
|
138
|
+
| `0` | Query succeeded; rows printed on stdout. |
|
|
139
|
+
| `1` | Any failure: config, SQL, rejected read, hook, or timeout. The diagnostic is on stderr. |
|
|
140
|
+
|
|
100
141
|
## `dirsql init`
|
|
101
142
|
|
|
102
143
|
Generates a `.dirsql.toml` by running the `claude` CLI over the target
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
# CLI
|
|
2
2
|
|
|
3
|
-
The `dirsql` binary has
|
|
3
|
+
The `dirsql` binary has three modes:
|
|
4
4
|
|
|
5
5
|
| Invocation | Behavior |
|
|
6
6
|
|---|---|
|
|
7
7
|
| `dirsql` (no subcommand) | Start a long-lived HTTP server exposing a SQL view of a directory. See [HTTP API](./http-api.md). |
|
|
8
|
+
| `dirsql query "<sql>"` | Build the index, run one query, print the rows as JSON, exit. No server, no watch. |
|
|
8
9
|
| `dirsql init` | Generate a starter `.dirsql.toml` by running `claude` over a directory. |
|
|
9
10
|
|
|
10
11
|
## Installation
|
|
@@ -97,6 +98,46 @@ with a JSON body describing the failure:
|
|
|
97
98
|
| `0` | Clean shutdown after `SIGINT` / `SIGTERM`. |
|
|
98
99
|
| `1` | Failed to bind `host:port`, or an error during shutdown. |
|
|
99
100
|
|
|
101
|
+
## `dirsql query`
|
|
102
|
+
|
|
103
|
+
Run one SQL query from the shell — for ad-hoc inspection, scripting, and
|
|
104
|
+
docs verification snippets — without booting the server and `curl`ing it:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
dirsql query "SELECT _basename, _size FROM files ORDER BY _size DESC LIMIT 5"
|
|
108
|
+
# [{"_basename":"model.bin","_size":104857600}, …]
|
|
109
|
+
|
|
110
|
+
dirsql query "SELECT COUNT(*) AS n FROM posts" | jq '.[0].n'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The subcommand builds the index, runs the SQL, prints the result rows as a
|
|
114
|
+
JSON array on stdout (byte-identical to the [`POST /query`](./http-api.md)
|
|
115
|
+
response body), and exits `0`.
|
|
116
|
+
|
|
117
|
+
`dirsql query` is a thin adapter over the **same query pipeline the server
|
|
118
|
+
uses**, so behavior is identical to `POST /query` by construction:
|
|
119
|
+
|
|
120
|
+
- **Config discovery** honors `--config` (default `./.dirsql.toml`),
|
|
121
|
+
[zero-config mode](#zero-config-mode), and `--extension` overrides,
|
|
122
|
+
exactly as server mode does.
|
|
123
|
+
- **Hooks** ([`pre-query`](./hooks.md#pre-query) /
|
|
124
|
+
[`post-query`](./hooks.md#post-query)) and the
|
|
125
|
+
[`[dirsql].hook-timeout`](./config.md#dirsql-keys) apply identically.
|
|
126
|
+
- The **30-second query timeout**, the **read-only rule**, and the
|
|
127
|
+
`_dirsql_*` **internal-table denial** apply identically. A rejected read
|
|
128
|
+
is an error, not empty output.
|
|
129
|
+
|
|
130
|
+
Errors print the same diagnostic the HTTP `{"error": …}` body carries —
|
|
131
|
+
config failures, SQL errors, rejected reads, hook failures, timeouts — to
|
|
132
|
+
stderr, with exit code `1`.
|
|
133
|
+
|
|
134
|
+
### Exit codes
|
|
135
|
+
|
|
136
|
+
| Code | Meaning |
|
|
137
|
+
|---|---|
|
|
138
|
+
| `0` | Query succeeded; rows printed on stdout. |
|
|
139
|
+
| `1` | Any failure: config, SQL, rejected read, hook, or timeout. The diagnostic is on stderr. |
|
|
140
|
+
|
|
100
141
|
## `dirsql init`
|
|
101
142
|
|
|
102
143
|
Generates a `.dirsql.toml` by running the `claude` CLI over the target
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
//! `dirsql` CLI binary.
|
|
1
|
+
//! `dirsql` CLI binary. Three modes:
|
|
2
2
|
//! - No subcommand: HTTP server documented in `docs/reference/cli.md`.
|
|
3
|
+
//! - `query`: one-shot query over the same pipeline the server uses; see
|
|
4
|
+
//! `docs/reference/cli.md`.
|
|
3
5
|
//! - `init`: starter `.dirsql.toml` generation; see `docs/reference/cli.md`.
|
|
4
6
|
//!
|
|
5
7
|
//! Only compiled with `--features cli`.
|
|
@@ -9,7 +11,8 @@ use std::process::ExitCode;
|
|
|
9
11
|
|
|
10
12
|
use clap::{Args, Parser, Subcommand};
|
|
11
13
|
use dirsql::cli::{
|
|
12
|
-
AppState, PostQuery, PreQuery, ServerConfig, init::InitOptions,
|
|
14
|
+
AppState, PostQuery, PreQuery, ServerConfig, execute::execute_query, init::InitOptions,
|
|
15
|
+
serve_with_state,
|
|
13
16
|
};
|
|
14
17
|
use dirsql::{DirSQL, Extension, Row, Table};
|
|
15
18
|
|
|
@@ -31,9 +34,9 @@ struct Cli {
|
|
|
31
34
|
|
|
32
35
|
/// Path to the config file (default: `./.dirsql.toml`). The index is
|
|
33
36
|
/// rooted at the directory containing this file. When the file does
|
|
34
|
-
/// not exist, a default `files` table is served. Used
|
|
35
|
-
///
|
|
36
|
-
#[arg(long, default_value = "./.dirsql.toml")]
|
|
37
|
+
/// not exist, a default `files` table is served. Used by server mode
|
|
38
|
+
/// and by the `query` subcommand.
|
|
39
|
+
#[arg(long, default_value = "./.dirsql.toml", global = true)]
|
|
37
40
|
config: PathBuf,
|
|
38
41
|
|
|
39
42
|
/// Bind address. Used when no subcommand is given.
|
|
@@ -53,8 +56,9 @@ struct Cli {
|
|
|
53
56
|
/// which need an interpreter this compiled binary lacks (see #227) — and
|
|
54
57
|
/// passes the resolved literal paths here. When any are present, the TOML
|
|
55
58
|
/// config's own extension entries are not loaded (the launcher already
|
|
56
|
-
/// merged and resolved them).
|
|
57
|
-
|
|
59
|
+
/// merged and resolved them). Used by server mode and by the `query`
|
|
60
|
+
/// subcommand.
|
|
61
|
+
#[arg(long = "extension", global = true)]
|
|
58
62
|
extension: Vec<String>,
|
|
59
63
|
}
|
|
60
64
|
|
|
@@ -63,6 +67,19 @@ enum Command {
|
|
|
63
67
|
/// Generate a starter `.dirsql.toml` by running `claude` over the
|
|
64
68
|
/// target directory.
|
|
65
69
|
Init(InitArgs),
|
|
70
|
+
|
|
71
|
+
/// Run one SQL query against the indexed directory, print the result
|
|
72
|
+
/// rows as JSON on stdout, and exit. No server, no watch. Shares the
|
|
73
|
+
/// server's query pipeline, so config discovery, hooks, the query
|
|
74
|
+
/// timeout, the read-only rule, and error classification are identical
|
|
75
|
+
/// to `POST /query`.
|
|
76
|
+
Query(QueryArgs),
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#[derive(Debug, Args)]
|
|
80
|
+
struct QueryArgs {
|
|
81
|
+
/// The SQL to run (a single read-only statement).
|
|
82
|
+
sql: String,
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
#[derive(Debug, Args)]
|
|
@@ -86,10 +103,51 @@ async fn main() -> ExitCode {
|
|
|
86
103
|
|
|
87
104
|
match cli.command.take() {
|
|
88
105
|
Some(Command::Init(args)) => run_init(args),
|
|
106
|
+
Some(Command::Query(args)) => run_query(&cli, args).await,
|
|
89
107
|
None => run_server(cli).await,
|
|
90
108
|
}
|
|
91
109
|
}
|
|
92
110
|
|
|
111
|
+
/// One-shot `dirsql query`: build the index exactly as server mode would
|
|
112
|
+
/// (same `load_state` / hook loading), run the SQL through the shared
|
|
113
|
+
/// [`execute_query`] pipeline, print the result JSON on stdout, and exit.
|
|
114
|
+
/// Any [`QueryFailure`](dirsql::cli::execute::QueryFailure) prints its
|
|
115
|
+
/// message — the same string the HTTP `{"error": …}` body carries — to
|
|
116
|
+
/// stderr with a non-zero exit.
|
|
117
|
+
async fn run_query(cli: &Cli, args: QueryArgs) -> ExitCode {
|
|
118
|
+
let state = load_state(cli);
|
|
119
|
+
let pre_query = load_pre_query(cli);
|
|
120
|
+
let post_query = load_post_query(cli);
|
|
121
|
+
// Same default the server binds with; the pipeline enforces it.
|
|
122
|
+
let timeout = ServerConfig::default().query_timeout;
|
|
123
|
+
|
|
124
|
+
match execute_query(
|
|
125
|
+
&state,
|
|
126
|
+
query_body(&args.sql),
|
|
127
|
+
timeout,
|
|
128
|
+
pre_query.as_ref(),
|
|
129
|
+
post_query.as_ref(),
|
|
130
|
+
)
|
|
131
|
+
.await
|
|
132
|
+
{
|
|
133
|
+
Ok(value) => {
|
|
134
|
+
println!("{value}");
|
|
135
|
+
ExitCode::SUCCESS
|
|
136
|
+
}
|
|
137
|
+
Err(failure) => {
|
|
138
|
+
eprintln!("dirsql query: {}", failure.message());
|
|
139
|
+
ExitCode::from(1)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Synthesize the exact `POST /query` body for a positional SQL argument,
|
|
145
|
+
/// so the shared pipeline's intake validation and `pre-query` hook see
|
|
146
|
+
/// byte-for-byte what an HTTP client would send.
|
|
147
|
+
fn query_body(sql: &str) -> String {
|
|
148
|
+
serde_json::json!({ "sql": sql }).to_string()
|
|
149
|
+
}
|
|
150
|
+
|
|
93
151
|
fn run_init(args: InitArgs) -> ExitCode {
|
|
94
152
|
let root = match args.root {
|
|
95
153
|
Some(p) => p,
|
|
@@ -322,6 +380,30 @@ async fn wait_for_shutdown() -> std::io::Result<()> {
|
|
|
322
380
|
mod tests {
|
|
323
381
|
use super::*;
|
|
324
382
|
|
|
383
|
+
#[test]
|
|
384
|
+
fn query_body_wraps_sql_in_the_http_request_shape() {
|
|
385
|
+
// The subcommand must feed the pipeline the exact body a curl'd
|
|
386
|
+
// `POST /query` carries, so intake validation stays single-sourced.
|
|
387
|
+
assert_eq!(query_body("SELECT 1"), r#"{"sql":"SELECT 1"}"#);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
#[test]
|
|
391
|
+
fn query_body_escapes_sql_as_json() {
|
|
392
|
+
// SQL containing quotes/newlines must arrive as valid JSON, not be
|
|
393
|
+
// spliced raw into the body.
|
|
394
|
+
assert_eq!(
|
|
395
|
+
query_body("SELECT \"a\"\nFROM t"),
|
|
396
|
+
r#"{"sql":"SELECT \"a\"\nFROM t"}"#
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#[test]
|
|
401
|
+
fn query_body_preserves_blank_sql_for_the_shared_rejection() {
|
|
402
|
+
// Blank SQL is NOT rejected here: it flows to the pipeline's shared
|
|
403
|
+
// empty-rejection so both surfaces emit the identical message.
|
|
404
|
+
assert_eq!(query_body(" "), r#"{"sql":" "}"#);
|
|
405
|
+
}
|
|
406
|
+
|
|
325
407
|
#[test]
|
|
326
408
|
fn parse_extension_specs_handles_bare_path_and_entrypoint() {
|
|
327
409
|
let specs = vec![
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
//! Transport-agnostic `/query` pipeline.
|
|
2
|
+
//!
|
|
3
|
+
//! The full orchestration for running one query — intake validation,
|
|
4
|
+
//! the `pre-query` hook, the query timeout, [`DirSQL::query`], row
|
|
5
|
+
//! serialization, the `post-query` hook, and error classification —
|
|
6
|
+
//! lives here exactly once. The HTTP handler and the one-shot
|
|
7
|
+
//! `dirsql query` subcommand (#399) are thin transport adapters over
|
|
8
|
+
//! [`execute_query`], so the two surfaces cannot drift behaviorally:
|
|
9
|
+
//! per-surface code only maps [`QueryFailure`] to a status code or an
|
|
10
|
+
//! exit code.
|
|
11
|
+
|
|
12
|
+
use std::time::Duration;
|
|
13
|
+
|
|
14
|
+
use serde::Deserialize;
|
|
15
|
+
use serde_json::Value;
|
|
16
|
+
|
|
17
|
+
use super::serialize::rows_to_json;
|
|
18
|
+
use super::{AppState, PostQuery, PreQuery};
|
|
19
|
+
use crate::command::{Placeholder, run_command};
|
|
20
|
+
use crate::{DirSQL, DirSqlError};
|
|
21
|
+
|
|
22
|
+
/// Cap on the serialized result payload passed as the `{args}` argv token.
|
|
23
|
+
/// Beyond this, `{args}` is emptied and the operator is directed to stdin
|
|
24
|
+
/// (which always carries the full payload) — comfortably under Linux's 128 KiB
|
|
25
|
+
/// single-arg `MAX_ARG_STRLEN`.
|
|
26
|
+
const POST_QUERY_ARGS_MAX: usize = 96 * 1024;
|
|
27
|
+
|
|
28
|
+
/// Why a query failed, classified independently of transport. The HTTP
|
|
29
|
+
/// adapter maps each arm to a status code (400 / 408 / 500 / 503); the CLI
|
|
30
|
+
/// adapter maps every arm to stderr + a non-zero exit.
|
|
31
|
+
#[derive(Debug)]
|
|
32
|
+
pub enum QueryFailure {
|
|
33
|
+
/// Malformed input the caller can fix: unparsable body, missing/empty
|
|
34
|
+
/// `sql`, or a SQL error from the core (HTTP 400).
|
|
35
|
+
BadRequest(String),
|
|
36
|
+
/// The query exceeded the configured timeout (HTTP 408).
|
|
37
|
+
Timeout(Duration),
|
|
38
|
+
/// A server-side fault: hook failure, join error, lock poisoning
|
|
39
|
+
/// (HTTP 500).
|
|
40
|
+
Internal(String),
|
|
41
|
+
/// The index never became ready — the degraded config state (HTTP 503).
|
|
42
|
+
Unavailable(String),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
impl QueryFailure {
|
|
46
|
+
/// The diagnostic message, identical across transports (the HTTP
|
|
47
|
+
/// `{"error": …}` body and the CLI stderr line).
|
|
48
|
+
pub fn message(&self) -> String {
|
|
49
|
+
match self {
|
|
50
|
+
Self::Timeout(timeout) => format!("query exceeded {timeout:?} timeout"),
|
|
51
|
+
Self::BadRequest(msg) | Self::Internal(msg) | Self::Unavailable(msg) => msg.clone(),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#[derive(Debug, Deserialize)]
|
|
57
|
+
struct QueryBody {
|
|
58
|
+
sql: Option<String>,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Run one query end to end: resolve the SQL from `raw_body` (through the
|
|
62
|
+
/// `pre-query` hook when present, else parsed as `{"sql": …}`), execute it
|
|
63
|
+
/// against the index under `timeout`, serialize the rows, and reshape them
|
|
64
|
+
/// through the `post-query` hook when present. `raw_body` is the exact
|
|
65
|
+
/// payload a `POST /query` request carries; the CLI adapter synthesizes the
|
|
66
|
+
/// same shape so both surfaces share intake validation and hook semantics.
|
|
67
|
+
pub async fn execute_query(
|
|
68
|
+
state: &AppState,
|
|
69
|
+
raw_body: String,
|
|
70
|
+
timeout: Duration,
|
|
71
|
+
pre_query: Option<&PreQuery>,
|
|
72
|
+
post_query: Option<&PostQuery>,
|
|
73
|
+
) -> Result<Value, QueryFailure> {
|
|
74
|
+
// Resolve the SQL to run. With a `pre-query` hook the raw body is
|
|
75
|
+
// rewritten by the command; without one it is parsed as `{"sql": …}`.
|
|
76
|
+
let sql = match pre_query {
|
|
77
|
+
Some(pq) => run_pre_query(pq, raw_body).await?,
|
|
78
|
+
None => parse_sql_body(&raw_body)?,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
let db = require_ready(state)?;
|
|
82
|
+
|
|
83
|
+
let join =
|
|
84
|
+
tokio::time::timeout(timeout, tokio::task::spawn_blocking(move || db.query(&sql))).await;
|
|
85
|
+
|
|
86
|
+
match join {
|
|
87
|
+
Ok(Ok(Ok(rows))) => {
|
|
88
|
+
let rows_json = rows_to_json(&rows);
|
|
89
|
+
match post_query {
|
|
90
|
+
Some(pq) => run_post_query(pq, rows_json).await,
|
|
91
|
+
None => Ok(Value::Array(rows_json)),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
Ok(Ok(Err(err))) => Err(classify_query_error(err)),
|
|
95
|
+
Ok(Err(join_err)) => Err(QueryFailure::Internal(join_err.to_string())),
|
|
96
|
+
Err(_elapsed) => Err(QueryFailure::Timeout(timeout)),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// Return a cloned [`DirSQL`] handle, or [`QueryFailure::Unavailable`] if
|
|
101
|
+
/// the index started in the degraded [`AppState::Unavailable`] state.
|
|
102
|
+
pub fn require_ready(state: &AppState) -> Result<DirSQL, QueryFailure> {
|
|
103
|
+
match state {
|
|
104
|
+
AppState::Ready(db) => Ok(db.clone()),
|
|
105
|
+
AppState::Unavailable(reason) => Err(QueryFailure::Unavailable(reason.clone())),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Parse a raw query body as `{"sql": …}` and return the trimmed SQL.
|
|
110
|
+
/// `BadRequest` on malformed JSON, and on a missing/empty `sql` field.
|
|
111
|
+
fn parse_sql_body(body: &str) -> Result<String, QueryFailure> {
|
|
112
|
+
let parsed: QueryBody =
|
|
113
|
+
serde_json::from_str(body).map_err(|err| QueryFailure::BadRequest(err.to_string()))?;
|
|
114
|
+
match parsed.sql.as_deref().map(str::trim) {
|
|
115
|
+
Some(s) if !s.is_empty() => Ok(s.to_string()),
|
|
116
|
+
Some(_) => Err(QueryFailure::BadRequest("`sql` must not be empty".into())),
|
|
117
|
+
None => Err(QueryFailure::BadRequest("missing `sql` field".into())),
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// SQL errors from the core are the caller's to fix (`BadRequest`); every
|
|
122
|
+
/// other failure (lock poisoning, watch/config faults) is server-side
|
|
123
|
+
/// (`Internal`).
|
|
124
|
+
fn classify_query_error(err: DirSqlError) -> QueryFailure {
|
|
125
|
+
match err {
|
|
126
|
+
DirSqlError::Core(_) => QueryFailure::BadRequest(err.to_string()),
|
|
127
|
+
_ => QueryFailure::Internal(err.to_string()),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// Run the `pre-query` hook over the raw request body and return the SQL it
|
|
132
|
+
/// prints. The body is passed as the injection-safe `{args}` placeholder (a
|
|
133
|
+
/// single argv token); the command's last non-empty stdout line is the SQL to
|
|
134
|
+
/// run. Any failure (non-zero exit, timeout, spawn error) maps to `Internal`
|
|
135
|
+
/// carrying the command's stderr tail.
|
|
136
|
+
async fn run_pre_query(pq: &PreQuery, raw_body: String) -> Result<String, QueryFailure> {
|
|
137
|
+
let command = pq.command.clone();
|
|
138
|
+
let config_dir = pq.config_dir.clone();
|
|
139
|
+
let timeout = pq.timeout;
|
|
140
|
+
// `run_command` is blocking — it spawns a child and joins drain threads —
|
|
141
|
+
// so run it off the async runtime. It enforces the hook's timeout
|
|
142
|
+
// (the global `[dirsql].hook-timeout`, default 30s) internally, so no outer
|
|
143
|
+
// `tokio::time::timeout` is needed.
|
|
144
|
+
let outcome = tokio::task::spawn_blocking(move || {
|
|
145
|
+
run_command(
|
|
146
|
+
&command,
|
|
147
|
+
&[Placeholder::new("args", &raw_body)],
|
|
148
|
+
&config_dir,
|
|
149
|
+
timeout,
|
|
150
|
+
None,
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
.await
|
|
154
|
+
.map_err(|join_err| QueryFailure::Internal(join_err.to_string()))?;
|
|
155
|
+
|
|
156
|
+
// `run_command` only returns `Ok` with a non-empty last stdout line
|
|
157
|
+
// (`EmptyOutput` otherwise), so the payload is the SQL as-is.
|
|
158
|
+
outcome
|
|
159
|
+
.map(|out| out.payload)
|
|
160
|
+
.map_err(|err| QueryFailure::Internal(err.to_string()))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/// Run the `post-query` hook over a successful result set and return the JSON
|
|
164
|
+
/// body it prints. The rows are serialized to a JSON array and delivered two
|
|
165
|
+
/// ways: always on the child's stdin (unbounded, injection-safe), and as the
|
|
166
|
+
/// `{args}` placeholder when the payload is within [`POST_QUERY_ARGS_MAX`]
|
|
167
|
+
/// (beyond that `{args}` is emptied and a warning names the size, directing
|
|
168
|
+
/// the operator to stdin — never silent truncation). The command's last
|
|
169
|
+
/// non-empty stdout line is parsed as JSON and returned as the result;
|
|
170
|
+
/// anything that isn't valid JSON, or any failure (non-zero exit, timeout,
|
|
171
|
+
/// spawn error), maps to `Internal`.
|
|
172
|
+
async fn run_post_query(pq: &PostQuery, rows: Vec<Value>) -> Result<Value, QueryFailure> {
|
|
173
|
+
let payload =
|
|
174
|
+
serde_json::to_string(&rows).map_err(|err| QueryFailure::Internal(err.to_string()))?;
|
|
175
|
+
let command = pq.command.clone();
|
|
176
|
+
let config_dir = pq.config_dir.clone();
|
|
177
|
+
let timeout = pq.timeout;
|
|
178
|
+
// `run_command` is blocking — it spawns a child and joins drain threads —
|
|
179
|
+
// so run it off the async runtime. It enforces the hook's timeout
|
|
180
|
+
// (the global `[dirsql].hook-timeout`, default 30s) internally, so no outer
|
|
181
|
+
// `tokio::time::timeout` is needed.
|
|
182
|
+
let outcome = tokio::task::spawn_blocking(move || {
|
|
183
|
+
let args_value = if payload.len() <= POST_QUERY_ARGS_MAX {
|
|
184
|
+
payload.clone()
|
|
185
|
+
} else {
|
|
186
|
+
eprintln!(
|
|
187
|
+
"dirsql: post-query result payload is {} bytes, exceeding the \
|
|
188
|
+
{POST_QUERY_ARGS_MAX}-byte argv threshold; `{{args}}` is emptied — \
|
|
189
|
+
read the rows from stdin instead",
|
|
190
|
+
payload.len()
|
|
191
|
+
);
|
|
192
|
+
String::new()
|
|
193
|
+
};
|
|
194
|
+
run_command(
|
|
195
|
+
&command,
|
|
196
|
+
&[Placeholder::new("args", &args_value)],
|
|
197
|
+
&config_dir,
|
|
198
|
+
timeout,
|
|
199
|
+
Some(payload.as_bytes()),
|
|
200
|
+
)
|
|
201
|
+
})
|
|
202
|
+
.await
|
|
203
|
+
.map_err(|join_err| QueryFailure::Internal(join_err.to_string()))?;
|
|
204
|
+
|
|
205
|
+
let out = outcome.map_err(|err| QueryFailure::Internal(err.to_string()))?;
|
|
206
|
+
|
|
207
|
+
// The command's payload (last non-empty stdout line) is the JSON result
|
|
208
|
+
// body; reject anything that doesn't parse as JSON.
|
|
209
|
+
serde_json::from_str(&out.payload).map_err(|err| {
|
|
210
|
+
QueryFailure::Internal(format!("post-query did not return valid JSON: {err}"))
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#[cfg(test)]
|
|
215
|
+
mod tests {
|
|
216
|
+
use super::*;
|
|
217
|
+
|
|
218
|
+
// The `DirSqlError::Core => BadRequest` arm of `classify_query_error` is
|
|
219
|
+
// exercised end-to-end at the integration tier by
|
|
220
|
+
// `post_query_malformed_sql_returns_400_not_500` in
|
|
221
|
+
// `tests/cli_integration.rs`, which posts malformed SQL to `/query` and
|
|
222
|
+
// asserts the 400. Constructing a `Core` value inline would require
|
|
223
|
+
// importing the first-party `crate::db::DbError`, which the
|
|
224
|
+
// `testing-conventions` `unit lint` isolation rule forbids (a unit test
|
|
225
|
+
// may reach only `super::` and pure `std`). The non-Core arm below is
|
|
226
|
+
// pure -- it builds a `super::DirSqlError::Lock` -- so it stays inline.
|
|
227
|
+
|
|
228
|
+
#[test]
|
|
229
|
+
fn classify_non_core_error_is_internal() {
|
|
230
|
+
// Lock/watch/config failures are server-side faults -> `Internal`.
|
|
231
|
+
// This drives the `_ =>` arm of `classify_query_error`.
|
|
232
|
+
let err = DirSqlError::Lock("poisoned".into());
|
|
233
|
+
let failure = classify_query_error(err);
|
|
234
|
+
assert!(
|
|
235
|
+
matches!(failure, QueryFailure::Internal(_)),
|
|
236
|
+
"got: {failure:?}"
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
#[test]
|
|
241
|
+
fn require_ready_fails_unavailable_when_degraded() {
|
|
242
|
+
// The degraded state yields `Unavailable` carrying the diagnostic
|
|
243
|
+
// verbatim instead of a `DirSQL` handle.
|
|
244
|
+
let state = AppState::Unavailable("config failed to load".into());
|
|
245
|
+
// `DirSQL` isn't `Debug`, so go through `.err()` (which drops the Ok
|
|
246
|
+
// value) rather than `expect_err`.
|
|
247
|
+
let failure = require_ready(&state)
|
|
248
|
+
.err()
|
|
249
|
+
.expect("Unavailable must not yield a db");
|
|
250
|
+
match failure {
|
|
251
|
+
QueryFailure::Unavailable(reason) => assert_eq!(reason, "config failed to load"),
|
|
252
|
+
other => panic!("expected Unavailable, got: {other:?}"),
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// `parse_sql_body` is the no-`pre-query` intake path: it is pure (serde
|
|
257
|
+
// only), so it is unit-tested here directly rather than through the async
|
|
258
|
+
// `execute_query` pipeline (which needs a live index and is covered at
|
|
259
|
+
// the integration tier).
|
|
260
|
+
|
|
261
|
+
#[test]
|
|
262
|
+
fn parse_sql_body_returns_trimmed_sql() {
|
|
263
|
+
// Surrounding whitespace is stripped; the inner SQL is returned as-is.
|
|
264
|
+
let sql = parse_sql_body(r#"{"sql": " SELECT 1 "}"#).expect("valid body");
|
|
265
|
+
assert_eq!(sql, "SELECT 1");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
#[test]
|
|
269
|
+
fn parse_sql_body_rejects_malformed_json() {
|
|
270
|
+
// A body that isn't JSON fails at the serde step -> `BadRequest`.
|
|
271
|
+
let failure = parse_sql_body("not json").expect_err("malformed JSON must be rejected");
|
|
272
|
+
assert!(
|
|
273
|
+
matches!(failure, QueryFailure::BadRequest(_)),
|
|
274
|
+
"got: {failure:?}"
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#[test]
|
|
279
|
+
fn parse_sql_body_rejects_whitespace_only_sql() {
|
|
280
|
+
// A present-but-blank `sql` trims to empty -> `BadRequest` (the
|
|
281
|
+
// `Some(_)` arm, and the `false` side of the `!s.is_empty()` guard).
|
|
282
|
+
let failure = parse_sql_body(r#"{"sql": " "}"#).expect_err("empty sql must be rejected");
|
|
283
|
+
match failure {
|
|
284
|
+
QueryFailure::BadRequest(msg) => assert_eq!(msg, "`sql` must not be empty"),
|
|
285
|
+
other => panic!("expected BadRequest, got: {other:?}"),
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
#[test]
|
|
290
|
+
fn parse_sql_body_rejects_missing_sql_field() {
|
|
291
|
+
// Valid JSON object with no `sql` key -> the `None` arm -> `BadRequest`.
|
|
292
|
+
let failure = parse_sql_body("{}").expect_err("missing sql must be rejected");
|
|
293
|
+
match failure {
|
|
294
|
+
QueryFailure::BadRequest(msg) => assert_eq!(msg, "missing `sql` field"),
|
|
295
|
+
other => panic!("expected BadRequest, got: {other:?}"),
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#[test]
|
|
300
|
+
fn timeout_failure_message_names_the_duration() {
|
|
301
|
+
// The timeout arm formats its message from the stored duration —
|
|
302
|
+
// the exact text the HTTP 408 body carried before the extraction.
|
|
303
|
+
let failure = QueryFailure::Timeout(Duration::from_secs(30));
|
|
304
|
+
assert_eq!(failure.message(), "query exceeded 30s timeout");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
#[test]
|
|
308
|
+
fn non_timeout_failure_messages_pass_through_verbatim() {
|
|
309
|
+
// The other three arms carry their diagnostic string as-is.
|
|
310
|
+
assert_eq!(QueryFailure::BadRequest("a".into()).message(), "a");
|
|
311
|
+
assert_eq!(QueryFailure::Internal("b".into()).message(), "b");
|
|
312
|
+
assert_eq!(QueryFailure::Unavailable("c".into()).message(), "c");
|
|
313
|
+
}
|
|
314
|
+
}
|