Skip to content

Commit

Permalink
Allowing node-db to work on node 0.4.x and 0.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano committed Oct 22, 2011
1 parent 61ed1cc commit fce911d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 39 deletions.
22 changes: 11 additions & 11 deletions binding.cc
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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#define BINDING_H_

#include <node.h>
#include <node_events.h>
#include <node_version.h>
#include <string>
#include "./node_defs.h"
#include "./connection.h"
#include "./events.h"
#include "./exception.h"
#include "./query.h"

namespace node_db {
class Binding : public node::EventEmitter {
class Binding : public EventEmitter {
public:
Connection* connection;

Expand All @@ -22,8 +23,6 @@ class Binding : public node::EventEmitter {
const char* error;
};
v8::Persistent<v8::Function>* cbConnect;
static v8::Persistent<v8::String> syReady;
static v8::Persistent<v8::String> syError;

Binding();
~Binding();
Expand All @@ -34,7 +33,13 @@ class Binding : public node::EventEmitter {
static v8::Handle<v8::Value> Escape(const v8::Arguments& args);
static v8::Handle<v8::Value> Name(const v8::Arguments& args);
static v8::Handle<v8::Value> Query(const v8::Arguments& args);
static int eioConnect(eio_req* req);
static
#if NODE_VERSION_AT_LEAST(0, 5, 0)
void
#else
int
#endif
eioConnect(eio_req* req);
static void connect(connect_request_t* request);
static void connectFinished(connect_request_t* request);
static int eioConnectFinished(eio_req* eioRequest);
Expand Down
53 changes: 53 additions & 0 deletions events.cc
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;
}
25 changes: 25 additions & 0 deletions events.h
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_
1 change: 0 additions & 1 deletion node_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <node.h>

#define NODE_CONSTANT(constant) v8::Integer::New(constant)
#define NODE_PERSISTENT_SYMBOL(s) v8::Persistent<v8::String>::New(v8::String::NewSymbol(s))

#define NODE_ADD_PROTOTYPE_METHOD(templ, name, callback) \
do { \
Expand Down
31 changes: 15 additions & 16 deletions query.cc
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;

Expand All @@ -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) {
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions query.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
#include <stdlib.h>
#include <node.h>
#include <node_buffer.h>
#include <node_events.h>
#include <node_version.h>
#include <algorithm>
#include <cctype>
#include <string>
#include <sstream>
#include <vector>
#include "./node_defs.h"
#include "./connection.h"
#include "./events.h"
#include "./exception.h"
#include "./result.h"

namespace node_db {
class Query : public node::EventEmitter {
class Query : public EventEmitter {
public:
static void Init(v8::Handle<v8::Object> target, v8::Persistent<v8::FunctionTemplate> constructorTemplate);
void setConnection(Connection* connection);
Expand Down Expand Up @@ -46,9 +47,6 @@ class Query : public node::EventEmitter {
v8::Persistent<v8::Function>* cbStart;
v8::Persistent<v8::Function>* cbExecute;
v8::Persistent<v8::Function>* cbFinish;
static v8::Persistent<v8::String> syError;
static v8::Persistent<v8::String> sySuccess;
static v8::Persistent<v8::String> syEach;

Query();
~Query();
Expand All @@ -67,7 +65,13 @@ class Query : public node::EventEmitter {
static v8::Handle<v8::Value> Delete(const v8::Arguments& args);
static v8::Handle<v8::Value> Sql(const v8::Arguments& args);
static v8::Handle<v8::Value> Execute(const v8::Arguments& args);
static int eioExecute(eio_req* eioRequest);
static
#if NODE_VERSION_AT_LEAST(0, 5, 0)
void
#else
int
#endif
eioExecute(eio_req* eioRequest);
static int eioExecuteFinished(eio_req* eioRequest);
void executeAsync(execute_request_t* request);
static void freeRequest(execute_request_t* request, bool freeAll = true);
Expand Down

0 comments on commit fce911d

Please sign in to comment.