Commit a9cc58e0 by Orlando Vazquez

add a check to ensure we don't step after a prepare if there are placeholders/bindings

parent 11883bc0
...@@ -17,38 +17,38 @@ function readTest(db, callback) { ...@@ -17,38 +17,38 @@ function readTest(db, callback) {
if (callback) callback(db); if (callback) callback(db);
} }
else { else {
// puts("got a row" + inspect(arguments));
rows++; rows++;
} }
}); });
} }
function writeTest(db, callback) { function writeTest(db, i, callback) {
var t0 = new Date; db.query("INSERT INTO t1 VALUES (1)", function (row) {
var count = i = 100000; if (!i--) {
// end of results
function innerFunction () { var dt = ((new Date)-t0)/1000;
db.query("INSERT INTO t1 VALUES (1)", function (row) { puts("**** " + count + " insertions in " + dt + "s (" + (count/dt) + "/s)");
if (!i--) {
// end of results if (callback) callback(db);
var dt = ((new Date)-t0)/1000; }
puts("**** " + count + " insertions in " + dt + "s (" + (count/dt) + "/s)"); else {
writeTest(db, i--, callback);
if (callback) callback(db); }
} });
else {
innerFunction();
}
});
}
innerFunction();
} }
var count = 100000;
var t0;
db.open(":memory:", function () { db.open(":memory:", function () {
puts(inspect(arguments)); puts(inspect(arguments));
puts("open cb"); puts("open cb");
db.query("CREATE TABLE t1 (alpha INTEGER)", function () { db.query("CREATE TABLE t1 (alpha INTEGER)", function () {
puts("create table callback" + inspect(arguments)); puts("create table callback" + inspect(arguments));
writeTest(db, readTest); // writeTest(db, readTest);
t0 = new Date;
writeTest(db, count, readTest);
}); });
}); });
...@@ -57,20 +57,19 @@ Database.prototype.query = function (sql, bindings, queryCallback) { ...@@ -57,20 +57,19 @@ Database.prototype.query = function (sql, bindings, queryCallback) {
// 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 just chain them via the event loop // simple as a for or while loop, we'll just chain them via the event loop
function _setBindingsByIndex(db, function _setBindingsByIndex(db,
statement, bindings, nextCallback, rowCallback, bindIndex) { statement, bindings, nextCallback, rowCallback, bindIndex) {
puts("setting bindings"); if (!bindings.length) {
if (!bindings.length) { nextCallback(db, statement, rowCallback);
nextCallback(db, statement, rowCallback); return;
return; }
}
bindIndex = bindIndex || 1; bindIndex = bindIndex || 1;
var value = bindings.shift(); var value = bindings.shift();
statement.bind(bindIndex, value, function () { statement.bind(bindIndex, value, function () {
_setBindingsByIndex(statement, bindings, nextCallback, rowCallback, bindIndex+1); _setBindingsByIndex(db, statement, bindings, nextCallback, rowCallback, bindIndex+1);
}); });
} }
function _queryDone(db, statement) { function _queryDone(db, statement) {
......
...@@ -329,10 +329,8 @@ protected: ...@@ -329,10 +329,8 @@ protected:
Local<Value> argv[2]; Local<Value> argv[2];
int argc = 0; int argc = 0;
bool err = false;
if (req->result != SQLITE_OK) { if (req->result != SQLITE_OK) {
err = true;
argv[0] = Exception::Error(String::New("Error preparing statement")); argv[0] = Exception::Error(String::New("Error preparing statement"));
argc = 1; argc = 1;
} }
...@@ -383,15 +381,22 @@ protected: ...@@ -383,15 +381,22 @@ protected:
req->result = rc; req->result = rc;
req->int1 = -1; req->int1 = -1;
if (rc == SQLITE_OK) { // This might be a INSERT statement. Let's try to get the first row.
// This might be a INSERT statement. Let's try to get the first row. // This is to optimize out further calls to the thread pool. This is only
// This is to optimize out further calls to the thread pool. // possible in the case where there are no variable placeholders/bindings
// in the SQL.
if (rc == SQLITE_OK && !sqlite3_bind_parameter_count(prep_req->stmt)) {
// if (rc == SQLITE_OK) {
rc = sqlite3_step(prep_req->stmt); rc = sqlite3_step(prep_req->stmt);
req->int1 = rc; req->int1 = rc;
if (rc == SQLITE_DONE) { if (rc == SQLITE_DONE) {
rc = sqlite3_finalize(prep_req->stmt); rc = sqlite3_finalize(prep_req->stmt);
prep_req->stmt = NULL;
assert(rc == SQLITE_OK); assert(rc == SQLITE_OK);
} }
else {
rc = sqlite3_reset(prep_req->stmt);
}
} }
return 0; return 0;
...@@ -850,7 +855,7 @@ protected: ...@@ -850,7 +855,7 @@ protected:
sqlite3_stmt *stmt = *sto; sqlite3_stmt *stmt = *sto;
int rc; int rc;
if (step_req->first_rc < 0) { if (step_req->first_rc != -1) {
rc = req->result = step_req->first_rc; rc = req->result = step_req->first_rc;
} else { } else {
rc = req->result = sqlite3_step(stmt); rc = req->result = sqlite3_step(stmt);
...@@ -903,7 +908,8 @@ protected: ...@@ -903,7 +908,8 @@ protected:
} }
} }
} }
else if (SQLITE_DONE) { else if (rc == SQLITE_DONE) {
// printf("no more results");
// no more results // no more results
} }
else { else {
......
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