Repository navigation
feat(cli): add --json to void db execute - #920
Draft
karthickeyan-dev wants to merge 6 commits into
Draft
karthickeyan-dev wants to merge 6 commits into
karthickeyan-dev wants to merge 6 commits into
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #912
Description
void db executeprinted rows only as a text table, followed by a success line on stdout. The table can't tellNULLfrom'NULL'or1from'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.--jsonwrites only a JSON array to stdout, with one entry per statement. Each entry has the same shape as a D1 result object, whichwrangler d1 execute --jsonalso 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
--jsonis unchanged.Per target:
sqlite3_complete(), on top of the sharedtokenizeSql, so literals and comments are respected and aCREATE 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. Likeexec(), statements commit as they run unless the SQL opens a transaction.metaholdschanges,last_row_idandduration.executeCloudflareD1Statements()keeps D1's result for each statement.executeCloudflareD1Sql()still flattens them for the table and for reading migrations.CALLreturns one set per procedure result plus a closing header, so those are grouped into one entry for theCALL. To find eachCALL, 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 inANSI_QUOTESmode. 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 … ENDnests only in a stored program or MariaDB'sBEGIN NOT ATOMIC, so a column namedbegindoesn't hide a semicolon. If the SQL contains a backslash or an executable comment,--jsonfirst readssql_modeandVERSION()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 shareexecuteExternalSql().--jsonwith several statements is refused before anything is sent. A singleCREATE TRIGGERcounts as one statement. The existingisSingleStatement()is left alone because the migration classifier hashes its result.--fileover 100 KB): returns no rows, so--jsonis refused before the prompt or upload.Design notes
--jsonalso skips the confirmation prompt. Here the prompt stays and renders on stderr, so a destructive--remote --jsonin a TTY still asks first.--yesand non-TTY behave as before.cloudflared access login --quietruns 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.SET sql_modeinside the SQL itself isn't followed when grouping MySQL results.byteavalues print as arrays of bytes, at any depth (bytea[]too), which is how D1 returns them.supportBigNumbersin JSON mode only, so the table is unchanged. PostgreSQLbigintwas already a string. This matches mysql2's own convention: a number when it fits, a string otherwise.Infinity,-InfinityandNaNprint as strings, as PostgreSQL'sto_jsondoes, instead ofnull, which would read as SQL NULL.numeric[]elements print as strings, like anumeric. Numbers insidejsonandjsonbvalues (and their arrays) keep the digits the database sent, throughJSON.parsesource text andJSON.rawJSON(Node 24). These parsers are set on the client in JSON mode only.CALLwith several result sets puts all their rows in oneresultsarray, in order. Adding each set's row count tometacould be a follow-up.--json:void <group> --jsonin a TTY used to draw the menu on stdout. It now goes straight to argument parsing, sovoid db --jsonprints a usage error on stderr.Test
db-commands.test.tscovers the real local SQLite path. It checks:RETURNINGcounts;BEGIN … COMMIT;last_row_id, and an infinite REAL next to NULL.db-execute-remote.test.tscovers direct D1 JSON, the stderr prompt, the import refusal, the proxy single result and multi-statement refusal, aCREATE TRIGGERthrough 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.tschecks the quiet login and that its stdout goes to stderr.db-execute-results.test.tscovers the node-postgres and mysql2 result shapes,CALLgrouping and its fallback, MySQL statement boundaries around stored program bodies, MySQL comments and quoted text (#,--before whitespace or control characters, backslash escapes with and withoutNO_BACKSLASH_ESCAPES,ANSI_QUOTESnames, 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),BEGINas a block only in stored programs andBEGIN NOT ATOMIC(not as a column orXA BEGIN), SQLite statement splitting (triggers,CASE … END,EXPLAIN, transactions, byte-order marks, a bare trailing/*, characters SQLite doesn't count as whitespace), PostgreSQLnumeric[]and JSON precision, and how bytes, big integers and non-finite numbers print.cloudflare-operations.test.tsanddb-args.test.tscover the new D1 function and the flag.vp check,vp run testandvpr test:allpass.🤖 Generated with Claude Code