Class: Babik::QuerySet::AbstractBase

Inherits:
Object
  • Object
show all
Includes:
Aggregatable, Bounded, Clonable, Countable, Deletable, Distinguishable, Filterable, Limitable, Lockable, NoneQuerySet, Projectable, RelatedSelector, SQLRenderizable, SetOperations, Sortable, Updatable, Enumerable
Defined in:
lib/babik/queryset.rb

Overview

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

Direct Known Subclasses

Base, SetOperation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Updatable

#update

Methods included from Sortable

#disorder!, #invert_order!, #order!, #order_by!, #ordered?

Methods included from SetOperations

#difference, #intersection, #union

Methods included from RelatedSelector

#select_related!

Methods included from SQLRenderizable

#sql

Methods included from Projectable

#project!, #projection?, #unproject!

Methods included from Lockable

#for_update!, #lock!, #lock?

Methods included from Limitable

#[], #exists?, #fetch, #limit!, #limit?, #unlimit!

Methods included from Filterable

#exclude!, #filter!, #get

Methods included from Distinguishable

#distinct!, #undistinct!

Methods included from NoneQuerySet

#none

Methods included from Deletable

#delete

Methods included from Countable

#count, #empty?, #exists?, #length, #size

Methods included from Clonable

#clone, #method_missing, #mutate_clone, #respond_to_missing?

Methods included from Bounded

#earliest, #first, #last, #latest

Methods included from Aggregatable

#aggregate

Constructor Details

#initialize(model_class) ⇒ AbstractBase

Returns a new instance of AbstractBase.



69
70
71
72
73
74
75
76
77
78
79
80
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Babik::QuerySet::Clonable

Instance Attribute Details

#_aggregationObject (readonly) Also known as: aggregation?

Returns the value of attribute _aggregation.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _aggregation
  @_aggregation
end

#_countObject (readonly) Also known as: count?

Returns the value of attribute _count.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _count
  @_count
end

#_distinctObject (readonly) Also known as: distinct?

Returns the value of attribute _distinct.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _distinct
  @_distinct
end

#_limitObject (readonly)

Returns the value of attribute _limit.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _limit
  @_limit
end

#_lock_typeObject (readonly)

Returns the value of attribute _lock_type.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _lock_type
  @_lock_type
end

#_orderObject (readonly)

Returns the value of attribute _order.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _order
  @_order
end

#_projectionObject (readonly)

Returns the value of attribute _projection.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _projection
  @_projection
end

Returns the value of attribute _select_related.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _select_related
  @_select_related
end

#_whereObject (readonly)

Returns the value of attribute _where.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def _where
  @_where
end

#modelObject (readonly)

Returns the value of attribute model.



58
59
60
# File 'lib/babik/queryset.rb', line 58

def model
  @model
end

Class Method Details

._execute_sql(sql) ⇒ Object

Execute SQL code

Parameters:

  • sql (String)

    SQL code

Returns:

  • SQL result set.



116
117
118
# File 'lib/babik/queryset.rb', line 116

def self._execute_sql(sql)
  ActiveRecord::Base.connection.exec_query(sql)
end

Instance Method Details

#allResultSet

Return a ResultSet with the ActiveRecord objects that match the condition given by the filters.

Returns:

  • (ResultSet)

    ActiveRecord objects that match the condition given by the filters.



84
85
86
87
88
89
# 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) ⇒ Object

Loop through the results with a block

Parameters:

  • block (Proc)

    Proc that will be applied to each object.



93
94
95
# File 'lib/babik/queryset.rb', line 93

def each(&block)
  self.all.each(&block)
end

#left_joins_by_aliasHash

Get the left joins grouped by alias in a hash.

Returns:

  • (Hash)

    Return a hash with the format :table_alias => SQL::Join



99
100
101
102
103
104
105
106
107
108
109
110
111
# 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