Commit bb15108e by King Koopa

Use NAN for future support.

parent fe69599b
......@@ -8,6 +8,7 @@
"targets": [
{
"target_name": "<(module_name)",
"include_dirs": ["<!(node -e \"require('nan')\")"],
"conditions": [
["sqlite != 'internal'", {
"libraries": [
......
......@@ -35,7 +35,11 @@
"url": "git://github.com/mapbox/node-sqlite3.git"
},
"dependencies": {
"node-pre-gyp": "~0.2.5"
"node-pre-gyp": "~0.2.5",
"nan": "~0.8.0"
},
"devDependencies": {
"set-immediate": "~0.1.1"
},
"bundledDependencies": [
"node-pre-gyp"
......@@ -46,7 +50,7 @@
"scripts": {
"install": "node-pre-gyp install --fallback-to-build",
"pretest": "node test/support/createdb.js",
"test": "mocha -R spec --timeout 200000"
"test": "mocha -R spec --timeout 200000 -s 20000"
},
"licenses": [
{
......
......@@ -10,26 +10,27 @@ using namespace node_sqlite3;
Persistent<FunctionTemplate> Database::constructor_template;
void Database::Init(Handle<Object> target) {
HandleScope scope;
NanScope();
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Database"));
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("Database"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "exec", Exec);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "wait", Wait);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "loadExtension", LoadExtension);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "serialize", Serialize);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "parallelize", Parallelize);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "configure", Configure);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
NODE_SET_PROTOTYPE_METHOD(t, "exec", Exec);
NODE_SET_PROTOTYPE_METHOD(t, "wait", Wait);
NODE_SET_PROTOTYPE_METHOD(t, "loadExtension", LoadExtension);
NODE_SET_PROTOTYPE_METHOD(t, "serialize", Serialize);
NODE_SET_PROTOTYPE_METHOD(t, "parallelize", Parallelize);
NODE_SET_PROTOTYPE_METHOD(t, "configure", Configure);
NODE_SET_GETTER(constructor_template, "open", OpenGetter);
NODE_SET_GETTER(t, "open", OpenGetter);
NanAssignPersistent(FunctionTemplate, constructor_template, t);
target->Set(String::NewSymbol("Database"),
constructor_template->GetFunction());
t->GetFunction());
}
void Database::Process() {
......@@ -41,8 +42,9 @@ void Database::Process() {
// Call all callbacks with the error object.
while (!queue.empty()) {
Call* call = queue.front();
if (!call->baton->callback.IsEmpty() && call->baton->callback->IsFunction()) {
TRY_CATCH_CALL(handle_, call->baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(call->baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
TRY_CATCH_CALL(NanObjectWrapHandle(this), cb, 1, argv);
called = true;
}
queue.pop();
......@@ -56,7 +58,7 @@ void Database::Process() {
// Database object.
if (!called) {
Local<Value> args[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(this), 2, args);
}
return;
}
......@@ -80,13 +82,14 @@ void Database::Process() {
void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
if (!open && locked) {
EXCEPTION(String::New("Database is closed"), SQLITE_MISUSE, exception);
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { exception };
TRY_CATCH_CALL(handle_, baton->callback, 1, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(this), cb, 1, argv);
}
else {
Local<Value> argv[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(handle_, 2, argv);
EMIT_EVENT(NanObjectWrapHandle(this), 2, argv);
}
return;
}
......@@ -100,13 +103,11 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
}
}
Handle<Value> Database::New(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::New) {
NanScope();
if (!args.IsConstructCall()) {
return ThrowException(Exception::TypeError(
String::New("Use the new operator to create new Database objects"))
);
return NanThrowTypeError("Use the new operator to create new Database objects");
}
REQUIRE_ARGUMENT_STRING(0, filename);
......@@ -134,7 +135,7 @@ Handle<Value> Database::New(const Arguments& args) {
OpenBaton* baton = new OpenBaton(db, callback, *filename, mode);
Work_BeginOpen(baton);
return args.This();
NanReturnValue(args.This());
}
void Database::Work_BeginOpen(Baton* baton) {
......@@ -149,24 +150,24 @@ void Database::Work_Open(uv_work_t* req) {
baton->status = sqlite3_open_v2(
baton->filename.c_str(),
&db->handle,
&db->_handle,
baton->mode,
NULL
);
if (baton->status != SQLITE_OK) {
baton->message = std::string(sqlite3_errmsg(db->handle));
sqlite3_close(db->handle);
db->handle = NULL;
baton->message = std::string(sqlite3_errmsg(db->_handle));
sqlite3_close(db->_handle);
db->_handle = NULL;
}
else {
// Set default database handle values.
sqlite3_busy_timeout(db->handle, 1000);
sqlite3_busy_timeout(db->_handle, 1000);
}
}
void Database::Work_AfterOpen(uv_work_t* req) {
HandleScope scope;
NanScope();
OpenBaton* baton = static_cast<OpenBaton*>(req->data);
Database* db = baton->db;
......@@ -177,47 +178,49 @@ void Database::Work_AfterOpen(uv_work_t* req) {
}
else {
db->open = true;
argv[0] = Local<Value>::New(Null());
argv[0] = NanNewLocal<Value>(Null());
}
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
else if (!db->open) {
Local<Value> args[] = { String::NewSymbol("error"), argv[0] };
EMIT_EVENT(db->handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(db), 2, args);
}
if (db->open) {
Local<Value> args[] = { String::NewSymbol("open") };
EMIT_EVENT(db->handle_, 1, args);
EMIT_EVENT(NanObjectWrapHandle(db), 1, args);
db->Process();
}
delete baton;
}
Handle<Value> Database::OpenGetter(Local<String> str, const AccessorInfo& accessor) {
HandleScope scope;
Database* db = ObjectWrap::Unwrap<Database>(accessor.This());
return Boolean::New(db->open);
NAN_GETTER(Database::OpenGetter) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
NanReturnValue(Boolean::New(db->open));
}
Handle<Value> Database::Close(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Close) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(db, callback);
db->Schedule(Work_BeginClose, baton, true);
return args.This();
NanReturnValue(args.This());
}
void Database::Work_BeginClose(Baton* baton) {
assert(baton->db->locked);
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
assert(baton->db->pending == 0);
baton->db->RemoveCallbacks();
......@@ -230,18 +233,18 @@ void Database::Work_Close(uv_work_t* req) {
Baton* baton = static_cast<Baton*>(req->data);
Database* db = baton->db;
baton->status = sqlite3_close(db->handle);
baton->status = sqlite3_close(db->_handle);
if (baton->status != SQLITE_OK) {
baton->message = std::string(sqlite3_errmsg(db->handle));
baton->message = std::string(sqlite3_errmsg(db->_handle));
}
else {
db->handle = NULL;
db->_handle = NULL;
}
}
void Database::Work_AfterClose(uv_work_t* req) {
HandleScope scope;
NanScope();
Baton* baton = static_cast<Baton*>(req->data);
Database* db = baton->db;
......@@ -254,29 +257,31 @@ void Database::Work_AfterClose(uv_work_t* req) {
db->open = false;
// Leave db->locked to indicate that this db object has reached
// the end of its life.
argv[0] = Local<Value>::New(Null());
argv[0] = NanNewLocal<Value>(Null());
}
Local<Function> cb = NanPersistentToLocal(baton->callback);
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
if (!cb.IsEmpty() && cb->IsFunction()) {
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
else if (db->open) {
Local<Value> args[] = { String::NewSymbol("error"), argv[0] };
EMIT_EVENT(db->handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(db), 2, args);
}
if (!db->open) {
Local<Value> args[] = { String::NewSymbol("close"), argv[0] };
EMIT_EVENT(db->handle_, 1, args);
EMIT_EVENT(NanObjectWrapHandle(db), 1, args);
db->Process();
}
delete baton;
}
Handle<Value> Database::Serialize(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Serialize) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
......@@ -290,11 +295,11 @@ Handle<Value> Database::Serialize(const Arguments& args) {
db->Process();
return args.This();
NanReturnValue(args.This());
}
Handle<Value> Database::Parallelize(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Parallelize) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
......@@ -308,11 +313,11 @@ Handle<Value> Database::Parallelize(const Arguments& args) {
db->Process();
return args.This();
NanReturnValue(args.This());
}
Handle<Value> Database::Configure(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Configure) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
REQUIRE_ARGUMENTS(2);
......@@ -329,9 +334,7 @@ Handle<Value> Database::Configure(const Arguments& args) {
}
else if (args[0]->Equals(String::NewSymbol("busyTimeout"))) {
if (!args[1]->IsInt32()) {
return ThrowException(Exception::TypeError(
String::New("Value must be an integer"))
);
return NanThrowTypeError("Value must be an integer");
}
Local<Function> handle;
Baton* baton = new Baton(db, handle);
......@@ -339,40 +342,40 @@ Handle<Value> Database::Configure(const Arguments& args) {
db->Schedule(SetBusyTimeout, baton);
}
else {
return ThrowException(Exception::Error(String::Concat(
return NanThrowError(Exception::Error(String::Concat(
args[0]->ToString(),
String::NewSymbol(" is not a valid configuration option")
String::New(" is not a valid configuration option")
)));
}
db->Process();
return args.This();
NanReturnValue(args.This());
}
void Database::SetBusyTimeout(Baton* baton) {
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
// Abuse the status field for passing the timeout.
sqlite3_busy_timeout(baton->db->handle, baton->status);
sqlite3_busy_timeout(baton->db->_handle, baton->status);
delete baton;
}
void Database::RegisterTraceCallback(Baton* baton) {
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
Database* db = baton->db;
if (db->debug_trace == NULL) {
// Add it.
db->debug_trace = new AsyncTrace(db, TraceCallback);
sqlite3_trace(db->handle, TraceCallback, db);
sqlite3_trace(db->_handle, TraceCallback, db);
}
else {
// Remove it.
sqlite3_trace(db->handle, NULL, NULL);
sqlite3_trace(db->_handle, NULL, NULL);
db->debug_trace->finish();
db->debug_trace = NULL;
}
......@@ -388,28 +391,28 @@ void Database::TraceCallback(void* db, const char* sql) {
void Database::TraceCallback(Database* db, std::string* sql) {
// Note: This function is called in the main V8 thread.
HandleScope scope;
NanScope();
Local<Value> argv[] = {
String::NewSymbol("trace"),
String::New(sql->c_str())
};
EMIT_EVENT(db->handle_, 2, argv);
EMIT_EVENT(NanObjectWrapHandle(db), 2, argv);
delete sql;
}
void Database::RegisterProfileCallback(Baton* baton) {
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
Database* db = baton->db;
if (db->debug_profile == NULL) {
// Add it.
db->debug_profile = new AsyncProfile(db, ProfileCallback);
sqlite3_profile(db->handle, ProfileCallback, db);
sqlite3_profile(db->_handle, ProfileCallback, db);
}
else {
// Remove it.
sqlite3_profile(db->handle, NULL, NULL);
sqlite3_profile(db->_handle, NULL, NULL);
db->debug_profile->finish();
db->debug_profile = NULL;
}
......@@ -427,29 +430,29 @@ void Database::ProfileCallback(void* db, const char* sql, sqlite3_uint64 nsecs)
}
void Database::ProfileCallback(Database *db, ProfileInfo* info) {
HandleScope scope;
NanScope();
Local<Value> argv[] = {
String::NewSymbol("profile"),
String::New(info->sql.c_str()),
Integer::New((double)info->nsecs / 1000000.0)
};
EMIT_EVENT(db->handle_, 3, argv);
EMIT_EVENT(NanObjectWrapHandle(db), 3, argv);
delete info;
}
void Database::RegisterUpdateCallback(Baton* baton) {
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
Database* db = baton->db;
if (db->update_event == NULL) {
// Add it.
db->update_event = new AsyncUpdate(db, UpdateCallback);
sqlite3_update_hook(db->handle, UpdateCallback, db);
sqlite3_update_hook(db->_handle, UpdateCallback, db);
}
else {
// Remove it.
sqlite3_update_hook(db->handle, NULL, NULL);
sqlite3_update_hook(db->_handle, NULL, NULL);
db->update_event->finish();
db->update_event = NULL;
}
......@@ -470,7 +473,7 @@ void Database::UpdateCallback(void* db, int type, const char* database,
}
void Database::UpdateCallback(Database *db, UpdateInfo* info) {
HandleScope scope;
NanScope();
Local<Value> argv[] = {
String::NewSymbol(sqlite_authorizer_string(info->type)),
......@@ -478,12 +481,12 @@ void Database::UpdateCallback(Database *db, UpdateInfo* info) {
String::New(info->table.c_str()),
Integer::New(info->rowid),
};
EMIT_EVENT(db->handle_, 4, argv);
EMIT_EVENT(NanObjectWrapHandle(db), 4, argv);
delete info;
}
Handle<Value> Database::Exec(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Exec) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
REQUIRE_ARGUMENT_STRING(0, sql);
......@@ -492,13 +495,13 @@ Handle<Value> Database::Exec(const Arguments& args) {
Baton* baton = new ExecBaton(db, callback, *sql);
db->Schedule(Work_BeginExec, baton, true);
return args.This();
NanReturnValue(args.This());
}
void Database::Work_BeginExec(Baton* baton) {
assert(baton->db->locked);
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(),
&baton->request, Work_Exec, (uv_after_work_cb)Work_AfterExec);
......@@ -510,7 +513,7 @@ void Database::Work_Exec(uv_work_t* req) {
char* message = NULL;
baton->status = sqlite3_exec(
baton->db->handle,
baton->db->_handle,
baton->sql.c_str(),
NULL,
NULL,
......@@ -524,26 +527,27 @@ void Database::Work_Exec(uv_work_t* req) {
}
void Database::Work_AfterExec(uv_work_t* req) {
HandleScope scope;
NanScope();
ExecBaton* baton = static_cast<ExecBaton*>(req->data);
Database* db = baton->db;
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (baton->status != SQLITE_OK) {
EXCEPTION(String::New(baton->message.c_str()), baton->status, exception);
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { exception };
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
else {
Local<Value> args[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(db->handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(db), 2, args);
}
}
else if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
else if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
db->Process();
......@@ -551,8 +555,8 @@ void Database::Work_AfterExec(uv_work_t* req) {
delete baton;
}
Handle<Value> Database::Wait(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::Wait) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
......@@ -560,20 +564,21 @@ Handle<Value> Database::Wait(const Arguments& args) {
Baton* baton = new Baton(db, callback);
db->Schedule(Work_Wait, baton, true);
return args.This();
NanReturnValue(args.This());
}
void Database::Work_Wait(Baton* baton) {
HandleScope scope;
NanScope();
assert(baton->db->locked);
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
assert(baton->db->pending == 0);
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(baton->db->handle_, baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(baton->db), cb, 1, argv);
}
baton->db->Process();
......@@ -581,8 +586,8 @@ void Database::Work_Wait(Baton* baton) {
delete baton;
}
Handle<Value> Database::LoadExtension(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Database::LoadExtension) {
NanScope();
Database* db = ObjectWrap::Unwrap<Database>(args.This());
REQUIRE_ARGUMENT_STRING(0, filename);
......@@ -591,13 +596,13 @@ Handle<Value> Database::LoadExtension(const Arguments& args) {
Baton* baton = new LoadExtensionBaton(db, callback, *filename);
db->Schedule(Work_BeginLoadExtension, baton, true);
return args.This();
NanReturnValue(args.This());
}
void Database::Work_BeginLoadExtension(Baton* baton) {
assert(baton->db->locked);
assert(baton->db->open);
assert(baton->db->handle);
assert(baton->db->_handle);
assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(),
&baton->request, Work_LoadExtension, (uv_after_work_cb)Work_AfterLoadExtension);
......@@ -607,17 +612,17 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
void Database::Work_LoadExtension(uv_work_t* req) {
LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(req->data);
sqlite3_enable_load_extension(baton->db->handle, 1);
sqlite3_enable_load_extension(baton->db->_handle, 1);
char* message = NULL;
baton->status = sqlite3_load_extension(
baton->db->handle,
baton->db->_handle,
baton->filename.c_str(),
0,
&message
);
sqlite3_enable_load_extension(baton->db->handle, 0);
sqlite3_enable_load_extension(baton->db->_handle, 0);
if (baton->status != SQLITE_OK && message != NULL) {
baton->message = std::string(message);
......@@ -626,25 +631,26 @@ void Database::Work_LoadExtension(uv_work_t* req) {
}
void Database::Work_AfterLoadExtension(uv_work_t* req) {
HandleScope scope;
NanScope();
LoadExtensionBaton* baton = static_cast<LoadExtensionBaton*>(req->data);
Database* db = baton->db;
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (baton->status != SQLITE_OK) {
EXCEPTION(String::New(baton->message.c_str()), baton->status, exception);
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { exception };
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
else {
Local<Value> args[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(db->handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(db), 2, args);
}
}
else if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(db->handle_, baton->callback, 1, argv);
else if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(db), cb, 1, argv);
}
db->Process();
......
......@@ -7,6 +7,7 @@
#include <queue>
#include <sqlite3.h>
#include "nan.h"
#include "async.h"
using namespace v8;
......@@ -25,7 +26,7 @@ public:
static inline bool HasInstance(Handle<Value> val) {
if (!val->IsObject()) return false;
Local<Object> obj = val->ToObject();
return constructor_template->HasInstance(obj);
return NanPersistentToLocal(constructor_template)->HasInstance(obj);
}
struct Baton {
......@@ -39,7 +40,7 @@ public:
db(db_), status(SQLITE_OK) {
db->Ref();
request.data = this;
callback = Persistent<Function>::New(cb_);
NanAssignPersistent(Function, callback, cb_);
}
virtual ~Baton() {
db->Unref();
......@@ -99,7 +100,7 @@ public:
protected:
Database() : ObjectWrap(),
handle(NULL),
_handle(NULL),
open(false),
locked(false),
pending(0),
......@@ -112,43 +113,43 @@ protected:
~Database() {
RemoveCallbacks();
sqlite3_close(handle);
handle = NULL;
sqlite3_close(_handle);
_handle = NULL;
open = false;
}
static Handle<Value> New(const Arguments& args);
static NAN_METHOD(New);
static void Work_BeginOpen(Baton* baton);
static void Work_Open(uv_work_t* req);
static void Work_AfterOpen(uv_work_t* req);
static Handle<Value> OpenGetter(Local<String> str, const AccessorInfo& accessor);
static NAN_GETTER(OpenGetter);
void Schedule(Work_Callback callback, Baton* baton, bool exclusive = false);
void Process();
static Handle<Value> Exec(const Arguments& args);
static NAN_METHOD(Exec);
static void Work_BeginExec(Baton* baton);
static void Work_Exec(uv_work_t* req);
static void Work_AfterExec(uv_work_t* req);
static Handle<Value> Wait(const Arguments& args);
static NAN_METHOD(Wait);
static void Work_Wait(Baton* baton);
static Handle<Value> Close(const Arguments& args);
static NAN_METHOD(Close);
static void Work_BeginClose(Baton* baton);
static void Work_Close(uv_work_t* req);
static void Work_AfterClose(uv_work_t* req);
static Handle<Value> LoadExtension(const Arguments& args);
static NAN_METHOD(LoadExtension);
static void Work_BeginLoadExtension(Baton* baton);
static void Work_LoadExtension(uv_work_t* req);
static void Work_AfterLoadExtension(uv_work_t* req);
static Handle<Value> Serialize(const Arguments& args);
static Handle<Value> Parallelize(const Arguments& args);
static NAN_METHOD(Serialize);
static NAN_METHOD(Parallelize);
static Handle<Value> Configure(const Arguments& args);
static NAN_METHOD(Configure);
static void SetBusyTimeout(Baton* baton);
......@@ -167,7 +168,7 @@ protected:
void RemoveCallbacks();
protected:
sqlite3* handle;
sqlite3* _handle;
bool open;
bool locked;
......
......@@ -7,35 +7,27 @@ const char* sqlite_authorizer_string(int type);
#define REQUIRE_ARGUMENTS(n) \
if (args.Length() < (n)) { \
return ThrowException( \
Exception::TypeError(String::New("Expected " #n "arguments")) \
); \
return NanThrowTypeError("Expected " #n "arguments"); \
}
#define REQUIRE_ARGUMENT_EXTERNAL(i, var) \
if (args.Length() <= (i) || !args[i]->IsExternal()) { \
return ThrowException( \
Exception::TypeError(String::New("Argument " #i " invalid")) \
); \
return NanThrowTypeError("Argument " #i " invalid"); \
} \
Local<External> var = Local<External>::Cast(args[i]);
#define REQUIRE_ARGUMENT_FUNCTION(i, var) \
if (args.Length() <= (i) || !args[i]->IsFunction()) { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #i " must be a function")) \
); \
return NanThrowTypeError("Argument " #i " must be a function"); \
} \
Local<Function> var = Local<Function>::Cast(args[i]);
#define REQUIRE_ARGUMENT_STRING(i, var) \
if (args.Length() <= (i) || !args[i]->IsString()) { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #i " must be a string")) \
); \
return NanThrowTypeError("Argument " #i " must be a string"); \
} \
String::Utf8Value var(args[i]->ToString());
......@@ -44,9 +36,7 @@ const char* sqlite_authorizer_string(int type);
Local<Function> var; \
if (args.Length() > i && !args[i]->IsUndefined()) { \
if (!args[i]->IsFunction()) { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #i " must be a function")) \
); \
return NanThrowTypeError("Argument " #i " must be a function"); \
} \
var = Local<Function>::Cast(args[i]); \
}
......@@ -61,9 +51,7 @@ const char* sqlite_authorizer_string(int type);
var = args[i]->Int32Value(); \
} \
else { \
return ThrowException(Exception::TypeError( \
String::New("Argument " #i " must be an integer")) \
); \
return NanThrowTypeError("Argument " #i " must be an integer"); \
}
......@@ -103,8 +91,8 @@ const char* sqlite_authorizer_string(int type);
) \
); \
Local<Object> name ##_obj = name->ToObject(); \
name ##_obj->Set(NODE_PSYMBOL("errno"), Integer::New(errno)); \
name ##_obj->Set(NODE_PSYMBOL("code"), \
name ##_obj->Set(String::NewSymbol("errno"), Integer::New(errno)); \
name ##_obj->Set(String::NewSymbol("code"), \
String::NewSymbol(sqlite_code_string(errno)));
......@@ -122,7 +110,7 @@ const char* sqlite_authorizer_string(int type);
} }
#define WORK_DEFINITION(name) \
static Handle<Value> name(const Arguments& args); \
static NAN_METHOD(name); \
static void Work_Begin##name(Baton* baton); \
static void Work_##name(uv_work_t* req); \
static void Work_After##name(uv_work_t* req);
......@@ -164,4 +152,3 @@ const char* sqlite_authorizer_string(int type);
}
#endif
......@@ -103,4 +103,4 @@ const char* sqlite_authorizer_string(int type) {
}
}
NODE_MODULE(node_sqlite3, RegisterModule);
NODE_MODULE(node_sqlite3, RegisterModule)
......@@ -12,24 +12,24 @@ using namespace node_sqlite3;
Persistent<FunctionTemplate> Statement::constructor_template;
void Statement::Init(Handle<Object> target) {
HandleScope scope;
NanScope();
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Statement"));
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("Statement"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "bind", Bind);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "get", Get);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "run", Run);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "all", All);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "each", Each);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "reset", Reset);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "finalize", Finalize);
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
NODE_SET_PROTOTYPE_METHOD(t, "get", Get);
NODE_SET_PROTOTYPE_METHOD(t, "run", Run);
NODE_SET_PROTOTYPE_METHOD(t, "all", All);
NODE_SET_PROTOTYPE_METHOD(t, "each", Each);
NODE_SET_PROTOTYPE_METHOD(t, "reset", Reset);
NODE_SET_PROTOTYPE_METHOD(t, "finalize", Finalize);
NanAssignPersistent(FunctionTemplate, constructor_template, t);
target->Set(String::NewSymbol("Statement"),
constructor_template->GetFunction());
t->GetFunction());
}
void Statement::Process() {
......@@ -65,39 +65,36 @@ template <class T> void Statement::Error(T* baton) {
assert(stmt->status != 0);
EXCEPTION(String::New(stmt->message.c_str()), stmt->status, exception);
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { exception };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
else {
Local<Value> argv[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(stmt->handle_, 2, argv);
EMIT_EVENT(NanObjectWrapHandle(stmt), 2, argv);
}
}
// { Database db, String sql, Array params, Function callback }
Handle<Value> Statement::New(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::New) {
NanScope();
if (!args.IsConstructCall()) {
return ThrowException(Exception::TypeError(
String::New("Use the new operator to create new Statement objects"))
);
return NanThrowTypeError("Use the new operator to create new Statement objects");
}
int length = args.Length();
if (length <= 0 || !Database::HasInstance(args[0])) {
return ThrowException(Exception::TypeError(
String::New("Database object expected")));
return NanThrowTypeError("Database object expected");
}
else if (length <= 1 || !args[1]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("SQL query expected")));
return NanThrowTypeError("SQL query expected");
}
else if (length > 2 && !args[2]->IsUndefined() && !args[2]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("Callback expected")));
return NanThrowTypeError("Callback expected");
}
Database* db = ObjectWrap::Unwrap<Database>(args[0]->ToObject());
......@@ -112,7 +109,7 @@ Handle<Value> Statement::New(const Arguments& args) {
baton->sql = std::string(*String::Utf8Value(sql));
db->Schedule(Work_BeginPrepare, baton);
return args.This();
NanReturnValue(args.This());
}
void Statement::Work_BeginPrepare(Database::Baton* baton) {
......@@ -128,27 +125,27 @@ void Statement::Work_Prepare(uv_work_t* req) {
// In case preparing fails, we use a mutex to make sure we get the associated
// error message.
sqlite3_mutex* mtx = sqlite3_db_mutex(baton->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(baton->db->_handle);
sqlite3_mutex_enter(mtx);
stmt->status = sqlite3_prepare_v2(
baton->db->handle,
baton->db->_handle,
baton->sql.c_str(),
baton->sql.size(),
&stmt->handle,
&stmt->_handle,
NULL
);
if (stmt->status != SQLITE_OK) {
stmt->message = std::string(sqlite3_errmsg(baton->db->handle));
stmt->handle = NULL;
stmt->message = std::string(sqlite3_errmsg(baton->db->_handle));
stmt->_handle = NULL;
}
sqlite3_mutex_leave(mtx);
}
void Statement::Work_AfterPrepare(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(PrepareBaton);
if (stmt->status != SQLITE_OK) {
......@@ -157,9 +154,10 @@ void Statement::Work_AfterPrepare(uv_work_t* req) {
}
else {
stmt->prepared = true;
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
}
......@@ -196,7 +194,7 @@ template <class T> Values::Field*
}
}
template <class T> T* Statement::Bind(const Arguments& args, int start, int last) {
template <class T> T* Statement::Bind(_NAN_METHOD_ARGS, int start, int last) {
if (last < 0) last = args.Length();
Local<Function> callback;
if (last > start && args[last - 1]->IsFunction()) {
......@@ -252,8 +250,8 @@ bool Statement::Bind(const Parameters & parameters) {
return true;
}
sqlite3_reset(handle);
sqlite3_clear_bindings(handle);
sqlite3_reset(_handle);
sqlite3_clear_bindings(_handle);
Parameters::const_iterator it = parameters.begin();
Parameters::const_iterator end = parameters.end();
......@@ -267,36 +265,36 @@ bool Statement::Bind(const Parameters & parameters) {
pos = field->index;
}
else {
pos = sqlite3_bind_parameter_index(handle, field->name.c_str());
pos = sqlite3_bind_parameter_index(_handle, field->name.c_str());
}
switch (field->type) {
case SQLITE_INTEGER: {
status = sqlite3_bind_int(handle, pos,
status = sqlite3_bind_int(_handle, pos,
((Values::Integer*)field)->value);
} break;
case SQLITE_FLOAT: {
status = sqlite3_bind_double(handle, pos,
status = sqlite3_bind_double(_handle, pos,
((Values::Float*)field)->value);
} break;
case SQLITE_TEXT: {
status = sqlite3_bind_text(handle, pos,
status = sqlite3_bind_text(_handle, pos,
((Values::Text*)field)->value.c_str(),
((Values::Text*)field)->value.size(), SQLITE_TRANSIENT);
} break;
case SQLITE_BLOB: {
status = sqlite3_bind_blob(handle, pos,
status = sqlite3_bind_blob(_handle, pos,
((Values::Blob*)field)->value,
((Values::Blob*)field)->length, SQLITE_TRANSIENT);
} break;
case SQLITE_NULL: {
status = sqlite3_bind_null(handle, pos);
status = sqlite3_bind_null(_handle, pos);
} break;
}
}
if (status != SQLITE_OK) {
message = std::string(sqlite3_errmsg(db->handle));
message = std::string(sqlite3_errmsg(db->_handle));
return false;
}
}
......@@ -304,17 +302,17 @@ bool Statement::Bind(const Parameters & parameters) {
return true;
}
Handle<Value> Statement::Bind(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Bind) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
Baton* baton = stmt->Bind<Baton>(args);
if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported")));
return NanThrowTypeError("Data type is not supported");
}
else {
stmt->Schedule(Work_BeginBind, baton);
return args.This();
NanReturnValue(args.This());
}
}
......@@ -325,14 +323,14 @@ void Statement::Work_BeginBind(Baton* baton) {
void Statement::Work_Bind(uv_work_t* req) {
STATEMENT_INIT(Baton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
sqlite3_mutex_enter(mtx);
stmt->Bind(baton->parameters);
sqlite3_mutex_leave(mtx);
}
void Statement::Work_AfterBind(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(Baton);
if (stmt->status != SQLITE_OK) {
......@@ -340,9 +338,10 @@ void Statement::Work_AfterBind(uv_work_t* req) {
}
else {
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
}
......@@ -351,17 +350,17 @@ void Statement::Work_AfterBind(uv_work_t* req) {
Handle<Value> Statement::Get(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Get) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
Baton* baton = stmt->Bind<RowBaton>(args);
if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported")));
return NanThrowError("Data type is not supported");
}
else {
stmt->Schedule(Work_BeginGet, baton);
return args.This();
NanReturnValue(args.This());
}
}
......@@ -373,14 +372,14 @@ void Statement::Work_Get(uv_work_t* req) {
STATEMENT_INIT(RowBaton);
if (stmt->status != SQLITE_DONE || baton->parameters.size()) {
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
sqlite3_mutex_enter(mtx);
if (stmt->Bind(baton->parameters)) {
stmt->status = sqlite3_step(stmt->handle);
stmt->status = sqlite3_step(stmt->_handle);
if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
}
}
......@@ -388,13 +387,13 @@ void Statement::Work_Get(uv_work_t* req) {
if (stmt->status == SQLITE_ROW) {
// Acquire one result row before returning.
GetRow(&baton->row, stmt->handle);
GetRow(&baton->row, stmt->_handle);
}
}
}
void Statement::Work_AfterGet(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(RowBaton);
if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) {
......@@ -402,15 +401,16 @@ void Statement::Work_AfterGet(uv_work_t* req) {
}
else {
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
if (stmt->status == SQLITE_ROW) {
// Create the result array from the data we acquired.
Local<Value> argv[] = { Local<Value>::New(Null()), RowToJS(&baton->row) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 2, argv);
Local<Value> argv[] = { NanNewLocal<Value>(Null()), RowToJS(&baton->row) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 2, argv);
}
else {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
}
}
......@@ -418,17 +418,17 @@ void Statement::Work_AfterGet(uv_work_t* req) {
STATEMENT_END();
}
Handle<Value> Statement::Run(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Run) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
Baton* baton = stmt->Bind<RunBaton>(args);
if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported")));
return NanThrowError("Data type is not supported");
}
else {
stmt->Schedule(Work_BeginRun, baton);
return args.This();
NanReturnValue(args.This());
}
}
......@@ -439,23 +439,23 @@ void Statement::Work_BeginRun(Baton* baton) {
void Statement::Work_Run(uv_work_t* req) {
STATEMENT_INIT(RunBaton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
sqlite3_mutex_enter(mtx);
// Make sure that we also reset when there are no parameters.
if (!baton->parameters.size()) {
sqlite3_reset(stmt->handle);
sqlite3_reset(stmt->_handle);
}
if (stmt->Bind(baton->parameters)) {
stmt->status = sqlite3_step(stmt->handle);
stmt->status = sqlite3_step(stmt->_handle);
if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
}
else {
baton->inserted_id = sqlite3_last_insert_rowid(stmt->db->handle);
baton->changes = sqlite3_changes(stmt->db->handle);
baton->inserted_id = sqlite3_last_insert_rowid(stmt->db->_handle);
baton->changes = sqlite3_changes(stmt->db->_handle);
}
}
......@@ -463,7 +463,7 @@ void Statement::Work_Run(uv_work_t* req) {
}
void Statement::Work_AfterRun(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(RunBaton);
if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) {
......@@ -471,29 +471,30 @@ void Statement::Work_AfterRun(uv_work_t* req) {
}
else {
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
stmt->handle_->Set(String::NewSymbol("lastID"), Local<Integer>(Integer::New(baton->inserted_id)));
stmt->handle_->Set(String::NewSymbol("changes"), Local<Integer>(Integer::New(baton->changes)));
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
NanObjectWrapHandle(stmt)->Set(String::NewSymbol("lastID"), Local<Integer>(Integer::New(baton->inserted_id)));
NanObjectWrapHandle(stmt)->Set(String::NewSymbol("changes"), Local<Integer>(Integer::New(baton->changes)));
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
}
STATEMENT_END();
}
Handle<Value> Statement::All(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::All) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
Baton* baton = stmt->Bind<RowsBaton>(args);
if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported")));
return NanThrowError("Data type is not supported");
}
else {
stmt->Schedule(Work_BeginAll, baton);
return args.This();
NanReturnValue(args.This());
}
}
......@@ -504,23 +505,23 @@ void Statement::Work_BeginAll(Baton* baton) {
void Statement::Work_All(uv_work_t* req) {
STATEMENT_INIT(RowsBaton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
sqlite3_mutex_enter(mtx);
// Make sure that we also reset when there are no parameters.
if (!baton->parameters.size()) {
sqlite3_reset(stmt->handle);
sqlite3_reset(stmt->_handle);
}
if (stmt->Bind(baton->parameters)) {
while ((stmt->status = sqlite3_step(stmt->handle)) == SQLITE_ROW) {
while ((stmt->status = sqlite3_step(stmt->_handle)) == SQLITE_ROW) {
Row* row = new Row();
GetRow(row, stmt->handle);
GetRow(row, stmt->_handle);
baton->rows.push_back(row);
}
if (stmt->status != SQLITE_DONE) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
}
}
......@@ -528,7 +529,7 @@ void Statement::Work_All(uv_work_t* req) {
}
void Statement::Work_AfterAll(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(RowsBaton);
if (stmt->status != SQLITE_DONE) {
......@@ -536,7 +537,8 @@ void Statement::Work_AfterAll(uv_work_t* req) {
}
else {
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
if (baton->rows.size()) {
// Create the result array from the data we acquired.
Local<Array> result(Array::New(baton->rows.size()));
......@@ -547,16 +549,16 @@ void Statement::Work_AfterAll(uv_work_t* req) {
delete *it;
}
Local<Value> argv[] = { Local<Value>::New(Null()), result };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 2, argv);
Local<Value> argv[] = { NanNewLocal<Value>(Null()), result };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 2, argv);
}
else {
// There were no result rows.
Local<Value> argv[] = {
Local<Value>::New(Null()),
Local<Value>::New(Array::New(0))
NanNewLocal<Value>(Null()),
NanNewLocal<Value>(Array::New(0))
};
TRY_CATCH_CALL(stmt->handle_, baton->callback, 2, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 2, argv);
}
}
}
......@@ -564,8 +566,8 @@ void Statement::Work_AfterAll(uv_work_t* req) {
STATEMENT_END();
}
Handle<Value> Statement::Each(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Each) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
int last = args.Length();
......@@ -577,12 +579,12 @@ Handle<Value> Statement::Each(const Arguments& args) {
EachBaton* baton = stmt->Bind<EachBaton>(args, 0, last);
if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported")));
return NanThrowError("Data type is not supported");
}
else {
baton->completed = Persistent<Function>::New(completed);
NanAssignPersistent(Function, baton->completed, completed);
stmt->Schedule(Work_BeginEach, baton);
return args.This();
NanReturnValue(args.This());
}
}
......@@ -591,8 +593,8 @@ void Statement::Work_BeginEach(Baton* baton) {
// the event loop. This prevents dangling events.
EachBaton* each_baton = static_cast<EachBaton*>(baton);
each_baton->async = new Async(each_baton->stmt, AsyncEach);
each_baton->async->item_cb = Persistent<Function>::New(each_baton->callback);
each_baton->async->completed_cb = Persistent<Function>::New(each_baton->completed);
NanAssignPersistent(Function, each_baton->async->item_cb, each_baton->callback);
NanAssignPersistent(Function, each_baton->async->completed_cb, each_baton->completed);
STATEMENT_BEGIN(Each);
}
......@@ -602,23 +604,23 @@ void Statement::Work_Each(uv_work_t* req) {
Async* async = baton->async;
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->_handle);
int retrieved = 0;
// Make sure that we also reset when there are no parameters.
if (!baton->parameters.size()) {
sqlite3_reset(stmt->handle);
sqlite3_reset(stmt->_handle);
}
if (stmt->Bind(baton->parameters)) {
while (true) {
sqlite3_mutex_enter(mtx);
stmt->status = sqlite3_step(stmt->handle);
stmt->status = sqlite3_step(stmt->_handle);
if (stmt->status == SQLITE_ROW) {
sqlite3_mutex_leave(mtx);
Row* row = new Row();
GetRow(row, stmt->handle);
GetRow(row, stmt->_handle);
NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
async->data.push_back(row);
retrieved++;
......@@ -628,7 +630,7 @@ void Statement::Work_Each(uv_work_t* req) {
}
else {
if (stmt->status != SQLITE_DONE) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
}
sqlite3_mutex_leave(mtx);
break;
......@@ -648,7 +650,7 @@ void Statement::CloseCallback(uv_handle_t* handle) {
}
void Statement::AsyncEach(uv_async_t* handle, int status) {
HandleScope scope;
NanScope();
Async* async = static_cast<Async*>(handle->data);
while (true) {
......@@ -662,36 +664,38 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
break;
}
if (!async->item_cb.IsEmpty() && async->item_cb->IsFunction()) {
Local<Function> cb = NanPersistentToLocal(async->item_cb);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[2];
argv[0] = Local<Value>::New(Null());
argv[0] = NanNewLocal<Value>(Null());
Rows::const_iterator it = rows.begin();
Rows::const_iterator end = rows.end();
for (int i = 0; it < end; ++it, i++) {
argv[1] = RowToJS(*it);
async->retrieved++;
TRY_CATCH_CALL(async->stmt->handle_, async->item_cb, 2, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(async->stmt), cb, 2, argv);
delete *it;
}
}
}
Local<Function> cb = NanPersistentToLocal(async->completed_cb);
if (async->completed) {
if (!async->completed_cb.IsEmpty() &&
async->completed_cb->IsFunction()) {
if (!cb.IsEmpty() &&
cb->IsFunction()) {
Local<Value> argv[] = {
Local<Value>::New(Null()),
NanNewLocal<Value>(Null()),
Integer::New(async->retrieved)
};
TRY_CATCH_CALL(async->stmt->handle_, async->completed_cb, 2, argv);
TRY_CATCH_CALL(NanObjectWrapHandle(async->stmt), cb, 2, argv);
}
uv_close((uv_handle_t*)handle, CloseCallback);
}
}
void Statement::Work_AfterEach(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(EachBaton);
if (stmt->status != SQLITE_DONE) {
......@@ -701,8 +705,8 @@ void Statement::Work_AfterEach(uv_work_t* req) {
STATEMENT_END();
}
Handle<Value> Statement::Reset(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Reset) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
......@@ -710,7 +714,7 @@ Handle<Value> Statement::Reset(const Arguments& args) {
Baton* baton = new Baton(stmt, callback);
stmt->Schedule(Work_BeginReset, baton);
return args.This();
NanReturnValue(args.This());
}
void Statement::Work_BeginReset(Baton* baton) {
......@@ -720,18 +724,19 @@ void Statement::Work_BeginReset(Baton* baton) {
void Statement::Work_Reset(uv_work_t* req) {
STATEMENT_INIT(Baton);
sqlite3_reset(stmt->handle);
sqlite3_reset(stmt->_handle);
stmt->status = SQLITE_OK;
}
void Statement::Work_AfterReset(uv_work_t* req) {
HandleScope scope;
NanScope();
STATEMENT_INIT(Baton);
// Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
Local<Value> argv[] = { NanNewLocal<Value>(Null()) };
TRY_CATCH_CALL(NanObjectWrapHandle(stmt), cb, 1, argv);
}
STATEMENT_END();
......@@ -758,14 +763,10 @@ Local<Object> Statement::RowToJS(Row* row) {
value = Local<Value>(String::New(((Values::Text*)field)->value.c_str(), ((Values::Text*)field)->value.size()));
} break;
case SQLITE_BLOB: {
#if NODE_VERSION_AT_LEAST(0, 11, 3)
value = Local<Value>::New(Buffer::New(((Values::Blob*)field)->value, ((Values::Blob*)field)->length));
#else
value = Local<Value>::New(Buffer::New(((Values::Blob*)field)->value, ((Values::Blob*)field)->length)->handle_);
#endif
value = NanNewLocal<Value>(NanNewBufferHandle(((Values::Blob*)field)->value, ((Values::Blob*)field)->length));
} break;
case SQLITE_NULL: {
value = Local<Value>::New(Null());
value = NanNewLocal<Value>(Null());
} break;
}
......@@ -809,23 +810,24 @@ void Statement::GetRow(Row* row, sqlite3_stmt* stmt) {
}
}
Handle<Value> Statement::Finalize(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Statement::Finalize) {
NanScope();
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
Baton* baton = new Baton(stmt, callback);
stmt->Schedule(Finalize, baton);
return stmt->db->handle_;
NanReturnValue(NanObjectWrapHandle(stmt->db));
}
void Statement::Finalize(Baton* baton) {
baton->stmt->Finalize();
// Fire callback in case there was one.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
TRY_CATCH_CALL(baton->stmt->handle_, baton->callback, 0, NULL);
Local<Function> cb = NanPersistentToLocal(baton->callback);
if (!cb.IsEmpty() && cb->IsFunction()) {
TRY_CATCH_CALL(NanObjectWrapHandle(baton->stmt), cb, 0, NULL);
}
delete baton;
......@@ -837,8 +839,8 @@ void Statement::Finalize() {
CleanQueue();
// Finalize returns the status code of the last operation. We already fired
// error events in case those failed.
sqlite3_finalize(handle);
handle = NULL;
sqlite3_finalize(_handle);
_handle = NULL;
db->Unref();
}
......@@ -855,9 +857,11 @@ void Statement::CleanQueue() {
Call* call = queue.front();
queue.pop();
if (prepared && !call->baton->callback.IsEmpty() &&
call->baton->callback->IsFunction()) {
TRY_CATCH_CALL(handle_, call->baton->callback, 1, argv);
Local<Function> cb = NanPersistentToLocal(call->baton->callback);
if (prepared && !cb.IsEmpty() &&
cb->IsFunction()) {
TRY_CATCH_CALL(NanObjectWrapHandle(this), cb, 1, argv);
called = true;
}
......@@ -871,7 +875,7 @@ void Statement::CleanQueue() {
// Statement object.
if (!called) {
Local<Value> args[] = { String::NewSymbol("error"), exception };
EMIT_EVENT(handle_, 2, args);
EMIT_EVENT(NanObjectWrapHandle(this), 2, args);
}
}
else while (!queue.empty()) {
......
......@@ -13,6 +13,7 @@
#include <vector>
#include <sqlite3.h>
#include "nan.h"
using namespace v8;
using namespace node;
......@@ -76,7 +77,7 @@ public:
static Persistent<FunctionTemplate> constructor_template;
static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments& args);
static NAN_METHOD(New);
struct Baton {
uv_work_t request;
......@@ -87,7 +88,7 @@ public:
Baton(Statement* stmt_, Handle<Function> cb_) : stmt(stmt_) {
stmt->Ref();
request.data = this;
callback = Persistent<Function>::New(cb_);
NanAssignPersistent(Function, callback, cb_);
}
virtual ~Baton() {
for (unsigned int i = 0; i < parameters.size(); i++) {
......@@ -183,7 +184,7 @@ public:
Statement(Database* db_) : ObjectWrap(),
db(db_),
handle(NULL),
_handle(NULL),
status(SQLITE_OK),
prepared(false),
locked(true),
......@@ -202,7 +203,7 @@ public:
WORK_DEFINITION(Each);
WORK_DEFINITION(Reset);
static Handle<Value> Finalize(const Arguments& args);
static NAN_METHOD(Finalize);
protected:
static void Work_BeginPrepare(Database::Baton* baton);
......@@ -216,8 +217,8 @@ protected:
void Finalize();
template <class T> inline Values::Field* BindParameter(const Handle<Value> source, T pos);
template <class T> T* Bind(const Arguments& args, int start = 0, int end = -1);
bool Bind(const Parameters & parameters);
template <class T> T* Bind(_NAN_METHOD_ARGS, int start = 0, int end = -1);
bool Bind(const Parameters &parameters);
static void GetRow(Row* row, sqlite3_stmt* stmt);
static Local<Object> RowToJS(Row* row);
......@@ -229,7 +230,7 @@ protected:
protected:
Database* db;
sqlite3_stmt* handle;
sqlite3_stmt* _handle;
int status;
std::string message;
......
require('set-immediate');
var sqlite3 = require('..');
var assert = require('assert');
var helper = require('./support/helper');
......@@ -30,7 +31,7 @@ describe('cache', function() {
var db1, db2;
db1 = new sqlite3.cached.Database(filename, function(err) {
if (err) throw err;
process.nextTick(function() {
setImmediate(function() {
db2 = new sqlite3.cached.Database(filename, function(err) {
done();
......
require('set-immediate');
var sqlite3 = require('..');
var assert = require('assert');
......@@ -31,7 +32,7 @@ describe('profiling', function() {
assert.ok(!create);
db.run("CREATE TABLE foo (id int)", function(err) {
if (err) throw err;
process.nextTick(function() {
setImmediate(function() {
assert.ok(create);
done();
});
......@@ -43,7 +44,7 @@ describe('profiling', function() {
assert.ok(!select);
db.run("SELECT * FROM foo", function(err) {
if (err) throw err;
process.nextTick(function() {
setImmediate(function() {
assert.ok(select);
done();
});
......
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