Class: Sequel::SQL::ComplexExpression

Inherits:
Expression show all
Includes:
SpecificExpressionMethods
Defined in:
lib/sequel_core/sql.rb,
lib/sequel_core/sql.rb

Overview

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 the blockless filter support in Sequel.

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.

Constant Summary collapse

OPERTATOR_INVERSIONS =

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

{: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}
MATHEMATICAL_OPERATORS =

Mathematical Operators used in NumericMethods

[:+, :-, :/, :*]
BITWISE_OPERATORS =

Mathematical Operators used in NumericMethods

[:&, :|, :^, :<<, :>>]
INEQUALITY_OPERATORS =

Inequality Operators used in InequalityMethods

[:<, :>, :<=, :>=]
BOOLEAN_OPERATOR_METHODS =

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

{:& => :AND, :| =>:OR}
TWO_ARITY_OPERATORS =

Operator symbols that take exactly two arguments

[:'=', :'!=', :IS, :'IS NOT', :LIKE, :'NOT LIKE', \
:~, :'!~', :'~*', :'!~*', :IN, :'NOT IN', :ILIKE, :'NOT ILIKE'] + \
INEQUALITY_OPERATORS + BITWISE_OPERATORS
N_ARITY_OPERATORS =

Operator symbols that take one or more arguments

[:AND, :OR, :'||'] + MATHEMATICAL_OPERATORS
ONE_ARITY_OPERATORS =

Operator symbols that take one argument

[:NOT, :NOOP, :'B~']

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OrderMethods

#asc, #desc

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from AliasMethods

#as

Methods inherited from Expression

#lit

Constructor Details

#initialize(op, *args) ⇒ ComplexExpression

Set the operator symbol and arguments for this object to the ones given. Convert all args that are hashes or arrays with all two pairs to ComplexExpressions. 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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sequel_core/sql.rb', line 71

def initialize(op, *args)
  args.collect! do |a|
    case a
    when Hash
      a.sql_expr
    when Array
      a.all_two_pairs? ? a.sql_expr : a
    else
      a
    end
  end
  case op
  when *N_ARITY_OPERATORS
    raise(Error, "The #{op} operator requires at least 1 argument") unless args.length >= 1
  when *TWO_ARITY_OPERATORS
    raise(Error, "The #{op} operator requires precisely 2 arguments") unless args.length == 2
  when *ONE_ARITY_OPERATORS
    raise(Error, "The #{op} operator requires a single argument") unless args.length == 1
  else
    raise(Error, "Invalid operator #{op}")
  end
  @op = op
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

An array of args for this object



62
63
64
# File 'lib/sequel_core/sql.rb', line 62

def args
  @args
end

#opObject (readonly)

The operator symbol for this object



65
66
67
# File 'lib/sequel_core/sql.rb', line 65

def op
  @op
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns true if the receiver is the same expression as the the other expression.

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/sequel_core/sql.rb', line 104

def eql?( other )
  return other.is_a?( self.class ) &&
    @op.eql?( other.op ) &&
    @args.eql?( other.args )
end

#to_s(ds) ⇒ Object

Delegate the creation of the resulting SQL to the given dataset, since it may be database dependent.



98
99
100
# File 'lib/sequel_core/sql.rb', line 98

def to_s(ds)
  ds.complex_expression_sql(@op, @args)
end