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
4 changes: 4 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ added: v22.5.0

* `enabled` {boolean} Enables or disables support for binding named parameters
without the prefix character.
* Returns: {StatementSync} The prepared statement.

The names of SQLite parameters begin with a prefix character. By default,
`node:sqlite` requires that this prefix character is present when binding
Expand All @@ -943,6 +944,7 @@ added:
-->

* `enabled` {boolean} Enables or disables support for unknown named parameters.
* Returns: {StatementSync} The prepared statement.

By default, if an unknown name is encountered while binding parameters, an
exception is thrown. This method allows unknown named parameters to be ignored.
Expand All @@ -956,6 +958,7 @@ added:
-->

* `enabled` {boolean} Enables or disables the return of query results as arrays.
* Returns: {StatementSync} The prepared statement.

When enabled, query results returned by the `all()`, `get()`, and `iterate()` methods will be returned as arrays instead
of objects.
Expand All @@ -968,6 +971,7 @@ added: v22.5.0

* `enabled` {boolean} Enables or disables the use of `BigInt`s when reading
`INTEGER` fields from the database.
* Returns: {StatementSync} The prepared statement.

When reading from the database, SQLite `INTEGER`s are mapped to JavaScript
numbers by default. However, SQLite `INTEGER`s can store values larger than
Expand Down
4 changes: 4 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,7 @@ void StatementSync::SetAllowBareNamedParameters(
}

stmt->allow_bare_named_params_ = args[0]->IsTrue();
args.GetReturnValue().Set(stmt->object());
}

void StatementSync::SetAllowUnknownNamedParameters(
Expand All @@ -2620,6 +2621,7 @@ void StatementSync::SetAllowUnknownNamedParameters(
}

stmt->allow_unknown_named_params_ = args[0]->IsTrue();
args.GetReturnValue().Set(stmt->object());
}

void StatementSync::SetReadBigInts(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -2636,6 +2638,7 @@ void StatementSync::SetReadBigInts(const FunctionCallbackInfo<Value>& args) {
}

stmt->use_big_ints_ = args[0]->IsTrue();
args.GetReturnValue().Set(stmt->object());
}

void StatementSync::SetReturnArrays(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -2652,6 +2655,7 @@ void StatementSync::SetReturnArrays(const FunctionCallbackInfo<Value>& args) {
}

stmt->return_arrays_ = args[0]->IsTrue();
args.GetReturnValue().Set(stmt->object());
}

