Class: Cassava::StatementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cassava/client.rb

Constant Summary collapse

CLAUSE_ORDERING =
{
 :main => 0,
 :from => 1,
 :where => 2,
 :order => 3,
 :limit => 4,
 :allow_filtering => 5
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executor, clauses = {}) ⇒ StatementBuilder

Returns a new instance of StatementBuilder.



74
75
76
77
78
# File 'lib/cassava/client.rb', line 74

def initialize(executor, clauses = {})
  @executor = executor
  @table = table
  @clauses = clauses
end

Instance Attribute Details

#clausesObject (readonly)

Returns the value of attribute clauses.



63
64
65
# File 'lib/cassava/client.rb', line 63

def clauses
  @clauses
end

#executorObject (readonly)

Returns the value of attribute executor.



63
64
65
# File 'lib/cassava/client.rb', line 63

def executor
  @executor
end

#tableObject (readonly)

Returns the value of attribute table.



63
64
65
# File 'lib/cassava/client.rb', line 63

def table
  @table
end

Instance Method Details

#allow_filteringStatementBuilder

Allow filtering for this query

Returns:



123
124
125
# File 'lib/cassava/client.rb', line 123

def allow_filtering
  add_clause('ALLOW FILTERING', :allow_filtering)
end

#countStatementBuilder

Return the count of objects rather than the objects themselves

Returns:



142
143
144
# File 'lib/cassava/client.rb', line 142

def count
  add_clause(clauses[:main].count, :main)
end

#delete(table, columns = nil) ⇒ StatementBuilder

Parameters:

  • table (Symbol)

    table to delete data from

  • columns (Array<Symbol>) (defaults to: nil)

    Columns to delete – defaults to all.

Returns:



104
105
106
# File 'lib/cassava/client.rb', line 104

def delete(table, columns = nil)
  add_clause(DeleteClause.new(table, columns), :main)
end

#execute(opts = {}) ⇒ Object

Execute the statement synchronously

Parameters:

  • opts (Hash) (defaults to: {})

    options accepted by Cassandra::Session



82
83
84
85
# 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 = {}) ⇒ Object

Execute the statement asynchronously

Parameters:

  • opts (Hash) (defaults to: {})

    options accepted by Cassandra::Session



89
90
91
92
# 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) ⇒ StatementBuilder

Parameters:

  • n (Integer)

    maximum number of results to return

Returns:



136
137
138
# File 'lib/cassava/client.rb', line 136

def limit(n)
  add_clause("LIMIT #{n.to_i}", :limit)
end

#order(clustering_column, direction = :asc) ⇒ StatementBuilder

Parameters:

  • clustering_column (Symbol)

    clustering_column to order by

  • direction (:asc|:desc) (defaults to: :asc)

    the direction to order by, defaults to :asc

Returns:



130
131
132
# 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) ⇒ StatementBuilder

Parameters:

  • table (Symbol)

    table to select data from

  • columns (Array<Symbol>) (defaults to: nil)

    Columns to select – defaults to all.

Returns:



97
98
99
# File 'lib/cassava/client.rb', line 97

def select(table, columns = nil)
  add_clause(SelectClause.new(table, columns), :main)
end

#statementString

Returns the CQL statement that this StatementBuilder represents.

Returns:

  • (String)

    the CQL statement that this StatementBuilder represents



147
148
149
# 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) ⇒ StatementBuilder

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

Examples:

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

Parameters:

  • args (Array)

    arguments representing the where condition

Returns:



116
117
118
119
# File 'lib/cassava/client.rb', line 116

def where(*args)
  clause = clauses[:where] || WhereClause.new([], [])
  add_clause(clause.where(*args), :where)
end