Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #51 from square/thread-safety
Make ToSql more thread safe.
  • Loading branch information
tenderlove committed May 11, 2011
2 parents a3d9c39 + 73ca393 commit ede507d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/arel/visitors/to_sql.rb
Expand Up @@ -8,14 +8,13 @@ def initialize engine
@engine = engine
@connection = nil
@pool = nil
@last_column = nil
@quoted_tables = {}
@quoted_columns = {}
end

def accept object
@last_column = nil
@pool = @engine.connection_pool
Thread.current[:arel_visitors_to_sql_last_column] = nil
@pool = @engine.connection_pool
@pool.with_connection do |conn|
@connection = conn
super
Expand Down Expand Up @@ -356,7 +355,7 @@ def visit_Arel_Nodes_UnqualifiedColumn o
end

def visit_Arel_Attributes_Attribute o
@last_column = column_for o
Thread.current[:arel_visitors_to_sql_last_column] = column_for o
join_name = o.relation.table_alias || o.relation.name
"#{quote_table_name join_name}.#{quote_column_name o.name}"
end
Expand All @@ -374,7 +373,7 @@ 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, Thread.current[:arel_visitors_to_sql_last_column]) end

alias :visit_ActiveSupport_Multibyte_Chars :quoted
alias :visit_ActiveSupport_StringInquirer :quoted
Expand Down
17 changes: 17 additions & 0 deletions test/visitors/test_to_sql.rb
@@ -1,5 +1,9 @@
require 'helper'

class Arel::Visitors::ToSql
def last_column; Thread.current[:arel_visitors_to_sql_last_column] || @last_column; end
end

module Arel
module Visitors
describe 'the to_sql visitor' do
Expand All @@ -9,6 +13,19 @@ module Visitors
@attr = @table[:id]
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

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

0 comments on commit ede507d

Please sign in to comment.