aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-27 13:12:15 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-27 13:12:15 -0700
commit77fa5fa5f7cc21824edb5b1c1ebccd58f92e982b (patch)
tree80c71416e19948b7e0c4c3b9aacd36672ab62d1d
parentec998ae9a6aeda09ad64bb94a50ac8b9fccd246d (diff)
downloadrails-77fa5fa5f7cc21824edb5b1c1ebccd58f92e982b.tar.gz
rails-77fa5fa5f7cc21824edb5b1c1ebccd58f92e982b.tar.bz2
rails-77fa5fa5f7cc21824edb5b1c1ebccd58f92e982b.zip
to_sql on nodes may be passed an engine
-rw-r--r--lib/arel/nodes/binary.rb10
-rw-r--r--lib/arel/nodes/function.rb7
-rw-r--r--lib/arel/nodes/grouping.rb10
-rw-r--r--lib/arel/nodes/node.rb10
-rw-r--r--spec/nodes/equality_spec.rb18
-rw-r--r--spec/spec_helper.rb2
-rw-r--r--spec/support/fake_record.rb15
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