Class: Believer::Query

Inherits:
FilterCommand show all
Includes:
ActAsEnumerable, Extending
Defined in:
lib/believer/query.rb

Direct Known Subclasses

EmptyResult, Relation::Collection

Constant Summary collapse

DEFAULT_FILTER_LIMIT =
10000

Instance Attribute Summary collapse

Attributes inherited from Command

#consistency_level, #execution_options

Instance Method Summary collapse

Methods included from ActAsEnumerable

#any?, #count, #exists?, #first, included, #last

Methods included from Extending

#apply_modules, #extending

Methods inherited from FilterCommand

#where, #wheres, #wheres=

Methods inherited from Command

#can_execute?, #clone, #command_name, #consistency, #execute

Constructor Details

#initialize(attrs) ⇒ Query



15
16
17
# File 'lib/believer/query.rb', line 15

def initialize(attrs)
  super
end

Instance Attribute Details

#filtering_allowedObject Also known as: filtering_allowed?

Returns the value of attribute filtering_allowed.



10
11
12
# File 'lib/believer/query.rb', line 10

def filtering_allowed
  @filtering_allowed
end

#limit_toObject

Returns the value of attribute limit_to.



10
11
12
# File 'lib/believer/query.rb', line 10

def limit_to
  @limit_to
end

#order_byObject

Returns the value of attribute order_by.



10
11
12
# File 'lib/believer/query.rb', line 10

def order_by
  @order_by
end

#record_classObject

Returns the value of attribute record_class.



10
11
12
# File 'lib/believer/query.rb', line 10

def record_class
  @record_class
end

#selectsObject

Returns the value of attribute selects.



10
11
12
# File 'lib/believer/query.rb', line 10

def selects
  @selects
end

Instance Method Details

#allow_filtering(b = true) ⇒ Object



78
79
80
81
82
# File 'lib/believer/query.rb', line 78

def allow_filtering(b = true)
  q = clone
  q.filtering_allowed = b
  q
end

#delete_allObject



110
111
112
113
114
# File 'lib/believer/query.rb', line 110

def delete_all
  del = Delete.new(:record_class => self.record_class)
  del.wheres = self.wheres.dup
  del.execute
end

#delete_all_chunked(options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/believer/query.rb', line 116

def delete_all_chunked(options = {})
  cnt = count
  chunk_size = options[:delete_batch_chunk_size] || (self.limit_to.nil? ? nil : self.limit_to.size) || cnt
  key_cols = self.record_class.primary_key_columns
  deleted_count = 0
  while deleted_count < cnt
    batch = Batch.new(:record_class => @record_class)
    rows_to_delete = clone.select(key_cols).limit(chunk_size).execute
    rows_to_delete.each do |row_to_delete|
      batch << Delete.new(:record_class => self.record_class).where(row_to_delete)
    end
    batch.execute
    deleted_count += batch.commands.size
  end
  deleted_count
end

#destroy_allObject



102
103
104
105
106
107
108
# File 'lib/believer/query.rb', line 102

def destroy_all
  objects = to_a
  objects.each do |obj|
    obj.destroy
  end
  objects.size
end

#limit(l) ⇒ Object



64
65
66
67
68
# File 'lib/believer/query.rb', line 64

def limit(l)
  q = clone
  q.limit_to = Limit.new(l)
  q
end

#order(field, order = :asc) ⇒ Object



58
59
60
61
62
# File 'lib/believer/query.rb', line 58

def order(field, order = :asc)
  q = clone
  q.order_by = OrderBy.new(field, order)
  q
end

#pluck(*fields) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/believer/query.rb', line 24

def pluck(*fields)
  fields.each do |f|
    raise "Unknown field #{f} for class #{record_class}" unless record_class.columns.has_key?(f)
  end
  q = clone
  q.selects = fields
  rows = q.execute
  pluck_res = []
  rows.each do |r|
    if fields.size > 1
      pluck_res << fields.map {|f|
        val = r[f.to_s]
        col = record_class.columns[f]
        val = (col.apply_cql_result_row_conversion? ? col.convert_to_type(val) : val)
        val
      }
    else
      f = fields[0]
      val = r[f.to_s]
      col = record_class.columns[f]
      pluck_res << (col.apply_cql_result_row_conversion? ? col.convert_to_type(val) : val)
    end
  end
  pluck_res
end

#query_attributesObject



19
20
21
22
# File 'lib/believer/query.rb', line 19

def query_attributes
  attrs = super
  attrs.merge(:order_by => @order_by, :selects => @selects, :limit_to => @limit_to, :filtering_allowed => filtering_allowed)
end

#select(*fields) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/believer/query.rb', line 50

def select(*fields)
  q = clone
  q.selects ||= []
  q.selects += fields
  q.selects.flatten!
  q
end

#to_aObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/believer/query.rb', line 133

def to_a
  if @loaded_objects.nil?
    result = execute
    notify_payload = {:class => @record_class}
    @loaded_objects = ActiveSupport::Notifications.instrument('deserialize.believer', notify_payload) do
      objects = []
      result.each do |row|
        objects << record_class.instantiate_from_result_rows(row)
      end
      notify_payload[:count] = objects.count
      objects
    end
  end
  @loaded_objects
end

#to_cqlObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/believer/query.rb', line 84

def to_cql
  cql = "SELECT "
  if @selects && @selects.any?
    cql << "#{@selects.join(', ')}"
  else
    #@record_class.environment.believer_configuration[:expand_columns]
    cql << @record_class.columns.keys.join(',')
    #cql << "*"
  end

  cql << " FROM #{@record_class.table_name}"
  cql << " WHERE #{wheres.map { |wc| "#{wc.to_cql}" }.join(' AND ')}" if wheres.any?
  cql << " #{order_by.to_cql}" unless order_by.nil?
  cql << " #{limit_to.to_cql}" unless limit_to.nil?
  cql << " ALLOW FILTERING" if filtering_allowed?
  cql
end