Class: Prequel::Sql::Query
- Inherits:
-
Object
- Object
- Prequel::Sql::Query
- Defined in:
- lib/prequel/sql/query.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#literals ⇒ Object
readonly
Returns the value of attribute literals.
-
#query_columns ⇒ Object
readonly
Returns the value of attribute query_columns.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#select_list ⇒ Object
Returns the value of attribute select_list.
-
#singular_table_refs ⇒ Object
readonly
Returns the value of attribute singular_table_refs.
-
#subquery_count ⇒ Object
readonly
Returns the value of attribute subquery_count.
-
#table_ref ⇒ Object
Returns the value of attribute table_ref.
- #tuple_builder ⇒ Object
Instance Method Summary collapse
- #add_condition(predicate) ⇒ Object
- #add_literal(literal) ⇒ Object
- #add_singular_table_ref(relation, table_ref) ⇒ Object
- #add_subquery(relation) ⇒ Object
- #all ⇒ Object
- #build ⇒ Object
- #first ⇒ Object
-
#initialize(relation) ⇒ Query
constructor
A new instance of Query.
- #resolve_derived_column(column, qualified = false) ⇒ Object
- #result_set ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(relation) ⇒ Query
Returns a new instance of Query.
8 9 10 11 12 13 14 15 |
# File 'lib/prequel/sql/query.rb', line 8 def initialize(relation) @relation = relation @conditions = [] @literals = {} @singular_table_refs = { relation => self } @subquery_count = 0 @query_columns = {} end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def conditions @conditions end |
#literals ⇒ Object (readonly)
Returns the value of attribute literals.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def literals @literals end |
#query_columns ⇒ Object (readonly)
Returns the value of attribute query_columns.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def query_columns @query_columns end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def relation @relation end |
#select_list ⇒ Object
Returns the value of attribute select_list.
4 5 6 |
# File 'lib/prequel/sql/query.rb', line 4 def select_list @select_list end |
#singular_table_refs ⇒ Object (readonly)
Returns the value of attribute singular_table_refs.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def singular_table_refs @singular_table_refs end |
#subquery_count ⇒ Object (readonly)
Returns the value of attribute subquery_count.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def subquery_count @subquery_count end |
#table_ref ⇒ Object
Returns the value of attribute table_ref.
5 6 7 |
# File 'lib/prequel/sql/query.rb', line 5 def table_ref @table_ref end |
#tuple_builder ⇒ Object
75 76 77 |
# File 'lib/prequel/sql/query.rb', line 75 def tuple_builder @tuple_builder || table_ref end |
Instance Method Details
#add_condition(predicate) ⇒ Object
46 47 48 |
# File 'lib/prequel/sql/query.rb', line 46 def add_condition(predicate) conditions.push(predicate) end |
#add_literal(literal) ⇒ Object
50 51 52 53 54 |
# File 'lib/prequel/sql/query.rb', line 50 def add_literal(literal) "v#{literals.size + 1}".to_sym.tap do |placeholder| literals[placeholder] = literal end end |
#add_singular_table_ref(relation, table_ref) ⇒ Object
56 57 58 |
# File 'lib/prequel/sql/query.rb', line 56 def add_singular_table_ref(relation, table_ref) singular_table_refs[relation] = table_ref end |
#add_subquery(relation) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/prequel/sql/query.rb', line 60 def add_subquery(relation) @subquery_count += 1 subquery = Subquery.new(self, relation, "t#{subquery_count}".to_sym) add_singular_table_ref(relation, subquery) subquery.build end |
#all ⇒ Object
17 18 19 20 21 |
# File 'lib/prequel/sql/query.rb', line 17 def all result_set.map do |field_values| tuple_builder.build_tuple(field_values) end end |
#build ⇒ Object
36 37 38 39 |
# File 'lib/prequel/sql/query.rb', line 36 def build relation.visit(self) self end |
#first ⇒ Object
23 24 25 26 |
# File 'lib/prequel/sql/query.rb', line 23 def first r = result_set r.empty?? nil : tuple_builder.build_tuple(r.first) end |
#resolve_derived_column(column, qualified = false) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/prequel/sql/query.rb', line 67 def resolve_derived_column(column, qualified=false) query_columns[column] ||= begin resolved_expression = column.expression.resolve_in_query(self) resolved_name = qualified ? resolved_expression.qualified_name : column.name Sql::DerivedQueryColumn.new(self, resolved_name, resolved_expression) end end |
#result_set ⇒ Object
28 29 30 |
# File 'lib/prequel/sql/query.rb', line 28 def result_set DB[*to_sql] end |
#to_sql ⇒ Object
32 33 34 |
# File 'lib/prequel/sql/query.rb', line 32 def to_sql [sql_string, literals] end |