Class: Prequel::Sql::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/prequel/sql/query.rb

Direct Known Subclasses

Subquery

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#conditionsObject (readonly)

Returns the value of attribute conditions.



5
6
7
# File 'lib/prequel/sql/query.rb', line 5

def conditions
  @conditions
end

#literalsObject (readonly)

Returns the value of attribute literals.



5
6
7
# File 'lib/prequel/sql/query.rb', line 5

def literals
  @literals
end

#query_columnsObject (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

#relationObject (readonly)

Returns the value of attribute relation.



5
6
7
# File 'lib/prequel/sql/query.rb', line 5

def relation
  @relation
end

#select_listObject

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_refsObject (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_countObject (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_refObject

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_builderObject



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

#allObject



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

#buildObject



36
37
38
39
# File 'lib/prequel/sql/query.rb', line 36

def build
  relation.visit(self)
  self
end

#firstObject



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_setObject



28
29
30
# File 'lib/prequel/sql/query.rb', line 28

def result_set
  DB[*to_sql]
end

#to_sqlObject



32
33
34
# File 'lib/prequel/sql/query.rb', line 32

def to_sql
  [sql_string, literals]
end