batchquery.py
changeset 105 10551741f61f
parent 104 d8ff52a0390f
child 106 db4c582a2abd
equal deleted inserted replaced
104:d8ff52a0390f 105:10551741f61f
     1 #!/usr/bin/env python3
       
     2 
       
     3 from pydbkit import toolbase
       
     4 
       
     5 
       
     6 class BatchQueryTool(toolbase.SimpleTool):
       
     7     def __init__(self):
       
     8         toolbase.SimpleTool.__init__(self, name='batchquery', desc='Run a query using columns from CSV file as arguments.')
       
     9         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).')
       
    10         self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
       
    11         self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
       
    12         self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
       
    13         self.parser.add_argument('--outputfunc', dest='outputfunc', type=str, help='Python function which will format results (format_row(args, rows)).')
       
    14         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.')
       
    15 
       
    16     def _split_line(self, line):
       
    17         return [x.strip() for x in line.split(',')]
       
    18 
       
    19     def main(self):
       
    20         results = []
       
    21         # load query from file
       
    22         with open(self.args.query, 'r', encoding='utf8') as f:
       
    23             query = f.read()
       
    24         # connect DB
       
    25         with self.pgm.cursor('target') as curs:
       
    26             # run init query
       
    27             if self.args.init:
       
    28                 with open(self.args.init, 'r', encoding='utf8') as f:
       
    29                     curs.execute(f.read(), [])
       
    30             # read CSV file
       
    31             with open(self.args.file, 'r', encoding='utf8') as f:
       
    32                 # read header
       
    33                 names = None
       
    34                 if self.args.header:
       
    35                     line = f.readline()
       
    36                     names = self._split_line(line)
       
    37                 # read and process lines
       
    38                 for line in f:
       
    39                     args = self._split_line(line)
       
    40                     if names:
       
    41                         args = dict(zip(names, args))
       
    42                     curs.execute(query, args)
       
    43                     rows = curs.fetchall()
       
    44                     results.append((args, rows))
       
    45             curs.connection.commit()
       
    46         # write results to output file
       
    47         if self.args.output:
       
    48             format_row = None
       
    49             if self.args.outputfunc:
       
    50                 with open(self.args.outputfunc, 'r', encoding='utf8') as f:
       
    51                     d = dict()
       
    52                     exec(f.read(), d)
       
    53                     format_row = d['format_row']
       
    54 
       
    55             with open(self.args.output, 'w', encoding='utf8') as f:
       
    56                 for args, rows in results:
       
    57                     if format_row:
       
    58                         f.write(format_row(args, rows))
       
    59                     else:
       
    60                         f.write(repr(args))
       
    61                         f.write(' -> ')
       
    62                         f.write(repr(rows))
       
    63                     f.write('\n')
       
    64 
       
    65 
       
    66 tool = BatchQueryTool()
       
    67 tool.setup()
       
    68 tool.main()
       
    69