Skip to content

feat(cli): add --json to void db execute - #920

Draft
karthickeyan-dev wants to merge 6 commits into
voidzero-dev:mainfrom
karthickeyan-dev:feat/db-execute-json
Draft

karthickeyan-dev wants to merge 6 commits into
voidzero-dev:mainfrom
karthickeyan-dev:feat/db-execute-json

Conversation

@karthickeyan-dev

@karthickeyan-dev karthickeyan-dev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #912

Description

void db execute printed rows only as a text table, followed by a success line on stdout. The table can't tell NULL from 'NULL' or 1 from '1', and a value containing a newline splits its row. SQL with several statements printed nothing locally and merged every statement's rows on direct D1.

--json writes only a JSON array to stdout, with one entry per statement. Each entry has the same shape as a D1 result object, which wrangler d1 execute --json also prints, so scripts can move between the two tools:

[{ "results": [{ "a": 1 }], "success": true, "meta": { "changes": 0, "last_row_id": 0, "duration": 0.02 } }]

Prompts, warnings and errors go to stderr. The success line is left out, and a failure exits non-zero with stdout empty. Table output without --json is unchanged.

Per target:

  • Local SQLite: runs the SQL one statement at a time. Statements are split with the rules of SQLite's own sqlite3_complete(), on top of the shared tokenizeSql, so literals and comments are respected and a CREATE TRIGGER … BEGIN …; END; stays one statement. Whitespace is what SQLite counts as whitespace: its five ASCII characters, plus a byte-order mark where a token starts. Input SQLite rejects, such as a bare /* at the end or a nonbreaking space, stays in a statement and fails there instead of being dropped. Like exec(), statements commit as they run unless the SQL opens a transaction. meta holds changes, last_row_id and duration.
  • Direct Cloudflare D1: executeCloudflareD1Statements() keeps D1's result for each statement. executeCloudflareD1Sql() still flattens them for the table and for reading migrations.
  • PostgreSQL / MySQL (local and remote): each statement's result comes straight from the driver. node-postgres returns an array of results, and its empty-query result (SQL with no statement) is left out. mysql2 returns one result per result set. A CALL returns one set per procedure result plus a closing header, so those are grouped into one entry for the CALL. To find each CALL, the SQL is read the way the server reads it: # comments, -- comments (followed by whitespace, a control character or the end of the input), quoted text with backslash escapes, and double quotes as names in ANSI_QUOTES mode. An executable comment counts as SQL only on a server that runs it: /*! by version, and on MariaDB also /*M!, with MariaDB's rules for six-digit versions and for skipping MySQL 5.7+ comments (50700 to 99999). A skipped version comment may hold one nested comment. BEGIN … END nests only in a stored program or MariaDB's BEGIN NOT ATOMIC, so a column named begin doesn't hide a semicolon. If the SQL contains a backslash or an executable comment, --json first reads sql_mode and VERSION() on a separate short-lived connection, so the session the SQL runs in is untouched (FOUND_ROWS(), ROW_COUNT(), warnings). Other SQL, and table mode, don't open that connection. If the statements and results can't be matched, Void prints one entry per result instead of guessing. Local and remote now share executeExternalSql().
  • Void platform D1 proxy: the proxy returns only the first statement's result, so --json with several statements is refused before anything is sent. A single CREATE TRIGGER counts as one statement. The existing isSingleStatement() is left alone because the migration classifier hashes its result.
  • Direct D1 import (a --file over 100 KB): returns no rows, so --json is refused before the prompt or upload.

