Skip to content

Commit

Permalink
fix(ext/sqlite): add sourceSQL and expandedSQL getters (#27921)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Feb 4, 2025
1 parent 441b1d3 commit c2832d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ext/node/ops/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub enum SqliteError {
#[class(generic)]
#[error("Invalid constructor")]
InvalidConstructor,
#[class(generic)]
#[error("Expanded SQL text would exceed configured limits")]
InvalidExpandedSql,
#[class(range)]
#[error("The value of column {0} is too large to be represented as a JavaScript number: {1}")]
NumberTooLarge(i32, i64),
Expand Down
34 changes: 34 additions & 0 deletions ext/node/ops/sqlite/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,38 @@ impl StatementSync {
fn set_read_big_ints(&self, enabled: bool) {
self.use_big_ints.set(enabled);
}

#[getter]
#[rename("sourceSQL")]
#[string]
fn source_sql(&self) -> String {
// SAFETY: `self.inner` is a valid pointer to a sqlite3_stmt
// as it lives as long as the StatementSync instance.
unsafe {
let raw = ffi::sqlite3_sql(self.inner);
std::ffi::CStr::from_ptr(raw as _)
.to_string_lossy()
.into_owned()
}
}

#[getter]
#[rename("expandedSQL")]
#[string]
fn expanded_sql(&self) -> Result<String, SqliteError> {
// SAFETY: `self.inner` is a valid pointer to a sqlite3_stmt
// as it lives as long as the StatementSync instance.
unsafe {
let raw = ffi::sqlite3_expanded_sql(self.inner);
if raw.is_null() {
return Err(SqliteError::InvalidExpandedSql);
}
let sql = std::ffi::CStr::from_ptr(raw as _)
.to_string_lossy()
.into_owned();
ffi::sqlite3_free(raw as _);

Ok(sql)
}
}
}
3 changes: 3 additions & 0 deletions tests/unit_node/sqlite_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Deno.test("[node/sqlite] StatementSync read bigints are supported", () => {

stmt.setReadBigInts(true);
assertEquals(stmt.get(), { key: 1n, __proto__: null });

assertEquals(stmt.sourceSQL, "SELECT * FROM data");
assertEquals(stmt.expandedSQL, "SELECT * FROM data");
});

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

0 comments on commit c2832d7

Please sign in to comment.