-
-
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
7
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.
Open
sqlite: add trace sql hook #62241
Changes from all commits
Commits
Show all changes
7 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 83627f5
sqlite: use diagnostic channel
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| 'use strict'; | ||
|
|
||
| const { skipIfSQLiteMissing } = require('../common'); | ||
| skipIfSQLiteMissing(); | ||
|
|
||
| const assert = require('node:assert'); | ||
| const dc = require('node:diagnostics_channel'); | ||
| const { DatabaseSync } = require('node:sqlite'); | ||
| const { suite, it } = require('node:test'); | ||
|
|
||
| suite('sqlite.db.query diagnostics channel', () => { | ||
| it('subscriber receives SQL string for exec() statements', (t) => { | ||
| const calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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)'); | ||
| }); | ||
|
|
||
| it('subscriber receives SQL string for prepared INSERT statements', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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)'); | ||
| }); | ||
|
|
||
| it('subscriber receives SQL string for prepared SELECT statements', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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'); | ||
| }); | ||
|
|
||
| it('subscriber receives SQL string for prepared UPDATE statements', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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'); | ||
| }); | ||
|
|
||
| it('subscriber receives SQL string for prepared DELETE statements', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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'); | ||
| }); | ||
|
|
||
| it('no calls received after unsubscribe', (t) => { | ||
| const calls = []; | ||
| const db = new DatabaseSync(':memory:'); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
|
|
||
| db.exec('CREATE TABLE t (x INTEGER)'); | ||
| assert.strictEqual(calls.length, 1); | ||
|
|
||
| dc.unsubscribe('sqlite.db.query', handler); | ||
| db.exec('INSERT INTO t VALUES (1)'); | ||
| assert.strictEqual(calls.length, 1); // No new calls after unsubscribe | ||
| }); | ||
|
|
||
| it('falls back to source SQL when expansion fails', (t) => { | ||
| let calls = []; | ||
| const db = new DatabaseSync(':memory:', { limits: { length: 1000 } }); | ||
| t.after(() => db.close()); | ||
|
|
||
| const handler = (sql) => calls.push(sql); | ||
| dc.subscribe('sqlite.db.query', handler); | ||
| t.after(() => dc.unsubscribe('sqlite.db.query', handler)); | ||
|
|
||
| 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 (?)'); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to get the query template and values as they were prior to generating the final SQL string? If at all possible, I think it would be valuable for Observability purposes to publish an event which includes: database instance, query template, query parameters, and final query string.