class Cassava::StatementBuilder

Constants

CLAUSE_ORDERING

Attributes

clauses[R]
executor[R]
table[R]

Public Class Methods

new(executor, clauses = {}) click to toggle source
# File lib/cassava/client.rb, line 74
def initialize(executor, clauses = {})
  @executor = executor
  @table = table
  @clauses = clauses
end

Public Instance Methods

allow_filtering() click to toggle source

Allow filtering for this query @return [StatementBuilder]

# File lib/cassava/client.rb, line 123
def allow_filtering
  add_clause('ALLOW FILTERING', :allow_filtering)
end
count() click to toggle source

Return the count of objects rather than the objects themselves @return [StatementBuilder]

# File lib/cassava/client.rb, line 142
def count
  add_clause(clauses[:main].count, :main)
end
delete(table, columns = nil) click to toggle source

@param table [Symbol] table to delete data from @param columns [Array<Symbol>] Columns to delete – defaults to all. @return [StatementBuilder]

# File lib/cassava/client.rb, line 104
def delete(table, columns = nil)
  add_clause(DeleteClause.new(table, columns), :main)
end
execute(opts = {}) click to toggle source

Execute the statement synchronously @param opts [Hash] options accepted by Cassandra::Session

# File lib/cassava/client.rb, line 82
def execute(opts = {})
  options = opts.dup.merge(:arguments => prepared_arguments)
  executor.execute(prepared_statement, options)
end
execute_async(opts = {}) click to toggle source

Execute the statement asynchronously @param opts [Hash] options accepted by Cassandra::Session

# File lib/cassava/client.rb, line 89
def execute_async(opts = {})
  options = opts.dup.merge(:arguments => prepared_arguments)
  executor.execute_async(prepared_statement, options)
end
limit(n) click to toggle source

@param n [Integer] maximum number of results to return @return [StatementBuilder]

# File lib/cassava/client.rb, line 136
def limit(n)
  add_clause("LIMIT #{n.to_i}", :limit)
end
order(clustering_column, direction = :asc) click to toggle source

@param clustering_column [Symbol] clustering_column to order by @param direction [:asc|:desc] the direction to order by, defaults to :asc @return [StatementBuilder]

# File lib/cassava/client.rb, line 130
def order(clustering_column, direction = :asc)
  add_clause("ORDER BY #{clustering_column.to_s} #{direction.to_s}", :order)
end
select(table, columns = nil) click to toggle source

@param table [Symbol] table to select data from @param columns [Array<Symbol>] Columns to select – defaults to all. @return [StatementBuilder]

# File lib/cassava/client.rb, line 97
def select(table, columns = nil)
  add_clause(SelectClause.new(table, columns), :main)
end
statement() click to toggle source

@return [String] the CQL statement that this StatementBuilder represents

# File lib/cassava/client.rb, line 147
def statement
  clauses.sort_by { |s| CLAUSE_ORDERING[s[0]] }.map { |s| s[1] }.join(' ')
end
where(*args) click to toggle source

Condition the query based on a condition Provide either a String and a list of arguments, or a hash. @example

statement.where('id = ? and field > ?', 1, 'a')

@example

statement.where(:id => 1, :field => 'x')

@param args [Array] arguments representing the where condition @return [StatementBuilder]

# File lib/cassava/client.rb, line 116
def where(*args)
  clause = clauses[:where] || WhereClause.new([], [])
  add_clause(clause.where(*args), :where)
end

Private Instance Methods

add_clause(clause, type) click to toggle source

Adds a clause of a given type. @return [StatementBuilder] A new StatementBuilder with the added clause

# File lib/cassava/client.rb, line 163
def add_clause(clause, type)
  clauses_copy = clauses.dup
  clauses_copy[type] = clause
  self.class.new(executor, clauses_copy)
end
prepared_arguments() click to toggle source
# File lib/cassava/client.rb, line 157
def prepared_arguments
  clauses[:where] ? clauses[:where].arguments : []
end
prepared_statement() click to toggle source
# File lib/cassava/client.rb, line 153
def prepared_statement
  executor.prepare(statement)
end