Skip to content

Commit c2832d7

Browse files
authored
fix(ext/sqlite): add sourceSQL and expandedSQL getters (#27921)
1 parent 441b1d3 commit c2832d7

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

ext/node/ops/sqlite/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub enum SqliteError {
4242
#[class(generic)]
4343
#[error("Invalid constructor")]
4444
InvalidConstructor,
45+
#[class(generic)]
46+
#[error("Expanded SQL text would exceed configured limits")]
47+
InvalidExpandedSql,
4548
#[class(range)]
4649
#[error("The value of column {0} is too large to be represented as a JavaScript number: {1}")]
4750
NumberTooLarge(i32, i64),

ext/node/ops/sqlite/statement.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,38 @@ impl StatementSync {
373373
fn set_read_big_ints(&self, enabled: bool) {
374374
self.use_big_ints.set(enabled);
375375
}
376+
377+
#[getter]
378+
#[rename("sourceSQL")]
379+
#[string]
380+
fn source_sql(&self) -> String {
381+
// SAFETY: `self.inner` is a valid pointer to a sqlite3_stmt
382+
// as it lives as long as the StatementSync instance.
383+
unsafe {
384+
let raw = ffi::sqlite3_sql(self.inner);
385+
std::ffi::CStr::from_ptr(raw as _)
386+
.to_string_lossy()
387+
.into_owned()
388+
}
389+
}
390+
391+
#[getter]
392+
#[rename("expandedSQL")]
393+
#[string]
394+
fn expanded_sql(&self) -> Result<String, SqliteError> {
395+
// SAFETY: `self.inner` is a valid pointer to a sqlite3_stmt
396+
// as it lives as long as the StatementSync instance.
397+
unsafe {
398+
let raw = ffi::sqlite3_expanded_sql(self.inner);
399+
if raw.is_null() {
400+
return Err(SqliteError::InvalidExpandedSql);
401+
}
402+
let sql = std::ffi::CStr::from_ptr(raw as _)
403+
.to_string_lossy()
404+
.into_owned();
405+
ffi::sqlite3_free(raw as _);
406+
407+
Ok(sql)
408+
}
409+
}
376410
}

tests/unit_node/sqlite_test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ Deno.test("[node/sqlite] StatementSync read bigints are supported", () => {
7272

7373
stmt.setReadBigInts(true);
7474
assertEquals(stmt.get(), { key: 1n, __proto__: null });
75+
76+
assertEquals(stmt.sourceSQL, "SELECT * FROM data");
77+
assertEquals(stmt.expandedSQL, "SELECT * FROM data");
7578
});
7679

7780
Deno.test("[node/sqlite] StatementSync integer too large", () => {

0 commit comments

Comments
 (0)