Permalink
Browse files

Add some hooks for analyzing which associations to preload, possibly …

…also which records
  • Loading branch information...
1 parent c1dff20 commit e17b794e525317be47b2c7ff998c2cfb66aa45dc @sdsykes sdsykes committed Mar 22, 2009
Showing with 37 additions and 11 deletions.
  1. +21 −3 lib/callsite.rb
  2. +8 −4 lib/optimizations/associations/macro.rb
  3. +8 −4 lib/optimizations/columns/macro.rb
View
@@ -27,9 +27,12 @@ def column!( column )
# Flag an association as seen
#
- def association!( association )
- Mtx.synchronize do
- associations << association if preloadable_association?( association )
+ def association!(association, record_id)
+ if preloadable_association?(association)
+ Mtx.synchronize do
+ associations << association
+ end
+ register_record_id(record_id)
end
end
@@ -55,6 +58,21 @@ def associations
@associations ||= setup_associations
end
+ # Analyze previously collected information
+ # and reset ready for a new query
+ #
+ def reset
+ # placeholder
+ end
+
+ def register_result_set(result_set)
+ # placeholder
+ end
+
+ def register_record_id(record_id)
+ # placeholder
+ end
+
private
def associations_for_inspect
@@ -34,9 +34,13 @@ def self.extended( base )
end
def preload_scrooge_associations(result_set, callsite_sig)
- scrooge_preloading_exclude do
- callsite_associations = scrooge_callsite(callsite_sig).associations.to_a
- preload_associations(result_set, callsite_associations) unless callsite_associations.empty?
+ if result_set.size > 1
+ scrooge_preloading_exclude do
+ callsite_associations = scrooge_callsite(callsite_sig).associations
+ unless callsite_associations.empty?
+ preload_associations(result_set, callsite_associations)
+ end
+ end
end
end
@@ -77,7 +81,7 @@ def load_target_with_scrooge
#
def scrooge_seen_association!( association )
if @owner.scrooged? && !@loaded
- @owner.class.scrooge_callsite(callsite_signature).association!(association)
+ @owner.class.scrooge_callsite(callsite_signature).association!(association, @owner.id)
end
end
@@ -73,24 +73,28 @@ def scope_with_scrooge?( sql )
#
def find_by_sql_with_scrooge(sql)
callsite_sig = callsite_signature( caller, callsite_sql( sql ) )
- callsite_columns = scrooge_callsite(callsite_sig).columns
- sql = sql.gsub(scrooge_select_regex, "SELECT #{scrooge_select_sql(callsite_columns)} FROM")
+ callsite = scrooge_callsite(callsite_sig)
+ callsite.reset
+
+ sql = sql.gsub(scrooge_select_regex, "SELECT #{scrooge_select_sql(callsite.columns)} FROM")
results = connection.select_all(sanitize_sql(sql), "#{name} Load Scrooged")
result_set = ResultSets::ResultArray.new
updateable = ResultSets::UpdateableResultSet.new(result_set, self)
results.inject(result_set) do |memo, record|
- memo << instantiate(ScroogedAttributes.setup(record, callsite_columns, self, callsite_sig, updateable))
+ memo << instantiate(ScroogedAttributes.setup(record, callsite.columns, self, callsite_sig, updateable))
end
+ callsite.register_result_set(result_set)
+
if Associations::Macro.scrooge_installed?
preload_scrooge_associations(result_set, callsite_sig)
end
result_set
end
- def find_by_sql_without_scrooge( sql)
+ def find_by_sql_without_scrooge(sql)
connection.select_all(sanitize_sql(sql), "#{name} Load").collect! do |record|
instantiate( UnscroogedAttributes.setup(record) )
end

0 comments on commit e17b794

Please sign in to comment.