Skip to content

Instantly share code, notes, and snippets.

@flyingmachine
Created January 28, 2013 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyingmachine/4657119 to your computer and use it in GitHub Desktop.
Save flyingmachine/4657119 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Simple, not robust script for pairing sql fields to values
# example usage: pbpaste | ./sql_pairs.rb
sql = STDIN.read
# sql = %{INSERT INTO "users" ("username", "email") VALUES ('joe schmoe', 'joe@schmoe.com')}
begin
fields = /\((.*?)\)/.match(sql)[1].split(",")
values = /values \((.*?)\)/i.match(sql)[1].split(",")
0.upto(fields.size) do |i|
puts "#{fields[i]} => #{values[i]}"
end
rescue => e
puts "Could not find pairs for string: #{sql}"
raise e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment