class Believer::Query

Constants

DEFAULT_FILTER_LIMIT

Attributes

filtering_allowed[RW]
filtering_allowed?[RW]
limit_to[RW]
order_by[RW]
record_class[RW]
selects[RW]

Public Class Methods

new(attrs) click to toggle source
Calls superclass method Believer::Command::new
# File lib/believer/query.rb, line 17
def initialize(attrs)
  super
end

Public Instance Methods

allow_filtering(b = true) click to toggle source
# File lib/believer/query.rb, line 80
def allow_filtering(b = true)
  q = clone
  q.filtering_allowed = b
  q
end
limit(l) click to toggle source
# File lib/believer/query.rb, line 66
def limit(l)
  q = clone
  q.limit_to = Limit.new(l)
  q
end
limit_to=(l) click to toggle source
# File lib/believer/query.rb, line 72
def limit_to=(l)
  if l.nil?
    @limit_to = nil
  else
    @limit_to = Limit.new(l.to_i)
  end
end
order(field, order = :asc) click to toggle source
# 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) click to toggle source
# 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_attributes() click to toggle source
# 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) click to toggle source
# File lib/believer/query.rb, line 52
def select(*fields)
  q = clone
  q.selects ||= []
  q.selects += fields
  q.selects.flatten!
  q
end
to_a() click to toggle source
# 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_cql() click to toggle source
# 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

Protected Instance Methods

loaded_objects() click to toggle source
# File lib/believer/query.rb, line 122
def loaded_objects
  @loaded_objects
end