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,21 +54,14 @@ Database.prototype.query = function (sql, bindings, queryCallback) { ...@@ -54,21 +54,14 @@ 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;
queryCallback = tmp;
}
// 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) { var innerFunction = function (statement, bindings, bindIndex) {
if (!bindings.length) { if (!bindings.length) {
callback(statement); nextCallback(db, statement, rowCallback);
return; return;
} }
...@@ -80,56 +73,64 @@ Database.prototype.executeQuery = function(sql, bindings, queryCallback) { ...@@ -80,56 +73,64 @@ Database.prototype.executeQuery = function(sql, bindings, queryCallback) {
}); });
}; };
innerFunction(statement, bindings, 1); innerFunction(statement, bindings, 1);
} }
function queryDone(statement) { function _queryDone(db, statement) {
if (statement.tail) { if (statement.tail) {
statement.finalize(function () { statement.finalize(function () {
self.db.prepare(statement.tail, onPrepare); db.driver.prepare(statement.tail, onPrepare);
}); });
return; return;
} }
statement.finalize(function () { statement.finalize(function () {
self.currentQuery = undefined; db.currentQuery = undefined;
// if there are any queries queued, let them know it's safe to go // if there are any queries queued, let them know it's safe to go
self.db.emit("ready"); db.driver.emit("ready");
}); });
} }
function doStep(statement) { function _doStep(db, statement, rowCallback) {
(function innerFunction() { (function innerFunction() {
statement.step(function (error, row) { statement.step(function (error, row) {
if (error) throw error; if (error) throw error;
if (!row) { if (!row) {
// rows.rowsAffected = this.changes(); // rows.rowsAffected = this.changes();
// rows.insertId = this.lastInsertRowid(); // rows.insertId = this.lastInsertRowid();
queryCallback(); rowCallback();
queryDone(statement); _queryDone(db, statement);
return; return;
} }
queryCallback(row); rowCallback(row);
innerFunction(); innerFunction();
}); });
})(); })();
} }
function onPrepare(error, statement) { function _onPrepare(db, error, statement, bindings, rowCallback) {
if (error) throw error; if (error) throw error;
if (bindings) { if (Array.isArray(bindings)) {
if (Object.prototype.toString.call(bindings) === "[object Array]") { if (bindings.length) {
doBindingsByIndex(statement, bindings, doStep); _setBindingsByIndex(db, statement, bindings, _doStep, rowCallback);
} }
else { else {
// TODO index by keys _doStep(db, statement, rowCallback);
} }
} }
else { else if (typeof(bindings) !== 'undefined') {
doStep(statement); // 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