FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

sqlite: bind Boolean · nodejs/node@49fb028 · GitHub

/ node Public

Commit 49fb028

Browse files
authored andcommitted
sqlite: bind Boolean
PR-URL: #62001 Fixes: #57862 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 09d0da9 commit 49fb028

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
@@ -2780,10 +2780,11 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
27802780

27812781
bool StatementSync::BindValue(const Local<Value>& value, const int index) {
27822782
// SQLite only supports a subset of JavaScript types. Some JS types such as
2783-
// functions don't make sense to support. Other JS types such as booleans and
2783+
// functions don't make sense to support. Other JS types such as
27842784
// Dates could be supported by converting them to numbers. However, there
27852785
// would not be a good way to read the values back from SQLite with the
2786-
// original type.
2786+
// original type. JS Boolean binds to 1 and 0 because SQLite maps true and
2787+
// false keywords to 1 and 0.
27872788
Isolate* isolate = env()->isolate();
27882789
int r;
27892790
if (value->IsNumber()) {
@@ -2817,6 +2818,8 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
28172818
buf.data(),
28182819
static_cast<sqlite3_uint64>(buf.length()),
28192820
SQLITE_TRANSIENT);
2821+
} else if (value->IsBoolean()) {
2822+
r = sqlite3_bind_int(statement_, index, value->IsTrue() ? 1 : 0);
28202823
} else if (value->IsBigInt()) {
28212824
bool lossless;
28222825
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
@@ -71,6 +71,15 @@ suite('data binding and mapping', () => {
7171
text: '',
7272
buf: new Uint8Array(),
7373
});
74+
75+
t.assert.deepStrictEqual(
76+
stmt.run(5, true, false, true, null),
77+
{ changes: 1, lastInsertRowid: 5 }
78+
);
79+
t.assert.deepStrictEqual(
80+
query.get(5),
81+
{ __proto__: null, key: 5, int: 1, double: 0, text: '1', buf: null }
82+
);
7483
});
7584

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

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL