From 21e052796d3007488d5dd9f00299a3a22fdb6249 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Wed, 9 Mar 2011 02:27:43 +0800
Subject: Replace MathOperation with InfixOperation to support more operators

---
 lib/arel/math.rb                  |  4 ++--
 lib/arel/nodes.rb                 |  2 +-
 lib/arel/nodes/infix_operation.rb | 42 +++++++++++++++++++++++++++++++++++++++
 lib/arel/nodes/math_operation.rb  | 15 --------------
 lib/arel/visitors/to_sql.rb       | 19 ++++++------------
 5 files changed, 51 insertions(+), 31 deletions(-)
 create mode 100644 lib/arel/nodes/infix_operation.rb
 delete mode 100644 lib/arel/nodes/math_operation.rb

(limited to 'lib')

diff --git a/lib/arel/math.rb b/lib/arel/math.rb
index b7c2419233..f3dbc7bc49 100644
--- a/lib/arel/math.rb
+++ b/lib/arel/math.rb
@@ -5,11 +5,11 @@ module Arel
     end
 
     def +(other)
-      Arel::Nodes::Addition.new(self, other)
+      Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new(self, other))
     end
 
     def -(other)
-      Arel::Nodes::Subtraction.new(self, other)
+      Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
     end
 
     def /(other)
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 4b97e28668..cf2aeb2ddc 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -18,7 +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'
+require 'arel/nodes/infix_operation'
 
 # nary
 require 'arel/nodes/and'
diff --git a/lib/arel/nodes/infix_operation.rb b/lib/arel/nodes/infix_operation.rb
new file mode 100644
index 0000000000..6847650fe4
--- /dev/null
+++ b/lib/arel/nodes/infix_operation.rb
@@ -0,0 +1,42 @@
+module Arel
+  module Nodes
+
+    class InfixOperation < Binary
+      include Arel::Expressions
+      include Arel::Predications
+      include Arel::Math
+
+      attr_reader :operator
+
+      def initialize operator, left, right
+        super(left, right)
+        @operator = operator
+      end
+    end
+
+    class Multiplication < InfixOperation
+      def initialize left, right
+        super(:*, left, right)
+      end
+    end
+
+    class Division < InfixOperation
+      def initialize left, right
+        super(:/, left, right)
+      end
+    end
+
+    class Addition < InfixOperation
+      def initialize left, right
+        super(:+, left, right)
+      end
+    end
+
+    class Subtraction < InfixOperation
+      def initialize left, right
+        super(:-, left, right)
+      end
+    end
+
+  end
+end
\ No newline at end of file
diff --git a/lib/arel/nodes/math_operation.rb b/lib/arel/nodes/math_operation.rb
deleted file mode 100644
index d9820f1ece..0000000000
--- a/lib/arel/nodes/math_operation.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-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 f30557e509..1550757cf4 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -373,21 +373,14 @@ 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}"
+      def visit_Arel_Nodes_InfixOperation o
+        "#{visit o.left} #{o.operator} #{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
+      alias :visit_Arel_Nodes_Addition       :visit_Arel_Nodes_InfixOperation
+      alias :visit_Arel_Nodes_Subtraction    :visit_Arel_Nodes_InfixOperation
+      alias :visit_Arel_Nodes_Multiplication :visit_Arel_Nodes_InfixOperation
+      alias :visit_Arel_Nodes_Division       :visit_Arel_Nodes_InfixOperation
 
       def visit_Array o
         o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
-- 
cgit v1.2.3


From 05887fc406820f2e8fecc5cedcc42b87cf9f0ab1 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Sat, 12 Mar 2011 10:53:47 +0800
Subject: Make as factory method convert alias name to SqlLiteral

---
 lib/arel/predications.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'lib')

diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 920a9ee374..75c4c75855 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -1,7 +1,7 @@
 module Arel
   module Predications
     def as other
-      Nodes::As.new self, other
+      Nodes::As.new self, Nodes::SqlLiteral.new(other)
     end
 
     def not_eq other
-- 
cgit v1.2.3