runquery.py
changeset 92 8816c917979c
parent 91 fba99a54ec49
child 93 b72591087495
equal deleted inserted replaced
91:fba99a54ec49 92:8816c917979c
     9 from pgtoolkit import toolbase
     9 from pgtoolkit import toolbase
    10 
    10 
    11 import logging.handlers
    11 import logging.handlers
    12 import time
    12 import time
    13 from datetime import datetime, timedelta
    13 from datetime import datetime, timedelta
       
    14 
       
    15 from psycopg2 import ProgrammingError
    14 
    16 
    15 
    17 
    16 class RunQueryTool(toolbase.ToolBase):
    18 class RunQueryTool(toolbase.ToolBase):
    17 
    19 
    18     def __init__(self):
    20     def __init__(self):
    64         logging.getLogger('pgmanager_notices').addHandler(handler)
    66         logging.getLogger('pgmanager_notices').addHandler(handler)
    65 
    67 
    66     def main(self):
    68     def main(self):
    67         """Execute the queries."""
    69         """Execute the queries."""
    68         for q in self.queries:
    70         for q in self.queries:
    69             q = q.format(**self.parameters)
    71             if self.parameters:
       
    72                 q = q.format(**self.parameters)
    70             self.log.info('%s', q if len(q) < 100 else q[:100]+'...')
    73             self.log.info('%s', q if len(q) < 100 else q[:100]+'...')
    71             with self.pgm.cursor('target') as curs:
    74             with self.pgm.cursor('target') as curs:
    72                 curs.execute(q)
    75                 curs.execute(q)
    73                 self.log.info('Rows affected: %d', curs.rowcount)
    76                 self.log.info('Rows affected: %d', curs.rowcount)
    74                 rows = curs.fetchall_dict()
    77                 try:
    75                 self._write_output_file(rows)
    78                     rows = curs.fetchall_dict()
       
    79                     self._write_output_file(rows)
       
    80                 except ProgrammingError:
       
    81                     pass
    76         self.log.info('Done')
    82         self.log.info('Done')
    77 
    83 
    78     def _write_output_file(self, rows):
    84     def _write_output_file(self, rows):
    79         if not self.args.output_file:
    85         if not self.args.output_file:
    80             return
    86             return