Skip to content

Instantly share code, notes, and snippets.

@booo
Created December 13, 2012 16:31
Show Gist options
  • Save booo/4277694 to your computer and use it in GitHub Desktop.
Save booo/4277694 to your computer and use it in GitHub Desktop.
class Batch
constructor: (@keyspace) ->
@statements = [] #list of statements aka insert delete update
toCQL: ->
"BEGIN BATCH
#{(stmt.toCQL() for stmt in @statements).join "\n"}
APPLY BATCH"
class Table
constructor: (@name, @columns, @primaryKeys, @options) ->
create: ->
"CREATE TABLE #{@name} (#{ ("#{name} #{type}" for [name, type] in @columns).join ", " } PRIMARY KEY(#{@primaryKeys.join ", "}))"
drop: ->
"DROP TABLE #{@name}"
truncate: ->
"TRUNCATE TABLE #{@name}"
select: (columns) ->
new SelectStatement @, columns
insert: ->
update: @insert
delete: ->
# a = 1 and b = 2 and c = 3
# eql("a", 1).and().eql("b", 2).eql("c", 3)
# a = 1 or b = 2 and c = 3
# eqal("a", 1).or().eql("b", 2).and().eql("c", 3)
# (a = 1 or b = 2) and c = 3
class Condition
constructor: (@operator, @left, @right) ->
toString: ->
"#{@left} #{@operator} #{@right}"
class SelectStatement
constructor: (@table, @columns) ->
@where = []
select: (columns) ->
@columns = @columns.concat columns
eql: (left, right) ->
c = new Condition "=", left, right
@where.push c
c
neql: (left, right) ->
@where.push new Condition "!=", left, right
geql: (left, right) ->
@where.push new Condition ">=", left, right
leql: (left, right) ->
@where.push new Condition "=<", left, right
lower: (left, right) ->
@where.push new Condition "<", left, right
greater: (left, right) ->
@where.push new Condition ">", left, right
toCQL: ->
cql = [
"SELECT #{@columns.join ", "} FROM #{@table.name}"
if @where.length then "WHERE #{@where.join " AND "}" else ""
].join " "
bar = new Table "bar", [["user_id", "varchar"],["tweet_id", "varchar"]], ["user_id"]
b = new Batch("foo")
stmt = bar.select ["user_id", "tweet_id"]
stmt.select ["user_name", "user_description"]
stmt.select "tweet_text"
stmt.eql "user_id", 42
stmt.eql "user_name", "foo"
console.log stmt.toCQL()
console.log b.toCQL()
console.log bar.create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment