aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel.rb1
-rw-r--r--lib/arel/nodes/equality.rb5
-rw-r--r--lib/arel/table.rb2
-rw-r--r--lib/arel/visitors/to_sql.rb38
-rw-r--r--spec/arel/attributes/attribute_spec.rb10
-rw-r--r--spec/arel/table_spec.rb8
6 files changed, 64 insertions, 0 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index 4584584c35..ea9a462d1f 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -2,6 +2,7 @@ require 'arel/version'
require 'arel/table'
require 'arel/attributes'
require 'arel/nodes'
+require 'arel/visitors/to_sql'
# below is deprecated
require 'arel/sql/engine'
diff --git a/lib/arel/nodes/equality.rb b/lib/arel/nodes/equality.rb
index 11383b1d89..d778380054 100644
--- a/lib/arel/nodes/equality.rb
+++ b/lib/arel/nodes/equality.rb
@@ -7,6 +7,11 @@ module Arel
@left = left
@right = right
end
+
+ def to_sql
+ viz = Visitors::ToSql.new left.relation.engine
+ viz.accept self
+ end
end
end
end
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index 78b3ea9933..0ab39f134d 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -3,6 +3,8 @@ module Arel
@engine = nil
class << self; attr_accessor :engine; end
+ attr_reader :name, :engine
+
def initialize name, engine = Table.engine
@name = name
@engine = engine
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
new file mode 100644
index 0000000000..ba92ca7ef1
--- /dev/null
+++ b/lib/arel/visitors/to_sql.rb
@@ -0,0 +1,38 @@
+module Arel
+ module Visitors
+ class ToSql
+ def initialize engine
+ @engine = engine
+ @connection = nil
+ end
+
+ def accept object
+ @connection = @engine.connection
+ visit object
+ end
+
+ private
+ def visit_Arel_Nodes_Equality o
+ "#{visit o.left} = #{visit o.right}"
+ end
+
+ def visit_Arel_Attributes_Integer o
+ "#{quote_table_name o.relation.name}.#{quote_column_name o.name}"
+ end
+
+ def visit_Fixnum o; o end
+
+ def visit object
+ send :"visit_#{object.class.name.gsub('::', '_')}", object
+ end
+
+ def quote_table_name name
+ @connection.quote_table_name name
+ end
+
+ def quote_column_name name
+ @connection.quote_column_name name
+ end
+ end
+ end
+end
diff --git a/spec/arel/attributes/attribute_spec.rb b/spec/arel/attributes/attribute_spec.rb
index 5599bb7b51..ef0e0787fa 100644
--- a/spec/arel/attributes/attribute_spec.rb
+++ b/spec/arel/attributes/attribute_spec.rb
@@ -13,5 +13,15 @@ module Arel
end
end
end
+
+ describe 'equality' do
+ describe '#to_sql' do
+ it 'should produce sql' do
+ table = Table.new :users
+ condition = table['id'].eq 1
+ condition.to_sql.should == '"users"."id" = 1'
+ end
+ end
+ end
end
end
diff --git a/spec/arel/table_spec.rb b/spec/arel/table_spec.rb
index c52a111489..be153438c4 100644
--- a/spec/arel/table_spec.rb
+++ b/spec/arel/table_spec.rb
@@ -14,6 +14,14 @@ module Arel
end
end
+ it "should have a name" do
+ @relation.name.should == :users
+ end
+
+ it "should have an engine" do
+ @relation.engine.should == Table.engine
+ end
+
describe '[]' do
describe 'when given a', Symbol do
it "manufactures an attribute if the symbol names an attribute within the relation" do