Module | Sequel::SQL::NumericMethods |
In: |
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+).
:a + :b # "a" + "b" :a - :b # "a" - "b" :a * :b # "a" * "b" :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'
Use || as the operator when called with StringExpression and String instances, and the + operator for LiteralStrings and all other types.
# File lib/sequel/sql.rb, line 777 777: def +(ce) 778: case ce 779: when LiteralString 780: NumericExpression.new(:+, self, ce) 781: when StringExpression, String 782: StringExpression.new('||''||', self, ce) 783: else 784: NumericExpression.new(:+, self, ce) 785: end 786: end
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))
# File lib/sequel/sql.rb, line 767 767: def coerce(other) 768: if other.is_a?(Numeric) 769: [SQL::NumericExpression.new(:NOOP, other), self] 770: else 771: super 772: end 773: end