From 77fa5fa5f7cc21824edb5b1c1ebccd58f92e982b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 13:12:15 -0700 Subject: to_sql on nodes may be passed an engine --- lib/arel/nodes/binary.rb | 10 ---------- lib/arel/nodes/function.rb | 7 +------ lib/arel/nodes/grouping.rb | 10 ---------- lib/arel/nodes/node.rb | 10 ++++++++++ spec/nodes/equality_spec.rb | 18 ++++++++++++++++++ spec/spec_helper.rb | 2 +- spec/support/fake_record.rb | 15 +++++++-------- 7 files changed, 37 insertions(+), 35 deletions(-) diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb index 090468adfa..cfa75909c5 100644 --- a/lib/arel/nodes/binary.rb +++ b/lib/arel/nodes/binary.rb @@ -7,16 +7,6 @@ module Arel @left = left @right = right end - - # FIXME: this method should go away. I don't like people calling - # to_sql on non-head nodes. This forces us to walk the AST until we - # can find a node that has a "relation" member. - # - # Maybe we should just use `Table.engine`? :'( - def to_sql - viz = Visitors::ToSql.new Table.engine - viz.accept self - end end end end diff --git a/lib/arel/nodes/function.rb b/lib/arel/nodes/function.rb index a1cd13d4d1..133dd66019 100644 --- a/lib/arel/nodes/function.rb +++ b/lib/arel/nodes/function.rb @@ -1,6 +1,6 @@ module Arel module Nodes - class Function + class Function < Arel::Nodes::Node include Arel::Expression attr_accessor :expressions, :alias @@ -13,11 +13,6 @@ module Arel self.alias = SqlLiteral.new(aliaz) self end - - def to_sql - viz = Visitors::ToSql.new Table.engine - viz.accept self - end end end end diff --git a/lib/arel/nodes/grouping.rb b/lib/arel/nodes/grouping.rb index 0af1df1f7a..d52671f169 100644 --- a/lib/arel/nodes/grouping.rb +++ b/lib/arel/nodes/grouping.rb @@ -6,16 +6,6 @@ module Arel def initialize expression @expr = expression end - - # FIXME: this method should go away. I don't like people calling - # to_sql on non-head nodes. This forces us to walk the AST until we - # can find a node that has a "relation" member. - # - # Maybe we should just use `Table.engine`? :'( - def to_sql - viz = Visitors::ToSql.new Table.engine - viz.accept self - end end end end diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb index fd5ea410ea..91c0a32573 100644 --- a/lib/arel/nodes/node.rb +++ b/lib/arel/nodes/node.rb @@ -15,6 +15,16 @@ module Arel def and right Nodes::And.new self, right end + + # FIXME: this method should go away. I don't like people calling + # to_sql on non-head nodes. This forces us to walk the AST until we + # can find a node that has a "relation" member. + # + # Maybe we should just use `Table.engine`? :'( + def to_sql engine = Table.engine + viz = Visitors::ToSql.new engine + viz.accept self + end end end end diff --git a/spec/nodes/equality_spec.rb b/spec/nodes/equality_spec.rb index 81eea4d482..f1ed7a6904 100644 --- a/spec/nodes/equality_spec.rb +++ b/spec/nodes/equality_spec.rb @@ -26,6 +26,24 @@ module Arel check left.right.should == left.operand2 end end + + describe 'to_sql' do + it 'takes an engine' do + engine = FakeRecord::Base.new + engine.connection.extend Module.new { + attr_accessor :quote_count + def quote(*args) @quote_count += 1; super; end + def quote_column_name(*args) @quote_count += 1; super; end + def quote_table_name(*args) @quote_count += 1; super; end + } + engine.connection.quote_count = 0 + + attr = Table.new(:users)[:id] + test = attr.eq(10) + test.to_sql engine + check engine.connection.quote_count.should == 2 + end + end end describe 'or' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0031f6903f..b9fd9db930 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,6 @@ Spec::Runner.configure do |config| config.include Check config.before do - Arel::Table.engine = Arel::Sql::Engine.new(FakeRecord::Base) + Arel::Table.engine = Arel::Sql::Engine.new(FakeRecord::Base.new) end end diff --git a/spec/support/fake_record.rb b/spec/support/fake_record.rb index 2aba0c10f2..30c49f2b16 100644 --- a/spec/support/fake_record.rb +++ b/spec/support/fake_record.rb @@ -63,14 +63,11 @@ module FakeRecord class Spec < Struct.new(:config) end - attr_reader :spec + attr_reader :spec, :connection def initialize @spec = Spec.new('sqlite3') - end - - def connection - Connection.new + @connection = Connection.new end def with_connection @@ -79,11 +76,13 @@ module FakeRecord end class Base - def self.connection_pool - ConnectionPool.new + attr_accessor :connection_pool + + def initialize + @connection_pool = ConnectionPool.new end - def self.connection + def connection connection_pool.connection end end -- cgit v1.2.3