void IllegalConstructor(const FunctionCallbackInfo<Value>& args) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-sqlite-named-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ suite('StatementSync.prototype.setAllowUnknownNamedParameters()', () => {
);
t.assert.strictEqual(setup, undefined);
const stmt = db.prepare('INSERT INTO data (key, val) VALUES ($k, $v)');
t.assert.strictEqual(stmt.setAllowUnknownNamedParameters(true), undefined);
t.assert.strictEqual(stmt.setAllowUnknownNamedParameters(true), stmt);
const params = { $a: 1, $b: 2, $k: 42, $y: 25, $v: 84, $z: 99 };
t.assert.deepStrictEqual(
stmt.run(params),
{ changes: 1, lastInsertRowid: 1 },
);
t.assert.strictEqual(stmt.setAllowUnknownNamedParameters(false), undefined);
t.assert.strictEqual(stmt.setAllowUnknownNamedParameters(false), stmt);
t.assert.throws(() => {
stmt.run(params);
}, {
Expand Down
25 changes: 10 additions & 15 deletions test/parallel/test-sqlite-statement-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,22 @@ suite('StatementSync.prototype.setReadBigInts()', () => {

const query = db.prepare('SELECT val FROM data');
t.assert.deepStrictEqual(query.get(), { __proto__: null, val: 42 });
t.assert.strictEqual(query.setReadBigInts(true), undefined);
t.assert.strictEqual(query.setReadBigInts(true), query);
t.assert.deepStrictEqual(query.get(), { __proto__: null, val: 42n });
t.assert.strictEqual(query.setReadBigInts(false), undefined);
t.assert.strictEqual(query.setReadBigInts(false), query);
t.assert.deepStrictEqual(query.get(), { __proto__: null, val: 42 });

const insert = db.prepare('INSERT INTO data (key) VALUES (?)');
t.assert.deepStrictEqual(
insert.run(10),
{ changes: 1, lastInsertRowid: 10 },
);
t.assert.strictEqual(insert.setReadBigInts(true), undefined);
t.assert.strictEqual(insert.setReadBigInts(true), insert);
t.assert.deepStrictEqual(
insert.run(20),
{ changes: 1n, lastInsertRowid: 20n },
);
t.assert.strictEqual(insert.setReadBigInts(false), undefined);
t.assert.strictEqual(insert.setReadBigInts(false), insert);
t.assert.deepStrictEqual(
insert.run(30),
{ changes: 1, lastInsertRowid: 30 },
Expand Down Expand Up @@ -365,8 +365,7 @@ suite('StatementSync.prototype.setReadBigInts()', () => {
code: 'ERR_OUT_OF_RANGE',
message: /^Value is too large to be represented as a JavaScript number: 9007199254740992$/,
});
const good = db.prepare(`SELECT ${Number.MAX_SAFE_INTEGER} + 1`);
good.setReadBigInts(true);
const good = db.prepare(`SELECT ${Number.MAX_SAFE_INTEGER} + 1`).setReadBigInts(true);
t.assert.deepStrictEqual(good.get(), {
__proto__: null,
[`${Number.MAX_SAFE_INTEGER} + 1`]: 2n ** 53n,
Expand Down Expand Up @@ -422,9 +421,7 @@ suite('StatementSync.prototype.get() with array output', () => {
`);
t.assert.strictEqual(setup, undefined);

const query = db.prepare('SELECT id, big_num FROM big_data');
query.setReturnArrays(true);
query.setReadBigInts(true);
const query = db.prepare('SELECT id, big_num FROM big_data').setReturnArrays(true).setReadBigInts(true);

const row = query.get();
t.assert.deepStrictEqual(row, expected);
Expand Down Expand Up @@ -488,8 +485,7 @@ suite('StatementSync.prototype.all() with array output', () => {
`);
t.assert.strictEqual(setup, undefined);

const query = db.prepare('SELECT * FROM wide_table');
query.setReturnArrays(true);
const query = db.prepare('SELECT * FROM wide_table').setReturnArrays(true);

const results = query.all();
t.assert.strictEqual(results.length, 1);
Expand Down Expand Up @@ -545,8 +541,7 @@ suite('StatementSync.prototype.iterate() with array output', () => {
INSERT INTO test (key, val) VALUES ('key1', 'val1');
INSERT INTO test (key, val) VALUES ('key2', 'val2');
`);
const stmt = db.prepare('SELECT key, val FROM test');
stmt.setReturnArrays(true);
const stmt = db.prepare('SELECT key, val FROM test').setReturnArrays(true);

const iterator = stmt.iterate();
const results = [];
Expand Down Expand Up @@ -579,14 +574,14 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
stmt.run({ k: 1, v: 2 }),
{ changes: 1, lastInsertRowid: 1 },
);
t.assert.strictEqual(stmt.setAllowBareNamedParameters(false), undefined);
t.assert.strictEqual(stmt.setAllowBareNamedParameters(false), stmt);
t.assert.throws(() => {
stmt.run({ k: 2, v: 4 });
}, {
code: 'ERR_INVALID_STATE',
message: /Unknown named parameter 'k'/,
});
t.assert.strictEqual(stmt.setAllowBareNamedParameters(true), undefined);
t.assert.strictEqual(stmt.setAllowBareNamedParameters(true), stmt);
t.assert.deepStrictEqual(
stmt.run({ k: 3, v: 6 }),
{ changes: 1, lastInsertRowid: 3 },
Expand Down
Loading