Class: Rusql::Query
- Inherits:
-
Object
- Object
- Rusql::Query
- Defined in:
- lib/rusql/query.rb
Instance Method Summary collapse
- #duplicate ⇒ Object
- #from(t) ⇒ Object
- #group_by(c) ⇒ Object
-
#initialize(selectors) ⇒ Query
constructor
A new instance of Query.
- #join(join) ⇒ Object
- #limit(c) ⇒ Object
- #order_by(*orders) ⇒ Object
- #select(*selectors) ⇒ Object
- #to_s ⇒ Object
- #where(condition) ⇒ Object
Constructor Details
#initialize(selectors) ⇒ Query
Returns a new instance of Query.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/rusql/query.rb', line 3 def initialize(selectors) selectors.each do |selector| raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector) end @selectors = selectors @joins = [] @orders = [] @group_by = nil end |
Instance Method Details
#duplicate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rusql/query.rb', line 14 def duplicate new_one = Query.new(self.instance_variable_get(:@selectors)) new_one.instance_variable_set( :@condition, self.instance_variable_get(:@condition) ) new_one.instance_variable_set( :@from_table, self.instance_variable_get(:@from_table) ) new_one.instance_variable_set( :@group_by, self.instance_variable_get(:@group_by) ) new_one.instance_variable_set( :@joins, self.instance_variable_get(:@joins) ) new_one.instance_variable_set( :@limit, self.instance_variable_get(:@limit) ) new_one.instance_variable_set( :@orders, self.instance_variable_get(:@orders) ) new_one end |
#from(t) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/rusql/query.rb', line 46 def from(t) raise TypeException.new(Table, t.class) unless t.is_a?(Table) new_one = self.duplicate new_one.instance_variable_set(:@from_table, t) new_one end |
#group_by(c) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/rusql/query.rb', line 55 def group_by(c) raise TypeException.new(Column, c.class) unless c.is_a?(Column) new_one = self.duplicate new_one.instance_variable_set(:@group_by, c) new_one end |
#join(join) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rusql/query.rb', line 64 def join(join) raise TypeException.new(Join, join.class) unless join.is_a?(Join) new_one = self.duplicate joins = new_one.instance_variable_get(:@joins) joins << join new_one.instance_variable_set(:@joins, joins) new_one end |
#limit(c) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/rusql/query.rb', line 37 def limit(c) raise TypeException.new(Fixnum, c.class) unless c.is_a?(Fixnum) new_one = self.duplicate new_one.instance_variable_set(:@limit, c) new_one end |
#order_by(*orders) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rusql/query.rb', line 95 def order_by(*orders) orders.each do |o| raise TypeException.new(Order, o.class) unless o.is_a?(Order) end new_one = self.duplicate new_one.instance_variable_set(:@orders, orders) new_one end |
#select(*selectors) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rusql/query.rb', line 26 def select(*selectors) selectors.each do |selector| raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector) end new_one = self.duplicate new_one.instance_variable_set(:@selectors, selectors) new_one end |
#to_s ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rusql/query.rb', line 106 def to_s join_part = @joins.map{ |j| "\n#{j.to_s}" }.join where_part = "\nWHERE" order_part = "\nORDER BY #{ @orders.map(&:to_s).join(", ") }" group_by_part = "\nGROUP BY #{ @group_by&.to_s }" if @condition.is_a?(BasicCondition) where_part += " " where_part += @condition.to_s elsif @condition.is_a?(ComplexCondition) where_part += "\n " where_part += @condition.to_s end <<-EOS SELECT #{ @selectors.map{ |s| " #{s.to_s}" }.join(",\n") } FROM #{ @from_table.to_s_for_aliasing }#{ (@joins.length > 0) ? join_part : "" }#{ @condition.nil? ? "" : where_part }#{ @orders.length > 0 ? order_part : "" }#{ @group_by.nil? ? "" : group_by_part } EOS end |
#where(condition) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/rusql/query.rb', line 86 def where(condition) raise TypeException.new(Condition, condition.class) unless condition.is_a?(Condition) new_one = self.duplicate new_one.instance_variable_set(:@condition, condition) new_one end |