batchupdate.py
changeset 32 d59c473c9ad7
child 33 bd0beda49bcb
equal deleted inserted replaced
31:c2e6e24b83d9 32:d59c473c9ad7
       
     1 #!/usr/bin/env python3.2
       
     2 
       
     3 import time
       
     4 
       
     5 from pgtoolkit import toolbase
       
     6 from pgtoolkit.highlight import highlight
       
     7 
       
     8 
       
     9 class BatchUpdateTool(toolbase.SimpleTool):
       
    10     def __init__(self):
       
    11         toolbase.SimpleTool.__init__(self, name='batchupdate', desc='Run an UPDATE query until it does not update any rows.')
       
    12         self.parser.add_argument('--query', dest='query', type=str, help='Query to run. It should be constructed using subquery with LIMIT.')
       
    13         self.parser.add_argument('--sleep', dest='sleep', default=0, type=int, help='Sleep SLEEP seconds between subsequent queries.')
       
    14         self.init()
       
    15 
       
    16     def main(self):
       
    17         # connect DB
       
    18         with self.pgm.cursor('target') as curs:
       
    19             rowcount = 1
       
    20             while rowcount > 0:
       
    21                 print('query:', self.args.query)
       
    22                 curs.execute(self.args.query, [])
       
    23                 rowcount = curs.rowcount
       
    24                 print('updated', rowcount)
       
    25                 curs.connection.commit()
       
    26                 print('sleep %s seconds' % self.args.sleep)
       
    27                 time.sleep(self.args.sleep)
       
    28 
       
    29 
       
    30 tool = BatchUpdateTool()
       
    31 tool.main()
       
    32