Skip to content

Commit

Permalink
Merge pull request #20 from derencius/postgres
Browse files Browse the repository at this point in the history
Postgres Support
  • Loading branch information
nettofarah committed Dec 4, 2015
2 parents 874c4c4 + e88b443 commit b69b915
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/polo/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ def initialize(base_class, id, dependency_tree={})
# ActiveSupport::Notifications block and collecting every generate SQL query.
#
def collect
ActiveSupport::Notifications.subscribed(collector, 'sql.active_record') do
base_finder = @base_class.includes(@dependency_tree).where(@base_class.primary_key => @id)
collect_sql(@base_class, base_finder.to_sql)
base_finder.to_a
unprepared_statement do
ActiveSupport::Notifications.subscribed(collector, 'sql.active_record') do
base_finder = @base_class.includes(@dependency_tree).where(@base_class.primary_key => @id)
collect_sql(@base_class, base_finder.to_sql)
base_finder.to_a
end
end

@selects.compact.uniq
Expand All @@ -33,8 +35,9 @@ def collect
#
def collector
lambda do |name, start, finish, id, payload|
return unless payload[:name] =~ /^(.*) Load$/
begin
class_name = payload[:name].gsub(' Load', '').constantize
class_name = $1.constantize
sql = payload[:sql]
collect_sql(class_name, sql)
rescue ActiveRecord::StatementInvalid, NameError
Expand All @@ -49,5 +52,15 @@ def collect_sql(klass, sql)
sql: sql
}
end

def unprepared_statement
if ActiveRecord::Base.connection.respond_to?(:unprepared_statement)
ActiveRecord::Base.connection.unprepared_statement do
yield
end
else
yield
end
end
end
end

0 comments on commit b69b915

Please sign in to comment.