17 def __init__(self): |
17 def __init__(self): |
18 toolbase.ToolBase.__init__(self, name='runquery', desc='Run configured queries.') |
18 toolbase.ToolBase.__init__(self, name='runquery', desc='Run configured queries.') |
19 self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database') |
19 self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database') |
20 self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).') |
20 self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).') |
21 self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.') |
21 self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.') |
|
22 self.parser.add_argument('-f', dest='file', metavar='FILE', help='Read query from file.') |
22 |
23 |
23 self.config.add_option('target', type=str, default=None) |
24 self.config.add_option('target', type=str, default=None) |
24 self.config.add_option('queries', type=list, default=[]) |
25 self.config.add_option('queries', type=list, default=[]) |
25 self.config.add_option('log_path', type=str) |
26 self.config.add_option('log_path', type=str) |
26 |
27 |
31 def init(self): |
32 def init(self): |
32 toolbase.ToolBase.init(self) |
33 toolbase.ToolBase.init(self) |
33 if self.args.config: |
34 if self.args.config: |
34 self.config.load(self.args.config) |
35 self.config.load(self.args.config) |
35 self.queries = self.args.queries or self.config.queries |
36 self.queries = self.args.queries or self.config.queries |
|
37 # read query from file |
|
38 if self.args.file: |
|
39 with open(self.args.file, 'r', encoding='utf8') as f: |
|
40 data = f.read() |
|
41 self.queries.insert(0, data) |
36 if self.config.log_path: |
42 if self.config.log_path: |
37 self.init_file_logs(self.config.log_path) |
43 self.init_file_logs(self.config.log_path) |
38 self.prepare_conns(target = self.args.target or self.config.target) |
44 self.prepare_conns(target = self.args.target or self.config.target) |
39 |
45 |
40 def init_file_logs(self, path): |
46 def init_file_logs(self, path): |
51 logging.getLogger('pgmanager_notices').addHandler(handler) |
57 logging.getLogger('pgmanager_notices').addHandler(handler) |
52 |
58 |
53 def main(self): |
59 def main(self): |
54 """Execute the queries.""" |
60 """Execute the queries.""" |
55 for q in self.queries: |
61 for q in self.queries: |
56 self.log.info('%s', q) |
62 self.log.info('%s', q if len(q) < 100 else q[:100]+'...') |
57 with self.pgm.cursor('target') as curs: |
63 with self.pgm.cursor('target') as curs: |
58 rows = curs.execute(q) |
64 rows = curs.execute(q) |
59 self.log.info('Rows affected: %d', curs.rowcount) |
65 self.log.info('Rows affected: %d', curs.rowcount) |
60 self.log.info('Result: %r', rows) |
66 self.log.info('Result: %r', rows) |
61 self.log.info('Done') |
67 self.log.info('Done') |