-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing node-db to work on node 0.4.x and 0.5.x
- Loading branch information
Showing
7 changed files
with
124 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
// Copyright 2011 Mariano Iglesias <[email protected]> | ||
#include "./binding.h" | ||
|
||
v8::Persistent<v8::String> node_db::Binding::syReady; | ||
v8::Persistent<v8::String> node_db::Binding::syError; | ||
|
||
node_db::Binding::Binding(): node::EventEmitter(), connection(NULL), cbConnect(NULL) { | ||
node_db::Binding::Binding(): node_db::EventEmitter(), connection(NULL) { | ||
} | ||
|
||
node_db::Binding::~Binding() { | ||
|
@@ -30,9 +27,6 @@ void node_db::Binding::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::Fu | |
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "escape", Escape); | ||
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "name", Name); | ||
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "query", Query); | ||
|
||
syReady = NODE_PERSISTENT_SYMBOL("ready"); | ||
syError = NODE_PERSISTENT_SYMBOL("error"); | ||
} | ||
|
||
v8::Handle<v8::Value> node_db::Binding::Connect(const v8::Arguments& args) { | ||
|
@@ -121,11 +115,11 @@ void node_db::Binding::connectFinished(connect_request_t* request) { | |
argv[0] = v8::Local<v8::Value>::New(v8::Null()); | ||
argv[1] = server; | ||
|
||
request->binding->Emit(syReady, 1, &argv[1]); | ||
request->binding->Emit("ready", 1, argv); | ||
} else { | ||
argv[0] = v8::String::New(request->error != NULL ? request->error : "(unknown error)"); | ||
|
||
request->binding->Emit(syError, 1, argv); | ||
request->binding->Emit("error", 1, argv); | ||
} | ||
|
||
if (request->binding->cbConnect != NULL && !request->binding->cbConnect->IsEmpty()) { | ||
|
@@ -141,13 +135,19 @@ void node_db::Binding::connectFinished(connect_request_t* request) { | |
delete request; | ||
} | ||
|
||
int node_db::Binding::eioConnect(eio_req* eioRequest) { | ||
#if NODE_VERSION_AT_LEAST(0, 5, 0) | ||
void | ||
#else | ||
int | ||
#endif | ||
node_db::Binding::eioConnect(eio_req* eioRequest) { | ||
connect_request_t* request = static_cast<connect_request_t*>(eioRequest->data); | ||
assert(request); | ||
|
||
connect(request); | ||
|
||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
return 0; | ||
#endif | ||
} | ||
|
||
int node_db::Binding::eioConnectFinished(eio_req* eioRequest) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2011 Mariano Iglesias <[email protected]> | ||
#include "./events.h" | ||
|
||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
v8::Persistent<v8::String> node_db::EventEmitter::syEmit; | ||
#endif | ||
|
||
node_db::EventEmitter::EventEmitter() : node::ObjectWrap() { | ||
} | ||
|
||
void node_db::EventEmitter::Init() { | ||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
syEmit = NODE_PSYMBOL("emit"); | ||
#endif | ||
} | ||
|
||
bool node_db::EventEmitter::Emit(const char* event, int argc, v8::Handle<v8::Value> argv[]) { | ||
v8::HandleScope scope; | ||
|
||
int nArgc = argc + 1; | ||
v8::Handle<v8::Value>* nArgv = new v8::Handle<v8::Value>[nArgc]; | ||
if (nArgv == NULL) { | ||
return false; | ||
} | ||
|
||
nArgv[0] = v8::String::New(event); | ||
for (int i=0; i < argc; i++) { | ||
nArgv[i + 1] = argv[i]; | ||
} | ||
|
||
#if NODE_VERSION_AT_LEAST(0, 5, 0) | ||
node::MakeCallback(this->handle_, "emit", nArgc, nArgv); | ||
#else | ||
v8::Local<v8::Value> emit_v = this->handle_->Get(syEmit); | ||
if (!emit_v->IsFunction()) { | ||
return false; | ||
} | ||
v8::Local<v8::Function> emit = v8::Local<v8::Function>::Cast(emit_v); | ||
|
||
v8::TryCatch try_catch; | ||
emit->Call(this->handle_, nArgc, nArgv); | ||
#endif | ||
|
||
delete [] nArgv; | ||
|
||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
if (try_catch.HasCaught()) { | ||
node::FatalException(try_catch); | ||
} | ||
#endif | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2011 Mariano Iglesias <[email protected]> | ||
#ifndef EVENTS_H_ | ||
#define EVENTS_H_ | ||
|
||
#include <v8.h> | ||
#include <node_object_wrap.h> | ||
#include <node_version.h> | ||
#include "./node_defs.h" | ||
|
||
namespace node_db { | ||
class EventEmitter : public node::ObjectWrap { | ||
public: | ||
static void Init(); | ||
|
||
protected: | ||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
static v8::Persistent<v8::String> syEmit; | ||
#endif | ||
|
||
EventEmitter(); | ||
bool Emit(const char* event, int argc, v8::Handle<v8::Value> argv[]); | ||
}; | ||
} | ||
|
||
#endif // BINDING_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
// Copyright 2011 Mariano Iglesias <[email protected]> | ||
#include "./query.h" | ||
|
||
v8::Persistent<v8::String> node_db::Query::sySuccess; | ||
v8::Persistent<v8::String> node_db::Query::syError; | ||
v8::Persistent<v8::String> node_db::Query::syEach; | ||
bool node_db::Query::gmtDeltaLoaded = false; | ||
int node_db::Query::gmtDelta; | ||
|
||
|
@@ -23,13 +20,9 @@ void node_db::Query::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::Func | |
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "delete", Delete); | ||
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "sql", Sql); | ||
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "execute", Execute); | ||
|
||
sySuccess = NODE_PERSISTENT_SYMBOL("success"); | ||
syError = NODE_PERSISTENT_SYMBOL("error"); | ||
syEach = NODE_PERSISTENT_SYMBOL("each"); | ||
} | ||
|
||
node_db::Query::Query(): node::EventEmitter(), | ||
node_db::Query::Query(): node_db::EventEmitter(), | ||
connection(NULL), async(true), cast(true), bufferText(false), cbStart(NULL), cbExecute(NULL), cbFinish(NULL) { | ||
} | ||
|
||
|
@@ -659,7 +652,12 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) { | |
return scope.Close(v8::Undefined()); | ||
} | ||
|
||
int node_db::Query::eioExecute(eio_req* eioRequest) { | ||
#if NODE_VERSION_AT_LEAST(0, 5, 0) | ||
void | ||
#else | ||
int | ||
#endif | ||
node_db::Query::eioExecute(eio_req* eioRequest) { | ||
execute_request_t *request = static_cast<execute_request_t *>(eioRequest->data); | ||
assert(request); | ||
|
||
|
@@ -728,8 +726,9 @@ int node_db::Query::eioExecute(eio_req* eioRequest) { | |
Query::freeRequest(request, false); | ||
request->error = exception.what(); | ||
} | ||
|
||
#if !NODE_VERSION_AT_LEAST(0, 5, 0) | ||
return 0; | ||
#endif | ||
} | ||
|
||
int node_db::Query::eioExecuteFinished(eio_req* eioRequest) { | ||
|
@@ -759,7 +758,7 @@ int node_db::Query::eioExecuteFinished(eio_req* eioRequest) { | |
eachArgv[1] = v8::Number::New(index); | ||
eachArgv[2] = v8::Local<v8::Value>::New((index == totalRows - 1) ? v8::True() : v8::False()); | ||
|
||
request->query->Emit(syEach, 3, eachArgv); | ||
request->query->Emit("each", 3, eachArgv); | ||
|
||
rows->Set(index, row); | ||
} | ||
|
@@ -785,7 +784,7 @@ int node_db::Query::eioExecuteFinished(eio_req* eioRequest) { | |
argv[1] = result; | ||
} | ||
|
||
request->query->Emit(sySuccess, !isEmpty ? 2 : 1, &argv[1]); | ||
request->query->Emit("success", !isEmpty ? 2 : 1, &argv[1]); | ||
|
||
if (request->query->cbExecute != NULL && !request->query->cbExecute->IsEmpty()) { | ||
v8::TryCatch tryCatch; | ||
|
@@ -798,7 +797,7 @@ int node_db::Query::eioExecuteFinished(eio_req* eioRequest) { | |
v8::Local<v8::Value> argv[1]; | ||
argv[0] = v8::String::New(request->error != NULL ? request->error : "(unknown error)"); | ||
|
||
request->query->Emit(syError, 1, argv); | ||
request->query->Emit("error", 1, argv); | ||
|
||
if (request->query->cbExecute != NULL && !request->query->cbExecute->IsEmpty()) { | ||
v8::TryCatch tryCatch; | ||
|
@@ -872,7 +871,7 @@ void node_db::Query::executeAsync(execute_request_t* request) { | |
eachArgv[1] = v8::Number::New(index); | ||
eachArgv[2] = v8::Local<v8::Value>::New(request->result->hasNext() ? v8::True() : v8::False()); | ||
|
||
this->Emit(syEach, 3, eachArgv); | ||
this->Emit("each", 3, eachArgv); | ||
|
||
rows->Set(index++, jsRow); | ||
} | ||
|
@@ -891,7 +890,7 @@ void node_db::Query::executeAsync(execute_request_t* request) { | |
argv[1] = result; | ||
} | ||
|
||
this->Emit(sySuccess, !isEmpty ? 2 : 1, &argv[1]); | ||
this->Emit("success", !isEmpty ? 2 : 1, &argv[1]); | ||
|
||
if (this->cbExecute != NULL && !this->cbExecute->IsEmpty()) { | ||
v8::TryCatch tryCatch; | ||
|
@@ -907,7 +906,7 @@ void node_db::Query::executeAsync(execute_request_t* request) { | |
v8::Local<v8::Value> argv[1]; | ||
argv[0] = v8::String::New(request->error != NULL ? exception.what() : "(unknown error)"); | ||
|
||
this->Emit(syError, 1, argv); | ||
this->Emit("error", 1, argv); | ||
|
||
if (this->cbExecute != NULL && !this->cbExecute->IsEmpty()) { | ||
v8::TryCatch tryCatch; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters