batchupdate.py
author Radek Brich <radek.brich@devl.cz>
Fri, 23 Mar 2012 14:54:04 +0100
changeset 32 d59c473c9ad7
child 33 bd0beda49bcb
permissions -rwxr-xr-x
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3.2
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import time
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
from pgtoolkit import toolbase
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
from pgtoolkit.highlight import highlight
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
class BatchUpdateTool(toolbase.SimpleTool):
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    def __init__(self):
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        toolbase.SimpleTool.__init__(self, name='batchupdate', desc='Run an UPDATE query until it does not update any rows.')
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.parser.add_argument('--query', dest='query', type=str, help='Query to run. It should be constructed using subquery with LIMIT.')
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.parser.add_argument('--sleep', dest='sleep', default=0, type=int, help='Sleep SLEEP seconds between subsequent queries.')
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.init()
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
    def main(self):
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        # connect DB
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        with self.pgm.cursor('target') as curs:
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
            rowcount = 1
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
            while rowcount > 0:
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
                print('query:', self.args.query)
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
                curs.execute(self.args.query, [])
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
                rowcount = curs.rowcount
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
                print('updated', rowcount)
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
                curs.connection.commit()
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
                print('sleep %s seconds' % self.args.sleep)
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
                time.sleep(self.args.sleep)
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
tool = BatchUpdateTool()
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
tool.main()
d59c473c9ad7 Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32