Class | Sequel::SQL::ComplexExpression |
In: |
lib/sequel/extensions/eval_inspect.rb
lib/sequel/sql.rb |
Parent: | Object |
Represents a complex SQL expression, with a given operator and one or more attributes (which may also be ComplexExpressions, forming a tree). This class is the backbone of Sequel‘s ruby expression DSL.
This is an abstract class that is not that useful by itself. The subclasses BooleanExpression, NumericExpression, and StringExpression define the behavior of the DSL via operators.
OPERTATOR_INVERSIONS | = | {:AND => :OR, :OR => :AND, :< => :>=, :> => :<=, :<= => :>, :>= => :<, :'=' => :'!=' , :'!=' => :'=', :LIKE => :'NOT LIKE', :'NOT LIKE' => :LIKE, :~ => :'!~', :'!~' => :~, :IN => :'NOT IN', :'NOT IN' => :IN, :IS => :'IS NOT', :'IS NOT' => :IS, :'~*' => :'!~*', :'!~*' => :'~*', :NOT => :NOOP, :NOOP => :NOT, :ILIKE => :'NOT ILIKE', :'NOT ILIKE'=>:ILIKE} | A hash of the opposite for each operator symbol, used for inverting objects. | |
MATHEMATICAL_OPERATORS | = | [:+, :-, :/, :*] | Standard mathematical operators used in NumericMethods | |
BITWISE_OPERATORS | = | [:&, :|, :^, :<<, :>>, :%] | Bitwise mathematical operators used in NumericMethods | |
EQUALITY_OPERATORS | = | [:'=', :'!='] | Operators that check for equality | |
INEQUALITY_OPERATORS | = | [:<, :>, :<=, :>=] | Inequality operators used in InequalityMethods | |
BOOLEAN_OPERATOR_METHODS | = | {:& => :AND, :| =>:OR} | Hash of ruby operator symbols to SQL operators, used in BooleanMethods | |
IN_OPERATORS | = | [:IN, :'NOT IN'] | Operators that use IN/NOT IN for inclusion/exclusion | |
IS_OPERATORS | = | [:IS, :'IS NOT'] | Operators that use IS, used for special casing to override literal true/false values | |
REGEXP_OPERATORS | = | [:~, :'!~', :'~*', :'!~*'] | Operators that do pattern matching via regular expressions | |
LIKE_OPERATORS | = | [:LIKE, :'NOT LIKE', :ILIKE, :'NOT ILIKE'] | Operators that do pattern matching via LIKE | |
TWO_ARITY_OPERATORS | = | EQUALITY_OPERATORS + INEQUALITY_OPERATORS + IS_OPERATORS + IN_OPERATORS + REGEXP_OPERATORS + LIKE_OPERATORS | Operator symbols that take exactly two arguments | |
N_ARITY_OPERATORS | = | [:AND, :OR, :'||'] + MATHEMATICAL_OPERATORS + BITWISE_OPERATORS | Operator symbols that take one or more arguments | |
ONE_ARITY_OPERATORS | = | [:NOT, :NOOP, :'B~'] | Operator symbols that take only a single argument | |
CUSTOM_EXPRESSIONS | = | [:extract] | Custom expressions that may have different syntax on different databases | |
CONSTANT_INVERSIONS | = | {Constants::TRUE=>Constants::FALSE, Constants::FALSE=>Constants::TRUE, Constants::NULL=>Constants::NOTNULL, Constants::NOTNULL=>Constants::NULL} | A hash of the opposite for each constant, used for inverting constants. |
args | [R] | An array of args for this object |
op | [R] | The operator symbol for this object |
Set the operator symbol and arguments for this object to the ones given. Convert all args that are hashes or arrays of two element arrays to BooleanExpressions, other than the second arg for an IN/NOT IN operator. Raise an Error if the operator doesn‘t allow boolean input and a boolean argument is given. Raise an Error if the wrong number of arguments for a given operator is used.
# File lib/sequel/sql.rb, line 202 202: def initialize(op, *args) 203: orig_args = args 204: args = args.map{|a| Sequel.condition_specifier?(a) ? SQL::BooleanExpression.from_value_pairs(a) : a} 205: case op 206: when *N_ARITY_OPERATORS 207: raise(Error, "The #{op} operator requires at least 1 argument") unless args.length >= 1 208: old_args = args 209: args = [] 210: old_args.each{|a| a.is_a?(self.class) && a.op == op ? args.concat(a.args) : args.push(a)} 211: when *TWO_ARITY_OPERATORS 212: raise(Error, "The #{op} operator requires precisely 2 arguments") unless args.length == 2 213: # With IN/NOT IN, even if the second argument is an array of two element arrays, 214: # don't convert it into a boolean expression, since it's definitely being used 215: # as a value list. 216: args[1] = orig_args[1] if IN_OPERATORS.include?(op) 217: when *ONE_ARITY_OPERATORS 218: raise(Error, "The #{op} operator requires a single argument") unless args.length == 1 219: when *CUSTOM_EXPRESSIONS 220: # nothing 221: else 222: raise(Error, "Invalid operator #{op}") 223: end 224: @op = op 225: @args = args 226: end
Return a BooleanExpression with the same op and args.
# File lib/sequel/sql.rb, line 1130 1130: def sql_boolean 1131: BooleanExpression.new(self.op, *self.args) 1132: end
Return a NumericExpression with the same op and args.
# File lib/sequel/sql.rb, line 1135 1135: def sql_number 1136: NumericExpression.new(self.op, *self.args) 1137: end
Return a StringExpression with the same op and args.
# File lib/sequel/sql.rb, line 1140 1140: def sql_string 1141: StringExpression.new(self.op, *self.args) 1142: end