Update runquery tool: Add --one-query-per-line parameter.
authorRadek Brich <brich.radek@ifortuna.cz>
Tue, 06 May 2014 18:34:38 +0200
changeset 99 245646538743
parent 98 024299702087
child 100 d6088dba8fea
Update runquery tool: Add --one-query-per-line parameter.
runquery.py
--- a/runquery.py	Tue Apr 29 17:50:15 2014 +0200
+++ b/runquery.py	Tue May 06 18:34:38 2014 +0200
@@ -23,6 +23,7 @@
         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.parser.add_argument('--one-query-per-line', action='store_true', help='When reading queries from file, consider each line as separate query.')
         self.parser.add_argument('-p', '--parameter', dest='parameters', metavar='PARAM=VALUE', nargs='*',
             help="If query should be used as format template, these parameters will be substituted.")
         self.parser.add_argument('--output-file', dest='output_file', metavar='OUTPUT_FILE', help='Write query result in specified file.')
@@ -45,6 +46,10 @@
         if self.args.file:
             with open(self.args.file, 'r', encoding='utf8') as f:
                 data = f.read()
+            if self.args.one_query_per_line:
+                file_queries = [ln for ln in data.splitlines() if not ln.lstrip().startswith('--')]
+                self.queries = file_queries + self.queries
+            else:
                 self.queries.insert(0, data)
         # prepare parameters
         self._prepare_parameters(self.args.parameters)