Class: Rusql::Query
- Inherits:
-
Object
- Object
- Rusql::Query
- Defined in:
- lib/rusql/query.rb
Instance Method Summary collapse
- #duplicate ⇒ Object
- #from(t) ⇒ 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 |
# 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 = [] end |
Instance Method Details
#duplicate ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rusql/query.rb', line 13 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( :@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
44 45 46 47 48 49 50 51 |
# File 'lib/rusql/query.rb', line 44 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 |
#join(join) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rusql/query.rb', line 53 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
35 36 37 38 39 40 41 42 |
# File 'lib/rusql/query.rb', line 35 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
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rusql/query.rb', line 84 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
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rusql/query.rb', line 24 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rusql/query.rb', line 95 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(", ") }" 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 : "" } EOS end |
#where(condition) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/rusql/query.rb', line 75 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 |