Skip to content

Turn javascript arrays into SQL lists #411

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion lib/sqlite.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, er
};

SQLitePluginTransaction.prototype.addStatement = function(sql, values, success, error) {
var j, len1, params, sqlStatement, t, v;
var j, len1, len2, params, sqlStatement, t, v, i, s;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len2 is not necessary if a simpler loop is used

sqlStatement = typeof sql === 'string' ? sql : sql.toString();
params = [];
if (!!values && values.constructor === Array) {
Expand All @@ -501,6 +501,16 @@ SQLitePluginTransaction.prototype.addStatement = function(sql, values, success,
} else if (t === 'boolean') {
//Convert true -> 1 / false -> 0
params.push(~~v);
} else if (Array.isArray(v)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps you can used values.constructor === Array for consistency since it is already used on line 495 ?

s = '(';
for (i = 0, len2 = v.length; len2 > 1 && i < len2 - 1; i++) {
Copy link
Owner

@andpor andpor Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is overly complicated for a simple thing it is trying to do. I would suggest to use the following standard loop format

for (i = 0; i < v.length; i++) {
    s += v[i].toString();
    s += (i < v.length - 1) ? "," : "";
}

s = s + v[i].toString() + ',';
}
if (len2 > 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no longer necessary if the suggested loop is used.

s = s + v[len2 - 1];
}
s = s + ')';
params.push(s);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so a couple of things.

  1. if the array is empty, we will generate "()" - is this an expected behavior ?
  2. Doing toString() inside the conversion loop essentially coerces all values into strings - is this what you had in mind? I understand that the values in the array should be of the same type, but what if they are numbers or boolean ? Should we handle these cases as well ?

}
else if (t !== 'function') {
params.push(v.toString());
Expand Down