module Sequel::SQL::NumericMethods

  1. lib/sequel/sql.rb

This module includes the standard mathematical methods (+, -, *, and /) that are defined on objects that can be used in a numeric context in SQL (Symbol, LiteralString, and SQL::GenericExpression).

Sequel[:a] + :b # "a" + "b"
Sequel[:a] - :b # "a" - "b"
Sequel[:a] * :b # "a" * "b"
Sequel[:a] / :b # "a" / "b"

One exception to this is if + is called with a String or StringExpression, in which case the || operator is used instead of the + operator:

Sequel[:a] + 'b' # "a" || 'b'

Methods

Public Instance

  1. +
  2. coerce

Public Instance methods

+(ce)

Use || as the operator when called with StringExpression and String instances, and the + operator for LiteralStrings and all other types.

[show source]
    # File lib/sequel/sql.rb
800 def +(ce)
801   case ce
802   when LiteralString
803     NumericExpression.new(:+, self, ce)
804   when StringExpression, String
805     StringExpression.new(:'||', self, ce)
806   else
807     NumericExpression.new(:+, self, ce)
808   end
809 end
coerce(other)

If the argument given is Numeric, treat it as a NumericExpression, allowing code such as:

1 + Sequel[:x] # SQL: (1 + x)
Sequel.expr{1 - x(y)} # SQL: (1 - x(y))
[show source]
    # File lib/sequel/sql.rb
788 def coerce(other)
789   if other.is_a?(Numeric)
790     [SQL::NumericExpression.new(:NOOP, other), self]
791   elsif defined?(super)
792     super 
793   else
794     [self, other]
795   end
796 end