Class: RedshiftConnector::UnloadQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_connector/query.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, bundle:, enable_sort: false) ⇒ UnloadQuery

Returns a new instance of UnloadQuery.



51
52
53
54
55
# File 'lib/redshift_connector/query.rb', line 51

def initialize(query:, bundle:, enable_sort: false)
  @query = query
  @bundle = bundle
  @enable_sort = enable_sort
end

Class Method Details

.wrap(query:, bundle:, enable_sort: false) ⇒ Object



47
48
49
# File 'lib/redshift_connector/query.rb', line 47

def UnloadQuery.wrap(query:, bundle:, enable_sort: false)
  new(query: ArbitraryQuery.new(query), bundle: bundle, enable_sort: enable_sort)
end

Instance Method Details

#descriptionObject



61
62
63
# File 'lib/redshift_connector/query.rb', line 61

def description
  @query.description
end

#escape_query(query) ⇒ Object



77
78
79
# File 'lib/redshift_connector/query.rb', line 77

def escape_query(query)
  query.gsub("'", "\\\\'")
end

#table_specObject



57
58
59
# File 'lib/redshift_connector/query.rb', line 57

def table_spec
  @query.table_spec
end

#to_sqlObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redshift_connector/query.rb', line 65

def to_sql
  <<-EndSQL.gsub(/^\s+/, '')
    unload ('#{escape_query(@query.to_sql)}')
    to '#{@bundle.url}'
    credentials '#{@bundle.credential_string}'
    gzip
    allowoverwrite
    parallel #{@enable_sort ? 'off' : 'on'}
    delimiter ',' escape addquotes
  EndSQL
end