class Sequel::SQL::ComplexExpression

Represents a 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.

Constants

ASSOCIATIVE_OPERATORS

Operator symbols that are associative

BITWISE_OPERATORS

Bitwise mathematical operators used in BitwiseMethods

BOOLEAN_OPERATOR_METHODS

Hash of ruby operator symbols to SQL operators, used in BooleanMethods

CONSTANT_INVERSIONS

A hash of the opposite for each constant, used for inverting constants.

CUSTOM_EXPRESSIONS

Custom expressions that may have different syntax on different databases

EQUALITY_OPERATORS

Operators that check for equality

INEQUALITY_OPERATORS

Inequality operators used in InequalityMethods

IN_OPERATORS

Operators that use IN/NOT IN for inclusion/exclusion

IS_OPERATORS

Operators that use IS, used for special casing to override literal true/false values

LIKE_OPERATORS

Operators that do pattern matching via LIKE

MATHEMATICAL_OPERATORS

Standard mathematical operators used in NumericMethods

N_ARITY_OPERATORS

Operator symbols that take one or more arguments

ONE_ARITY_OPERATORS

Operator symbols that take only a single argument

OPERTATOR_INVERSIONS

A hash of the opposite for each operator symbol, used for inverting objects.

REGEXP_OPERATORS

Operators that do pattern matching via regular expressions

TWO_ARITY_OPERATORS

Operator symbols that take exactly two arguments

Attributes

args[R]

An array of args for this object

op[R]

The operator symbol for this object

Public Class Methods

new(op, *args) click to toggle source

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
216 def initialize(op, *args)
217   orig_args = args
218   args = args.map{|a| Sequel.condition_specifier?(a) ? SQL::BooleanExpression.from_value_pairs(a) : a}
219   case op
220   when *N_ARITY_OPERATORS
221     raise(Error, "The #{op} operator requires at least 1 argument") unless args.length >= 1
222     args.map!{|a| a.is_a?(self.class) && a.op == :NOOP ? a.args.first : a}
223     if ASSOCIATIVE_OPERATORS.include?(op)
224       old_args = args
225       args = []
226       old_args.each{|a| a.is_a?(self.class) && a.op == op ? args.concat(a.args) : args.push(a)}
227     end
228   when *TWO_ARITY_OPERATORS
229     raise(Error, "The #{op} operator requires precisely 2 arguments") unless args.length == 2
230     # With IN/NOT IN, even if the second argument is an array of two element arrays,
231     # don't convert it into a boolean expression, since it's definitely being used
232     # as a value list.
233     args[1] = orig_args[1] if IN_OPERATORS.include?(op)
234   when *ONE_ARITY_OPERATORS
235     raise(Error, "The #{op} operator requires a single argument") unless args.length == 1
236   when *CUSTOM_EXPRESSIONS
237     # nothing
238   else
239     raise(Error, "Invalid operator #{op}")
240   end
241   @op = op
242   @args = args.freeze
243   freeze
244 end

Public Instance Methods

sql_boolean() click to toggle source

Return a BooleanExpression with the same op and args.

     # File lib/sequel/sql.rb
1272 def sql_boolean
1273   BooleanExpression.new(op, *args)
1274 end
sql_number() click to toggle source

Return a NumericExpression with the same op and args.

     # File lib/sequel/sql.rb
1277 def sql_number
1278   NumericExpression.new(op, *args)
1279 end
sql_string() click to toggle source

Return a StringExpression with the same op and args.

     # File lib/sequel/sql.rb
1282 def sql_string
1283   StringExpression.new(op, *args)
1284 end

Private Instance Methods

inspect_args() click to toggle source

ComplexExpression's initializer uses a splat for the operator arguments.

   # File lib/sequel/extensions/eval_inspect.rb
97 def inspect_args
98   [:op, "*args"]
99 end