Class: SqlQueryExecutor::Query::SubQuery
- Inherits:
-
Object
- Object
- SqlQueryExecutor::Query::SubQuery
- 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
-
#binding_operator ⇒ Object
readonly
Returns the value of attribute binding_operator.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
Instance Method Summary collapse
- #execute!(collection, data = []) ⇒ Object
-
#initialize(query) ⇒ SubQuery
constructor
A new instance of SubQuery.
- #logic(is_hash = false) ⇒ Object
- #selector ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(query) ⇒ SubQuery
Returns a new instance of SubQuery.
10 11 12 13 14 |
# File 'lib/sql_query_executor/query/sub_query.rb', line 10 def initialize(query) @children = [] @query = query initialize_attributes end |
Instance Attribute Details
#binding_operator ⇒ Object (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 |
#children ⇒ Object (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 |
#kind ⇒ Object (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!(collection, data = []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sql_query_executor/query/sub_query.rb', line 16 def execute!(collection, data=[]) is_hash = collection.first.is_a?(Hash) method_eval = logic(is_hash) klass = collection.first.class klass.instance_eval do eval("define_method(:this_method_will_be_removed_in_a_little_while) { #{method_eval} }") end result = collection.select{ |object| object.this_method_will_be_removed_in_a_little_while } klass.class_eval { undef :this_method_will_be_removed_in_a_little_while } is_hash ? result.sort_by{ |hash| hash[:id] } : result.sort_by(&:id) end |
#logic(is_hash = false) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sql_query_executor/query/sub_query.rb', line 51 def logic(is_hash=false) string = '' @children.each do |child| if child.respond_to?(:binding_operator) && child.binding_operator operator = BINDING_OPERATORS.invert[child.binding_operator] string = "(#{string} #{operator} #{child.logic(is_hash)})" else string += child.logic(is_hash) end end string end |
#selector ⇒ Object
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_sql ⇒ Object
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 |