Skip to content
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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/node/ops/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum SqliteError {
#[class(generic)]
#[error("Invalid constructor")]
InvalidConstructor,
#[class(generic)]
#[error("Expanded SQL text would exceed configured limits")]
InvalidExpandedSql,
Comment on lines +42 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try creating a test that does that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there is a reliable way. A test would have to create a query string takes up all available memory.

#[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 @@ -71,6 +71,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