class Cassava::StatementBuilder
Constants
- CLAUSE_ORDERING
Attributes
Public Class Methods
# File lib/cassava/client.rb, line 74 def initialize(executor, clauses = {}) @executor = executor @table = table @clauses = clauses end
Public Instance Methods
Allow filtering for this query @return [StatementBuilder]
# File lib/cassava/client.rb, line 123 def allow_filtering add_clause('ALLOW FILTERING', :allow_filtering) end
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
@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 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 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
@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
@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
@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
@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
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
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
# File lib/cassava/client.rb, line 157 def prepared_arguments clauses[:where] ? clauses[:where].arguments : [] end
# File lib/cassava/client.rb, line 153 def prepared_statement executor.prepare(statement) end