From 74ad1ccc50d2051e582edd54e4e62c4c87c62dd9 Mon Sep 17 00:00:00 2001 From: Rob Anderton Date: Sun, 9 Aug 2009 15:06:24 +0100 Subject: [PATCH] Quote aliased table names in SQL generated by JoinAssociation --- activerecord/lib/active_record/associations.rb | 8 ++++---- .../associations/inner_join_association_test.rb | 14 ++++++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 7732bb5..14a5962 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -2117,9 +2117,9 @@ module ActiveRecord foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key " #{join_type} %s ON %s.%s = %s.%s " % [ table_name_and_alias, - aliased_table_name, + connection.quote_table_name(aliased_table_name), foreign_key, - parent.aliased_table_name, + connection.quote_table_name(parent.aliased_table_name), reflection.options[:primary_key] || parent.primary_key ] end @@ -2138,7 +2138,7 @@ module ActiveRecord klass.send(:type_condition, aliased_table_name)] unless klass.descends_from_active_record? [through_reflection, reflection].each do |ref| - join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions] + join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], connection.quote_table_name(aliased_table_name)))} " if ref && ref.options[:conditions] end join @@ -2169,7 +2169,7 @@ module ActiveRecord end def table_alias_for(table_name, table_alias) - "#{reflection.active_record.connection.quote_table_name(table_name)} #{table_alias if table_name != table_alias}".strip + "#{reflection.active_record.connection.quote_table_name(table_name)} #{reflection.active_record.connection.quote_table_name(table_alias) if table_name != table_alias}".strip end def table_name_and_alias diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index 7141531..5b8451a 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -10,13 +10,13 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase def test_construct_finder_sql_creates_inner_joins sql = Author.send(:construct_finder_sql, :joins => :posts) - assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql + assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = .?authors.?.id/, sql end def test_construct_finder_sql_cascades_inner_joins sql = Author.send(:construct_finder_sql, :joins => {:posts => :comments}) - assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql - assert_match /INNER JOIN .?comments.? ON .?comments.?.post_id = posts.id/, sql + assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = .?authors.?.id/, sql + assert_match /INNER JOIN .?comments.? ON .?comments.?.post_id = .?posts.?.id/, sql end def test_construct_finder_sql_inner_joins_through_associations @@ -34,10 +34,16 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase assert_equal authors(:david), result.first end + def test_construct_finder_sql_quotes_aliased_table_names_on_association_conditions + quoted_table_name = Post.connection.quote_table_name('welcome_posts_authors') + sql = Author.send(:construct_finder_sql, :joins => [:thinking_posts, :welcome_posts]) + assert_match /INNER JOIN .?posts.? #{quoted_table_name} ON #{quoted_table_name}\.author_id = .?authors.?\.id AND #{quoted_table_name}\./, sql + end + def test_construct_finder_sql_unpacks_nested_joins sql = Author.send(:construct_finder_sql, :joins => {:posts => [[:comments]]}) assert_no_match /inner join.*inner join.*inner join/i, sql, "only two join clauses should be present" - assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql + assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = .?authors.?.id/, sql assert_match /INNER JOIN .?comments.? ON .?comments.?.post_id = .?posts.?.id/, sql end -- 1.6.3.2.1299.gee46c