Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/mariano/node-db
Browse files Browse the repository at this point in the history
  • Loading branch information
imkira committed Mar 26, 2013
2 parents 76fe394 + a0fa16a commit 60c810b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
24 changes: 18 additions & 6 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ node_db::Binding::~Binding() {
}
}

uv_async_t node_db::Binding::g_async;

void node_db::Binding::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::FunctionTemplate> constructorTemplate) {
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_STRING, node_db::Result::Column::STRING);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_BOOL, node_db::Result::Column::BOOL);
Expand Down Expand Up @@ -86,9 +88,14 @@ v8::Handle<v8::Value> node_db::Binding::Connect(const v8::Arguments& args) {

uv_work_t* req = new uv_work_t();
req->data = request;
uv_queue_work(uv_default_loop(), req, uvConnect, uvConnectFinished);
uv_queue_work(uv_default_loop(), req, uvConnect, (uv_after_work_cb)uvConnectFinished);

#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_ref((uv_handle_t *)&g_async);
#else
uv_ref(uv_default_loop());
#endif

uv_ref(uv_default_loop());
} else {
connect(request);
connectFinished(request);
Expand All @@ -100,7 +107,7 @@ v8::Handle<v8::Value> node_db::Binding::Connect(const v8::Arguments& args) {
void node_db::Binding::connect(connect_request_t* request) {
try {
request->binding->connection->open();
} catch(const node_db::Exception& exception) {
} catch(node_db::Exception const& exception) {
request->error = exception.what();
}
}
Expand Down Expand Up @@ -146,13 +153,18 @@ void node_db::Binding::uvConnect(uv_work_t* uvRequest) {
connect(request);
}

void node_db::Binding::uvConnectFinished(uv_work_t* uvRequest) {
void node_db::Binding::uvConnectFinished(uv_work_t* uvRequest, int status) {
v8::HandleScope scope;

connect_request_t* request = static_cast<connect_request_t*>(uvRequest->data);
assert(request);

#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_unref((uv_handle_t *)&g_async);
#else
uv_unref(uv_default_loop());
#endif

request->binding->Unref();

connectFinished(request);
Expand Down Expand Up @@ -192,7 +204,7 @@ v8::Handle<v8::Value> node_db::Binding::Escape(const v8::Arguments& args) {
v8::String::Utf8Value string(args[0]->ToString());
std::string unescaped(*string);
escaped = binding->connection->escape(unescaped);
} catch(const node_db::Exception& exception) {
} catch(node_db::Exception const& exception) {
THROW_EXCEPTION(exception.what())
}

Expand All @@ -213,7 +225,7 @@ v8::Handle<v8::Value> node_db::Binding::Name(const v8::Arguments& args) {
v8::String::Utf8Value string(args[0]->ToString());
std::string unescaped(*string);
escaped << binding->connection->escapeName(unescaped);
} catch(const node_db::Exception& exception) {
} catch(node_db::Exception const& exception) {
THROW_EXCEPTION(exception.what())
}

Expand Down
5 changes: 4 additions & 1 deletion binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#ifndef BINDING_H_
#define BINDING_H_

#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <node_version.h>
#include <string>
#include "./node_defs.h"
Expand Down Expand Up @@ -33,8 +35,9 @@ class Binding : public 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 uv_async_t g_async;
static void uvConnect(uv_work_t* uvRequest);
static void uvConnectFinished(uv_work_t* uvRequest);
static void uvConnectFinished(uv_work_t* uvRequest, int status);
static void connect(connect_request_t* request);
static void connectFinished(connect_request_t* request);
virtual v8::Handle<v8::Value> set(const v8::Local<v8::Object> options) = 0;
Expand Down
16 changes: 14 additions & 2 deletions query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
bool node_db::Query::gmtDeltaLoaded = false;
int node_db::Query::gmtDelta;

uv_async_t node_db::Query::g_async;

void node_db::Query::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::FunctionTemplate> constructorTemplate) {
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "select", Select);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "from", From);
Expand Down Expand Up @@ -647,9 +649,14 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) {

uv_work_t* req = new uv_work_t();
req->data = request;
uv_queue_work(uv_default_loop(), req, uvExecute, uvExecuteFinished);
uv_queue_work(uv_default_loop(), req, uvExecute, (uv_after_work_cb)uvExecuteFinished);

#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_ref((uv_handle_t *)&g_async);
#else
uv_ref(uv_default_loop());
#endif

} else {
request->query->executeAsync(request);
}
Expand Down Expand Up @@ -728,7 +735,7 @@ void node_db::Query::uvExecute(uv_work_t* uvRequest) {
}
}

void node_db::Query::uvExecuteFinished(uv_work_t* uvRequest) {
void node_db::Query::uvExecuteFinished(uv_work_t* uvRequest, int status) {
v8::HandleScope scope;

execute_request_t *request = static_cast<execute_request_t *>(uvRequest->data);
Expand Down Expand Up @@ -813,7 +820,12 @@ void node_db::Query::uvExecuteFinished(uv_work_t* uvRequest) {
}
}

#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_unref((uv_handle_t *)&g_async);
#else
uv_unref(uv_default_loop());
#endif

request->query->Unref();

Query::freeRequest(request);
Expand Down
4 changes: 3 additions & 1 deletion query.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef QUERY_H_
#define QUERY_H_

#include <v8.h>
#include <stdlib.h>
#include <node.h>
#include <node_buffer.h>
Expand Down Expand Up @@ -66,8 +67,9 @@ class Query : public 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 uv_async_t g_async;
static void uvExecute(uv_work_t* uvRequest);
static void uvExecuteFinished(uv_work_t* uvRequest);
static void uvExecuteFinished(uv_work_t* uvRequest, int status);
void executeAsync(execute_request_t* request);
static void freeRequest(execute_request_t* request, bool freeAll = true);
std::string fieldName(v8::Local<v8::Value> value) const throw(Exception&);
Expand Down
20 changes: 20 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ exports.get = function(createDbClient, quoteName) {
test.equal(quoteName + "table" + quoteName + ".*", client.name("table.*"));

test.done();
},
"multiple results": function(test) {
var client = this.client;
test.expect(1);

client.query(
'CREATE PROCEDURE multiple_result () ' +
'BEGIN ' +
' SELECT 0; ' +
' SELECT 1; ' +
'END'
).execute(function () {
client.query('call multiple_result()').execute(function (error1, rows, cols) {
client.query('call multiple_result()').execute(function (error2, rows, cols) {
test.equal(null, error2);
client.query('DROP PROCEDURE IF EXISTS multiple_result').execute();
test.done();
});
});
});
}
});

Expand Down

0 comments on commit 60c810b

Please sign in to comment.