Skip to content

Commit

Permalink
fix(ext/node): sqlite bind support bigint values (#27890)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Jan 31, 2025
1 parent b8a878c commit 1cbaee9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ext/node/ops/sqlite/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ impl StatementSync {
ffi::SQLITE_TRANSIENT(),
);
}
} else if value.is_big_int() {
let value: v8::Local<v8::BigInt> = value.try_into().unwrap();
let (as_int, lossless) = value.i64_value();
if !lossless {
return Err(SqliteError::FailedBind(
"BigInt value is too large to bind",
));
}

// SAFETY: `self.inner` is a valid pointer to a sqlite3_stmt
// as it lives as long as the StatementSync instance.
unsafe {
ffi::sqlite3_bind_int64(raw, i + 1, as_int);
}
} else {
return Err(SqliteError::FailedBind("Unsupported type"));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_node/sqlite_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ Deno.test(
},
);

Deno.test("[node/sqlite] StatementSync bind bigints", () => {
const db = new DatabaseSync(":memory:");
db.exec("CREATE TABLE data(key INTEGER PRIMARY KEY);");

const stmt = db.prepare("INSERT INTO data (key) VALUES (?)");
assertEquals(stmt.run(100n), { lastInsertRowid: 100, changes: 1 });
db.close();
});

Deno.test("[node/sqlite] StatementSync read bigints are supported", () => {
const db = new DatabaseSync(":memory:");
db.exec("CREATE TABLE data(key INTEGER PRIMARY KEY);");
Expand Down

0 comments on commit 1cbaee9

Please sign in to comment.