Commit a26b79dc by Konstantin Käfer

make sure that the callback is called when preparing for Database# functions

parent 09b55954
build/
TODO
wiki
lib/sqlite3_bindings.node
\ No newline at end of file
......@@ -3,48 +3,55 @@ var sqlite3 = module.exports = exports = require('./sqlite3_bindings');
var Database = sqlite3.Database;
var Statement = sqlite3.Statement;
function errorCallback(args) {
if (typeof args[args.length - 1] === 'function') {
var callback = args[args.length - 1];
return function(err) { if (err) callback(err); };
}
}
// Database#prepare(sql, [bind1, bind2, ...], [callback])
Database.prototype.prepare = function(sql) {
var callback, params = Array.prototype.slice.call(arguments, 1);
var params = Array.prototype.slice.call(arguments, 1);
if (!params.length || (params.length === 1 && typeof params[0] === 'function')) {
return new Statement(this, sql, params[0]);
}
else {
var statement = new Statement(this, sql, function(err) {
if (err && typeof params[params.length - 1] === 'function') {
params[params.length - 1](err);
}
});
var statement = new Statement(this, sql, errorCallback(params));
return statement.bind.apply(statement, params);
}
};
// Database#run(sql, [bind1, bind2, ...], [callback])
Database.prototype.run = function(sql) {
var statement = new Statement(this, sql);
statement.run.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize();
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));
statement.run.apply(statement, params).finalize();
return this;
}
// Database#get(sql, [bind1, bind2, ...], [callback])
Database.prototype.get = function(sql) {
var statement = new Statement(this, sql);
statement.get.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize();
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));
statement.get.apply(statement, params).finalize();
return this;
}
// Database#all(sql, [bind1, bind2, ...], [callback])
Database.prototype.all = function(sql) {
var statement = new Statement(this, sql);
statement.all.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize();
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));
statement.all.apply(statement, params).finalize();
return this;
}
// Database#each(sql, [bind1, bind2, ...], [callback])
// Database#each(sql, [bind1, bind2, ...], [callback], [complete])
Database.prototype.each = function(sql) {
var statement = new Statement(this, sql);
statement.each.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize();
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));
statement.each.apply(statement, params).finalize();
return this;
}
......
......@@ -687,8 +687,11 @@ void Statement::AsyncEach(EV_P_ ev_async *w, int revents) {
if (async->completed) {
if (!async->completed_callback.IsEmpty() &&
async->completed_callback->IsFunction()) {
Local<Value> argv[] = { Integer::New(async->retrieved) };
TRY_CATCH_CALL(async->stmt->handle_, async->completed_callback, 1, argv);
Local<Value> argv[] = {
Local<Value>::New(Null()),
Integer::New(async->retrieved)
};
TRY_CATCH_CALL(async->stmt->handle_, async->completed_callback, 2, argv);
}
delete async;
w->data = NULL;
......
var sqlite3 = require('sqlite3');
var assert = require('assert');
exports['test Database#get prepare fail'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.get('SELECT id, txt FROM foo', function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#all prepare fail'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.all('SELECT id, txt FROM foo', function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#run prepare fail'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.run('SELECT id, txt FROM foo', function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#each prepare fail'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.each('SELECT id, txt FROM foo', function(err, row) {
assert.ok(false, "this should not be called");
}, function(err, num) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#each prepare fail without completion handler'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.each('SELECT id, txt FROM foo', function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
else {
assert.ok(false, 'this should not be called')
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#get prepare fail with param binding'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.get('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#all prepare fail with param binding'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.all('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#run prepare fail with param binding'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.run('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#each prepare fail with param binding'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.each('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
assert.ok(false, "this should not be called");
}, function(err, num) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
});
beforeExit(function() {
assert.ok(completed);
});
};
exports['test Database#each prepare fail with param binding without completion handler'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var completed = false;
db.each('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
if (err) {
assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
assert.equal(err.errno, sqlite3.ERROR);
assert.equal(err.code, 'SQLITE_ERROR');
completed = true;
}
else {
assert.ok(false, 'this should not be called')
}
});
beforeExit(function() {
assert.ok(completed);
});
};
......@@ -30,7 +30,7 @@ exports['test Statement#each with complete callback'] = function(beforeExit) {
db.each('SELECT id, txt FROM foo LIMIT 0, ?', total, function(err, row) {
if (err) throw err;
retrieved++;
}, function(num) {
}, function(err, num) {
assert.equal(retrieved, num);
completed = true;
});
......
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