Design notes

  • Compared with wrangler: wrangler's --json also skips the confirmation prompt. Here the prompt stays and renders on stderr, so a destructive --remote --json in a TTY still asks first. --yes and non-TTY behave as before.
  • Errors in JSON mode go to stderr as plain text. They are not printed as a JSON error object on stdout.
  • Cloudflare Access login: when the platform sits behind Access, cloudflared access login --quiet runs with its stdout sent to stderr, so the token it prints never reaches the JSON on stdout. This applies to every command that logs in, not just --json.
  • Known limit: a SET sql_mode inside the SQL itself isn't followed when grouping MySQL results.
  • Values JSON can't hold as-is:
    • BLOB and bytea values print as arrays of bytes, at any depth (bytea[] too), which is how D1 returns them.
    • Integers beyond ±(2^53 − 1) print as strings of their digits, so an ID never rounds silently. Local SQLite reads integers as BigInt. MySQL connects with supportBigNumbers in JSON mode only, so the table is unchanged. PostgreSQL bigint was already a string. This matches mysql2's own convention: a number when it fits, a string otherwise.
    • Infinity, -Infinity and NaN print as strings, as PostgreSQL's to_json does, instead of null, which would read as SQL NULL.
    • PostgreSQL numeric[] elements print as strings, like a numeric. Numbers inside json and jsonb values (and their arrays) keep the digits the database sent, through JSON.parse source text and JSON.rawJSON (Node 24). These parsers are set on the client in JSON mode only.
    • Remote D1 values are printed as D1's API returns them.
  • A MySQL CALL with several result sets puts all their rows in one results array, in order. Adding each set's row count to meta could be a follow-up.
  • No subcommand menu with --json: void <group> --json in a TTY used to draw the menu on stdout. It now goes straight to argument parsing, so void db --json prints a usage error on stderr.
  • Not changed: table mode still merges rows on direct D1, prints nothing for scripts, and rounds SQLite integers past 2^53. Printing one table per statement, and printing exact integers, could be follow-ups.

