Skip to content

Commit c154225

Browse files
mike-git374aduh95
authored andcommitted
sqlite: bind Boolean
PR-URL: #62001 Fixes: #57862 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent cdb732b commit c154225

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/node_sqlite.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,10 +2736,11 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
27362736

27372737
bool StatementSync::BindValue(const Local<Value>& value, const int index) {
27382738
// SQLite only supports a subset of JavaScript types. Some JS types such as
2739-
// functions don't make sense to support. Other JS types such as booleans and
2739+
// functions don't make sense to support. Other JS types such as
27402740
// Dates could be supported by converting them to numbers. However, there
27412741
// would not be a good way to read the values back from SQLite with the
2742-
// original type.
2742+
// original type. JS Boolean binds to 1 and 0 because SQLite maps true and
2743+
// false keywords to 1 and 0.
27432744
Isolate* isolate = env()->isolate();
27442745
int r;
27452746
if (value->IsNumber()) {
@@ -2773,6 +2774,8 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
27732774
buf.data(),
27742775
static_cast<sqlite3_uint64>(buf.length()),
27752776
SQLITE_TRANSIENT);
2777+
} else if (value->IsBoolean()) {
2778+
r = sqlite3_bind_int(statement_, index, value->IsTrue() ? 1 : 0);
27762779
} else if (value->IsBigInt()) {
27772780
bool lossless;
27782781
int64_t as_int = value.As<BigInt>()->Int64Value(&lossless);

test/parallel/test-sqlite-data-types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ suite('data binding and mapping', () => {
8080
text: '',
8181
buf: new Uint8Array(),
8282
});
83+
84+
t.assert.deepStrictEqual(
85+
stmt.run(5, true, false, true, null),
86+
{ changes: 1, lastInsertRowid: 5 }
87+
);
88+
t.assert.deepStrictEqual(
89+
query.get(5),
90+
{ __proto__: null, key: 5, int: 1, double: 0, text: '1', buf: null }
91+
);
8392
});
8493

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

0 commit comments

Comments
 (0)