Class: Believer::Query

Inherits:
FilterCommand show all
Includes:
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 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

Returns a new instance of Query.



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

def initialize(attrs)
  super
end

Instance Attribute Details

#filtering_allowedObject Also known as: filtering_allowed?

Returns the value of attribute filtering_allowed.



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

def filtering_allowed
  @filtering_allowed
end

#limit_toObject

Returns the value of attribute limit_to.



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

def limit_to
  @limit_to
end

#order_byObject

Returns the value of attribute order_by.



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

def order_by
  @order_by
end

#record_classObject

Returns the value of attribute record_class.



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

def record_class
  @record_class
end

#selectsObject

Returns the value of attribute selects.



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

def selects
  @selects
end

Instance Method Details

#allow_filtering(b = true) ⇒ Object



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

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

#countObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/believer/query.rb', line 150

def count
  return @loaded_objects.size unless @loaded_objects.nil?

  count_q = clone
  count_q.selects = ['COUNT(*)']
  result = count_q.execute

  cnt = -1
  result.each do |row|
    cnt = row['count']
  end
  cnt
end

#delete_allObject



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

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

#delete_all_chunked(options = {}) ⇒ Object



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

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



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

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

#exists?(*args) ⇒ Boolean

Tests if there are any records present which conform to the argument filter(s)

Parameters:

  • args (Object)

    a filter condition. This argument has the same usage as the where method

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/believer/query.rb', line 166

def exists?(*args)
  return count > 0 if args.nil? || args.size == 0
  where(*args).exists?
end

#firstObject



171
172
173
174
# File 'lib/believer/query.rb', line 171

def first
  return @loaded_objects.first unless @loaded_objects.nil?
  clone.limit(1)[0]
end

#lastObject



176
177
178
179
180
181
182
# File 'lib/believer/query.rb', line 176

def last
  return @loaded_objects.last unless @loaded_objects.nil?
  raise "Cannot retrieve last if no order column is set" if @order_by.nil?
  lq = clone.limit(1)
  lq.order_by = @order_by.inverse
  lq[0]
end

#limit(l) ⇒ Object



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

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

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



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

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

#pluck(*fields) ⇒ Object



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

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



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

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



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

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

#to_aObject



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

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



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

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