Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

calling cache methods against the connection

  • Loading branch information...
commit c5f9fbf0d66ddeaf1fb2992e696ffe88244bda82 1 parent 9a0b1c4
@tenderlove tenderlove authored
View
8 lib/arel/select_manager.rb
@@ -51,7 +51,7 @@ def where_clauses
if $VERBOSE
warn "(#{caller.first}) where_clauses is deprecated and will be removed in arel 3.0.0 with no replacement"
end
- to_sql = Visitors::ToSql.new @engine.connection_pool
+ to_sql = Visitors::ToSql.new @engine.connection
@ctx.wheres.map { |c| to_sql.accept c }
end
@@ -161,13 +161,13 @@ def orders
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.connection_pool, @ctx.wheres
+ Compatibility::Wheres.new @engine.connection, @ctx.wheres
end
def where_sql
return if @ctx.wheres.empty?
- viz = Visitors::WhereSql.new @engine.connection_pool
+ viz = Visitors::WhereSql.new @engine.connection
Nodes::SqlLiteral.new viz.accept @ctx
end
@@ -222,7 +222,7 @@ def join_sql
end
def order_clauses
- visitor = Visitors::OrderClauses.new(@engine.connection_pool)
+ visitor = Visitors::OrderClauses.new(@engine.connection)
visitor.accept(@ast).map { |x|
Nodes::SqlLiteral.new x
}
View
30 lib/arel/visitors/to_sql.rb
@@ -4,30 +4,22 @@
module Arel
module Visitors
class ToSql < Arel::Visitors::Visitor
- def initialize pool
- @pool = pool
- @connection = nil
+ attr_accessor :last_column
+
+ def initialize connection
+ @connection = connection
+ @schema_cache = connection.schema_cache
@quoted_tables = {}
@quoted_columns = {}
+ @last_column = nil
end
def accept object
self.last_column = nil
- @pool.with_connection do |conn|
- @connection = conn
- super
- end
+ super
end
private
- def last_column= col
- Thread.current[:arel_visitors_to_sql_last_column] = col
- end
-
- def last_column
- Thread.current[:arel_visitors_to_sql_last_column]
- end
-
def visit_Arel_Nodes_DeleteStatement o
[
"DELETE FROM #{visit o.relation}",
@@ -97,7 +89,7 @@ def visit_Arel_Nodes_False o
end
def table_exists? name
- @pool.table_exists? name
+ @schema_cache.table_exists? name
end
def column_for attr
@@ -110,7 +102,7 @@ def column_for attr
end
def column_cache
- @pool.columns_hash
+ @schema_cache.columns_hash
end
def visit_Arel_Nodes_Values o
@@ -387,7 +379,9 @@ def literal o; o end
alias :visit_Bignum :literal
alias :visit_Fixnum :literal
- def quoted o; quote(o, last_column) end
+ def quoted o
+ quote(o, last_column)
+ end
alias :visit_ActiveSupport_Multibyte_Chars :quoted
alias :visit_ActiveSupport_StringInquirer :quoted
View
16 test/support/fake_record.rb
@@ -3,9 +3,10 @@ class Column < Struct.new(:name, :type)
end
class Connection
- attr_reader :tables, :columns_hash, :visitor
+ attr_reader :tables, :columns_hash
+ attr_accessor :visitor
- def initialize(visitor)
+ def initialize(visitor = nil)
@tables = %w{ users photos developers products}
@columns = {
'users' => [
@@ -50,6 +51,10 @@ def quote_column_name name
"\"#{name.to_s}\""
end
+ def schema_cache
+ self
+ end
+
def quote thing, column = nil
if column && column.type == :integer
return 'NULL' if thing.nil?
@@ -79,7 +84,8 @@ class Spec < Struct.new(:config)
def initialize
@spec = Spec.new(:adapter => 'america')
- @connection = Connection.new(Arel::Visitors::ToSql.new(self))
+ @connection = Connection.new
+ @connection.visitor = Arel::Visitors::ToSql.new(connection)
end
def with_connection
@@ -93,6 +99,10 @@ def table_exists? name
def columns_hash
connection.columns_hash
end
+
+ def schema_cache
+ connection
+ end
end
class Base
View
2  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.connection_pool
+ @visitor = IBM_DB.new Table.engine.connection
end
it 'uses FETCH FIRST n ROWS to limit results' do
View
2  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.connection_pool
+ @visitor = Informix.new Table.engine.connection
end
it 'uses LIMIT n to limit results' do
View
2  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.connection_pool
+ @visitor = ToSql.new Table.engine.connection
@visitor.extend(JoinSql)
end
View
2  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.connection_pool
+ @visitor = MSSQL.new Table.engine.connection
@table = Arel::Table.new "users"
end
View
2  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.connection_pool
+ @visitor = MySQL.new Table.engine.connection
end
it 'squashes parenthesis on multiple unions' do
View
2  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.connection_pool
+ @visitor = PostgreSQL.new Table.engine.connection
end
describe 'locking' do
View
24 test/visitors/test_to_sql.rb
@@ -1,14 +1,10 @@
require 'helper'
-class Arel::Visitors::ToSql
- def last_column; Thread.current[:arel_visitors_to_sql_last_column]; end
-end
-
module Arel
module Visitors
describe 'the to_sql visitor' do
before do
- @visitor = ToSql.new Table.engine.connection_pool
+ @visitor = ToSql.new Table.engine.connection
@table = Table.new(:users)
@attr = @table[:id]
end
@@ -29,20 +25,6 @@ def dispatch
assert visited, 'hello method was called'
end
- it "should be thread safe around usage of last_column" do
- visit_integer_column = Thread.new do
- Thread.stop
- @visitor.send(:visit_Arel_Attributes_Attribute, @attr)
- end
-
- sleep 0.2
- @visitor.accept(@table[:name])
- assert_equal(:string, @visitor.last_column.type)
- visit_integer_column.run
- visit_integer_column.join
- assert_equal(:string, @visitor.last_column.type)
- end
-
it 'should not quote sql literals' do
node = @table[Arel.star]
sql = @visitor.accept node
@@ -220,7 +202,7 @@ def quote value, column = nil
end
end
in_node = Nodes::In.new @attr, %w{ a b c }
- visitor = visitor.new(Table.engine.connection_pool)
+ visitor = visitor.new(Table.engine.connection)
visitor.expected = Table.engine.connection.columns(:users).find { |x|
x.name == 'name'
}
@@ -308,7 +290,7 @@ def quote value, column = nil
end
end
in_node = Nodes::NotIn.new @attr, %w{ a b c }
- visitor = visitor.new(Table.engine.connection_pool)
+ visitor = visitor.new(Table.engine.connection)
visitor.expected = Table.engine.connection.columns(:users).find { |x|
x.name == 'name'
}
Please sign in to comment.
Something went wrong with that request. Please try again.