Permalink
Browse files

Merge pull request #18 from imkira/master

Handle BIGINTs as strings
  • Loading branch information...
2 parents 2b916b0 + e4cb163 commit dc07598022e810caec759a83c6afa7c5db211574 @mariano committed May 15, 2013
Showing with 23 additions and 9 deletions.
  1. +1 −0 binding.cc
  2. +21 −9 query.cc
  3. +1 −0 result.h
View
@@ -16,6 +16,7 @@ void node_db::Binding::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::Fu
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_STRING, node_db::Result::Column::STRING);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_BOOL, node_db::Result::Column::BOOL);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_INT, node_db::Result::Column::INT);
+ NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_BIGINT, node_db::Result::Column::BIGINT);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_NUMBER, node_db::Result::Column::NUMBER);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_DATE, node_db::Result::Column::DATE);
NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_TIME, node_db::Result::Column::TIME);
View
@@ -7,6 +7,13 @@ int node_db::Query::gmtDelta;
uv_async_t node_db::Query::g_async;
+v8::Local<v8::String> v8StringFromUInt64(uint64_t num, std::ostringstream &reusableStream) {
+ reusableStream.clear();
+ reusableStream.seekp(0);
+ reusableStream << num << std::ends;
+ return v8::String::New(reusableStream.str().c_str());
+}
+
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);
@@ -755,13 +762,14 @@ void node_db::Query::uvExecuteFinished(uv_work_t* uvRequest, int status) {
v8::Local<v8::Array> rows = v8::Array::New(totalRows);
uint64_t index = 0;
+ std::ostringstream reusableStream;
for (std::vector<row_t*>::iterator iterator = request->rows->begin(), end = request->rows->end(); iterator != end; ++iterator, index++) {
row_t* currentRow = *iterator;
v8::Local<v8::Object> row = request->query->row(request->result, currentRow);
v8::Local<v8::Value> eachArgv[3];
eachArgv[0] = row;
- eachArgv[1] = v8::Number::New(index);
+ eachArgv[1] = v8StringFromUInt64(index, reusableStream);
eachArgv[2] = v8::Local<v8::Value>::New((index == totalRows - 1) ? v8::True() : v8::False());
request->query->Emit("each", 3, eachArgv);
@@ -784,9 +792,10 @@ void node_db::Query::uvExecuteFinished(uv_work_t* uvRequest, int status) {
argv[2] = columns;
} else {
v8::Local<v8::Object> result = v8::Object::New();
- result->Set(v8::String::New("id"), v8::Number::New(request->result->insertId()));
- result->Set(v8::String::New("affected"), v8::Number::New(request->result->affectedCount()));
- result->Set(v8::String::New("warning"), v8::Number::New(request->result->warningCount()));
+ std::ostringstream reusableStream;
+ result->Set(v8::String::New("id"), v8StringFromUInt64(request->result->insertId(), reusableStream));
+ result->Set(v8::String::New("affected"), v8StringFromUInt64(request->result->affectedCount(), reusableStream));
+ result->Set(v8::String::New("warning"), v8StringFromUInt64(request->result->warningCount(), reusableStream));
argv[1] = result;
}
@@ -868,6 +877,7 @@ void node_db::Query::executeAsync(execute_request_t* request) {
row_t row;
uint64_t index = 0;
+ std::ostringstream reusableStream;
while (request->result->hasNext()) {
row.columnLengths = (unsigned long*) request->result->columnLengths();
@@ -877,7 +887,7 @@ void node_db::Query::executeAsync(execute_request_t* request) {
v8::Local<v8::Value> eachArgv[3];
eachArgv[0] = jsRow;
- eachArgv[1] = v8::Number::New(index);
+ eachArgv[1] = v8StringFromUInt64(index, reusableStream);
eachArgv[2] = v8::Local<v8::Value>::New(request->result->hasNext() ? v8::True() : v8::False());
this->Emit("each", 3, eachArgv);
@@ -893,9 +903,10 @@ void node_db::Query::executeAsync(execute_request_t* request) {
argv[2] = columns;
} else {
v8::Local<v8::Object> result = v8::Object::New();
- result->Set(v8::String::New("id"), v8::Number::New(request->result->insertId()));
- result->Set(v8::String::New("affected"), v8::Number::New(request->result->affectedCount()));
- result->Set(v8::String::New("warning"), v8::Number::New(request->result->warningCount()));
+ std::ostringstream reusableStream;
+ result->Set(v8::String::New("id"), v8StringFromUInt64(request->result->insertId(), reusableStream));
+ result->Set(v8::String::New("affected"), v8StringFromUInt64(request->result->affectedCount(), reusableStream));
+ result->Set(v8::String::New("warning"), v8StringFromUInt64(request->result->warningCount(), reusableStream));
argv[1] = result;
}
@@ -1318,9 +1329,10 @@ v8::Local<v8::Object> node_db::Query::row(node_db::Result* result, row_t* curren
std::istringstream stream(currentValue);
std::string item;
uint64_t index = 0;
+ std::ostringstream reusableStream;
while (std::getline(stream, item, ',')) {
if (!item.empty()) {
- values->Set(v8::Integer::New(index++), v8::String::New(item.c_str()));
+ values->Set(v8StringFromUInt64(index++, reusableStream), v8::String::New(item.c_str()));
}
}
value = values;
View
@@ -16,6 +16,7 @@ class Result {
STRING,
TEXT,
INT,
+ BIGINT,
NUMBER,
DATE,
TIME,

0 comments on commit dc07598

Please sign in to comment.