Test

  • db-commands.test.ts covers the real local SQLite path. It checks:
    • per-statement rows and changes;
    • that NULL, text, numbers, newlines and blobs keep their types;
    • RETURNING counts;
    • semicolons inside literals, comments, quoted identifiers and a trigger body;
    • that a failing statement stops the run while earlier writes stay;
    • incomplete SQL, comment-only SQL, and an explicit BEGIN … COMMIT;
    • integers past 2^53 in rows and last_row_id, and an infinite REAL next to NULL.
  • db-execute-remote.test.ts covers direct D1 JSON, the stderr prompt, the import refusal, the proxy single result and multi-statement refusal, a CREATE TRIGGER through the proxy, stderr errors, multi-statement PostgreSQL, MySQL's big-number options and PostgreSQL's type parsers in JSON mode only, and the MySQL mode query running on its own connection, only when the SQL needs it. cf-access.test.ts checks the quiet login and that its stdout goes to stderr.
  • db-execute-results.test.ts covers the node-postgres and mysql2 result shapes, CALL grouping and its fallback, MySQL statement boundaries around stored program bodies, MySQL comments and quoted text (#, -- before whitespace or control characters, backslash escapes with and without NO_BACKSLASH_ESCAPES, ANSI_QUOTES names, version comments above and below the server version, five- and six-digit versions, nested comments in skipped version comments, MariaDB's /*M! and MySQL-only ranges, optimizer hints), BEGIN as a block only in stored programs and BEGIN NOT ATOMIC (not as a column or XA BEGIN), SQLite statement splitting (triggers, CASE … END, EXPLAIN, transactions, byte-order marks, a bare trailing /*, characters SQLite doesn't count as whitespace), PostgreSQL numeric[] and JSON precision, and how bytes, big integers and non-finite numbers print. cloudflare-operations.test.ts and db-args.test.ts cover the new D1 function and the flag.
  • Ran the issue's reproduction against a built CLI and a real local D1 file:
$ void db execute --json "select 1 as a; select 2 as b"   # two entries, nothing else on stdout
$ void db execute --json "select * from nope"             # exit 1, stdout empty, "no such table: nope" on stderr
$ void db execute --json "select count(*) as total from users" | node -e '…JSON.parse…'   # parses
$ void db execute --json "insert into items values (9007199254740993, 'big'); create trigger … begin …; end; select id, 1e999 as inf from items"
#   one entry per statement; id "9007199254740993", inf "Infinity"
$ void db --json > out.json     # in a TTY: no menu, out.json empty, usage error on stderr
$ void db execute --json "SELECT 1;/*"                    # exit 1, stdout empty, syntax error on stderr
$ void db execute --json --file bom.sql   # file starts with a BOM and a CREATE TRIGGER: three entries, trigger intact

vp check, vp run test and vpr test:all pass.

🤖 Generated with Claude Code

karthickeyan-dev and others added 6 commits November 25, 2026 13:51
`void db execute --json` writes a JSON array with one D1-shaped
`{ results, success, meta }` entry per statement to stdout, matching
`wrangler d1 execute --json`. Prompts, warnings and errors go to stderr,
and the success line is left out.

- Local SQLite runs the SQL statement by statement. The shared SQL
  tokenizer cuts at top-level semicolons, and a piece SQLite reports as
  incomplete input (a trigger body) continues into the next one.
- Direct Cloudflare D1 keeps each statement's result instead of merging
  their rows.
- PostgreSQL and MySQL read each statement's result from the driver.
- A direct D1 import (a file over 100 KB) and several statements
  through the Void platform proxy are refused before anything runs,
  since neither can return every statement's rows.

Table output without `--json` is unchanged.

Fixes voidzero-dev#912

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Read SQLite integers as BigInt and MySQL BIGINTs with supportBigNumbers;
  integers past 2^53 print as strings of their digits.
- Print Infinity, -Infinity and NaN as strings instead of null.
- Serialize bytes at any depth, so bytea[] elements print as byte arrays.
- Split SQLite SQL the way sqlite3_complete() does, so a CREATE TRIGGER is
  one statement locally and through the platform proxy.
- Group a MySQL CALL's result sets and header into one entry.
- Leave out PostgreSQL's empty-query result.
- Do not open a subcommand menu when --json is passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Run `cloudflared access login` with `--quiet` and send its output to
  stderr, so renewing Access never puts the JWT or login text on stdout.
- Find MySQL `CALL` statements with MySQL's own lexical rules: `#`
  comments, `--` only before whitespace, backslash escapes in quotes,
  and `/*!` / `/*+` comments as SQL.
- Give the pg client lossless parsers in JSON mode: `numeric[]`
  elements stay strings, and numbers in json, jsonb and their arrays
  keep the digits the database sent.
- Skip only the whitespace and comments SQLite skips, so a trailing
  bare `/*` or nonbreaking space fails as SQLite reports it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SQLite: a byte-order mark where a token starts is whitespace, so a file
  that begins with one keeps its `CREATE TRIGGER` whole.
- MySQL: grouping a `CALL`'s results follows the server's sql_mode and
  version, read only when the SQL holds a backslash or a `/*!` comment.
  With NO_BACKSLASH_ESCAPES a backslash is an ordinary character, and a
  version comment counts only on a server that runs it.
- MySQL: a control character after `--` starts a comment, and `/*+`
  hints outside a statement are ordinary comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…te --json` CALL results

- Read the server mode on a separate connection so the user's session state
  (FOUND_ROWS(), ROW_COUNT(), warnings) is untouched.
- Treat BEGIN as a block opener only in stored programs and MariaDB's
  BEGIN NOT ATOMIC, not as a column name or XA BEGIN.
- Follow MariaDB's executable comments: /*M!, six-digit versions, and
  skipping /*! comments for versions 50700 to 99999.
- Read double quotes as identifiers in ANSI_QUOTES mode.
- Allow one nested comment inside a skipped version comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…son` output exact

- Read later statements with the mode a `SET sql_mode` to a quoted list leaves, and refuse
  before running SQL whose CALL results could not be grouped after an unreadable change
- Read reserved words after a dot (`t.case`, `t.end`) as names
- Keep `ALTER EVENT ... DO BEGIN ... END` bodies in one statement
- Print MySQL dates and times as the text MySQL returns
- Leave --json out of the interactive `void db` menu, which draws on stdout
- Drop URL options that would change JSON output, such as `debug`, from both connections

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

void db execute prints results only as a table

1 participant