Skip to content

void db execute prints results only as a table #912

Backlog

Description

@karthickeyan-dev

void db execute prints a query's rows only as an aligned text table, and prints "✓ SQL executed successfully." to stdout after it. A script that needs the rows, such as a check after an import, has to parse that table. SQL with several statements gives a script nothing to parse: locally it prints no rows, as the reference says. On a direct Cloudflare D1 deploy, it prints every statement's rows merged into one table.

Reproduction

Against the local D1 database of any SQLite project:

void db execute "select 1 as a, 'x' as b"
void db execute "select 1 as a; select 2 as b"
void db execute "select NULL as a, 'NULL' as b, 1 as c, '1' as d, 'x' || char(10) || 'y' as e"
void db execute --json "select 1 as a"

Expected

A way to get each statement's rows as JSON on stdout, with nothing else on stdout, as wrangler d1 execute --json gives.

Actual

$ void db execute "select 1 as a, 'x' as b"

a  b
────
1  x

✓ SQL executed successfully.

$ void db execute "select 1 as a; select 2 as b"


✓ SQL executed successfully.

$ void db execute "select NULL as a, 'NULL' as b, 1 as c, '1' as d, 'x' || char(10) || 'y' as e"

a     b     c  d  e
─────────────────────
NULL  NULL  1  1  x
y

✓ SQL executed successfully.

$ void db execute --json "select 1 as a"
■ Unknown option '--json'. To specify a positional argument starting with a '-',
   place it at the end of the command after '--', as in '-- "--json"

The table and the success line both go to stdout. The table can't tell NULL from the string 'NULL', or 1 from '1', and a value containing a newline splits its row.

Relevant implementation

In runExecute(), every path that has rows prints them with printTable(), which calls String() on each value:

  • Local SQLite: better-sqlite3's prepare() throws a RangeError for SQL with several statements. The error is caught, and the SQL runs through db.exec(), which returns no rows. fix(cli): print rows for every returning statement in db execute #864 kept this deliberately, and it's documented.
  • Remote D1 on a direct Cloudflare deploy: executeCloudflareD1Sql() gets one result per statement, but it joins all their rows into one array. printTable() then takes the columns from the first row, so the later statements' columns don't show. The reference says SQL with several statements prints nothing, so this path doesn't match it. A --file over 100 KB goes through D1's import API instead, which returns no rows, as the command warns.
  • The platform proxy, PostgreSQL and MySQL print the rows their response or driver returns.

Workaround

Run a single SELECT that returns one JSON value, and pick that line out of the table:

const output = execFileSync('pnpm', ['void', 'db', 'execute', '--yes', '--remote', `SELECT json_object(${pairs}) AS counts`], { encoding: 'utf8' });
const line = output.split('\n').find((text) => text.trimStart().startsWith('{'));
const counts = JSON.parse(line);

This only works when the whole result fits in one statement and one value.

Proposal

Add a --json flag. It would write to stdout a JSON array with one entry per statement, each holding that statement's rows, and nothing else. Warnings and the success line would go to stderr, or be left out. wrangler d1 execute --json prints D1's result objects (results, success, meta). Using the same shape would let scripts move between the two tools.

Notes for a fix

  • Local SQLite has to split the statements. Today it falls back to db.exec(), which returns no rows. With --json, the SQL would be split into statements, and each one run with all() when statement.reader is true, or run() when it isn't.
  • Remote D1 already gets one result per statement. executeCloudflareD1Sql() could return them as they are, instead of joining their rows.
  • A bulk import has no rows. With --json, it could print [] or fail with a clear message. Either way, --json must never print the table.

Environment: Void 1.11.0, Node 26.13.1, macOS, direct Cloudflare deploy with D1.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions