Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent bind error with undefined/null parameters #375

Merged
merged 6 commits into from
Mar 31, 2020
Merged
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
13 changes: 7 additions & 6 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
}

/** @typedef {string|number|null|Uint8Array} Database.SqlValue */
/** @typedef {Database.SqlValue[]|Object<string, Database.SqlValue>} Statement.BindParams */
/** @typedef {Database.SqlValue[]|Object<string, Database.SqlValue>|null} Statement.BindParams
*/

/** Bind values to the parameters, after having reseted the statement
/** Bind values to the parameters, after having reseted the statement.
* If values is null, do nothing and return true.
*
* SQL statements can have parameters, named *'?', '?NNN', ':VVV', '@VVV', '$VVV'*,
* where NNN is a number and VVV a string.
Expand Down Expand Up @@ -251,10 +253,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
throw "Statement closed";
}
this["reset"]();
if (Array.isArray(values)) {
return this.bindFromArray(values);
}
return this.bindFromObject(values);
if (Array.isArray(values)) return this.bindFromArray(values);
if (values != null && typeof values === "object") return this.bindFromObject(values);
return true;
};

/** Execute the statement, fetching the the next line of result,
Expand Down
8 changes: 8 additions & 0 deletions test/test_statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ exports.test = function(sql, assert){
result = stmt.get({':start':1, ':end':1});
assert.deepEqual(result, ['a',1], "Binding named parameters");

// Prepare statement, pass null to bind() and check that it works
stmt = db.prepare("SELECT 'bind-with-null'");
result = stmt.bind(null);
assert.equal(result, true);
stmt.step();
result = stmt.get();
assert.equal(result,"bind-with-null")

// Close the database and all associated statements
db.close();
};
Expand Down