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
# 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

  str = [
    "SELECT #{select_string} FROM #{@scope.klass.column_family}",
    where_string_async(@scope.id_values)
  ]
  str << 'ALLOW FILTERING' if @scope.klass.allow_filtering
  [str.delete_if(&:blank?) * ' ']
end

#where_string_async(ids) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 42

def where_string_async(ids)
  wheres = @scope.where_values.dup.select.each_with_index { |_, i| i.even? }
  if ids.present?
    wheres << if ids.size > 1
                "#{@scope._key} IN (#{ids.map { |id| "'#{id}'" }.join(',')})"
              else
                "#{@scope._key} = '#{ids.first}'"
              end
  end
  "WHERE #{wheres * ' AND '}" if wheres.any?
end