Skip to content

Commit 732d3c8

Browse files
committed
Fix bind parameter handling
We had some functions using open-coded parameter conversion logic, which was broken.
1 parent ea5327e commit 732d3c8

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Statement {
218218
* Executes the SQL statement and returns an info object.
219219
*/
220220
run(...bindParameters) {
221-
if (typeof bindParameters[0] === "object" && bindParameters[0] !== null) {
221+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
222222
return statementRun.call(this.stmt, bindParameters[0]);
223223
} else {
224224
return statementRun.call(this.stmt, bindParameters.flat());
@@ -231,7 +231,7 @@ class Statement {
231231
* @param bindParameters - The bind parameters for executing the statement.
232232
*/
233233
get(...bindParameters) {
234-
if (typeof bindParameters[0] === "object" && bindParameters[0] !== null) {
234+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
235235
return statementGet.call(this.stmt, bindParameters[0]);
236236
} else {
237237
return statementGet.call(this.stmt, bindParameters.flat());
@@ -244,7 +244,12 @@ class Statement {
244244
* @param bindParameters - The bind parameters for executing the statement.
245245
*/
246246
iterate(...bindParameters) {
247-
const rows = statementRowsSync.call(this.stmt, ...bindParameters);
247+
var rows = undefined;
248+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
249+
rows = statementRowsSync.call(this.stmt, bindParameters[0]);
250+
} else {
251+
rows = statementRowsSync.call(this.stmt, bindParameters.flat());
252+
}
248253
const iter = {
249254
next() {
250255
const row = rowsNext.call(rows);

integration-tests/tests/sync.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ test.serial("Statement.iterate() [empty]", async (t) => {
9494

9595
const stmt = db.prepare("SELECT * FROM users WHERE id = 0");
9696
t.is(stmt.iterate().next().done, true);
97+
t.is(stmt.iterate([]).next().done, true);
98+
t.is(stmt.iterate({}).next().done, true);
9799
});
98100

99101
test.serial("Statement.iterate()", async (t) => {

promise.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Statement {
219219
* Executes the SQL statement and returns an info object.
220220
*/
221221
run(...bindParameters) {
222-
if (typeof bindParameters[0] === "object" && bindParameters[0] !== null) {
222+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
223223
return statementRun.call(this.stmt, bindParameters[0]);
224224
} else {
225225
return statementRun.call(this.stmt, bindParameters.flat());
@@ -232,7 +232,7 @@ class Statement {
232232
* @param bindParameters - The bind parameters for executing the statement.
233233
*/
234234
get(...bindParameters) {
235-
if (typeof bindParameters[0] === "object" && bindParameters[0] !== null) {
235+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
236236
return statementGet.call(this.stmt, bindParameters[0]);
237237
} else {
238238
return statementGet.call(this.stmt, bindParameters.flat());
@@ -245,7 +245,12 @@ class Statement {
245245
* @param bindParameters - The bind parameters for executing the statement.
246246
*/
247247
async iterate(...bindParameters) {
248-
const rows = await statementRowsAsync.call(this.stmt, ...bindParameters);
248+
var rows = undefined;
249+
if (bindParameters.length == 1 && typeof bindParameters[0] === "object") {
250+
rows = await statementRowsAsync.call(this.stmt, bindParameters[0]);
251+
} else {
252+
rows = await statementRowsAsync.call(this.stmt, bindParameters.flat());
253+
}
249254
const iter = {
250255
next() {
251256
const row = rowsNext.call(rows);

src/lib.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,8 @@ impl Statement {
286286

287287
fn js_rows_sync(mut cx: FunctionContext) -> JsResult<JsValue> {
288288
let stmt: Handle<'_, JsBox<Statement>> = cx.this()?;
289-
let mut params = vec![];
290-
for i in 0..cx.len() {
291-
let v = cx.argument::<JsValue>(i)?;
292-
let v = js_value_to_value(&mut cx, v)?;
293-
params.push(v);
294-
}
295-
let params = libsql::Params::Positional(params);
289+
let params = cx.argument::<JsValue>(0)?;
290+
let params = convert_params(&mut cx, &stmt, params)?;
296291
stmt.stmt.reset();
297292
let fut = stmt.stmt.query(&params);
298293
let result = stmt.rt.block_on(fut);
@@ -307,13 +302,8 @@ impl Statement {
307302

308303
fn js_rows_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
309304
let stmt: Handle<'_, JsBox<Statement>> = cx.this()?;
310-
let mut params = vec![];
311-
for i in 0..cx.len() {
312-
let v = cx.argument::<JsValue>(i)?;
313-
let v = js_value_to_value(&mut cx, v)?;
314-
params.push(v);
315-
}
316-
let params = libsql::Params::Positional(params);
305+
let params = cx.argument::<JsValue>(0)?;
306+
let params = convert_params(&mut cx, &stmt, params)?;
317307
stmt.stmt.reset();
318308
let (deferred, promise) = cx.promise();
319309
let channel = cx.channel();

0 commit comments

Comments
 (0)