batchquery.py
changeset 30 a8b7cd92f39f
parent 29 8636719a30f6
child 39 0cef3540b69f
equal deleted inserted replaced
29:8636719a30f6 30:a8b7cd92f39f
     9         toolbase.SimpleTool.__init__(self, name='batchquery', desc='Run a query using columns from CSV file as arguments.')
     9         toolbase.SimpleTool.__init__(self, name='batchquery', desc='Run a query using columns from CSV file as arguments.')
    10         self.parser.add_argument('--query', dest='query', type=str, help='Query to run. Use %s for arguments, or %(name)s for named arguments (see --names).')
    10         self.parser.add_argument('--query', dest='query', type=str, help='Query to run. Use %s for arguments, or %(name)s for named arguments (see --names).')
    11         self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
    11         self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
    12         self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
    12         self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
    13         self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
    13         self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
       
    14         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         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         self.init()
    16         self.init()
    16 
    17 
    17     def _split_line(self, line):
    18     def _split_line(self, line):
    18         return [x.strip() for x in line.split(',')]
    19         return [x.strip() for x in line.split(',')]
    44                     rows = curs.fetchall()
    45                     rows = curs.fetchall()
    45                     results.append((args, rows))
    46                     results.append((args, rows))
    46             curs.connection.commit()
    47             curs.connection.commit()
    47         # write results to output file
    48         # write results to output file
    48         if self.args.output:
    49         if self.args.output:
       
    50             format_row = None
       
    51             if self.args.outputfunc:
       
    52                 with open(self.args.outputfunc, 'r', encoding='utf8') as f:
       
    53                     d = dict()
       
    54                     exec(f.read(), d)
       
    55                     format_row = d['format_row']
       
    56             
    49             with open(self.args.output, 'w', encoding='utf8') as f:
    57             with open(self.args.output, 'w', encoding='utf8') as f:
    50                 for args, rows in results:
    58                 for args, rows in results:
    51                     f.write(repr(args))
    59                     if format_row:
    52                     f.write(' -> ')
    60                         f.write(format_row(args, rows))
    53                     f.write(repr(rows))
    61                     else:
       
    62                         f.write(repr(args))
       
    63                         f.write(' -> ')
       
    64                         f.write(repr(rows))
    54                     f.write('\n')
    65                     f.write('\n')
    55 
    66 
    56 
    67 
    57 tool = BatchQueryTool()
    68 tool = BatchQueryTool()
    58 tool.main()
    69 tool.main()