1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

to_sql visitor started

This commit is contained in:
Aaron Patterson 2010-08-12 15:40:58 -07:00
parent 199d17b2db
commit d827d6e9ee
6 changed files with 64 additions and 0 deletions

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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