Permalink
Browse files

Merge pull request #131 from tim-dev/master

feat: adds Date support to escapeCQL function
  • Loading branch information...
2 parents 45fc1ac + cb54876 commit 5905031f5e3ab5ae10691202e46f25fa2caba6b9 @devdazed devdazed committed Dec 15, 2013
Showing with 48 additions and 0 deletions.
  1. +22 −0 lib/connection.js
  2. +19 −0 test/cql3.js
  3. +7 −0 test/helpers/cql3.json
View
@@ -16,6 +16,10 @@ var util = require('util'),
* @memberOf Connection
*/
var NOOP = function(){};
+/**
+ * Adds zero left-padding to numbers, when necessary
+ */
+var zero_pad = function(n){ return n < 10 ? "0" + n : n }
/**
* Default port for cassandra
@@ -102,6 +106,24 @@ function escapeCQL(val) {
return val.toString();
}
+ if(val instanceof Date){
+ _val = val.getTime()
+ if(isNaN(_val)){
+ throw errors.create(new Error('Invalid Date'));
+ }
+ // C* < 2.0.2 doesn't work with negative longs
+ if(_val<0){
+ return "'" + val.getUTCFullYear()
+ + '-' + zero_pad(val.getUTCMonth() + 1)
+ + '-' + zero_pad(val.getUTCDate())
+ + ' ' + zero_pad(val.getUTCHours())
+ + ':' + zero_pad(val.getUTCMinutes())
+ + ':' + zero_pad(val.getUTCSeconds())
+ + "'"
+ }
+ return _val
+ }
+
if(typeof val === 'number' || typeof val === 'boolean'){
return val.toString();
}
View
@@ -67,6 +67,14 @@ function testResultless(){
return testCql.apply(testCql, args);
}
+function makeDates(dates_array){
+ var ret = [], i = 0, len = dates_array.length;
+ for(i;i<len;i++){
+ ret.push(new Date(dates_array[i]));
+ }
+ return ret;
+}
+
module.exports = {
'setUp':function(test, assert){
Helenus = require('helenus');
@@ -293,6 +301,17 @@ module.exports = {
assert.strictEqual(res[0].get('varnumber').value, -8388607);//test a 3 byte-long variable integer
}),
+ 'test cql timestamp create table':testResultless(config['timestamp_create']),
+ 'test cql timestamp update1':testResultless(config['timestamp_update#cql'], makeDates(config['timestamp_update#vals1'])),
+ 'test cql timestamp update2':testResultless(config['timestamp_update#cql'], makeDates(config['timestamp_update#vals2'])),
+ 'test cql timestamp update3':testResultless(config['timestamp_update#cql'], makeDates(config['timestamp_update#vals3'])),
+ 'test cql timestamp invalid date': function(test, assert) {
+ assert.throws(function(){
+ conn.cql(config['timestamp_invalid#cql'], makeDates(config['timestamp_invalid#vals']))
+ }, Error)
+ test.finish();
+ },
+
'test cql drop keyspace':testResultless(config['drop_ks#cql']),
'tearDown':function(test, assert){
@@ -51,6 +51,13 @@
"integers_select2#cql" : "SELECT * FROM integers where number = -1",
"integers_select3#cql" : "SELECT * FROM integers where number = -2",
+ "timestamp_create" : "CREATE TABLE timestamps (ts1 timestamp, ts2 timestamp, PRIMARY KEY (ts1))",
+ "timestamp_update#cql" : "UPDATE timestamps SET ts2 = ? WHERE ts1 = ?",
+ "timestamp_update#vals1" : [null, null],
+ "timestamp_update#vals2" : [null, "2100-01-01"],
+ "timestamp_update#vals3" : [null, "1969-07-20"],
+ "timestamp_invalid#cql" : "UPDATE timestamps SET ts2 = ? ts1 old = ?",
+ "timestamp_invalid#vals" : [null, "THIS ISNT A DATE"],
"prepare#cql" : "SELECT * FROM ? WHERE KEY = ?",
"error#cql" : "SOME INVALID CQL"

0 comments on commit 5905031

Please sign in to comment.