Class: SQLConditional
Overview
This class represents an SQL conditional statement.
Author:: Vasiliy Korol (mailto:vakorol@mail.ru)
Copyright:: Vasiliy Korol (c) 2014
License:: Distributes under terms of GPLv2
Defined Under Namespace
Classes: BasicCond
Instance Attribute Summary collapse
Attributes inherited from SQLObject
#alias, #inline, #name, #separator
Instance Method Summary
collapse
-
#and ⇒ Object
-
#eq(expr1, expr2) ⇒ Object
-
#gt(expr1, expr2) ⇒ Object
-
#gte(expr1, expr2) ⇒ Object
-
#in(expr1, expr2) ⇒ Object
-
#initialize(params = nil) ⇒ SQLConditional
constructor
-
#is(cond) ⇒ Object
Adds another SQLConditional object to the conditions list of the current object.
-
#is_not_null(expr) ⇒ Object
-
#is_null(expr) ⇒ Object
-
#like(expr1, expr2) ⇒ Object
-
#lt(expr1, expr2) ⇒ Object
-
#lte(expr1, expr2) ⇒ Object
-
#method_missing(method, *args) ⇒ Object
Return control to the object stored in @caller.
-
#ne(expr1, expr2) ⇒ Object
-
#not(*expr) ⇒ Object
Negates the following conditional statement.
-
#not_in(expr1, expr2) ⇒ Object
-
#not_is(cond) ⇒ Object
Same as .is(), but negated.
-
#not_like(expr1, expr2) ⇒ Object
-
#or ⇒ Object
-
#to_s ⇒ Object
-
#to_str ⇒ Object
Dirty hack to make .join work on an array of SQLObjects.
Methods inherited from SQLObject
#_name, #_string, #_string=, get
Constructor Details
Class constructor. Accepts an optional parameter to set the @caller
attribute, which is used in method_missing magic method to return
control to the calling SQLConstructor object (see method_missing for
more info).
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/sqlconditional.rb', line 22
def initialize ( params = nil )
@dialect, @tidy, @separator = nil, false, " "
if params.is_a? Hash
@caller = params[ :caller ]
if @caller
@dialect = params[ :dialect ] || @caller.dialect
@tidy = params[ :tidy ] || @caller.tidy
@separator = @caller.exporter.separator if @caller.exporter
end
end
@list = [ ]
@objects = [ ]
@string = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Return control to the object stored in @caller.
This allows mixing the methods different classes, i.e.:
<tt>SQLConstructor.new.select(':a').from('tab').where.eq(':b',3).limit(5)</tt>
Here .where.eq() returns an SQLConditional object, but further usage
of the foreign method .limit() returns back to the SQLConstructor object.
143
144
145
146
|
# File 'lib/sqlconditional.rb', line 143
def method_missing ( method, *args )
return @caller.send( method.to_sym, *args ) if @caller
raise NoMethodError, ERR_UNKNOWN_METHOD + ": " + method.to_s
end
|
Instance Attribute Details
#caller ⇒ Object
Returns the value of attribute caller.
11
12
13
|
# File 'lib/sqlconditional.rb', line 11
def caller
@caller
end
|
Instance Method Details
#and ⇒ Object
65
66
67
|
# File 'lib/sqlconditional.rb', line 65
def and
_addBasicCond( :AND, BasicCond::MIDDLE )
end
|
#eq(expr1, expr2) ⇒ Object
73
74
75
|
# File 'lib/sqlconditional.rb', line 73
def eq ( expr1, expr2 )
_addBasicCond( :'=', BasicCond::MIDDLE, expr1, expr2 )
end
|
#gt(expr1, expr2) ⇒ Object
81
82
83
|
# File 'lib/sqlconditional.rb', line 81
def gt ( expr1, expr2 )
_addBasicCond( :'>', BasicCond::MIDDLE, expr1, expr2 )
end
|
#gte(expr1, expr2) ⇒ Object
89
90
91
|
# File 'lib/sqlconditional.rb', line 89
def gte ( expr1, expr2 )
_addBasicCond( :'>=', BasicCond::MIDDLE, expr1, expr2 )
end
|
#in(expr1, expr2) ⇒ Object
105
106
107
|
# File 'lib/sqlconditional.rb', line 105
def in ( expr1, expr2 )
_addBasicCond( :'IN', BasicCond::MIDDLE, expr1, expr2 )
end
|
#is(cond) ⇒ Object
Adds another SQLConditional object to the conditions list of the
current object. Example:
<tt>cond1 = SQLConditional.new.eq(':c1',3)</tt>
<tt>cond2 = SQLConditional.new.lt(':c2',5).and.is(cond2)</tt>
43
44
45
46
47
48
49
|
# File 'lib/sqlconditional.rb', line 43
def is ( cond )
raise SQLException, ERR_INVALID_CONDITIONAL if ! cond.is_a? SQLObject
cond.separator = @separator
@list << cond
@string = nil
return self
end
|
#is_not_null(expr) ⇒ Object
101
102
103
|
# File 'lib/sqlconditional.rb', line 101
def is_not_null ( expr )
_addBasicCond( :'IS NOT NULL', BasicCond::RHS, SQLObject.get( expr ) )
end
|
#is_null(expr) ⇒ Object
97
98
99
|
# File 'lib/sqlconditional.rb', line 97
def is_null ( expr )
_addBasicCond( :'IS NULL', BasicCond::RHS, SQLObject.get( expr ) )
end
|
#like(expr1, expr2) ⇒ Object
113
114
115
|
# File 'lib/sqlconditional.rb', line 113
def like ( expr1, expr2 )
_addBasicCond( :'LIKE', BasicCond::MIDDLE, expr1, expr2 )
end
|
#lt(expr1, expr2) ⇒ Object
85
86
87
|
# File 'lib/sqlconditional.rb', line 85
def lt ( expr1, expr2 )
_addBasicCond( :'<', BasicCond::MIDDLE, expr1, expr2 )
end
|
#lte(expr1, expr2) ⇒ Object
93
94
95
|
# File 'lib/sqlconditional.rb', line 93
def lte ( expr1, expr2 )
_addBasicCond( :'<=', BasicCond::MIDDLE, expr1, expr2 )
end
|
#ne(expr1, expr2) ⇒ Object
77
78
79
|
# File 'lib/sqlconditional.rb', line 77
def ne ( expr1, expr2 )
_addBasicCond( :'!=', BasicCond::MIDDLE, expr1, expr2 )
end
|
#not(*expr) ⇒ Object
Negates the following conditional statement.
61
62
63
|
# File 'lib/sqlconditional.rb', line 61
def not ( *expr )
_addBasicCond( :NOT, BasicCond::LHS, *expr )
end
|
#not_in(expr1, expr2) ⇒ Object
109
110
111
|
# File 'lib/sqlconditional.rb', line 109
def not_in ( expr1, expr2 )
_addBasicCond( :'NOT IN', BasicCond::MIDDLE, expr1, expr2 )
end
|
#not_is(cond) ⇒ Object
Same as .is(), but negated
54
55
56
|
# File 'lib/sqlconditional.rb', line 54
def not_is ( cond )
self.not( cond )
end
|
#not_like(expr1, expr2) ⇒ Object
117
118
119
|
# File 'lib/sqlconditional.rb', line 117
def not_like ( expr1, expr2 )
_addBasicCond( :'NOT LIKE', BasicCond::MIDDLE, expr1, expr2 )
end
|
#or ⇒ Object
69
70
71
|
# File 'lib/sqlconditional.rb', line 69
def or
_addBasicCond( :OR, BasicCond::MIDDLE )
end
|
#to_s ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/sqlconditional.rb', line 122
def to_s
return @string if @string
@string = @separator
@string += "("
@list.each do |item|
@string += item.to_s
end
@string += ")"
return @string
end
|
#to_str ⇒ Object
Dirty hack to make .join work on an array of SQLObjects
14
|
# File 'lib/sqlconditional.rb', line 14
alias :to_str :to_s
|