Ensure table names are quoted by the association preloading code.

[#45 state:resolved]

Signed-off-by: Michael Koziarski <michael@koziarski.com>
This commit is contained in:
Frederick Cheung 2008-04-25 23:23:48 +01:00 committed by Michael Koziarski
parent a37546517d
commit 44d2142352
8 changed files with 83 additions and 10 deletions

View File

@ -85,9 +85,8 @@ module ActiveRecord
return id_to_record_map, ids
end
# FIXME: quoting
def preload_has_and_belongs_to_many_association(records, reflection, preload_options={})
table_name = reflection.klass.table_name
table_name = reflection.klass.quoted_table_name
id_to_record_map, ids = construct_id_map(records)
records.each {|record| record.send(reflection.name).loaded}
options = reflection.options
@ -97,7 +96,7 @@ module ActiveRecord
associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
:joins => "INNER JOIN #{options[:join_table]} as t0 ON #{reflection.klass.table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
:joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} as t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
:select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as _parent_record_id",
:order => options[:order])
@ -157,7 +156,7 @@ module ActiveRecord
if reflection.options[:source_type]
interface = reflection.source_reflection.options[:foreign_type]
preload_options = {:conditions => ["#{interface} = ?", reflection.options[:source_type]]}
preload_options = {:conditions => ["#{connection.quote_column_name interface} = ?", reflection.options[:source_type]]}
records.compact!
records.first.class.preload_associations(records, through_association, preload_options)
@ -216,7 +215,7 @@ module ActiveRecord
klass_name, id_map = *klass_and_id
klass = klass_name.constantize
table_name = klass.table_name
table_name = klass.quoted_table_name
primary_key = klass.primary_key
conditions = "#{table_name}.#{primary_key} IN (?)"
conditions << append_conditions(options, preload_options)
@ -229,16 +228,15 @@ module ActiveRecord
end
end
# FIXME: quoting
def find_associated_records(ids, reflection, preload_options)
options = reflection.options
table_name = reflection.klass.table_name
table_name = reflection.klass.quoted_table_name
if interface = reflection.options[:as]
conditions = "#{reflection.klass.table_name}.#{interface}_id IN (?) and #{reflection.klass.table_name}.#{interface}_type = '#{self.base_class.name.demodulize}'"
conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} IN (?) and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.name.demodulize}'"
else
foreign_key = reflection.primary_key_name
conditions = "#{reflection.klass.table_name}.#{foreign_key} IN (?)"
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} IN (?)"
end
conditions << append_conditions(options, preload_options)

View File

@ -9,11 +9,13 @@ require 'models/person'
require 'models/reader'
require 'models/owner'
require 'models/pet'
require 'models/reference'
require 'models/job'
class EagerAssociationTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors, :categories, :categories_posts,
:companies, :accounts, :tags, :taggings, :people, :readers,
:owners, :pets, :author_favorites
:owners, :pets, :author_favorites, :jobs, :references
def test_loading_with_one_association
posts = Post.find(:all, :include => :comments)
@ -194,6 +196,30 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
end
def test_eager_load_belongs_to_quotes_table_and_column_names
job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
references(:michael_unicyclist)
assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
end
def test_eager_load_has_one_quotes_table_and_column_names
michael = Person.find(people(:michael), :include => :favourite_reference)
references(:michael_unicyclist)
assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
end
def test_eager_load_has_many_quotes_table_and_column_names
michael = Person.find(people(:michael), :include => :references)
references(:michael_magician,:michael_unicyclist)
assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
end
def test_eager_load_has_many_through_quotes_table_and_column_names
michael = Person.find(people(:michael), :include => :jobs)
jobs(:magician, :unicyclist)
assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
end
def test_eager_association_loading_with_explicit_join
posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
assert_equal 1, posts.length

7
activerecord/test/fixtures/jobs.yml vendored Normal file
View File

@ -0,0 +1,7 @@
unicyclist:
id: 1
ideal_reference_id: 2
clown:
id: 2
magician:
id: 3

View File

@ -0,0 +1,17 @@
michael_magician:
id: 1
person_id: 1
job_id: 3
favourite: false
michael_unicyclist:
id: 2
person_id: 1
job_id: 1
favourite: true
david_unicyclist:
id: 3
person_id: 2
job_id: 1
favourite: false

View File

@ -0,0 +1,5 @@
class Job < ActiveRecord::Base
has_many :references
has_many :people, :through => :references
belongs_to :ideal_reference, :class_name => 'Reference'
end

View File

@ -2,4 +2,9 @@ class Person < ActiveRecord::Base
has_many :readers
has_many :posts, :through => :readers
has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null'
has_many :references
has_many :jobs, :through => :references
has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
end

View File

@ -0,0 +1,4 @@
class Reference < ActiveRecord::Base
belongs_to :person
belongs_to :job
end

View File

@ -162,6 +162,11 @@ ActiveRecord::Schema.define do
t.column :type, :string
end
create_table :jobs, :force => true do |t|
t.integer :ideal_reference_id
end
create_table :keyboards, :force => true, :id => false do |t|
t.primary_key :key_number
t.string :name
@ -197,6 +202,12 @@ ActiveRecord::Schema.define do
t.string :type
end
create_table :references, :force => true do |t|
t.integer :person_id
t.integer :job_id
t.boolean :favourite
end
create_table :minimalistics, :force => true do |t|
end