Class: Rusql::BasicCondition

Inherits:
Condition show all
Defined in:
lib/rusql/basic_condition.rb

Constant Summary collapse

TYPES =
i(
  equals
  greater_than
  greater_than_or_equals
  in
  less_than
  less_than_or_equals
  like
  not_equals
  not_in
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rightObject

Returns the value of attribute right.



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

def right
  @right
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#and(condition) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/rusql/basic_condition.rb', line 25

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

  c = ComplexCondition.new
  c.type = :and
  c.add_condition(self)
  c.add_condition(condition)

  c
end

#or(condition) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/rusql/basic_condition.rb', line 36

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

  c = ComplexCondition.new
  c.type = :or
  c.add_condition(self)
  c.add_condition(condition)

  c
end

#to_s(indent_level: 1, multiline: true) ⇒ Object

dummy params to make it compatible with complex condition to_s



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rusql/basic_condition.rb', line 47

def to_s(indent_level: 1, multiline: true) # dummy params to make it compatible with complex condition to_s
  case self.type
  when :equals
    if self.right.nil?
      "#{left.to_s} IS NULL" 
    else
      "#{left.to_s} = #{convert_value(self.right)}"
    end
  when :greater_than
    "#{left.to_s} > #{convert_value(self.right)}"
  when :greater_than_or_equals
    "#{left.to_s} >= #{convert_value(self.right)}"
  when :in
    "#{left.to_s} IN (#{ self.right.map{|v| convert_value(v) }.join(", ") })"
  when :less_than
    "#{left.to_s} < #{convert_value(self.right)}"
  when :less_than_or_equals
    "#{left.to_s} <= #{convert_value(self.right)}"
  when :like
    "#{left.to_s} LIKE #{convert_value(self.right)}"
  when :not_equals
    if self.right.nil?
      "#{left.to_s} IS NOT NULL"
    else
      "#{left.to_s} != #{convert_value(self.right)}"
    end
  when :not_in
    "#{left.to_s} NOT IN (#{ self.right.map{|v| convert_value(v) }.join(", ") })"
  end
end