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

Add support for Node 0.8 domains #81

Closed
wants to merge 3 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
28 changes: 25 additions & 3 deletions lib/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -55,20 +57,28 @@ inherits(Statement, EventEmitter);
// Database#prepare(sql, [bind1, bind2, ...], [callback])
Database.prototype.prepare = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement;

if (!params.length || (params.length === 1 && typeof params[0] === 'function')) {
return new Statement(this, sql, params[0]);
statement = new Statement(this, sql, params[0]);
}
else {
var statement = new Statement(this, sql, errorCallback(params));
return statement.bind.apply(statement, params);
statement = new Statement(this, sql, errorCallback(params));
statement = statement.bind.apply(statement, params);
}

statement.domain = process.domain;

return statement
};

// Database#run(sql, [bind1, bind2, ...], [callback])
Database.prototype.run = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));

statement.domain = process.domain;

statement.run.apply(statement, params).finalize();
return this;
};
Expand All @@ -77,6 +87,9 @@ Database.prototype.run = function(sql) {
Database.prototype.get = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));

statement.domain = process.domain;

statement.get.apply(statement, params).finalize();
return this;
};
Expand All @@ -85,6 +98,9 @@ Database.prototype.get = function(sql) {
Database.prototype.all = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));

statement.domain = process.domain;

statement.all.apply(statement, params).finalize();
return this;
};
Expand All @@ -93,13 +109,19 @@ Database.prototype.all = function(sql) {
Database.prototype.each = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));

statement.domain = process.domain;

statement.each.apply(statement, params).finalize();
return this;
};

Database.prototype.map = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));

statement.domain = process.domain;

statement.map.apply(statement, params).finalize();
return 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
107 changes: 107 additions & 0 deletions test/domain.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var sqlite3 = require('sqlite3');
var assert = require('assert');
var shouldSkip = false;
var domain;

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


if(shouldSkip) {
exports['skipping domain tests'] = function(beforeExit) {
beforeExit(function() {

});
};
return;
}

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

var protect = function(fn) {
return function(beforeExit) {
var oldListeners = process.listeners('uncaughtException').splice(0);

oldListeners.filter(function(fn) {
// XXX: This is a bit of a hack:
// we know the name of the function that
// the domain module adds as a listener
// for `uncaughtException`, and we want
// that to remain available -- but we
// don't want expresso's uncaughtHandler
// to be fired as well.
return fn.name === 'uncaughtHandler'
}).forEach(function(fn) {
process.on('uncaughtException', fn)
})

var realBeforeExit;
var injectBeforeExit = function(fn) {
realBeforeExit = fn;
};

beforeExit(function() {
var listeners = process.listeners('uncaughtException');

// put expresso's uncaughtException handler
// back in place.
listeners.splice.apply(
listeners, [0, listeners.length].concat(oldListeners)
);

// call the real `beforeExit` function defined
// by the test, if any.
if (realBeforeExit) {
realBeforeExit();
}
})

fn(injectBeforeExit);
};
}

var testStatementMethod = function(method) {
return function(beforeExit) {
var dom = domain.create();
var db = new sqlite3.Database(':memory:');
var caughtCount = 0;
var expected = 1 + ~~(Math.random() * 10);

dom.on('error', function(err) {
++caughtCount;
});

dom.run(function() {
db[method]('garbled nonsense', function(err) {
// set a timeout, so that we ensure that the
// domain chain gets passed along.

for (var i = 0; i < expected; ++i) {
setTimeout(function() {
if (err) {
throw err;
}
}, 0);
}
});
});

beforeExit(function() {
assert.equal(caughtCount, expected);
});
};
}

exports['test Database#run works with domains'] = protect(testStatementMethod('run'));
exports['test Database#prepare works with domains'] = protect(testStatementMethod('prepare'));
exports['test Database#get works with domains'] = protect(testStatementMethod('get'));
exports['test Database#map works with domains'] = protect(testStatementMethod('all'));
exports['test Database#each works with domains'] = protect(testStatementMethod('each'));
exports['test Database#map works with domains'] = protect(testStatementMethod('map'));