brendano (owner)

Revisions

gist: 62935 Download_button fork
public
Description:
2sql - turn a newline or whitespace-separated list into an SQL set/tuple/list literal
Public Clone URL: git://gist.github.com/62935.git
Embed All Files: show embed
2sql #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env ruby
 
# 112
# 114
# 531
#
# =>
#
# (112,114,531)
#
# Intended for copy-and-paste into SQL statements
# E.g. vim via: %!2sql
 
items = STDIN.read.strip.split
if items.all?{|x| x =~ /^[0-9]+$/}
  # they're fine raw, use as integers
else
  # make them string literals
  # um not sure what the single quote escape rule is
  items = items.map{|x| x.gsub("'", "\\'") }
  items = items.map{|x| "'#{x}'" }
end
 
puts "(" + items.join(",") + ")"