class Babik::QuerySet::AbstractBase

Abstract Base class for QuerySet, implements a container for database results.

Attributes

_aggregation[R]
_count[R]
_distinct[R]
_limit[R]
_lock_type[R]
_order[R]
_projection[R]
_where[R]
aggregation?[R]
count?[R]
distinct?[R]
model[R]

Public Class Methods

_execute_sql(sql) click to toggle source

Execute SQL code @param [String] sql SQL code @return SQL result set.

# File lib/babik/queryset.rb, line 116
def self._execute_sql(sql)
  ActiveRecord::Base.connection.exec_query(sql)
end
new(model_class) click to toggle source
# File lib/babik/queryset.rb, line 69
def initialize(model_class)
  @model = model_class
  @_count = false
  @_distinct = false
  @_order = nil
  @_lock_type = nil
  @_where = Babik::QuerySet::Where.new(@model)
  @_aggregation = nil
  @_limit = nil
  @_projection = nil
  @_select_related = nil
end

Public Instance Methods

all() click to toggle source

Return a ResultSet with the ActiveRecord objects that match the condition given by the filters. @return [ResultSet] ActiveRecord objects that match the condition given by the filters.

# File lib/babik/queryset.rb, line 84
def all
  sql_select = self.sql.select
  return @_projection.apply_transforms(self.class._execute_sql(sql_select)) if @_projection
  return @_select_related.all_with_related(self.class._execute_sql(sql_select)) if @_select_related
  @model.find_by_sql(sql_select)
end
each(&block) click to toggle source

Loop through the results with a block @param block [Proc] Proc that will be applied to each object.

# File lib/babik/queryset.rb, line 93
def each(&block)
  self.all.each(&block)
end
left_joins_by_alias() click to toggle source

Get the left joins grouped by alias in a hash. @return [Hash] Return a hash with the format :table_alias => SQL::Join

# File lib/babik/queryset.rb, line 99
def left_joins_by_alias
  left_joins_by_alias = {}
  # Merge where
  left_joins_by_alias.merge!(@_where.left_joins_by_alias)
  # Merge order
  left_joins_by_alias.merge!(@_order.left_joins_by_alias) if @_order
  # Merge aggregation
  left_joins_by_alias.merge!(@_aggregation.left_joins_by_alias) if @_aggregation
  # Merge prefetchs
  left_joins_by_alias.merge!(@_select_related.left_joins_by_alias) if @_select_related
  # Return the left joins by alias
  left_joins_by_alias
end