aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/arel.rb1
-rw-r--r--lib/arel/attributes/attribute.rb10
-rw-r--r--lib/arel/math.rb21
-rw-r--r--lib/arel/nodes.rb1
-rw-r--r--lib/arel/nodes/math_operation.rb15
-rw-r--r--lib/arel/visitors/to_sql.rb16
6 files changed, 61 insertions, 3 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index 32fc8d9bc0..de429f532e 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -3,6 +3,7 @@ require 'arel/factory_methods'
require 'arel/expressions'
require 'arel/predications'
+require 'arel/math'
require 'arel/table'
require 'arel/attributes'
require 'arel/compatibility/wheres'
diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb
index 9a42e5a4da..bd3f4b58f1 100644
--- a/lib/arel/attributes/attribute.rb
+++ b/lib/arel/attributes/attribute.rb
@@ -5,12 +5,16 @@ module Arel
include Arel::Predications
end
+ class NumericAttribute < Attribute
+ include Arel::Math
+ end
+
class String < Attribute; end
class Time < Attribute; end
class Boolean < Attribute; end
- class Decimal < Attribute; end
- class Float < Attribute; end
- class Integer < Attribute; end
+ class Decimal < NumericAttribute; end
+ class Float < NumericAttribute; end
+ class Integer < NumericAttribute; end
class Undefined < Attribute; end
end
diff --git a/lib/arel/math.rb b/lib/arel/math.rb
new file mode 100644
index 0000000000..551b1f1010
--- /dev/null
+++ b/lib/arel/math.rb
@@ -0,0 +1,21 @@
+module Arel
+ module Math
+
+ def *(other)
+ Arel::Nodes::Multiplication.new(self, other)
+ end
+
+ def +(other)
+ Arel::Nodes::Addition.new(self, other)
+ end
+
+ def -(other)
+ Arel::Nodes::Subtraction.new(self, other)
+ end
+
+ def /(other)
+ Arel::Nodes::Division.new(self, other)
+ end
+
+ end
+end \ No newline at end of file
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 442b313593..4b97e28668 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -18,6 +18,7 @@ require 'arel/nodes/join_source'
require 'arel/nodes/ordering'
require 'arel/nodes/delete_statement'
require 'arel/nodes/table_alias'
+require 'arel/nodes/math_operation'
# nary
require 'arel/nodes/and'
diff --git a/lib/arel/nodes/math_operation.rb b/lib/arel/nodes/math_operation.rb
new file mode 100644
index 0000000000..d9820f1ece
--- /dev/null
+++ b/lib/arel/nodes/math_operation.rb
@@ -0,0 +1,15 @@
+module Arel
+ module Nodes
+ class MathOperation < Binary
+ include Arel::Expressions
+ include Arel::Predications
+ include Arel::Math
+ end
+
+ class Multiplication < MathOperation; end
+ class Division < MathOperation; end
+ class Addition < MathOperation; end
+ class Subtraction < MathOperation; end
+
+ end
+end \ No newline at end of file
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 70e10a5e0f..7eaaff8e13 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -373,6 +373,22 @@ key on UpdateManager using UpdateManager#key=
alias :visit_Time :quoted
alias :visit_TrueClass :quoted
+ def visit_Arel_Nodes_Multiplication o
+ "#{visit o.left} * #{visit o.right}"
+ end
+
+ def visit_Arel_Nodes_Division o
+ "#{visit o.left} / #{visit o.right}"
+ end
+
+ def visit_Arel_Nodes_Addition o
+ "(#{visit o.left} + #{visit o.right})"
+ end
+
+ def visit_Arel_Nodes_Subtraction o
+ "(#{visit o.left} - #{visit o.right})"
+ end
+
def visit_Array o
o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
end