Commit a26b79dc by Konstantin Käfer

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

parent 09b55954
build/ 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'); ...@@ -3,48 +3,55 @@ var sqlite3 = module.exports = exports = require('./sqlite3_bindings');
var Database = sqlite3.Database; var Database = sqlite3.Database;
var Statement = sqlite3.Statement; 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#prepare(sql, [bind1, bind2, ...], [callback])
Database.prototype.prepare = function(sql) { 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')) { if (!params.length || (params.length === 1 && typeof params[0] === 'function')) {
return new Statement(this, sql, params[0]); return new Statement(this, sql, params[0]);
} }
else { else {
var statement = new Statement(this, sql, function(err) { var statement = new Statement(this, sql, errorCallback(params));
if (err && typeof params[params.length - 1] === 'function') {
params[params.length - 1](err);
}
});
return statement.bind.apply(statement, params); return statement.bind.apply(statement, params);
} }
}; };
// Database#run(sql, [bind1, bind2, ...], [callback]) // Database#run(sql, [bind1, bind2, ...], [callback])
Database.prototype.run = function(sql) { Database.prototype.run = function(sql) {
var statement = new Statement(this, sql); var params = Array.prototype.slice.call(arguments, 1);
statement.run.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize(); var statement = new Statement(this, sql, errorCallback(params));
statement.run.apply(statement, params).finalize();
return this; return this;
} }
// Database#get(sql, [bind1, bind2, ...], [callback]) // Database#get(sql, [bind1, bind2, ...], [callback])
Database.prototype.get = function(sql) { Database.prototype.get = function(sql) {
var statement = new Statement(this, sql); var params = Array.prototype.slice.call(arguments, 1);
statement.get.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize(); var statement = new Statement(this, sql, errorCallback(params));
statement.get.apply(statement, params).finalize();
return this; return this;
} }
// Database#all(sql, [bind1, bind2, ...], [callback]) // Database#all(sql, [bind1, bind2, ...], [callback])
Database.prototype.all = function(sql) { Database.prototype.all = function(sql) {
var statement = new Statement(this, sql); var params = Array.prototype.slice.call(arguments, 1);
statement.all.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize(); var statement = new Statement(this, sql, errorCallback(params));
statement.all.apply(statement, params).finalize();
return this; return this;
} }
// Database#each(sql, [bind1, bind2, ...], [callback]) // Database#each(sql, [bind1, bind2, ...], [callback], [complete])
Database.prototype.each = function(sql) { Database.prototype.each = function(sql) {
var statement = new Statement(this, sql); var params = Array.prototype.slice.call(arguments, 1);
statement.each.apply(statement, Array.prototype.slice.call(arguments, 1)).finalize(); var statement = new Statement(this, sql, errorCallback(params));
statement.each.apply(statement, params).finalize();
return this; return this;
} }
......
...@@ -687,8 +687,11 @@ void Statement::AsyncEach(EV_P_ ev_async *w, int revents) { ...@@ -687,8 +687,11 @@ void Statement::AsyncEach(EV_P_ ev_async *w, int revents) {
if (async->completed) { if (async->completed) {
if (!async->completed_callback.IsEmpty() && if (!async->completed_callback.IsEmpty() &&
async->completed_callback->IsFunction()) { async->completed_callback->IsFunction()) {
Local<Value> argv[] = { Integer::New(async->retrieved) }; Local<Value> argv[] = {
TRY_CATCH_CALL(async->stmt->handle_, async->completed_callback, 1, argv); Local<Value>::New(Null()),
Integer::New(async->retrieved)
};
TRY_CATCH_CALL(async->stmt->handle_, async->completed_callback, 2, argv);
} }
delete async; delete async;
w->data = NULL; 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) { ...@@ -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) { db.each('SELECT id, txt FROM foo LIMIT 0, ?', total, function(err, row) {
if (err) throw err; if (err) throw err;
retrieved++; retrieved++;
}, function(num) { }, function(err, num) {
assert.equal(retrieved, num); assert.equal(retrieved, num);
completed = true; 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