public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
Speeded up eager loading a whole bunch

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1212 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
dhh (author)
Mon Apr 18 11:49:34 -0700 2005
commit  49d0f0cb66bc7b6d9cf69bd07b168669b3818cee
tree    71b84270335a7dbcd7af6b0119654400fee9928c
parent  3b9bf64130df2512d058d841341d9a082493141e
...
683
684
685
686
687
688
689
 
 
 
 
 
 
 
690
691
692
693
694
695
696
 
 
697
698
699
700
 
 
 
 
701
702
703
704
 
 
 
705
706
707
708
 
 
709
710
 
 
 
 
 
711
712
713
 
 
714
715
716
...
736
737
738
739
740
 
 
 
 
 
 
741
 
742
743
744
...
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
 
 
 
 
797
798
799
800
801
802
803
804
805
806
807
808
809
810
 
 
811
812
813
...
683
684
685
 
 
 
 
686
687
688
689
690
691
692
693
 
 
 
 
 
694
695
696
697
698
 
 
699
700
701
702
703
 
 
 
704
705
706
707
708
709
710
711
712
713
 
714
715
716
717
718
719
 
 
720
721
722
723
724
...
744
745
746
 
 
747
748
749
750
751
752
753
754
755
756
757
...
788
789
790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791
 
 
 
 
792
793
794
795
796
 
 
 
 
 
 
 
 
 
 
 
 
 
797
798
799
800
801
0
@@ -683,34 +683,42 @@ module ActiveRecord
0
           schema_abbreviations = generate_schema_abbreviations(reflections)
0
           primary_key_table = generate_primary_key_table(reflections, schema_abbreviations)
0
 
0
- rows = select_all_rows(options, schema_abbreviations, reflections)
0
- records = extract_and_instantiate_records(schema_abbreviations, rows)
0
-
0
- assign_associations_to_records(rows, records, reflections, schema_abbreviations, primary_key_table)
0
+ rows = select_all_rows(options, schema_abbreviations, reflections)
0
+ records = { }
0
+ primary_key = primary_key_table[table_name]
0
+
0
+ for row in rows
0
+ id = row[primary_key]
0
+ records[id] ||= instantiate(extract_record(schema_abbreviations, table_name, row))
0
           
0
- return records
0
- end
0
-
0
- def assign_associations_to_records(rows, records, reflections, schema_abbreviations, primary_key_table)
0
- records.each do |record|
0
             reflections.each do |reflection|
0
+ next unless row[primary_key_table[reflection.table_name]]
0
+
0
               case reflection.macro
0
                 when :has_many, :has_and_belongs_to_many
0
- record.send(reflection.name).target =
0
- extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection)
0
+ records[id].send(reflection.name)
0
+ records[id].instance_variable_get("@#{reflection.name}").target.push(
0
+ reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
0
+ )
0
                 when :has_one, :belongs_to
0
- record.send(
0
- "set_#{reflection.name}_target",
0
- extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection).first
0
+ records[id].send(
0
+ "#{reflection.name}=",
0
+ reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
0
                   )
0
               end
0
             end
0
           end
0
+
0
+ return records.values
0
         end
0
-
0
+
0
+ def reflect_on_included_associations(associations)
0
+ [ associations ].flatten.collect { |association| reflect_on_association(association) }
0
+ end
0
+
0
         def generate_schema_abbreviations(reflections)
0
- schema = [ [ table_name, columns.collect { |c| c.name } ] ]
0
- schema += reflections.collect { |r| [ r.klass.table_name, r.klass.columns.collect { |c| c.name } ] }
0
+ schema = [ [ table_name, column_names ] ]
0
+ schema += reflections.collect { |r| [ r.table_name, r.klass.column_names ] }
0
 
0
           schema_abbreviations = {}
0
           schema.each_with_index do |table_and_columns, i|
0
@@ -736,9 +744,14 @@ module ActiveRecord
0
         end
0
 
0
 
0
- def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections)
0
- habtm_associations = reflections.find_all { |r| r.macro == :has_and_belongs_to_many }
0
+ def select_all_rows(options, schema_abbreviations, reflections)
0
+ connection.select_all(
0
+ construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections),
0
+ "#{name} Load Including Associations"
0
+ )
0
+ end
0
 
0
+ def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections)
0
           sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{table_name} "
0
           sql << reflections.collect { |reflection| association_join(reflection) }.to_s
0
           sql << "#{options[:joins]} " if options[:joins]
0
@@ -775,39 +788,14 @@ module ActiveRecord
0
         end
0
 
0
 
0
- def extract_and_instantiate_records(schema_abbreviations, rows)
0
- rows.collect { |row| instantiate(extract_record(schema_abbreviations, table_name, row)) }.uniq
0
- end
0
-
0
- def extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection)
0
- association = rows.collect do |row|
0
- if row[primary_key_table[table_name]].to_s == record.id.to_s && !row[primary_key_table[reflection.klass.table_name]].nil?
0
- reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.klass.table_name, row))
0
- end
0
- end
0
-
0
- return association.uniq.compact
0
- end
0
-
0
         def extract_record(schema_abbreviations, table_name, row)
0
- row.inject({}) do |record, pair|
0
- prefix, column_name = schema_abbreviations[pair.first]
0
- record[column_name] = pair.last if prefix == table_name
0
- record
0
+ record = {}
0
+ row.each do |column, value|
0
+ prefix, column_name = schema_abbreviations[column]
0
+ record[column_name] = value if prefix == table_name
0
           end
0
- end
0
-
0
-
0
- def reflect_on_included_associations(associations)
0
- [ associations ].flatten.collect { |association| reflect_on_association(association) }
0
- end
0
-
0
- def select_all_rows(options, schema_abbreviations, reflections)
0
- connection.select_all(
0
- construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections),
0
- "#{name} Load Including Associations"
0
- )
0
- end
0
+ return record
0
+ end
0
     end
0
   end
0
 end
...
32
33
34
 
 
 
 
35
36
37
...
32
33
34
35
36
37
38
39
40
41
0
@@ -32,6 +32,10 @@ module ActiveRecord
0
         @loaded
0
       end
0
       
0
+ def target
0
+ @target
0
+ end
0
+
0
       def target=(t)
0
         @target = t
0
         @loaded = true
...
618
619
620
 
 
 
 
621
622
623
...
640
641
642
643
 
644
645
646
...
618
619
620
621
622
623
624
625
626
627
...
644
645
646
 
647
648
649
650
0
@@ -618,6 +618,10 @@ module ActiveRecord #:nodoc:
0
       def columns_hash
0
         @columns_hash ||= columns.inject({}) { |hash, column| hash[column.name] = column; hash }
0
       end
0
+
0
+ def column_names
0
+ @column_names ||= columns_hash.keys
0
+ end
0
 
0
       # Returns an array of columns objects where the primary id, all columns ending in "_id" or "_count",
0
       # and columns used for single table inheritance has been removed.
0
@@ -640,7 +644,7 @@ module ActiveRecord #:nodoc:
0
       
0
       # Resets all the cached information about columns, which will cause they to be reloaded on the next request.
0
       def reset_column_information
0
- @columns = @columns_hash = @content_columns = @dynamic_methods_hash = nil
0
+ @column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = nil
0
       end
0
 
0
       def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc:
...
114
115
116
117
 
 
 
 
 
118
119
120
...
114
115
116
 
117
118
119
120
121
122
123
124
0
@@ -114,7 +114,11 @@ module ActiveRecord
0
     # Holds all the meta-data about an association as it was specified in the Active Record class.
0
     class AssociationReflection < MacroReflection #:nodoc:
0
       def klass
0
- active_record.send(:compute_type, (name_to_class_name(name.id2name)))
0
+ @klass ||= active_record.send(:compute_type, (name_to_class_name(name.id2name)))
0
+ end
0
+
0
+ def table_name
0
+ @table_name ||= klass.table_name
0
       end
0
 
0
       private

Comments

    No one has commented yet.