author | Radek Brich <radek.brich@devl.cz> |
Sun, 10 Mar 2013 16:14:53 +0100 | |
changeset 76 | 3a41b351b122 |
parent 39 | 0cef3540b69f |
child 83 | 515fadd3d286 |
permissions | -rwxr-xr-x |
29 | 1 |
#!/usr/bin/env python3.2 |
2 |
||
3 |
from pgtoolkit import toolbase |
|
4 |
from pgtoolkit.highlight import highlight |
|
5 |
||
6 |
||
7 |
class BatchQueryTool(toolbase.SimpleTool): |
|
8 |
def __init__(self): |
|
9 |
toolbase.SimpleTool.__init__(self, name='batchquery', desc='Run a query using columns from CSV file as arguments.') |
|
39 | 10 |
self.parser.add_argument('--query', dest='query', type=str, help='Query to run. Use %%s for arguments, or %%(name)s for named arguments (see --header).') |
29 | 11 |
self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.') |
12 |
self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).') |
|
13 |
self.parser.add_argument('--output', dest='output', type=str, help='File name for results.') |
|
30
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
14 |
self.parser.add_argument('--outputfunc', dest='outputfunc', type=str, help='Python function which will format results (format_row(args, rows)).') |
29 | 15 |
self.parser.add_argument('--header', dest='header', action='store_true', help='First line of CSV is header with names for columns. These name can be used in query.') |
16 |
self.init() |
|
17 |
||
18 |
def _split_line(self, line): |
|
19 |
return [x.strip() for x in line.split(',')] |
|
20 |
||
21 |
def main(self): |
|
22 |
results = [] |
|
23 |
# load query from file |
|
24 |
with open(self.args.query, 'r', encoding='utf8') as f: |
|
25 |
query = f.read() |
|
26 |
# connect DB |
|
27 |
with self.pgm.cursor('target') as curs: |
|
28 |
# run init query |
|
29 |
if self.args.init: |
|
30 |
with open(self.args.init, 'r', encoding='utf8') as f: |
|
31 |
curs.execute(f.read(), []) |
|
32 |
# read CSV file |
|
33 |
with open(self.args.file, 'r', encoding='utf8') as f: |
|
34 |
# read header |
|
35 |
names = None |
|
36 |
if self.args.header: |
|
37 |
line = f.readline() |
|
38 |
names = self._split_line(line) |
|
39 |
# read and process lines |
|
40 |
for line in f: |
|
41 |
args = self._split_line(line) |
|
42 |
if names: |
|
43 |
args = dict(zip(names, args)) |
|
44 |
curs.execute(query, args) |
|
45 |
rows = curs.fetchall() |
|
46 |
results.append((args, rows)) |
|
47 |
curs.connection.commit() |
|
48 |
# write results to output file |
|
49 |
if self.args.output: |
|
30
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
50 |
format_row = None |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
51 |
if self.args.outputfunc: |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
52 |
with open(self.args.outputfunc, 'r', encoding='utf8') as f: |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
53 |
d = dict() |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
54 |
exec(f.read(), d) |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
55 |
format_row = d['format_row'] |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
56 |
|
29 | 57 |
with open(self.args.output, 'w', encoding='utf8') as f: |
58 |
for args, rows in results: |
|
30
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
59 |
if format_row: |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
60 |
f.write(format_row(args, rows)) |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
61 |
else: |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
62 |
f.write(repr(args)) |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
63 |
f.write(' -> ') |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
64 |
f.write(repr(rows)) |
29 | 65 |
f.write('\n') |
66 |
||
67 |
||
68 |
tool = BatchQueryTool() |
|
69 |
tool.main() |
|
70 |