batchquery.py
author Radek Brich <radek.brich@devl.cz>
Tue, 14 Feb 2012 18:15:56 +0100
changeset 29 8636719a30f6
child 30 a8b7cd92f39f
permissions -rwxr-xr-x
Add batchquery tool.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3.2
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from pgtoolkit import toolbase
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
from pgtoolkit.highlight import highlight
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
class BatchQueryTool(toolbase.SimpleTool):
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    def __init__(self):
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
        toolbase.SimpleTool.__init__(self, name='batchquery', desc='Run a query using columns from CSV file as arguments.')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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).')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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.')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        self.init()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
    def _split_line(self, line):
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        return [x.strip() for x in line.split(',')]
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
    def main(self):
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        results = []
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        # load query from file
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        with open(self.args.query, 'r', encoding='utf8') as f:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
            query = f.read()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        # connect DB
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        with self.pgm.cursor('target') as curs:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
            # run init query
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
            if self.args.init:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
                with open(self.args.init, 'r', encoding='utf8') as f:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
                    curs.execute(f.read(), [])
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
            # read CSV file
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
            with open(self.args.file, 'r', encoding='utf8') as f:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
                # read header
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
                names = None
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
                if self.args.header:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
                    line = f.readline()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
                    names = self._split_line(line)
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
                # read and process lines
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
                for line in f:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
                    args = self._split_line(line)
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
                    if names:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
                        args = dict(zip(names, args))
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
                    curs.execute(query, args)
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
                    rows = curs.fetchall()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
                    results.append((args, rows))
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
            curs.connection.commit()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        # write results to output file
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        if self.args.output:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
            with open(self.args.output, 'w', encoding='utf8') as f:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
                for args, rows in results:
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
                    f.write(repr(args))
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
                    f.write(' -> ')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
                    f.write(repr(rows))
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
                    f.write('\n')
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
tool = BatchQueryTool()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
tool.main()
8636719a30f6 Add batchquery tool.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59