Commit 317b3811 by Orlando Vazquez

move some nested functions out of performance hot spots

parent 989f5a6b
...@@ -21,8 +21,8 @@ var sqlite = require("./sqlite3_bindings"); ...@@ -21,8 +21,8 @@ var sqlite = require("./sqlite3_bindings");
var Database = exports.Database = function () { var Database = exports.Database = function () {
var self = this; var self = this;
this.queue = []; this.queue = [];
this.db = new sqlite.Database(); this.driver = new sqlite.Database();
this.db.addListener("ready", function () { this.driver.addListener("ready", function () {
self.dispatch(); self.dispatch();
}); });
}; };
...@@ -37,15 +37,15 @@ Database.prototype.dispatch = function () { ...@@ -37,15 +37,15 @@ Database.prototype.dispatch = function () {
} }
Database.prototype.open = function () { Database.prototype.open = function () {
this.db.open.apply(this.db, arguments); this.driver.open.apply(this.driver, arguments);
} }
Database.prototype.close = function () { Database.prototype.close = function () {
this.db.close.apply(this.db, arguments); this.driver.close.apply(this.driver, arguments);
} }
Database.prototype.prepare = function () { Database.prototype.prepare = function () {
this.db.prepare.apply(this.db, arguments); this.driver.prepare.apply(this.driver, arguments);
} }
Database.prototype.query = function (sql, bindings, queryCallback) { Database.prototype.query = function (sql, bindings, queryCallback) {
...@@ -54,82 +54,83 @@ Database.prototype.query = function (sql, bindings, queryCallback) { ...@@ -54,82 +54,83 @@ Database.prototype.query = function (sql, bindings, queryCallback) {
this.dispatch(); this.dispatch();
} }
Database.prototype.executeQuery = function(sql, bindings, queryCallback) { // Iterate over the list of bindings. Since we can't use something as
var self = this; // simple as a for or while loop, we'll just chain them via the event loop
function _setBindingsByIndex(db,
if (typeof(bindings) == "function") { statement, bindings, nextCallback, rowCallback) {
var tmp = bindings; puts("setting bindings");
bindings = queryCallback; var innerFunction = function (statement, bindings, bindIndex) {
queryCallback = tmp; if (!bindings.length) {
} nextCallback(db, statement, rowCallback);
// Iterate over the list of bindings. Since we can't use something as
// simple as a for or while loop, we'll just chain them via the event loop
function doBindingsByIndex(statement, bindings, callback) {
var innerFunction = function (statement, bindings, bindIndex) {
if (!bindings.length) {
callback(statement);
return;
}
bindIndex = bindIndex || 1;
var value = bindings.shift();
statement.bind(bindIndex, value, function () {
innerFunction(statement, bindings, bindIndex+1);
});
};
innerFunction(statement, bindings, 1);
}
function queryDone(statement) {
if (statement.tail) {
statement.finalize(function () {
self.db.prepare(statement.tail, onPrepare);
});
return; return;
} }
bindIndex = bindIndex || 1;
var value = bindings.shift();
statement.bind(bindIndex, value, function () {
innerFunction(statement, bindings, bindIndex+1);
});
};
innerFunction(statement, bindings, 1);
}
function _queryDone(db, statement) {
if (statement.tail) {
statement.finalize(function () { statement.finalize(function () {
self.currentQuery = undefined; db.driver.prepare(statement.tail, onPrepare);
// if there are any queries queued, let them know it's safe to go
self.db.emit("ready");
}); });
return;
} }
function doStep(statement) { statement.finalize(function () {
(function innerFunction() { db.currentQuery = undefined;
statement.step(function (error, row) { // if there are any queries queued, let them know it's safe to go
if (error) throw error; db.driver.emit("ready");
if (!row) { });
// rows.rowsAffected = this.changes(); }
// rows.insertId = this.lastInsertRowid();
queryCallback();
queryDone(statement);
return;
}
queryCallback(row);
innerFunction();
});
})();
}
function onPrepare(error, statement) { function _doStep(db, statement, rowCallback) {
if (error) throw error; (function innerFunction() {
if (bindings) { statement.step(function (error, row) {
if (Object.prototype.toString.call(bindings) === "[object Array]") { if (error) throw error;
doBindingsByIndex(statement, bindings, doStep); if (!row) {
} // rows.rowsAffected = this.changes();
else { // rows.insertId = this.lastInsertRowid();
// TODO index by keys rowCallback();
_queryDone(db, statement);
return;
} }
rowCallback(row);
innerFunction();
});
})();
}
function _onPrepare(db, error, statement, bindings, rowCallback) {
if (error) throw error;
if (Array.isArray(bindings)) {
if (bindings.length) {
_setBindingsByIndex(db, statement, bindings, _doStep, rowCallback);
} }
else { else {
doStep(statement); _doStep(db, statement, rowCallback);
} }
} }
else if (typeof(bindings) !== 'undefined') {
// TODO index by keys
}
}
Database.prototype.executeQuery = function(sql, bindings, rowCallback) {
var self = this;
if (typeof(bindings) == "function") {
rowCallback = bindings;
bindings = [];
}
this.db.prepare(sql, onPrepare); this.driver.prepare(sql, function(error, statement) { _onPrepare(self, error, statement, bindings, rowCallback) });
} }
function SQLTransactionSync(db, txCallback, errCallback, successCallback) { function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment