Class: Rusql::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/rusql/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selectors) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
15
16
17
18
# File 'lib/rusql/query.rb', line 10

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 Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



6
7
8
# File 'lib/rusql/query.rb', line 6

def condition
  @condition
end

#from_tableObject (readonly)

Returns the value of attribute from_table.



5
6
7
# File 'lib/rusql/query.rb', line 5

def from_table
  @from_table
end

#joinsObject (readonly)

Returns the value of attribute joins.



4
5
6
# File 'lib/rusql/query.rb', line 4

def joins
  @joins
end

#limit(c) ⇒ Object (readonly)

Returns the value of attribute limit.

Raises:



8
9
10
# File 'lib/rusql/query.rb', line 8

def limit
  @limit
end

#ordersObject (readonly)

Returns the value of attribute orders.



7
8
9
# File 'lib/rusql/query.rb', line 7

def orders
  @orders
end

#selectorsObject (readonly)

Returns the value of attribute selectors.



3
4
5
# File 'lib/rusql/query.rb', line 3

def selectors
  @selectors
end

Instance Method Details

#from(t) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/rusql/query.rb', line 38

def from(t)
  raise TypeException unless t.is_a?(Table)

  @from_table = t

  self
end

#inner_join(table, condition) ⇒ Object



46
47
48
49
50
# File 'lib/rusql/query.rb', line 46

def inner_join(table,condition)
  self.joins << Join.new(:inner_join, table, condition)

  self
end

#left_outer_join(table, condition) ⇒ Object



58
59
60
61
62
# File 'lib/rusql/query.rb', line 58

def left_outer_join(table,condition)
  self.joins << Join.new(:left_outer_join, table, condition)

  self
end

#order_by(*orders) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rusql/query.rb', line 78

def order_by(*orders)
  orders.each do |o|
    raise TypeException.new(Order, o.class) unless o.is_a?(Order)
  end

  @orders = orders

  self
end

#outer_join(table, condition) ⇒ Object



52
53
54
55
56
# File 'lib/rusql/query.rb', line 52

def outer_join(table,condition)
  self.joins << Join.new(:outer_join, table, condition)

  self
end

#right_outer_join(table, condition) ⇒ Object



64
65
66
67
68
# File 'lib/rusql/query.rb', line 64

def right_outer_join(table,condition)
  self.joins << Join.new(:right_outer_join, table, condition)

  self
end

#select(*selectors) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rusql/query.rb', line 20

def select(*selectors)
  selectors.each do |selector|
    raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector)
  end

  @selectors = selectors

  self
end

#to_sObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rusql/query.rb', line 88

def to_s
  join_part = self.joins.map{ |j| "\n#{j.to_s}" }.join
  where_part = "\nWHERE"
  order_part = "\nORDER BY #{ self.orders.map(&:to_s).join(", ") }"

  if self.condition.is_a?(BasicCondition)
    where_part += " "
    where_part += self.condition.to_s
  elsif self.condition.is_a?(ComplexCondition)
    where_part += "\n  "
    where_part += self.condition.to_s
  end

  <<-EOS
SELECT
#{ self.selectors.map{ |s| "  #{s.to_s}" }.join(",\n") }
FROM #{ self.from_table.to_s_for_aliasing }#{ (self.joins.length > 0) ? join_part : ""  }#{ self.condition.nil? ? "" : where_part }#{ self.orders.length > 0 ? order_part : "" }
  EOS
end

#where(condition) ⇒ Object

Raises:



70
71
72
73
74
75
76
# File 'lib/rusql/query.rb', line 70

def where(condition)
  raise TypeException.new(Condition, condition.class) unless condition.is_a?(Condition)

  @condition = condition

  self
end