Class: BinaryOperation

Inherits:
Expr
  • Object
show all
Defined in:
lib/lilit_sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Expr

#*, #+, #<, #<=, #>, #>=, #and, #asc, #desc, #eq, #in, #minus, #ne, #not, #plus

Constructor Details

#initialize(left, op, right) ⇒ BinaryOperation



110
111
112
113
114
# File 'lib/lilit_sql.rb', line 110

def initialize(left, op, right)
  @left = left
  @op = op
  @right = lit(right)
end

Instance Attribute Details

#leftObject

Returns the value of attribute left.



108
109
110
# File 'lib/lilit_sql.rb', line 108

def left
  @left
end

#opObject

Returns the value of attribute op.



108
109
110
# File 'lib/lilit_sql.rb', line 108

def op
  @op
end

#rightObject

Returns the value of attribute right.



108
109
110
# File 'lib/lilit_sql.rb', line 108

def right
  @right
end

Instance Method Details

#==(other) ⇒ Object



136
137
138
# File 'lib/lilit_sql.rb', line 136

def ==(other)
  other.class == self.class && other.state == state
end

#ref_sqlObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lilit_sql.rb', line 116

def ref_sql
  if @right.is_a?(Literal) && @right.value.nil?
    return (
      if @op == :'='
        "#{@left.ref_sql} is #{@right.ref_sql}"
      elsif @op == :!=
        "#{@left.ref_sql} is not #{@right.ref_sql}"
      else
        raise ArgumentError.new("Nil doesn't support the operator: #{@op}")
      end
    )
  end

  if @op == :in
    return "#{@left.ref_sql} in (#{@right.map(&:ref_sql).join(', ')})"
  end

  "#{@left.ref_sql} #{@op} #{@right.ref_sql}"
end

#stateObject



140
141
142
# File 'lib/lilit_sql.rb', line 140

def state
  instance_variables.map { |variable| instance_variable_get variable }
end