fix(ext/sqlite): add sourceSQL and expandedSQL getters (#27921)

This commit is contained in:
Divy Srivastava 2025-02-04 17:38:41 +05:30 committed by GitHub
parent 441b1d307f
commit c2832d70a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 0 deletions

View file

@ -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),

View file

@ -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)
}
}
}

View file

@ -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", () => {