Class: SqlQueryExecutor::Query::SubQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_query_executor/query/sub_query.rb

Constant Summary collapse

BINDING_OPERATORS =
{ "or" => "+", "and" => "&" }
ADD_CHILDREN_METHODS =
{ sentence: :add_sentence_children, sub_query: :add_sub_query_children }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, collection) ⇒ SubQuery

Returns a new instance of SubQuery.



9
10
11
12
13
14
15
16
# File 'lib/sql_query_executor/query/sub_query.rb', line 9

def initialize(query, collection)
  @collection = collection
  @query      = query
  @children   = []
  set_binding_operator
  set_kind
  set_children
end

Instance Attribute Details

#binding_operatorObject (readonly)

Returns the value of attribute binding_operator.



7
8
9
# File 'lib/sql_query_executor/query/sub_query.rb', line 7

def binding_operator
  @binding_operator
end

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/sql_query_executor/query/sub_query.rb', line 7

def children
  @children
end

#kindObject (readonly)

Returns the value of attribute kind.



7
8
9
# File 'lib/sql_query_executor/query/sub_query.rb', line 7

def kind
  @kind
end

Instance Method Details

#execute!(data = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sql_query_executor/query/sub_query.rb', line 18

def execute!(data=[])
  return [] unless @children

  result = []

  @children.each do |child|
    result = child.execute!(result)
  end

  result = (data || []).send(@binding_operator, result) if @binding_operator

  result.sort_by(&:id)
end

#selectorObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sql_query_executor/query/sub_query.rb', line 32

def selector
  hash = {}

  @children.each do |child|
    if child.respond_to?(:binding_operator) && child.binding_operator
      operator = BINDING_OPERATORS.invert[child.binding_operator]
      hash = {"$#{operator}" => [hash,child.selector]}
    else
      hash.merge!(child.selector)
    end
  end

  hash
end

#to_sqlObject



47
48
49
# File 'lib/sql_query_executor/query/sub_query.rb', line 47

def to_sql
  SqlQueryExecutor::Query::Normalizers::QueryNormalizer.clean_query(@query)
end