Commit 5f45466b by Orlando Vazquez

add simple queueing to queries

parent b72f6bcd
...@@ -19,23 +19,55 @@ var sys = require("sys"); ...@@ -19,23 +19,55 @@ var sys = require("sys");
var puts = sys.puts; var puts = sys.puts;
var sqlite = require("./sqlite3_bindings"); var sqlite = require("./sqlite3_bindings");
var Database = exports.Database = sqlite.Database; var Database = exports.Database = function () {
var self = this;
this.queue = [];
this.db = new sqlite.Database();
};
Database.prototype.dispatch = function () {
puts('dispatching');
if (!this.queue || this.currentQuery
|| !this.queue.length) {
puts("no queries\n" + inspect([this.queue, this.currentQuery]));
return;
}
this.currentQuery = this.queue.shift();
puts("current query\n" + inspect(this.currentQuery));
this.executeQuery.apply(this, this.currentQuery);
}
Database.prototype.open = function () {
this.db.open.apply(this.db, arguments);
}
Database.prototype.close = function () {
this.db.close.apply(this.db, arguments);
}
Database.prototype.prepare = function () {
this.db.prepare.apply(this.db, arguments);
}
Database.prototype.query = function (sql, bindings, queryCallback) { Database.prototype.query = function (sql, bindings, queryCallback) {
this.queue = this.queue || [];
this.queue.push([sql, bindings, queryCallback]);
this.dispatch();
}
Database.prototype.executeQuery = function(sql, bindings, queryCallback) {
var self = this; var self = this;
if (typeof(bindings) == "function") { if (typeof(bindings) == "function") {
var tmp = bindings; var tmp = bindings;
bindings = callback; bindings = queryCallback;
callback = tmp; queryCallback = tmp;
} }
var all = [];
// Iterate over the list of bindings. Since we can't use something as // Iterate over the list of bindings. Since we can't use something as
// simple as a for or while loop, we'll use the event loop // simple as a for or while loop, we'll just chain them via the event loop
function doBindingsByIndex(statement, bindings, queryCallback, startIndex) { function doBindingsByIndex(statement, bindings, queryCallback) {
(function (statement, bindings, startIndex) { (function (statement, bindings, bindIndex) {
var innerFunction = arguments.callee; var innerFunction = arguments.callee;
if (!bindings.length) { if (!bindings.length) {
process.nextTick(function () { process.nextTick(function () {
...@@ -44,23 +76,28 @@ Database.prototype.query = function (sql, bindings, queryCallback) { ...@@ -44,23 +76,28 @@ Database.prototype.query = function (sql, bindings, queryCallback) {
return; return;
} }
startIndex = startIndex || 1; bindIndex = bindIndex || 1;
var value = bindings.shift(); var value = bindings.shift();
puts("setting index " + startIndex + " to " + value); puts("setting index " + bindIndex + " to " + value);
process.nextTick(function () { process.nextTick(function () {
statement.bind(startIndex, value, function () { statement.bind(bindIndex, value, function () {
innerFunction(statement, bindings, startIndex+1); innerFunction(statement, bindings, bindIndex+1);
}); });
}); });
})(statement, bindings, startIndex); })(statement, bindings, 1);
} }
function queryDone(statement, rows) { function queryDone(statement, rows) {
if (statement.tail) { if (statement.tail) {
puts("omg it has a tail"); puts("omg it has a tail");
self.prepare(statement.tail, onPrepare); statement.finalize(function () {
self.db.prepare(statement.tail, onPrepare);
});
} }
// if there are any queries queued, let them know it's safe to go
self.db.emit("ready");
queryCallback(undefined, rows); queryCallback(undefined, rows);
} }
...@@ -97,7 +134,8 @@ Database.prototype.query = function (sql, bindings, queryCallback) { ...@@ -97,7 +134,8 @@ Database.prototype.query = function (sql, bindings, queryCallback) {
} }
} }
this.prepare(sql, onPrepare); puts("preparing");
this.db.prepare(sql, onPrepare);
} }
function SQLTransactionSync(db, txCallback, errCallback, successCallback) { function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
......
...@@ -145,6 +145,7 @@ protected: ...@@ -145,6 +145,7 @@ protected:
FatalException(try_catch); FatalException(try_catch);
} }
open_req->dbo->Emit(String::New("ready"), 0, NULL);
open_req->cb.Dispose(); open_req->cb.Dispose();
free(open_req); free(open_req);
......
...@@ -8,8 +8,14 @@ var db = new sqlite.Database(); ...@@ -8,8 +8,14 @@ var db = new sqlite.Database();
db.open("mydatabase.db", function () { db.open("mydatabase.db", function () {
puts("opened the db"); puts("opened the db");
db.query("SELECT * FROM foo WHERE baz < ? OR baz > ?; SELECT 1;", [6, 3], function (error, result) { db.query("SELECT * FROM foo WHERE baz < ? AND baz > ?", [6, 3], function (error, result) {
puts(inspect(arguments)); ok(!error);
puts("query callback " + inspect(result)); puts("query callback " + inspect(result));
equal(result.length, 2);
});
db.query("SELECT 1", function (error, result) {
ok(!error);
puts("query callback " + inspect(result));
equal(result.length, 1);
}); });
}); });
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