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.



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

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



32
33
34
35
# File 'lib/riddle/query/select.rb', line 32

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

#group_best(count) ⇒ Object



67
68
69
70
# File 'lib/riddle/query/select.rb', line 67

def group_best(count)
  @group_best = count
  self
end

#group_by(attribute) ⇒ Object



62
63
64
65
# File 'lib/riddle/query/select.rb', line 62

def group_by(attribute)
  @group_by = attribute
  self
end

#having(*conditions) ⇒ Object



72
73
74
75
# File 'lib/riddle/query/select.rb', line 72

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

#limit(limit) ⇒ Object



87
88
89
90
# File 'lib/riddle/query/select.rb', line 87

def limit(limit)
  @limit = limit
  self
end

#matching(match) ⇒ Object



37
38
39
40
# File 'lib/riddle/query/select.rb', line 37

def matching(match)
  @matching = match
  self
end

#offset(offset) ⇒ Object



92
93
94
95
# File 'lib/riddle/query/select.rb', line 92

def offset(offset)
  @offset = offset
  self
end

#order_by(order) ⇒ Object



77
78
79
80
# File 'lib/riddle/query/select.rb', line 77

def order_by(order)
  @order_by = order
  self
end

#order_within_group_by(order) ⇒ Object



82
83
84
85
# File 'lib/riddle/query/select.rb', line 82

def order_within_group_by(order)
  @order_within_group_by = order
  self
end

#prepend_values(*values) ⇒ Object



27
28
29
30
# File 'lib/riddle/query/select.rb', line 27

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

#to_sqlObject



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

def to_sql
  sql = StringIO.new String.new(""), "w"
  sql << "SELECT #{ extended_values } FROM #{ @indices.join(', ') }"
  sql << " WHERE #{ combined_wheres }" if wheres?
  sql << " #{group_prefix} #{escape_columns(@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.string
end

#values(*values) ⇒ Object



22
23
24
25
# File 'lib/riddle/query/select.rb', line 22

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

#where(filters = {}) ⇒ Object



42
43
44
45
# File 'lib/riddle/query/select.rb', line 42

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

#where_all(filters = {}) ⇒ Object



47
48
49
50
# File 'lib/riddle/query/select.rb', line 47

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

#where_not(filters = {}) ⇒ Object



52
53
54
55
# File 'lib/riddle/query/select.rb', line 52

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

#where_not_all(filters = {}) ⇒ Object



57
58
59
60
# File 'lib/riddle/query/select.rb', line 57

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

#with_options(options = {}) ⇒ Object



97
98
99
100
# File 'lib/riddle/query/select.rb', line 97

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