class SQLConditional

This class represents an SQL conditional statement.

Author

Vasiliy Korol (vakorol@mail.ru)

Copyright

Vasiliy Korol © 2014

License

Distributes under terms of GPLv2

Attributes

caller[RW]

Public Class Methods

new( params = nil ) click to toggle source

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

Public Instance Methods

and() click to toggle source
# File lib/sqlconditional.rb, line 65
def and
    _addBasicCond( :AND, BasicCond::MIDDLE )
end
eq( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 73
def eq ( expr1, expr2 )
    _addBasicCond( :'=', BasicCond::MIDDLE, expr1, expr2 )
end
gt( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 81
def gt ( expr1, expr2 )
    _addBasicCond( :'>', BasicCond::MIDDLE, expr1, expr2 )
end
gte( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 89
def gte ( expr1, expr2 ) 
    _addBasicCond( :'>=', BasicCond::MIDDLE, expr1, expr2 )
end
in( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 105
def in ( expr1, expr2 ) 
    _addBasicCond( :'IN', BasicCond::MIDDLE, expr1, expr2 )
end
is( cond ) click to toggle source

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
is_not_null( expr ) click to toggle source
# File lib/sqlconditional.rb, line 101
def is_not_null ( expr )
    _addBasicCond( :'IS NOT NULL', BasicCond::RHS, SQLObject.get( expr ) )
end
is_null( expr ) click to toggle source
# File lib/sqlconditional.rb, line 97
def is_null ( expr )
    _addBasicCond( :'IS NULL', BasicCond::RHS, SQLObject.get( expr ) )
end
like( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 113
def like ( expr1, expr2 )
    _addBasicCond( :'LIKE', BasicCond::MIDDLE, expr1, expr2 )
end
lt( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 85
def lt ( expr1, expr2 )
    _addBasicCond( :'<', BasicCond::MIDDLE, expr1, expr2 )
end
lte( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 93
def lte ( expr1, expr2 ) 
    _addBasicCond( :'<=', BasicCond::MIDDLE, expr1, expr2 )
end
method_missing( method, *args ) click to toggle source

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
ne( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 77
def ne ( expr1, expr2 )
    _addBasicCond( :'!=', BasicCond::MIDDLE, expr1, expr2 )
end
not( *expr ) click to toggle source

Negates the following conditional statement.

# File lib/sqlconditional.rb, line 61
def not ( *expr )
    _addBasicCond( :NOT, BasicCond::LHS, *expr )
end
not_in( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 109
def not_in ( expr1, expr2 ) 
    _addBasicCond( :'NOT IN', BasicCond::MIDDLE, expr1, expr2 )
end
not_is( cond ) click to toggle source

Same as .is(), but negated

# File lib/sqlconditional.rb, line 54
def not_is ( cond )
    self.not( cond )
end
not_like( expr1, expr2 ) click to toggle source
# File lib/sqlconditional.rb, line 117
def not_like ( expr1, expr2 )
    _addBasicCond( :'NOT LIKE', BasicCond::MIDDLE, expr1, expr2 )
end
or() click to toggle source
# File lib/sqlconditional.rb, line 69
def or
    _addBasicCond( :OR, BasicCond::MIDDLE )
end
to_s() click to toggle source
# 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
Also aliased as: to_str
to_str()

Dirty hack to make .join work on an array of SQLObjects

Alias for: to_s

Private Instance Methods

_addBasicCond( operator, type, *expressions ) click to toggle source
# File lib/sqlconditional.rb, line 151
def _addBasicCond ( operator, type, *expressions )
    objects = SQLObject.get( *expressions )
    @list << BasicCond.new( operator, type, *objects )
    @string = nil
    return self
end