Class: CassandraObject::Adapters::CassandraAdapter::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_object/adapters/cassandra_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter, scope) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



10
11
12
13
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 10

def initialize(adapter, scope)
  @adapter = adapter
  @scope = scope
end

Instance Method Details

#select_stringObject



15
16
17
18
19
20
21
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 15

def select_string
  if @scope.select_values.any?
    (['KEY'] | @scope.select_values) * ','
  else
    '*'
  end
end

#to_query_asyncObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 23

def to_query_async
  # empty ids
  if @scope.id_values.empty?
    str = [
        "SELECT #{select_string} FROM #{@scope.klass.column_family}",
        where_string_async(nil)
    ]
    str << "ALLOW FILTERING" if @scope.klass.allow_filtering
    return [] << str.delete_if(&:blank?) * ' '
  end
  @scope.id_values.map do |id|
    str = [
        "SELECT #{select_string} FROM #{@scope.klass.column_family}",
        where_string_async(id)
    ]
    str << "ALLOW FILTERING" if @scope.klass.allow_filtering
    str.delete_if(&:blank?) * ' '
  end
end

#where_string_async(id) ⇒ Object



43
44
45
46
47
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 43

def where_string_async(id)
  wheres = @scope.where_values.dup.select.each_with_index { |_, i| i.even? }
  wheres << "#{@scope._key} = '#{id}'" if !id.nil?
  "WHERE #{wheres * ' AND '}" if wheres.any?
end