Browse files

Merge branch 'master' of github.com:simplereach/helenus

* 'master' of github.com:simplereach/helenus:
  fix (datetype): uses long if post-epoch date, datestring for pre, to account for https://issues.apache.org/jira/browse/CASSANDRA-6212
  Fix Thrift readme example
  fix: escapes date as iso string in utc to work with all versions of cql
  fix: handles pre-epoch dates for cassandra < 2.*
  fix: separated the different timestamps (current, future, pre-epoch) out into their own tests
  fix: escapeCql handles invalid date objects tests: adds tests for timestamps
  feat: adds Date support to escapeCQL function
  • Loading branch information...
2 parents 5e07897 + 5905031 commit 4020e5737f2e63cf9df5cbaf926e0738ae734d55 @devdazed devdazed committed Dec 15, 2013
Showing with 49 additions and 1 deletion.
  1. +1 −1 Readme.md
  2. +22 −0 lib/connection.js
  3. +19 −0 test/cql3.js
  4. +7 −0 test/helpers/cql3.json
View
2 Readme.md
@@ -99,7 +99,7 @@ If you do not want to use CQL, you can make calls using the thrift driver
//get what we just put in
//the driver will return a helenus.Row object just like CQL
- cf.get('foo', {consistency:helenus.ConsistencyLevel.ONE} function(err, row){
+ cf.get('foo', {consistency:helenus.ConsistencyLevel.ONE}, function(err, row){
if(err){
throw(err);
}
View
22 lib/connection.js
@@ -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
19 test/cql3.js
@@ -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){
View
7 test/helpers/cql3.json
@@ -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 4020e57

Please sign in to comment.