Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created September 21, 2018 21:34
Show Gist options
  • Save mikehale/1b469fd796c50fc5fbb9d8a9ec0468c0 to your computer and use it in GitHub Desktop.
Save mikehale/1b469fd796c50fc5fbb9d8a9ec0468c0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Read CSV data from STDIN, output a WITH clause containing the CSV data
require 'csv'
csv = CSV.parse($stdin, headers: true, converters: :numeric, header_converters: :symbol)
columns = csv.headers.join(",")
cte_name = ENV.fetch("CTE_NAME", 'temp')
puts "WITH #{cte_name} (#{columns}) AS (VALUES " +
csv.map { |row|
row.fields.map { |f|
case f
when Numeric
f
else
"'#{f}'"
end
}.join(",")
}.join(",") + ")"
# WITH temp (k,v) AS (VALUES (0,-9999), (1, 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment