Update runquery: Add parameter to read queries from file.
authorRadek Brich <radek.brich@devl.cz>
Fri, 04 Oct 2013 16:38:24 +0200
changeset 90 43e5d2cf4ddb
parent 89 6b72d61837b1
child 91 fba99a54ec49
Update runquery: Add parameter to read queries from file.
runquery.py
--- a/runquery.py	Fri Oct 04 16:37:29 2013 +0200
+++ b/runquery.py	Fri Oct 04 16:38:24 2013 +0200
@@ -19,6 +19,7 @@
         self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database')
         self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).')
         self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.')
+        self.parser.add_argument('-f', dest='file', metavar='FILE', help='Read query from file.')
 
         self.config.add_option('target', type=str, default=None)
         self.config.add_option('queries', type=list, default=[])
@@ -33,6 +34,11 @@
         if self.args.config:
             self.config.load(self.args.config)
         self.queries = self.args.queries or self.config.queries
+        # read query from file
+        if self.args.file:
+            with open(self.args.file, 'r', encoding='utf8') as f:
+                data = f.read()
+                self.queries.insert(0, data)
         if self.config.log_path:
             self.init_file_logs(self.config.log_path)
         self.prepare_conns(target = self.args.target or self.config.target)
@@ -53,7 +59,7 @@
     def main(self):
         """Execute the queries."""
         for q in self.queries:
-            self.log.info('%s', q)
+            self.log.info('%s', q if len(q) < 100 else q[:100]+'...')
             with self.pgm.cursor('target') as curs:
                 rows = curs.execute(q)
                 self.log.info('Rows affected: %d', curs.rowcount)