aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-08-08 23:23:51 +0100
committerJon Leighton <j@jonathanleighton.com>2011-08-08 23:23:51 +0100
commit79411322ae225289e1c051f4f68ed84c6349e4a0 (patch)
tree424499366dc9c7ced8a271756b86a865ce17c977
parent03b6ca269ac8dfec8f70f2b98439d45b873f9e97 (diff)
downloadrails-79411322ae225289e1c051f4f68ed84c6349e4a0.tar.gz
rails-79411322ae225289e1c051f4f68ed84c6349e4a0.tar.bz2
rails-79411322ae225289e1c051f4f68ed84c6349e4a0.zip
Make it the responsibility of the connection to hold on to a visitor for generating SQL, rather than the TreeManager. (There is a related commit coming in Active Record.)
-rw-r--r--lib/arel/nodes/node.rb3
-rw-r--r--lib/arel/select_manager.rb9
-rw-r--r--lib/arel/tree_manager.rb12
-rw-r--r--lib/arel/visitors/to_sql.rb6
-rw-r--r--test/nodes/test_bin.rb4
-rw-r--r--test/nodes/test_select_core.rb2
-rw-r--r--test/nodes/test_sql_literal.rb19
-rw-r--r--test/support/fake_record.rb7
-rw-r--r--test/test_select_manager.rb4
-rw-r--r--test/visitors/test_ibm_db.rb2
-rw-r--r--test/visitors/test_informix.rb2
-rw-r--r--test/visitors/test_join_sql.rb2
-rw-r--r--test/visitors/test_mssql.rb2
-rw-r--r--test/visitors/test_mysql.rb2
-rw-r--r--test/visitors/test_oracle.rb2
-rw-r--r--test/visitors/test_postgres.rb2
-rw-r--r--test/visitors/test_sqlite.rb2
-rw-r--r--test/visitors/test_to_sql.rb6
18 files changed, 46 insertions, 42 deletions
diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb
index 1a5bc27856..4faace3782 100644
--- a/lib/arel/nodes/node.rb
+++ b/lib/arel/nodes/node.rb
@@ -32,8 +32,7 @@ module Arel
#
# Maybe we should just use `Table.engine`? :'(
def to_sql engine = Table.engine
- viz = Visitors.for engine
- viz.accept self
+ engine.connection.visitor.accept self
end
# Iterate through AST, nodes will be yielded depth-first
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index d95a259177..dc80150957 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -149,13 +149,13 @@ module Arel
def wheres
warn "#{caller[0]}: SelectManager#wheres is deprecated and will be removed in ARel 3.0.0 with no replacement"
- Compatibility::Wheres.new @engine, @ctx.wheres
+ Compatibility::Wheres.new @engine.connection_pool, @ctx.wheres
end
def where_sql
return if @ctx.wheres.empty?
- viz = Visitors::WhereSql.new @engine
+ viz = Visitors::WhereSql.new @engine.connection_pool
Nodes::SqlLiteral.new viz.accept @ctx
end
@@ -205,12 +205,13 @@ module Arel
def join_sql
return nil if @ctx.source.right.empty?
- sql = @visitor.dup.extend(Visitors::JoinSql).accept @ctx
+ sql = visitor.dup.extend(Visitors::JoinSql).accept @ctx
Nodes::SqlLiteral.new sql
end
def order_clauses
- Visitors::OrderClauses.new(@engine).accept(@ast).map { |x|
+ visitor = Visitors::OrderClauses.new(@engine.connection_pool)
+ visitor.accept(@ast).map { |x|
Nodes::SqlLiteral.new x
}
end
diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb
index 5722baca77..99a7164e2e 100644
--- a/lib/arel/tree_manager.rb
+++ b/lib/arel/tree_manager.rb
@@ -4,21 +4,23 @@ module Arel
include Arel::Relation
include Arel::FactoryMethods
- attr_accessor :visitor
attr_reader :ast, :engine
def initialize engine
- @engine = engine
- @visitor = Visitors.visitor_for @engine
- @ctx = nil
+ @engine = engine
+ @ctx = nil
end
def to_dot
Visitors::Dot.new.accept @ast
end
+ def visitor
+ engine.connection.visitor
+ end
+
def to_sql
- @visitor.accept @ast
+ visitor.accept @ast
end
def initialize_copy other
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index ec62f4fb89..d424f8602e 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -4,17 +4,15 @@ require 'date'
module Arel
module Visitors
class ToSql < Arel::Visitors::Visitor
- def initialize engine
- @engine = engine
+ def initialize pool
+ @pool = pool
@connection = nil
- @pool = nil
@quoted_tables = {}
@quoted_columns = {}
end
def accept object
self.last_column = nil
- @pool = @engine.connection_pool
@pool.with_connection do |conn|
@connection = conn
super
diff --git a/test/nodes/test_bin.rb b/test/nodes/test_bin.rb
index b06aeb0b0d..7f123eab13 100644
--- a/test/nodes/test_bin.rb
+++ b/test/nodes/test_bin.rb
@@ -8,13 +8,13 @@ module Arel
end
def test_default_to_sql
- viz = Arel::Visitors::ToSql.new Table.engine
+ viz = Arel::Visitors::ToSql.new Table.engine.connection_pool
node = Arel::Nodes::Bin.new(Arel.sql('zomg'))
assert_equal 'zomg', viz.accept(node)
end
def test_mysql_to_sql
- viz = Arel::Visitors::MySQL.new Table.engine
+ viz = Arel::Visitors::MySQL.new Table.engine.connection_pool
node = Arel::Nodes::Bin.new(Arel.sql('zomg'))
assert_equal 'BINARY zomg', viz.accept(node)
end
diff --git a/test/nodes/test_select_core.rb b/test/nodes/test_select_core.rb
index 47f85aee8a..4382c79865 100644
--- a/test/nodes/test_select_core.rb
+++ b/test/nodes/test_select_core.rb
@@ -23,7 +23,7 @@ module Arel
def test_set_quantifier
core = Arel::Nodes::SelectCore.new
core.set_quantifier = Arel::Nodes::Distinct.new
- viz = Arel::Visitors::ToSql.new Table.engine
+ viz = Arel::Visitors::ToSql.new Table.engine.connection_pool
assert_match 'DISTINCT', viz.accept(core)
end
end
diff --git a/test/nodes/test_sql_literal.rb b/test/nodes/test_sql_literal.rb
index d280d6d928..54d1d4417f 100644
--- a/test/nodes/test_sql_literal.rb
+++ b/test/nodes/test_sql_literal.rb
@@ -3,6 +3,10 @@ require 'helper'
module Arel
module Nodes
describe 'sql literal' do
+ before do
+ @visitor = Visitors::ToSql.new Table.engine.connection_pool
+ end
+
describe 'sql' do
it 'makes a sql literal node' do
sql = Arel.sql 'foo'
@@ -13,38 +17,33 @@ module Arel
describe 'count' do
it 'makes a count node' do
node = SqlLiteral.new('*').count
- viz = Visitors::ToSql.new Table.engine
- viz.accept(node).must_be_like %{ COUNT(*) }
+ @visitor.accept(node).must_be_like %{ COUNT(*) }
end
it 'makes a distinct node' do
node = SqlLiteral.new('*').count true
- viz = Visitors::ToSql.new Table.engine
- viz.accept(node).must_be_like %{ COUNT(DISTINCT *) }
+ @visitor.accept(node).must_be_like %{ COUNT(DISTINCT *) }
end
end
describe 'equality' do
it 'makes an equality node' do
node = SqlLiteral.new('foo').eq(1)
- viz = Visitors::ToSql.new Table.engine
- viz.accept(node).must_be_like %{ foo = 1 }
+ @visitor.accept(node).must_be_like %{ foo = 1 }
end
end
describe 'grouped "or" equality' do
it 'makes a grouping node with an or node' do
node = SqlLiteral.new('foo').eq_any([1,2])
- viz = Visitors::ToSql.new Table.engine
- viz.accept(node).must_be_like %{ (foo = 1 OR foo = 2) }
+ @visitor.accept(node).must_be_like %{ (foo = 1 OR foo = 2) }
end
end
describe 'grouped "and" equality' do
it 'makes a grouping node with an or node' do
node = SqlLiteral.new('foo').eq_all([1,2])
- viz = Visitors::ToSql.new Table.engine
- viz.accept(node).must_be_like %{ (foo = 1 AND foo = 2) }
+ @visitor.accept(node).must_be_like %{ (foo = 1 AND foo = 2) }
end
end
end
diff --git a/test/support/fake_record.rb b/test/support/fake_record.rb
index babf5fa25b..ddef7b66c5 100644
--- a/test/support/fake_record.rb
+++ b/test/support/fake_record.rb
@@ -3,9 +3,9 @@ module FakeRecord
end
class Connection
- attr_reader :tables, :columns_hash
+ attr_reader :tables, :columns_hash, :visitor
- def initialize
+ def initialize(visitor)
@tables = %w{ users photos developers products}
@columns = {
'users' => [
@@ -27,6 +27,7 @@ module FakeRecord
'users' => 'id',
'products' => 'id'
}
+ @visitor = visitor
end
def primary_key name
@@ -78,7 +79,7 @@ module FakeRecord
def initialize
@spec = Spec.new(:adapter => 'america')
- @connection = Connection.new
+ @connection = Connection.new(Arel::Visitors::ToSql.new(self))
end
def with_connection
diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb
index 119ad3ec4f..3d43ecfc11 100644
--- a/test/test_select_manager.rb
+++ b/test/test_select_manager.rb
@@ -42,6 +42,10 @@ module Arel
@engine.connection.tables
end
+ def visitor
+ @engine.connection.visitor
+ end
+
def execute sql, name = nil, *args
@executed << sql
end
diff --git a/test/visitors/test_ibm_db.rb b/test/visitors/test_ibm_db.rb
index cc35bb768d..90008ba05e 100644
--- a/test/visitors/test_ibm_db.rb
+++ b/test/visitors/test_ibm_db.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the ibm_db visitor' do
before do
- @visitor = IBM_DB.new Table.engine
+ @visitor = IBM_DB.new Table.engine.connection_pool
end
it 'uses FETCH FIRST n ROWS to limit results' do
diff --git a/test/visitors/test_informix.rb b/test/visitors/test_informix.rb
index a8a52a0160..422da846fe 100644
--- a/test/visitors/test_informix.rb
+++ b/test/visitors/test_informix.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the informix visitor' do
before do
- @visitor = Informix.new Table.engine
+ @visitor = Informix.new Table.engine.connection_pool
end
it 'uses LIMIT n to limit results' do
diff --git a/test/visitors/test_join_sql.rb b/test/visitors/test_join_sql.rb
index b672f88ecf..6f7440cc47 100644
--- a/test/visitors/test_join_sql.rb
+++ b/test/visitors/test_join_sql.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the join_sql visitor' do
before do
- @visitor = ToSql.new Table.engine
+ @visitor = ToSql.new Table.engine.connection_pool
@visitor.extend(JoinSql)
end
diff --git a/test/visitors/test_mssql.rb b/test/visitors/test_mssql.rb
index 8b2b756900..21588f53cf 100644
--- a/test/visitors/test_mssql.rb
+++ b/test/visitors/test_mssql.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the mssql visitor' do
before do
- @visitor = MSSQL.new Table.engine
+ @visitor = MSSQL.new Table.engine.connection_pool
@table = Arel::Table.new "users"
end
diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb
index 8d220ac04b..487db325e8 100644
--- a/test/visitors/test_mysql.rb
+++ b/test/visitors/test_mysql.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the mysql visitor' do
before do
- @visitor = MySQL.new Table.engine
+ @visitor = MySQL.new Table.engine.connection_pool
end
it 'squashes parenthesis on multiple unions' do
diff --git a/test/visitors/test_oracle.rb b/test/visitors/test_oracle.rb
index eaf68013a7..b53690a1a8 100644
--- a/test/visitors/test_oracle.rb
+++ b/test/visitors/test_oracle.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the oracle visitor' do
before do
- @visitor = Oracle.new Table.engine
+ @visitor = Oracle.new Table.engine.connection_pool
end
it 'modifies order when there is distinct and first value' do
diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb
index 446eae0c4a..e8df681269 100644
--- a/test/visitors/test_postgres.rb
+++ b/test/visitors/test_postgres.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the postgres visitor' do
before do
- @visitor = PostgreSQL.new Table.engine
+ @visitor = PostgreSQL.new Table.engine.connection_pool
end
describe 'locking' do
diff --git a/test/visitors/test_sqlite.rb b/test/visitors/test_sqlite.rb
index fb8392c504..5b81ea90c5 100644
--- a/test/visitors/test_sqlite.rb
+++ b/test/visitors/test_sqlite.rb
@@ -4,7 +4,7 @@ module Arel
module Visitors
describe 'the sqlite visitor' do
before do
- @visitor = SQLite.new Table.engine
+ @visitor = SQLite.new Table.engine.connection_pool
end
it 'defaults limit to -1' do
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index 3b58c71cd8..12af197596 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -8,7 +8,7 @@ module Arel
module Visitors
describe 'the to_sql visitor' do
before do
- @visitor = ToSql.new Table.engine
+ @visitor = ToSql.new Table.engine.connection_pool
@table = Table.new(:users)
@attr = @table[:id]
end
@@ -220,7 +220,7 @@ module Arel
end
end
in_node = Nodes::In.new @attr, %w{ a b c }
- visitor = visitor.new(Table.engine)
+ visitor = visitor.new(Table.engine.connection_pool)
visitor.expected = Table.engine.connection.columns(:users).find { |x|
x.name == 'name'
}
@@ -308,7 +308,7 @@ module Arel
end
end
in_node = Nodes::NotIn.new @attr, %w{ a b c }
- visitor = visitor.new(Table.engine)
+ visitor = visitor.new(Table.engine.connection_pool)
visitor.expected = Table.engine.connection.columns(:users).find { |x|
x.name == 'name'
}