Business intelligence made simple
Added use_transaction option
Andrewc2eb0c7b87e3
diff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rbindex b49f7a85..7f95b7df 100644--- a/lib/blazer/data_source.rb+++ b/lib/blazer/data_source.rb@@ -34,19 +34,24 @@ module Blazersettings["timeout"]end+ def use_transaction?+ settings.key?("use_transaction") ? settings["use_transaction"] : true+ end+def run_statement(statement, options = {})rows = []error = nil- connection_model.transaction do+ comment = "blazer"+ if options[:user].respond_to?(:id)+ comment << ",user_id:#{options[:user].id}"+ end+ if options[:query].respond_to?(:id)+ comment << ",query_id:#{options[:query].id}"+ end++ in_transaction dobeginconnection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}") if timeout && postgresql?- comment = "blazer"- if options[:user].respond_to?(:id)- comment << ",user_id:#{options[:user].id}"- end- if options[:query].respond_to?(:id)- comment << ",query_id:#{options[:query].id}"- endresult = connection_model.connection.select_all("#{statement} /*#{comment}*/")result.each do |untyped_row|row = {}@@ -57,10 +62,9 @@ module Blazerendrescue ActiveRecord::StatementInvalid => eerror = e.message.sub(/.+ERROR: /, "")- ensure- raise ActiveRecord::Rollbackendend+[rows, error]end@@ -74,5 +78,21 @@ module Blazerdef postgresql?["PostgreSQL", "Redshift"].include?(connection_model.connection.adapter_name)end++ protected++ def in_transaction+ if use_transaction?+ connection_model.transaction do+ begin+ yield+ ensure+ raise ActiveRecord::Rollback+ end+ end+ else+ yield+ end+ endendend