Class: Believer::Query

Inherits:
FilterCommand show all
Includes:
ActAsEnumerable, Extending, Purging, Updating
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

Instance Method Summary collapse

Methods included from ActAsEnumerable

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

Methods included from Purging

#delete_all, #delete_all_chunked, #destroy_all

Methods included from Updating

#update_all

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, #execution_options, #execution_options=, #override_execution_options

Constructor Details

#initialize(attrs) ⇒ Query

Returns a new instance of Query.



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

def initialize(attrs)
  super
end

Instance Attribute Details

#filtering_allowedObject Also known as: filtering_allowed?

Returns the value of attribute filtering_allowed.



12
13
14
# File 'lib/believer/query.rb', line 12

def filtering_allowed
  @filtering_allowed
end

#limit_toObject

Returns the value of attribute limit_to.



12
13
14
# File 'lib/believer/query.rb', line 12

def limit_to
  @limit_to
end

#order_byObject

Returns the value of attribute order_by.



12
13
14
# File 'lib/believer/query.rb', line 12

def order_by
  @order_by
end

#record_classObject

Returns the value of attribute record_class.



12
13
14
# File 'lib/believer/query.rb', line 12

def record_class
  @record_class
end

#selectsObject

Returns the value of attribute selects.



12
13
14
# File 'lib/believer/query.rb', line 12

def selects
  @selects
end

Instance Method Details

#allow_filtering(b = true) ⇒ Object



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

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

#limit(l) ⇒ Object



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

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

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



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

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

#pluck(*fields) ⇒ Object



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

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



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

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



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

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

#to_aObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/believer/query.rb', line 104

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



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

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