Skip to content

Commit 1fa2969

Browse files
author
Frederic de Zorzi
committed
initial python version
1 parent c123fb3 commit 1fa2969

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

python/pg_dump2insert.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
import fileinput
3+
import re
4+
5+
mode = "clone"
6+
start_table_pattern = re.compile("^COPY (\w+) \(([\w, ]+)\) FROM stdin;")
7+
for line in fileinput.input():
8+
if mode == "clone":
9+
start_table = start_table_pattern.match(line)
10+
if start_table:
11+
table_name = start_table.group(1)
12+
fields = start_table.group(2)
13+
mode = "insert"
14+
continue
15+
print line,
16+
else:
17+
if line == "\\.\n":
18+
mode = "clone"
19+
continue
20+
values = [ v == '\\N' and 'NULL' or "'%s'" % v for v in line[:-1].split("\t") ]
21+
print "INSERT INTO %s (%s) VALUES (%s);" % (table_name, fields, ", ".join(values))
22+

0 commit comments

Comments
 (0)