Skip to content

Instantly share code, notes, and snippets.

@ixaxaar
Created July 11, 2014 08:29
Show Gist options
  • Save ixaxaar/5d665e1b011289c236aa to your computer and use it in GitHub Desktop.
Save ixaxaar/5d665e1b011289c236aa to your computer and use it in GitHub Desktop.
CQL query generator in node.js
var _ = require('underscore');
var cassandra = function(client) {
var that = this;
that.__query__ = "";
that.__error__ = false;
that.__q__ = null;
return that;
};
cassandra.prototype.select = function() {
var args = Array.prototype.slice.call(arguments);
var that = this;
that.__q__ = "SELECT";
that.__query__ += " SELECT ";
if (args.length) {
args.forEach(function(arg){ that.__query__ += (" " + arg + ",") });
console.log()
that.__query__ = that.__query__.slice(0, -1); // chop off the last comma
}
return that;
};
cassandra.prototype.from = function() {
var that = this;
var args = Array.prototype.slice.call(arguments);
if (!args.length) that.__error__ = that.__q__;
that.__q__ = "FROM";
that.__query__ += " FROM ";
that.__query__ += arguments[0];
return that;
};
cassandra.prototype.where = function(condition) {
var that = this;
if (!condition) that.__error__ = that.__q__;
that.__q__ = "WHERE";
that.__query__ += " WHERE ";
that.__query__ += condition;
return that;
};
cassandra.prototype.and = function(condition) {
var that = this;
if (!condition) that.__error__ = that.__q__;
if (that.__q__ === "WHERE" || that.__q__ === "USING" || that.__q__ === "IF")
that.__query__ += " AND ";
else that.__query__ += ", ";
that.__query__ += condition;
return that;
};
cassandra.prototype.or = function(condition) {
var that = this;
if (!condition) that.__error__ = that.__q__;
if (that.__q__ === "WHERE") that.__query__ += " OR ";
else that.__query__ += ", ";
that.__query__ += condition;
return that;
};
cassandra.prototype.orderBy = function(arg) {
var that = this;
if (!arg) that.__error__ = that.__q__;
that.__q__ = "ORDER BY";
that.__query__ += " ORDER BY " + arg;
return that;
};
cassandra.prototype.desc = function() {
var that = this;
that.__query__ += " DESC ";
return that;
};
cassandra.prototype.asc = function() {
var that = this;
that.__query__ += " ASC ";
return that;
};
cassandra.prototype.distinct = function() {
var that = this;
that.__q__ = "DISTINCT";
that.__query__ += " DISTINCT ";
return that;
};
cassandra.prototype.limit = function(entries) {
var that = this;
if (!entries) entries = 10; // should we set a default?
that.__q__ = "LIMIT";
that.__query__ += " LIMIT ";
that.__query__ += entries;
return that;
};
cassandra.prototype.insertInto = function(into) {
var that = this;
if (!into) that.__error__ = false;
that.__q__ = "INSERT";
that.__query__ += " INSERT INTO ";
that.__query__ += into;
return that;
};
cassandra.prototype.values = function(values) {
var that = this;
if (!values) that.__error__ = that.__q__;
that.__q__ = "VALUES";
var columns = " (";
var columnValues = " (";
Object.keys(values).forEach(function(k) {
columns += k + ", ";
columnValues += values[k] + ", ";
});
columns = columns.slice(0, -2); // remove the trailing comma
columns += ") ";
columnValues = columnValues.slice(0, -2);
columnValues += ") ";
that.__query__ += columns + " VALUES " + columnValues;
return that;
};
cassandra.prototype.using = function(option, value) {
var that = this;
if (!option || !value) that.__error__ = that.__q__;
that.__q__ = "USING";
if (option === "ttl")
that.__query__ += " USING TTL " + value;
else if (option === "timestamp")
that.__query__ += " USING TIMESTAMP " + value;
return that;
};
cassandra.prototype.update = function(table) {
var that = this;
if (!table) that.__error__ = that.__q__;
that.__q__ = "UPDATE";
that.__query__ += " UPDATE " + table;
return that;
};
cassandra.prototype.set = function(values) {
var that = this;
if (!values || !_.isObject(values)) that.__error__ = that.__q__;
that.__q__ = "SET";
that.__query__ += " SET ";
Object.keys(values).forEach(function(k) {
that.__query__ += (k + "=" + values[k]);
that.__query__ += ", ";
});
that.__query__ = that.__query__.slice(0, -2); // remove trailing space and comma
return that;
};
cassandra.prototype.if = function(condition) {
var that = this;
if (!condition) that.__error__ = that.__q__;
that.__q__ = "IF";
that.__query__ += " IF ";
that.__query__ += condition;
};
c = new cassandra();
c.select("col1", "col2")
.from("table1")
.where("col3 > 10")
.and("col4 < 10")
.and("col5 = 5")
.orderBy("col6").asc()
.distinct()
.limit(100)
;
console.log(c);
d = new cassandra();
d.insertInto("table1")
.values({
col1: 1,
col2: 2,
col3: "haha"
})
.using("ttl", 60000)
;
console.log(d);
e = new cassandra();
e.update("table1")
.using("ttl", 60000)
.set({
col1: 1,
col2: 2,
col3: 3
})
.where("col4 > 5")
.and("col5 > 6")
.and("col7 > 8")
.if("col8 = 1")
;
console.log(e);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment