class Rubyq::QueryBuilder
Public Class Methods
new()
click to toggle source
# File lib/rubyq.rb, line 5 def initialize reset end
Public Instance Methods
and_where(condition)
click to toggle source
# File lib/rubyq.rb, line 59 def and_where(condition) @parts[:where].push({ condition: condition, operator: 'AND' }) self end
delete(name)
click to toggle source
# File lib/rubyq.rb, line 25 def delete(name) @type = :delete @parts[:from] = name self end
distinct(flag = true)
click to toggle source
# File lib/rubyq.rb, line 33 def distinct(flag = true) @parts[:distinct] = flag self end
from(name)
click to toggle source
# File lib/rubyq.rb, line 39 def from(name) raise 'This method only for SELECT' unless @type == :select @parts[:from].push(name) self end
get_query()
click to toggle source
# File lib/rubyq.rb, line 83 def get_query query = case @type when :select then build_query_for_select when :update then build_query_for_update when :delete then build_query_for_delete end reset query end
group_by(field)
click to toggle source
# File lib/rubyq.rb, line 77 def group_by(field) @parts[:group_by].push(field) self end
or_where(condition)
click to toggle source
# File lib/rubyq.rb, line 65 def or_where(condition) @parts[:where].push({ condition: condition, operator: 'OR' }) self end
order_by(field, sort = nil)
click to toggle source
# File lib/rubyq.rb, line 71 def order_by(field, sort = nil) @parts[:order_by].push({ field: field, sort: sort }) self end
select(fields)
click to toggle source
# File lib/rubyq.rb, line 9 def select(fields) @type = :select @parts[:select] = [*fields] self end
set(field, value)
click to toggle source
# File lib/rubyq.rb, line 47 def set(field, value) @parts[:set].push({ field: field, value: value }) self end
update(name)
click to toggle source
# File lib/rubyq.rb, line 17 def update(name) @type = :update @parts[:from] = name self end
where(condition)
click to toggle source
# File lib/rubyq.rb, line 53 def where(condition) @parts[:where] = [ { condition: condition, operator: 'AND' } ] self end
Private Instance Methods
build_query_for_delete()
click to toggle source
# File lib/rubyq.rb, line 156 def build_query_for_delete query = "DELETE #{@parts[:from]}" if @parts[:where].any? query << " WHERE#{build_where}" end if @parts[:order_by].any? query << " ORDER BY " query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ') end query end
build_query_for_select()
click to toggle source
# File lib/rubyq.rb, line 114 def build_query_for_select query = "SELECT#{@parts[:distinct] == true ? ' DISTINCT ' : ' '}#{@parts[:select].empty? ? '*' : @parts[:select].join(', ')}" if @parts[:from].any? query << " FROM #{@parts[:from].join(', ')}" end if @parts[:where].any? query << " WHERE#{build_where}" end if @parts[:group_by].any? query << " GROUP BY #{@parts[:group_by].join(', ')}" end if @parts[:order_by].any? query << " ORDER BY " query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ') end query end
build_query_for_update()
click to toggle source
# File lib/rubyq.rb, line 137 def build_query_for_update query = "UPDATE #{@parts[:from]}" if @parts[:set].any? query << " SET #{@parts[:set].map { |set| "#{set[:field]} = '#{set[:value]}'" }.join(', ')}" end if @parts[:where].any? query << " WHERE#{build_where}" end if @parts[:order_by].any? query << " ORDER BY " query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ') end query end
build_where()
click to toggle source
# File lib/rubyq.rb, line 171 def build_where whereStatement = '' @parts[:where].each do |where| if !whereStatement.empty? whereStatement << " #{where[:operator]}" end whereStatement << " #{where[:condition]}" end whereStatement end
reset()
click to toggle source
# File lib/rubyq.rb, line 98 def reset @type = :select @parts = { select: [], distinct: false, from: [], set: [], where: [], group_by: [], order_by: [], limit: nil, offset: nil, } end