-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
sqlite: add trace sql hook #62241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
araujogui
wants to merge
6
commits into
nodejs:main
Choose a base branch
from
araujogui:sqlite-verbose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−0
Open
sqlite: add trace sql hook #62241
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8784e0
sqlite: add verbose option
araujogui df28174
sqlite: add more verbose test cases
araujogui 51a3ee4
doc: add verbose option doc
araujogui 0e8bfa9
sqlite: rename verbose to trace
araujogui b68b4be
sqlite: rename to setSqlTraceHook
araujogui 0d78adb
src: format file
araujogui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| 'use strict'; | ||
|
|
||
| const { skipIfSQLiteMissing } = require('../common'); | ||
| skipIfSQLiteMissing(); | ||
|
|
||
| const assert = require('node:assert'); | ||
| const { DatabaseSync } = require('node:sqlite'); | ||
| const { suite, it } = require('node:test'); | ||
|
|
||
| suite('DatabaseSync verbose option', () => { | ||
| it('callback receives SQL string for exec() statements', (t) => { | ||
| const calls = []; | ||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| db.exec('INSERT INTO t VALUES (1)'); | ||
|
|
||
| assert.strictEqual(calls.length, 2); | ||
| assert.strictEqual(calls[0], 'CREATE TABLE t (x INTEGER)'); | ||
| assert.strictEqual(calls[1], 'INSERT INTO t VALUES (1)'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('callback receives SQL string for prepared statement execution', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| calls = []; // reset after setup | ||
|
|
||
| const stmt = db.prepare('INSERT INTO t VALUES (?)'); | ||
| stmt.run(42); | ||
|
|
||
| assert.strictEqual(calls.length, 1); | ||
| assert.strictEqual(calls[0], 'INSERT INTO t VALUES (42.0)'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('callback receives SQL string for SELECT statements', () => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| db.exec('INSERT INTO t VALUES (1)'); | ||
| calls = []; // reset after setup | ||
|
|
||
| const stmt = db.prepare('SELECT x FROM t WHERE x = ?'); | ||
| stmt.get(1); | ||
|
|
||
| assert.strictEqual(calls.length, 1); | ||
| assert.strictEqual(calls[0], 'SELECT x FROM t WHERE x = 1.0'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('callback receives SQL string for UPDATE statements', () => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| db.exec('INSERT INTO t VALUES (1)'); | ||
| calls = []; // reset after setup | ||
|
|
||
| const stmt = db.prepare('UPDATE t SET x = ? WHERE x = ?'); | ||
| stmt.run(2, 1); | ||
|
|
||
| assert.strictEqual(calls.length, 1); | ||
| assert.strictEqual(calls[0], 'UPDATE t SET x = 2.0 WHERE x = 1.0'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('callback receives SQL string for DELETE statements', () => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| db.exec('INSERT INTO t VALUES (1)'); | ||
| calls = []; // reset after setup | ||
|
|
||
| const stmt = db.prepare('DELETE FROM t WHERE x = ?'); | ||
| stmt.run(1); | ||
|
|
||
| assert.strictEqual(calls.length, 1); | ||
| assert.strictEqual(calls[0], 'DELETE FROM t WHERE x = 1.0'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('falls back to source SQL when expansion fails', () => { | ||
| let calls = []; | ||
|
|
||
| const db = new DatabaseSync(':memory:', { | ||
| verbose: (sql) => calls.push(sql), | ||
| limits: { length: 1000 }, | ||
| }); | ||
|
|
||
| db.exec('CREATE TABLE t (x TEXT)'); | ||
| calls = []; // reset after setup | ||
|
|
||
| const stmt = db.prepare('INSERT INTO t VALUES (?)'); | ||
|
|
||
| const longValue = 'a'.repeat(977); | ||
| stmt.run(longValue); | ||
|
|
||
| assert.strictEqual(calls.length, 1); | ||
| // Falls back to source SQL with unexpanded '?' placeholder | ||
| assert.strictEqual(calls[0], 'INSERT INTO t VALUES (?)'); | ||
| db.close(); | ||
| }); | ||
|
|
||
| it('invalid type for verbose throws ERR_INVALID_ARG_TYPE', () => { | ||
| assert.throws(() => { | ||
| new DatabaseSync(':memory:', { verbose: 'not-a-function' }); | ||
| }, { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| message: /The "options\.verbose" argument must be a function\./, | ||
| }); | ||
|
|
||
| assert.throws(() => { | ||
| new DatabaseSync(':memory:', { verbose: 42 }); | ||
| }, { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| message: /The "options\.verbose" argument must be a function\./, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.