ソースを参照

Created helper for run statement

Andrew Kane 9 年 前
コミット
f58994dcb3
1 ファイル変更35 行追加29 行削除
  1. 35 29
      lib/blazer/data_source.rb

+ 35 - 29
lib/blazer/data_source.rb

@@ -69,9 +69,6 @@ module Blazer
       end
 
       unless rows
-        columns = []
-        rows = []
-
         comment = "blazer"
         if options[:user].respond_to?(:id)
           comment << ",user_id:#{options[:user].id}"
@@ -83,32 +80,7 @@ module Blazer
         if options[:query].respond_to?(:id)
           comment << ",query_id:#{options[:query].id}"
         end
-
-        in_transaction do
-          begin
-            if timeout
-              if postgresql? || redshift?
-                connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}")
-              elsif mysql?
-                connection_model.connection.execute("SET max_execution_time = #{timeout.to_i * 1000}")
-              else
-                raise Blazer::TimeoutNotSupported, "Timeout not supported for #{adapter_name} adapter"
-              end
-            end
-
-            result = connection_model.connection.select_all("#{statement} /*#{comment}*/")
-            columns = result.columns
-            cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value
-            result.rows.each do |untyped_row|
-              rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| result.column_types[c].send(cast_method, untyped_row[i]) })
-            end
-          rescue ActiveRecord::StatementInvalid => e
-            error = e.message.sub(/.+ERROR: /, "")
-            error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) }
-          end
-        end
-
-        Blazer.cache.write(cache_key, Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache
+        columns, rows, error = run_statement_helper(statement, comment)
       end
 
       [columns, rows, error, cached_at]
@@ -150,6 +122,40 @@ module Blazer
 
     protected
 
+    def run_statement_helper(statement, comment)
+      columns = []
+      rows = []
+      error = nil
+
+      in_transaction do
+        begin
+          if timeout
+            if postgresql? || redshift?
+              connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}")
+            elsif mysql?
+              connection_model.connection.execute("SET max_execution_time = #{timeout.to_i * 1000}")
+            else
+              raise Blazer::TimeoutNotSupported, "Timeout not supported for #{adapter_name} adapter"
+            end
+          end
+
+          result = connection_model.connection.select_all("#{statement} /*#{comment}*/")
+          columns = result.columns
+          cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value
+          result.rows.each do |untyped_row|
+            rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| result.column_types[c].send(cast_method, untyped_row[i]) })
+          end
+        rescue ActiveRecord::StatementInvalid => e
+          error = e.message.sub(/.+ERROR: /, "")
+          error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) }
+        end
+      end
+
+      Blazer.cache.write(cache_key, Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache
+
+      [columns, rows, error]
+    end
+
     def adapter_name
       connection_model.connection.adapter_name
     end