This class represents an SQL conditional statement.
Author |
Vasiliy Korol (vakorol@mail.ru) |
Copyright |
Vasiliy Korol © 2014 |
License |
Distributes under terms of GPLv2 |
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).
# 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
# File lib/sqlconditional.rb, line 65 def and _addBasicCond( :AND, BasicCond::MIDDLE ) end
# File lib/sqlconditional.rb, line 73 def eq ( expr1, expr2 ) _addBasicCond( :'=', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 81 def gt ( expr1, expr2 ) _addBasicCond( :'>', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 89 def gte ( expr1, expr2 ) _addBasicCond( :'>=', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 105 def in ( expr1, expr2 ) _addBasicCond( :'IN', BasicCond::MIDDLE, expr1, expr2 ) end
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>
# 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
# File lib/sqlconditional.rb, line 101 def is_not_null ( expr ) _addBasicCond( :'IS NOT NULL', BasicCond::RHS, SQLObject.get( expr ) ) end
# File lib/sqlconditional.rb, line 97 def is_null ( expr ) _addBasicCond( :'IS NULL', BasicCond::RHS, SQLObject.get( expr ) ) end
# File lib/sqlconditional.rb, line 113 def like ( expr1, expr2 ) _addBasicCond( :'LIKE', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 85 def lt ( expr1, expr2 ) _addBasicCond( :'<', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 93 def lte ( expr1, expr2 ) _addBasicCond( :'<=', BasicCond::MIDDLE, expr1, expr2 ) end
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.
# 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
# File lib/sqlconditional.rb, line 77 def ne ( expr1, expr2 ) _addBasicCond( :'!=', BasicCond::MIDDLE, expr1, expr2 ) end
Negates the following conditional statement.
# File lib/sqlconditional.rb, line 61 def not ( *expr ) _addBasicCond( :NOT, BasicCond::LHS, *expr ) end
# File lib/sqlconditional.rb, line 109 def not_in ( expr1, expr2 ) _addBasicCond( :'NOT IN', BasicCond::MIDDLE, expr1, expr2 ) end
Same as .is(), but negated
# File lib/sqlconditional.rb, line 54 def not_is ( cond ) self.not( cond ) end
# File lib/sqlconditional.rb, line 117 def not_like ( expr1, expr2 ) _addBasicCond( :'NOT LIKE', BasicCond::MIDDLE, expr1, expr2 ) end
# File lib/sqlconditional.rb, line 69 def or _addBasicCond( :OR, BasicCond::MIDDLE ) end
# 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
Dirty hack to make .join work on an array of SQLObjects
Generated with the Darkfish Rdoc Generator 2.