Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an insertMany method

  • Loading branch information...
commit 38af2eda323626c2886cf325df993d7c53d5eac2 1 parent 4811a41
@orlandov authored
Showing with 52 additions and 0 deletions.
  1. +51 −0 sqlite.js
  2. +1 −0  src/statement.cc
View
51 sqlite.js
@@ -169,3 +169,54 @@ Database.prototype.executeScript = function (script, callback) {
});
})(script);
}
+
+Database.prototype.insertMany = function (table, columns, rows, callback) {
+ var columnsFragment = columns.join(",");
+ var placeholdersFragment = [];
+ var i = columns.length;
+
+ while (i--) {
+ placeholdersFragment.push('?');
+ }
+ placeholdersFragment = placeholdersFragment.join(", ");
+
+ var sql = [ 'INSERT INTO'
+ , table
+ , '('
+ , columnsFragment
+ , ')'
+ , 'VALUES'
+ , '('
+ , placeholdersFragment
+ , ')'
+ ]
+ .join(" ");
+
+ var i = rows.length;
+
+ var statement;
+
+ function doStep(i) {
+ statement.bindArray(rows[i], function () {
+ statement.step(function (error, row) {
+ if (error) return callback(error);
+ statement.reset();
+ if (i) {
+ doStep(--i);
+ }
+ else {
+ statement.finalize(function () {
+ callback();
+ });
+ }
+ });
+ });
+ }
+
+ this.prepare(sql, function (error, stmt) {
+ if (error) return callback(error);
+ statement = stmt;
+ doStep(--i);
+ });
+}
+
View
1  src/statement.cc
@@ -668,6 +668,7 @@ int Statement::EIO_Step(eio_req *req) {
}
else {
sto->error_ = true;
+ sto->cells = NULL;
}
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.