class Riddle::Query::Select

Public Class Methods

new() click to toggle source
# 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
  @order_by              = nil
  @order_within_group_by = nil
  @offset                = nil
  @limit                 = nil
  @options               = {}
end

Public Instance Methods

from(*indices) click to toggle source
# File lib/riddle/query/select.rb, line 23
def from(*indices)
  @indices += indices
  self
end
group_by(attribute) click to toggle source
# File lib/riddle/query/select.rb, line 53
def group_by(attribute)
  @group_by = attribute
  self
end
limit(limit) click to toggle source
# File lib/riddle/query/select.rb, line 68
def limit(limit)
  @limit = limit
  self
end
matching(match) click to toggle source
# File lib/riddle/query/select.rb, line 28
def matching(match)
  @matching = match
  self
end
offset(offset) click to toggle source
# File lib/riddle/query/select.rb, line 73
def offset(offset)
  @offset = offset
  self
end
order_by(order) click to toggle source
# File lib/riddle/query/select.rb, line 58
def order_by(order)
  @order_by = order
  self
end
order_within_group_by(order) click to toggle source
# File lib/riddle/query/select.rb, line 63
def order_within_group_by(order)
  @order_within_group_by = order
  self
end
to_sql() click to toggle source
# File lib/riddle/query/select.rb, line 83
def to_sql
  sql = "SELECT #{ @values.join(', ') } FROM #{ @indices.join(', ') }"
  sql << " WHERE #{ combined_wheres }" if wheres?
  sql << " GROUP BY #{@group_by}"      if !@group_by.nil?
  sql << " ORDER BY #{@order_by}"      if !@order_by.nil?
  unless @order_within_group_by.nil?
    sql << " WITHIN GROUP ORDER BY #{@order_within_group_by}"
  end
  sql << " #{limit_clause}"   unless @limit.nil? && @offset.nil?
  sql << " #{options_clause}" unless @options.empty?

  sql
end
values(*values) click to toggle source
# File lib/riddle/query/select.rb, line 18
def values(*values)
  @values += values
  self
end
where(filters = {}) click to toggle source
# File lib/riddle/query/select.rb, line 33
def where(filters = {})
  @wheres.merge!(filters)
  self
end
where_all(filters = {}) click to toggle source
# File lib/riddle/query/select.rb, line 38
def where_all(filters = {})
  @where_alls.merge!(filters)
  self
end
where_not(filters = {}) click to toggle source
# File lib/riddle/query/select.rb, line 43
def where_not(filters = {})
  @where_nots.merge!(filters)
  self
end
where_not_all(filters = {}) click to toggle source
# File lib/riddle/query/select.rb, line 48
def where_not_all(filters = {})
  @where_not_alls.merge!(filters)
  self
end
with_options(options = {}) click to toggle source
# File lib/riddle/query/select.rb, line 78
def with_options(options = {})
  @options.merge! options
  self
end