Permalink
Browse files

Reimplement bindings as a thin wrapper

  • Loading branch information...
1 parent a3401c8 commit 779ee011a1ba9d4ca4c1a4d79b24fbc117ced3e7 @grumdrig committed with Eric Fredricksen Dec 12, 2009
Showing with 240 additions and 134 deletions.
  1. +45 −0 sqlite.js
  2. +195 −134 sqlite3_bindings.cc
View
@@ -33,6 +33,51 @@ exports.openDatabaseSync = function (name, version, displayName,
}
+DatabaseSync.prototype.performQuery = function(sql, bindings,
+ callback, errback) {
+ var all = [];
+
+ // bindings param is optional
+ if (typeof bindings == 'function') {
+ errback = callback;
+ callback = bindings;
+ bindings = null;
+ }
+
+ var stmt = this.prepare(sql);
+ while(stmt) {
+ if (bindings) {
+ if (Object.prototype.toString.call(bindings) === "[object Array]") {
+ for (var i = 0; i < stmt.bindParameterCount(); ++i)
+ stmt.bind(i+1, bindings.shift());
+ } else {
+ for (var key in bindings)
+ if (bindings.hasOwnProperty(key))
+ stmt.bind(key, bindings[key]);
+ }
+ }
+
+ var rows = [];
+
+ while (true) {
+ var row = stmt.step();
+ if (!row) break;
+ rows.push(row);
+ }
+
+ rows.rowsAffected = this.changes();
+ rows.insertId = this.lastInsertRowid();
+
+ all.push(rows);
+
+ stmt.finalize();
+ stmt = this.prepare(stmt.tail);
+ }
+
+ return all;
+}
+
+
DatabaseSync.prototype.query = function (sql, bindings, callback) {
// TODO: error callback
if (typeof(bindings) == "function") {
Oops, something went wrong.

0 comments on commit 779ee01

Please sign in to comment.