Permalink
Browse files

Allowing user to change precision, and adding and fixing tests

  • Loading branch information...
mariano committed Oct 23, 2011
1 parent 42af451 commit 39aab3548dbd4e3fd83dc97b10c47f9b615a4c60
Showing with 61 additions and 14 deletions.
  1. +37 −8 query.cc
  2. +2 −1 query.h
  3. +22 −5 tests.js
View
@@ -586,10 +586,6 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) {
node_db::Query *query = node::ObjectWrap::Unwrap<node_db::Query>(args.This());
assert(query);
- if (!query->connection->isAlive(false)) {
- THROW_EXCEPTION("Can't execute a query without being connected")
- }
-
if (args.Length() > 0) {
v8::Handle<v8::Value> set = query->set(args);
if (!set.IsEmpty()) {
@@ -625,6 +621,10 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) {
}
}
+ if (!query->connection->isAlive(false)) {
+ THROW_EXCEPTION("Can't execute a query without being connected")
+ }
+
execute_request_t *request = new execute_request_t();
if (request == NULL) {
THROW_EXCEPTION("Could not create EIO request")
@@ -1103,8 +1103,10 @@ std::string node_db::Query::fieldName(v8::Local<v8::Value> value) const throw(no
v8::Local<v8::Object> currentObject = currentValue->ToObject();
v8::Local<v8::String> escapeKey = v8::String::New("escape");
v8::Local<v8::String> valueKey = v8::String::New("value");
+ v8::Local<v8::String> precisionKey = v8::String::New("precision");
v8::Local<v8::Value> optionValue;
bool escape = false;
+ int precision = -1;
if (!currentObject->Has(valueKey)) {
throw node_db::Exception("The \"value\" option for the select field object must be specified");
@@ -1118,11 +1120,19 @@ std::string node_db::Query::fieldName(v8::Local<v8::Value> value) const throw(no
escape = optionValue->IsTrue();
}
+ if (currentObject->Has(precisionKey)) {
+ optionValue = currentObject->Get(precisionKey);
+ if (!optionValue->IsNumber() || optionValue->IntegerValue() < 0) {
+ throw new node_db::Exception("Specify a number equal or greater than 0 for precision");
+ }
+ precision = optionValue->IntegerValue();
+ }
+
if (j > 0) {
buffer += ',';
}
- buffer += this->value(currentObject->Get(valueKey), false, escape);
+ buffer += this->value(currentObject->Get(valueKey), false, escape, precision);
} else {
if (j > 0) {
buffer += ',';
@@ -1375,7 +1385,7 @@ std::string node_db::Query::parseQuery() const throw(node_db::Exception&) {
return parsed;
}
-std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool escape) const throw(node_db::Exception&) {
+std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool escape, int precision) const throw(node_db::Exception&) {
std::ostringstream currentStream;
if (value->IsNull()) {
@@ -1404,7 +1414,19 @@ std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool
v8::Local<v8::Object> object = value->ToObject();
v8::Handle<v8::String> valueKey = v8::String::New("value");
v8::Handle<v8::String> escapeKey = v8::String::New("escape");
+
if (object->Has(valueKey)) {
+ v8::Handle<v8::String> precisionKey = v8::String::New("precision");
+ int precision = -1;
+
+ if (object->Has(precisionKey)) {
+ v8::Local<v8::Value> optionValue = object->Get(precisionKey);
+ if (!optionValue->IsNumber() || optionValue->IntegerValue() < 0) {
+ throw new node_db::Exception("Specify a number equal or greater than 0 for precision");
+ }
+ precision = optionValue->IntegerValue();
+ }
+
bool innerEscape = true;
if (object->Has(escapeKey)) {
v8::Local<v8::Value> escapeValue = object->Get(escapeKey);
@@ -1413,7 +1435,7 @@ std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool
}
innerEscape = escapeValue->IsTrue();
}
- currentStream << this->value(object->Get(valueKey), false, innerEscape);
+ currentStream << this->value(object->Get(valueKey), false, innerEscape, precision);
} else {
v8::Handle<v8::String> sqlKey = v8::String::New("sql");
if (!object->Has(sqlKey) || !object->Get(sqlKey)->IsFunction()) {
@@ -1432,8 +1454,15 @@ std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool
}
} else if (value->IsBoolean()) {
currentStream << (value->IsTrue() ? '1' : '0');
+ } else if (value->IsUint32() || value->IsInt32() || value->NumberValue() == value->IntegerValue()) {
+ currentStream << value->IntegerValue();
} else if (value->IsNumber()) {
- currentStream << std::fixed << value->NumberValue();
+ if (precision == -1) {
+ v8::String::Utf8Value currentString(value->ToString());
+ currentStream << *currentString;
+ } else {
+ currentStream << std::fixed << std::setprecision(precision) << value->NumberValue();
+ }
} else if (value->IsString()) {
v8::String::Utf8Value currentString(value->ToString());
std::string string = *currentString;
View
@@ -8,6 +8,7 @@
#include <node_version.h>
#include <algorithm>
#include <cctype>
+#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
@@ -82,7 +83,7 @@ class Query : public EventEmitter {
virtual std::string parseQuery() const throw(Exception&);
virtual std::vector<std::string::size_type> placeholders(std::string* parsed) const throw(Exception&);
virtual Result* execute() const throw(Exception&);
- std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true) const throw(Exception&);
+ std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true, int precision = -1) const throw(Exception&);
private:
View
@@ -17,8 +17,11 @@ exports.get = function(createDbClient, quoteName) {
exports["Client"] = testCase({
"setUp": function(callback) {
- this.client = createDbClient();
- callback();
+ var self = this;
+ createDbClient(function(client) {
+ self.client = client;
+ callback();
+ });
},
"escape()": function(test) {
var client = this.client;
@@ -49,8 +52,11 @@ exports.get = function(createDbClient, quoteName) {
exports["Query"] = testCase({
"setUp": function(callback) {
- this.client = createDbClient();
- callback();
+ var self = this;
+ createDbClient(function(client) {
+ self.client = client;
+ callback();
+ });
},
"select markers": function(test) {
var client = this.client;
@@ -194,7 +200,7 @@ exports.get = function(createDbClient, quoteName) {
},
"insert markers": function(test) {
var client = this.client;
- test.expect(4);
+ test.expect(5);
var created = new Date();
client.query(
@@ -252,6 +258,17 @@ exports.get = function(createDbClient, quoteName) {
}}
).execute();
+ client.query(
+ "INSERT INTO numbers(n1, n2, n3, n4) VALUES ?",
+ [
+ [ 10, 3.1415627, 70686626206955, { value: 3.1415627, precision: 2 } ],
+ ],
+ { start: function (query) {
+ test.equal("INSERT INTO numbers(n1, n2, n3, n4) VALUES (10,3.1415627,70686626206955,3.14)", query);
+ return false;
+ }}
+ ).execute();
+
test.done();
},
"select()": function(test) {

0 comments on commit 39aab35

Please sign in to comment.