Skip to content

Commit

Permalink
fix issue #1: when running a statement multiple times, continue even …
Browse files Browse the repository at this point in the history
…after one query might not have returned any results
  • Loading branch information
kkaefer committed Feb 25, 2011
1 parent fd65a63 commit b51e977
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void Statement::EIO_BeginGet(Baton* baton) {
int Statement::EIO_Get(eio_req *req) {
STATEMENT_INIT(RowBaton);

if (stmt->status != SQLITE_DONE) {
if (stmt->status != SQLITE_DONE || baton->parameters.size()) {
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex_enter(mtx);

Expand Down
43 changes: 43 additions & 0 deletions test/rerun.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var sqlite3 = require('sqlite3');
var assert = require('assert');

if (process.setMaxListeners) process.setMaxListeners(0);

exports['test running the same statement multiple times'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');

var count = 10;
var inserted = 0;
var retrieved = 0;

db.serialize(function() {
db.run("CREATE TABLE foo (id int)");

var stmt = db.prepare("INSERT INTO foo VALUES(?)");
for (var i = 5; i < count; i++) {
stmt.run(i, function(err) {
if (err) throw err;
inserted++;
});
}
stmt.finalize();

var collected = [];
var stmt = db.prepare("SELECT id FROM foo WHERE id = ?");
for (var i = 0; i < count; i++) {
stmt.get(i, function(err, row) {
if (err) throw err;
if (row) collected.push(row);
});
}
stmt.finalize(function() {
retrieved += collected.length;
assert.deepEqual(collected, [ { id: 5 }, { id: 6 }, { id: 7 }, { id: 8 }, { id: 9 } ]);
});
});

beforeExit(function() {
assert.equal(inserted, 5);
assert.equal(retrieved, 5);
});
};

0 comments on commit b51e977

Please sign in to comment.