Skip to content

fix(cli): print rows for every returning statement in db execute - #864

Open
0froq wants to merge 1 commit into
voidzero-dev:mainfrom
0froq:fix/862-db-execute-results
Open

0froq wants to merge 1 commit into
voidzero-dev:mainfrom
0froq:fix/862-db-execute-results

Conversation

@0froq

@0froq 0froq commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Fixes #862

Description

Local void db execute printed rows only when the SQL started with SELECT, so WITH, PRAGMA, EXPLAIN and RETURNING ran and reported success with no output. The remote D1, PostgreSQL and MySQL paths already print rows for any statement that returns them.

The local path now asks better-sqlite3 whether the prepared statement reads rows (statement.reader) and prints the table when it does, instead of matching the statement text. Writes and DDL stay silent. A script with several statements still runs through exec(), which reports no rows, and the reference docs now say so.

Test

Tests in packages/void/test/unit/db-commands.test.ts run the local SQLite path against a real database file. WITH ... SELECT, PRAGMA database_list, EXPLAIN QUERY PLAN, a SELECT behind a leading comment and INSERT ... RETURNING print a table. INSERT, CREATE TABLE and an empty result print nothing. A two-statement script runs without output, and SELECT 'a;b' stays one statement.

Same command on both branches:

$ void db execute "PRAGMA table_info(smoke)"     # base
✓ SQL executed successfully.

$ void db execute "PRAGMA table_info(smoke)"     # this branch
cid  name  type     notnull  dflt_value  pk
───────────────────────────────────────────
0    id    INTEGER  0        NULL        1
1    name  TEXT     0        NULL        0
✓ SQL executed successfully.

Local `void db execute` printed results only when the SQL started with
`SELECT`, so `WITH`, `PRAGMA`, `EXPLAIN` and `RETURNING` succeeded
silently. The remote D1, PostgreSQL and MySQL paths already print rows
for any statement that returns them.

Ask better-sqlite3 whether the prepared statement reads rows instead of
matching the statement text, and leave a multi-statement script on
`exec()`, which reports no rows.
@0froq

0froq commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

👁️‍🗨️ Review this pull request with grouped, summarized diffs at:
👉 https://pulls.review/gh/voidzero-dev/void/864?from=0froq

raw result

💭 analyzed by openai-compatible/opencode-go/deepseek-v4.1-flash
🕰️ 2026-11-17 06:20 UTC
🔗 head eb919df
🤖 automated by pulls.review

{
  "headSha": "eb919dff6844998d123c05e5cc7a4903ce1bec64",
  "result": {
    "overallSummary": "Fixes the local `db execute` output: rows were printed only when the SQL text started with `SELECT`, so `WITH`, `PRAGMA`, `EXPLAIN` and `RETURNING` succeeded silently. The local path now asks better-sqlite3 whether the prepared statement reads rows (`statement.reader`), matching the remote D1/PostgreSQL/MySQL behavior. **Review order:** the CLI change first, then the tests that pin it, then the one-line doc update.",
    "groups": [
      {
        "key": "local-execute-rows",
        "label": "Local execute row printing",
        "summary": "Classify the statement by what SQLite reports (`statement.reader`) instead of a `^SELECT` regex, so any row-returning statement prints a table and writes/DDL stay silent. Multi-statement scripts still fall back to `exec()`. The comment/semicolon stripping used by the safety check is extracted into `stripSqlDecoration`, and the sqlite statement type gains the `reader` flag that this relies on.",
        "category": "cli",
        "filePaths": [
          "packages/void/src/cli/db.ts",
          "packages/void/src/migrations/runner.ts"
        ],
        "critical": false,
        "fileNotes": [
          {
            "path": "packages/void/src/cli/db.ts",
            "text": "Two behavior changes beyond the printed rows: the `No rows returned.` info message is gone (empty results are now silent, like the remote paths), and a failing `prepare()` no longer surfaces when the SQL contains a semicolon - it is re-run through `exec()` instead, which also swallows the fact that nothing was printed."
          },
          {
            "path": "packages/void/src/migrations/runner.ts",
            "text": "Type-only addition documenting the better-sqlite3 statement shape; no runtime change."
          }
        ]
      },
      {
        "key": "tests",
        "label": "Execute tests",
        "summary": "New cases run the unmocked local SQLite path against a real database file so `reader` is exercised for real: `WITH ... SELECT`, `PRAGMA`, `EXPLAIN QUERY PLAN`, a `SELECT` behind a comment and `INSERT ... RETURNING` print tables, while writes, DDL, empty results and multi-statement scripts print nothing.",
        "category": "tests",
        "filePaths": [
          "packages/void/test/unit/db-commands.test.ts"
        ],
        "fileNotes": [
          {
            "path": "packages/void/test/unit/db-commands.test.ts",
            "text": "`tableOutput()` drops blank lines because `runDbCommand` prints a leading blank line, so it only sees actual table output; the two-statement script test also reopens the database to confirm the script really ran."
          }
        ]
      },
      {
        "key": "docs",
        "label": "Docs wording",
        "summary": "Reword the `void db execute` reference to describe the new contract: row-returning statements print a table, writes are silent, and multi-statement SQL runs as a script without results.",
        "category": "docs",
        "filePaths": [
          "docs/reference/cli/database.md"
        ]
      }
    ],
    "schemaVersion": 1,
    "source": "llm",
    "generatedAt": "2026-11-17T06:20:57.979Z",
    "model": "openai-compatible/opencode-go/deepseek-v4.1-flash",
    "locale": "en"
  }
}

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two issues:

  • Failed writes can execute twice because the catch block in db.ts retries through exec. With UPDATE OR FAIL … SET count=count+1, note='a;b', a constraint failure on the second row leaves the first row incremented twice.
  • Scripts containing quoted comment markers fail. Two inserts where the first value is '--' fail here without inserting either row. The regex mistakes string contents for comments and hides statement separators; '/' and '/' reproduce it too. We should use SQLite’s preparation result instead.

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.

Local void db execute hides results for WITH, PRAGMA and EXPLAIN queries

2 participants