Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allowing user to change precision, and adding and fixing tests

  • Loading branch information...
commit 39aab3548dbd4e3fd83dc97b10c47f9b615a4c60 1 parent 42af451
Mariano Iglesias authored October 23, 2011
45  query.cc
@@ -586,10 +586,6 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) {
586 586
     node_db::Query *query = node::ObjectWrap::Unwrap<node_db::Query>(args.This());
587 587
     assert(query);
588 588
 
589  
-    if (!query->connection->isAlive(false)) {
590  
-        THROW_EXCEPTION("Can't execute a query without being connected")
591  
-    }
592  
-
593 589
     if (args.Length() > 0) {
594 590
         v8::Handle<v8::Value> set = query->set(args);
595 591
         if (!set.IsEmpty()) {
@@ -625,6 +621,10 @@ v8::Handle<v8::Value> node_db::Query::Execute(const v8::Arguments& args) {
625 621
         }
626 622
     }
627 623
 
  624
+    if (!query->connection->isAlive(false)) {
  625
+        THROW_EXCEPTION("Can't execute a query without being connected")
  626
+    }
  627
+
628 628
     execute_request_t *request = new execute_request_t();
629 629
     if (request == NULL) {
630 630
         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
1103 1103
                 v8::Local<v8::Object> currentObject = currentValue->ToObject();
1104 1104
                 v8::Local<v8::String> escapeKey = v8::String::New("escape");
1105 1105
                 v8::Local<v8::String> valueKey = v8::String::New("value");
  1106
+                v8::Local<v8::String> precisionKey = v8::String::New("precision");
1106 1107
                 v8::Local<v8::Value> optionValue;
1107 1108
                 bool escape = false;
  1109
+                int precision = -1;
1108 1110
 
1109 1111
                 if (!currentObject->Has(valueKey)) {
1110 1112
                     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
1118 1120
                     escape = optionValue->IsTrue();
1119 1121
                 }
1120 1122
 
  1123
+                if (currentObject->Has(precisionKey)) {
  1124
+                    optionValue = currentObject->Get(precisionKey);
  1125
+                    if (!optionValue->IsNumber() || optionValue->IntegerValue() < 0) {
  1126
+                        throw new node_db::Exception("Specify a number equal or greater than 0 for precision");
  1127
+                    }
  1128
+                    precision = optionValue->IntegerValue();
  1129
+                }
  1130
+
1121 1131
                 if (j > 0) {
1122 1132
                     buffer += ',';
1123 1133
                 }
1124 1134
 
1125  
-                buffer += this->value(currentObject->Get(valueKey), false, escape);
  1135
+                buffer += this->value(currentObject->Get(valueKey), false, escape, precision);
1126 1136
             } else {
1127 1137
                 if (j > 0) {
1128 1138
                     buffer += ',';
@@ -1375,7 +1385,7 @@ std::string node_db::Query::parseQuery() const throw(node_db::Exception&) {
1375 1385
     return parsed;
1376 1386
 }
1377 1387
 
1378  
-std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool escape) const throw(node_db::Exception&) {
  1388
+std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool escape, int precision) const throw(node_db::Exception&) {
1379 1389
     std::ostringstream currentStream;
1380 1390
 
1381 1391
     if (value->IsNull()) {
@@ -1404,7 +1414,19 @@ std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool
1404 1414
         v8::Local<v8::Object> object = value->ToObject();
1405 1415
         v8::Handle<v8::String> valueKey = v8::String::New("value");
1406 1416
         v8::Handle<v8::String> escapeKey = v8::String::New("escape");
  1417
+
1407 1418
         if (object->Has(valueKey)) {
  1419
+            v8::Handle<v8::String> precisionKey = v8::String::New("precision");
  1420
+            int precision = -1;
  1421
+
  1422
+            if (object->Has(precisionKey)) {
  1423
+                v8::Local<v8::Value> optionValue = object->Get(precisionKey);
  1424
+                if (!optionValue->IsNumber() || optionValue->IntegerValue() < 0) {
  1425
+                    throw new node_db::Exception("Specify a number equal or greater than 0 for precision");
  1426
+                }
  1427
+                precision = optionValue->IntegerValue();
  1428
+            }
  1429
+
1408 1430
             bool innerEscape = true;
1409 1431
             if (object->Has(escapeKey)) {
1410 1432
                 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
1413 1435
                 }
1414 1436
                 innerEscape = escapeValue->IsTrue();
1415 1437
             }
1416  
-            currentStream << this->value(object->Get(valueKey), false, innerEscape);
  1438
+            currentStream << this->value(object->Get(valueKey), false, innerEscape, precision);
1417 1439
         } else {
1418 1440
             v8::Handle<v8::String> sqlKey = v8::String::New("sql");
1419 1441
             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
1432 1454
         }
1433 1455
     } else if (value->IsBoolean()) {
1434 1456
         currentStream << (value->IsTrue() ? '1' : '0');
  1457
+    } else if (value->IsUint32() || value->IsInt32() || value->NumberValue() == value->IntegerValue()) {
  1458
+        currentStream << value->IntegerValue();
1435 1459
     } else if (value->IsNumber()) {
1436  
-        currentStream << std::fixed << value->NumberValue();
  1460
+        if (precision == -1) {
  1461
+            v8::String::Utf8Value currentString(value->ToString());
  1462
+            currentStream << *currentString;
  1463
+        } else {
  1464
+            currentStream << std::fixed << std::setprecision(precision) << value->NumberValue();
  1465
+        }
1437 1466
     } else if (value->IsString()) {
1438 1467
         v8::String::Utf8Value currentString(value->ToString());
1439 1468
         std::string string = *currentString;
3  query.h
@@ -8,6 +8,7 @@
8 8
 #include <node_version.h>
9 9
 #include <algorithm>
10 10
 #include <cctype>
  11
+#include <iomanip>
11 12
 #include <string>
12 13
 #include <sstream>
13 14
 #include <vector>
@@ -82,7 +83,7 @@ class Query : public EventEmitter {
82 83
         virtual std::string parseQuery() const throw(Exception&);
83 84
         virtual std::vector<std::string::size_type> placeholders(std::string* parsed) const throw(Exception&);
84 85
         virtual Result* execute() const throw(Exception&);
85  
-        std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true) const throw(Exception&);
  86
+        std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true, int precision = -1) const throw(Exception&);
86 87
 
87 88
 
88 89
     private:
27  tests.js
@@ -17,8 +17,11 @@ exports.get = function(createDbClient, quoteName) {
17 17
 
18 18
     exports["Client"] = testCase({
19 19
         "setUp": function(callback) {
20  
-            this.client = createDbClient();
21  
-            callback();
  20
+            var self = this;
  21
+            createDbClient(function(client) {
  22
+                self.client = client;
  23
+                callback();
  24
+            });
22 25
         },
23 26
         "escape()": function(test) {
24 27
             var client = this.client;
@@ -49,8 +52,11 @@ exports.get = function(createDbClient, quoteName) {
49 52
 
50 53
     exports["Query"] = testCase({
51 54
         "setUp": function(callback) {
52  
-            this.client = createDbClient();
53  
-            callback();
  55
+            var self = this;
  56
+            createDbClient(function(client) {
  57
+                self.client = client;
  58
+                callback();
  59
+            });
54 60
         },
55 61
         "select markers": function(test) {
56 62
             var client = this.client;
@@ -194,7 +200,7 @@ exports.get = function(createDbClient, quoteName) {
194 200
         },
195 201
         "insert markers": function(test) {
196 202
             var client = this.client;
197  
-            test.expect(4);
  203
+            test.expect(5);
198 204
 
199 205
             var created = new Date();
200 206
             client.query(
@@ -252,6 +258,17 @@ exports.get = function(createDbClient, quoteName) {
252 258
                 }}
253 259
             ).execute();
254 260
 
  261
+            client.query(
  262
+                "INSERT INTO numbers(n1, n2, n3, n4) VALUES ?", 
  263
+                [
  264
+                    [ 10, 3.1415627, 70686626206955, { value: 3.1415627, precision: 2 } ],
  265
+                ],
  266
+                { start: function (query) {
  267
+                    test.equal("INSERT INTO numbers(n1, n2, n3, n4) VALUES (10,3.1415627,70686626206955,3.14)", query);
  268
+                    return false;
  269
+                }}
  270
+            ).execute();
  271
+
255 272
             test.done();
256 273
         },
257 274
         "select()": function(test) {

0 notes on commit 39aab35

Please sign in to comment.
Something went wrong with that request. Please try again.