Class: Riddle::Query::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/riddle/query/select.rb

Instance Method Summary collapse

Constructor Details

#initializeSelect

Returns a new instance of Select.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/riddle/query/select.rb', line 2

def initialize
  @values                = []
  @indices               = []
  @matching              = nil
  @wheres                = {}
  @where_alls            = {}
  @where_nots            = {}
  @where_not_alls        = {}
  @group_by              = nil
  @group_best            = nil
  @having                = []
  @order_by              = nil
  @order_within_group_by = nil
  @offset                = nil
  @limit                 = nil
  @options               = {}
end

Instance Method Details

#from(*indices) ⇒ Object



30
31
32
33
# File 'lib/riddle/query/select.rb', line 30

def from(*indices)
  @indices += indices
  self
end

#group_best(count) ⇒ Object



65
66
67
68
# File 'lib/riddle/query/select.rb', line 65

def group_best(count)
  @group_best = count
  self
end

#group_by(attribute) ⇒ Object



60
61
62
63
# File 'lib/riddle/query/select.rb', line 60

def group_by(attribute)
  @group_by = attribute
  self
end

#having(*conditions) ⇒ Object



70
71
72
73
# File 'lib/riddle/query/select.rb', line 70

def having(*conditions)
  @having += conditions
  self
end

#limit(limit) ⇒ Object



85
86
87
88
# File 'lib/riddle/query/select.rb', line 85

def limit(limit)
  @limit = limit
  self
end

#matching(match) ⇒ Object



35
36
37
38
# File 'lib/riddle/query/select.rb', line 35

def matching(match)
  @matching = match
  self
end

#offset(offset) ⇒ Object



90
91
92
93
# File 'lib/riddle/query/select.rb', line 90

def offset(offset)
  @offset = offset
  self
end

#order_by(order) ⇒ Object



75
76
77
78
# File 'lib/riddle/query/select.rb', line 75

def order_by(order)
  @order_by = order
  self
end

#order_within_group_by(order) ⇒ Object



80
81
82
83
# File 'lib/riddle/query/select.rb', line 80

def order_within_group_by(order)
  @order_within_group_by = order
  self
end

#prepend_values(*values) ⇒ Object



25
26
27
28
# File 'lib/riddle/query/select.rb', line 25

def prepend_values(*values)
  @values.insert 0, *values
  self
end

#to_sqlObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/riddle/query/select.rb', line 100

def to_sql
  sql = "SELECT #{ extended_values } FROM #{ @indices.join(', ') }"
  sql << " WHERE #{ combined_wheres }" if wheres?
  sql << " #{group_prefix} #{escape_column(@group_by)}" if !@group_by.nil?
  unless @order_within_group_by.nil?
    sql << " WITHIN GROUP ORDER BY #{escape_columns(@order_within_group_by)}"
  end
  sql << " HAVING #{@having.join(' AND ')}" unless @having.empty?
  sql << " ORDER BY #{escape_columns(@order_by)}" if !@order_by.nil?
  sql << " #{limit_clause}"   unless @limit.nil? && @offset.nil?
  sql << " #{options_clause}" unless @options.empty?

  sql
end

#values(*values) ⇒ Object



20
21
22
23
# File 'lib/riddle/query/select.rb', line 20

def values(*values)
  @values += values
  self
end

#where(filters = {}) ⇒ Object



40
41
42
43
# File 'lib/riddle/query/select.rb', line 40

def where(filters = {})
  @wheres.merge!(filters)
  self
end

#where_all(filters = {}) ⇒ Object



45
46
47
48
# File 'lib/riddle/query/select.rb', line 45

def where_all(filters = {})
  @where_alls.merge!(filters)
  self
end

#where_not(filters = {}) ⇒ Object



50
51
52
53
# File 'lib/riddle/query/select.rb', line 50

def where_not(filters = {})
  @where_nots.merge!(filters)
  self
end

#where_not_all(filters = {}) ⇒ Object



55
56
57
58
# File 'lib/riddle/query/select.rb', line 55

def where_not_all(filters = {})
  @where_not_alls.merge!(filters)
  self
end

#with_options(options = {}) ⇒ Object



95
96
97
98
# File 'lib/riddle/query/select.rb', line 95

def with_options(options = {})
  @options.merge! options
  self
end