Class: QueryExecution

Inherits:
Object
  • Object
show all
Defined in:
app/models/query_execution.rb

Constant Summary collapse

NUM_SAMPLE_ROWS =
100
SNOWFLAKE_UNLOAD_SQL =
"COPY INTO %{location} FROM (\n%{query}\n)\nFILE_FORMAT = (TYPE = 'csv' FIELD_DELIMITER = ',' RECORD_DELIMITER = '\\\\n' FIELD_OPTIONALLY_ENCLOSED_BY = '\"' \n  NULL_IF = ('') COMPRESSION = NONE)\nHEADER = TRUE \nSINGLE = TRUE\nOVERWRITE = TRUE\nMAX_FILE_SIZE = %{max_file_size}\n"

Class Method Summary collapse

Class Method Details

.perform(result_id, role) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/query_execution.rb', line 16

def self.perform(result_id, role)
  result = Result.find(result_id)
  csv_service = CsvService.new(result_id)

  unless Role.configured_connections.include?(role)
    raise "Role '#{role}' does not have connection credentials configured."
  end

  body = result.compiled_body
  result.mark_running!
  sample_callback = ->(sample) { result.mark_processing_from_sample(sample) }

  connection = AnalyticDBConnectionPool.instance.get(role)
  if connection.is_a? RedshiftPG::Connection
    query_redshift(connection, body, result, sample_callback, csv_service)
  else
    query_snowflake(connection, body, result, sample_callback)
  end

rescue => e
  if result && csv_service
    csv_service.clear_tmp_file
    result.mark_failed!(e.message)
  end
  raise
end