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

Private Instance Methods

combined_wheres() click to toggle source
# File lib/riddle/query/select.rb, line 103
def combined_wheres
  if @matching.nil?
    wheres_to_s
  elsif @wheres.empty? && @where_nots.empty? && @where_alls.empty? && @where_not_alls.empty?
    "MATCH('#{@matching}')"
  else
    "MATCH('#{@matching}') AND #{wheres_to_s}"
  end
end
exclusive_filter_comparison_and_value(attribute, value) click to toggle source
# File lib/riddle/query/select.rb, line 145
def exclusive_filter_comparison_and_value(attribute, value)
  case value
  when Array
    "#{attribute} NOT IN (#{value.collect { |val| filter_value(val) }.join(', ')})"
  when Range
    "#{attribute} < #{filter_value(value.first)} OR #{attribute} > #{filter_value(value.last)}"
  else
    "#{attribute} <> #{filter_value(value)}"
  end
end
filter_comparison_and_value(attribute, value) click to toggle source
# File lib/riddle/query/select.rb, line 134
def filter_comparison_and_value(attribute, value)
  case value
  when Array
    "#{attribute} IN (#{value.collect { |val| filter_value(val) }.join(', ')})"
  when Range
    "#{attribute} BETWEEN #{filter_value(value.first)} AND #{filter_value(value.last)}"
  else
    "#{attribute} = #{filter_value(value)}"
  end
end
filter_value(value) click to toggle source
# File lib/riddle/query/select.rb, line 156
def filter_value(value)
  case value
  when TrueClass
    1
  when FalseClass
    0
  when Time
    value.to_i
  else
    value
  end
end
limit_clause() click to toggle source
# File lib/riddle/query/select.rb, line 169
def limit_clause
  if @offset.nil?
    "LIMIT #{@limit}"
  else
    "LIMIT #{@offset}, #{@limit || 20}"
  end
end
option_value(value) click to toggle source
# File lib/riddle/query/select.rb, line 183
def option_value(value)
  case value
  when Hash
    '(' + value.collect { |key, value| "#{key}=#{value}" }.join(', ') + ')'
  else
    value
  end
end
options_clause() click to toggle source
# File lib/riddle/query/select.rb, line 177
def options_clause
  'OPTION ' + @options.keys.collect { |key|
    "#{key}=#{option_value @options[key]}"
  }.join(', ')
end
wheres?() click to toggle source
# File lib/riddle/query/select.rb, line 99
def wheres?
  !(@wheres.empty? && @where_alls.empty? && @where_nots.empty? && @where_not_alls.empty? && @matching.nil?)
end
wheres_to_s() click to toggle source
# File lib/riddle/query/select.rb, line 113
def wheres_to_s
  (
    @wheres.keys.collect { |key|
      filter_comparison_and_value key, @wheres[key]
    } +
    @where_alls.collect { |key, values|
      values.collect { |value|
        filter_comparison_and_value key, value
      }
    } +
    @where_nots.keys.collect { |key|
      exclusive_filter_comparison_and_value key, @where_nots[key]
    } +
    @where_not_alls.collect { |key, values|
      '(' + values.collect { |value|
        exclusive_filter_comparison_and_value key, value
      }.join(' OR ') + ')'
    }
  ).flatten.join(' AND ')
end