Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions of Database and Statement should be domain-aware #258

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function inherits(target, source) {

sqlite3.cached = {
Database: function(file, a, b) {
EventEmitter.call(this);

if (file === '' || file === ':memory:') {
// Don't cache special databases.
return new Database(file, a, b);
Expand Down Expand Up @@ -57,6 +59,28 @@ var Statement = sqlite3.Statement;
inherits(Database, EventEmitter);
inherits(Statement, EventEmitter);

// Managed counterpart of Database::new
Database.prototype._init = function() {
EventEmitter.call(this);
};

// Managed counterpart of Statement::new
Statement.prototype._init = function() {
EventEmitter.call(this);
};

var nativeExec = Database.prototype.exec;
// Database#exec(sql, [callback])
Database.prototype.exec = function(sql) {
var params = arguments;
if (process.domain && params.length === 2 && typeof params[1] === 'function') {
params = Array.prototype.slice.call(params,0);
// Make sure the callback is called in the current domain.
params[1] = process.domain.bind(params[1]);
}
return nativeExec.apply(this,params);
};

// Database#prepare(sql, [bind1, bind2, ...], [callback])
Database.prototype.prepare = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
Expand Down
3 changes: 3 additions & 0 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ Handle<Value> Database::New(const Arguments& args) {
OpenBaton* baton = new OpenBaton(db, callback, *filename, mode);
Work_BeginOpen(baton);

Handle<Value> argv[0];
MakeCallback(args.This(),"_init",0,argv);

return args.This();
}

Expand Down
7 changes: 7 additions & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef NODE_SQLITE3_SRC_MACROS_H
#define NODE_SQLITE3_SRC_MACROS_H
#include <node_version.h>

const char* sqlite_code_string(int code);
const char* sqlite_authorizer_string(int type);
Expand Down Expand Up @@ -114,12 +115,18 @@ const char* sqlite_authorizer_string(int type);
argc, argv \
);

#if NODE_VERSION_AT_LEAST(0,8,0)
#define TRY_CATCH_CALL(context, callback, argc, argv) \
{ MakeCallback(context, callback, argc, argv); }
#else
#define TRY_CATCH_CALL(context, callback, argc, argv) \
{ TryCatch try_catch; \
(callback)->Call((context), (argc), (argv)); \
if (try_catch.HasCaught()) { \
FatalException(try_catch); \
} }
#endif


#define WORK_DEFINITION(name) \
static Handle<Value> name(const Arguments& args); \
Expand Down
3 changes: 3 additions & 0 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Handle<Value> Statement::New(const Arguments& args) {
baton->sql = std::string(*String::Utf8Value(sql));
db->Schedule(Work_BeginPrepare, baton);

Handle<Value> argv[0];
MakeCallback(args.This(),"_init",0,argv);

return args.This();
}

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

try {
domain = require('domain');
} catch (e) {
return;
}

function testThrowException(dom,done) {
var expectedException = 'THIS IS AN EXPECTED EXCEPTION';
var oldListeners;
function handleExceptionListeners() {
// Detatch existing exception listeners (of Mocha)
oldListeners = process.listeners('uncaughtException').slice(0);
oldListeners.forEach(function(fn) {
process.removeListener('uncaughtException',fn);
});
// Attach exception listeners of this test.
process.on('uncaughtException',processExceptionCallback);
dom.on('error',domainExceptionCallback);
}
function unhandleExceptionListeners() {
// Detach exception listeners of this test.
process.removeListener('uncaughtException',processExceptionCallback);
dom.removeListener('error',domainExceptionCallback);
// Reattach the existing exception listeners (of Mocha)
oldListeners.forEach(function(fn) {
process.on('uncaughtException',fn);
});
}
function processExceptionCallback(e) {
unhandleExceptionListeners();
done('Exception was not caught by domain:',e);
}
function domainExceptionCallback(e) {
unhandleExceptionListeners();
assert.equal(expectedException,e);

done();
}

handleExceptionListeners();

throw expectedException;
}

describe('domain',function() {
describe('on database creation',function() {
var dom;
before(function() {
dom = domain.create();
});

it('should work for open',function(done) {
dom.run(function() {
var db = new sqlite3.Database(':memory:',function() {
assert.equal(process.domain, dom);
testThrowException(dom,done);
});
});
});
});

describe('on individual calls',function() {
var db,dom;
beforeEach(function(done) {
dom = domain.create();
db = new sqlite3.Database(':memory:',function() {
done();
});
});

function testFn(functionName) {
it('should work for Database#'+functionName,function(done) {
dom.run(function() {
db[functionName]('select 0',function() {
assert.equal(process.domain,dom);
testThrowException(dom,done);
});
});
});
}

testFn('run');
testFn('exec');
testFn('prepare');
testFn('get');
testFn('map');
testFn('each');

afterEach(function(done) {
dom.dispose();
done();
});
});
});

8 changes: 4 additions & 4 deletions test/profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ describe('profiling', function() {
assert.ok(!create);
db.run("CREATE TABLE foo (id int)", function(err) {
if (err) throw err;
process.nextTick(function() {
setTimeout(function() {
assert.ok(create);
done();
});
},10);
});
});

Expand All @@ -43,10 +43,10 @@ describe('profiling', function() {
assert.ok(!select);
db.run("SELECT * FROM foo", function(err) {
if (err) throw err;
process.nextTick(function() {
setTimeout(function() {
assert.ok(select);
done();
});
},10);
});
});

Expand Down