Skip to content
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
7 changes: 5 additions & 2 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2257,10 +2257,11 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {

bool StatementSync::BindValue(const Local<Value>& value, const int index) {
// SQLite only supports a subset of JavaScript types. Some JS types such as
// functions don't make sense to support. Other JS types such as booleans and
// functions don't make sense to support. Other JS types such as
// Dates could be supported by converting them to numbers. However, there
// would not be a good way to read the values back from SQLite with the
// original type.
// original type. We make an exception for JS Boolean binding to 1 and 0
// because SQLite maps true and false keywords to 1 and 0.
Isolate* isolate = env()->isolate();
int r;
if (value->IsNumber()) {
Expand Down Expand Up @@ -2293,6 +2294,8 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
buf.data(),
static_cast<sqlite3_uint64>(buf.length()),
SQLITE_TRANSIENT);
} else if (value->IsBoolean()) {
r = sqlite3_bind_int(statement_, index, value->IsTrue() ? 1 : 0);
} else if (value->IsBigInt()) {
bool lossless;
int64_t as_int = value.As<BigInt>()->Int64Value(&lossless);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-sqlite-data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ suite('data binding and mapping', () => {
stmt.run(4, 99n, 0xf, '', new Uint8Array()),
{ changes: 1, lastInsertRowid: 4 },
);
t.assert.deepStrictEqual(
stmt.run(5, true, false, true, null),
{ changes: 1, lastInsertRowid: 5 },
);

const query = db.prepare('SELECT * FROM types WHERE key = ?');
t.assert.deepStrictEqual(query.get(1), {
Expand Down Expand Up @@ -80,6 +84,14 @@ suite('data binding and mapping', () => {
text: '',
buf: new Uint8Array(),
});
t.assert.deepStrictEqual(query.get(5), {
__proto__: null,
key: 5,
int: 1,
double: 0,
text: '1',
buf: null,
});
});

test('large strings are bound correctly', (t) => {
Expand Down