Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
authorRadek Brich <radek.brich@devl.cz>
Mon, 27 Feb 2012 15:12:40 +0100
changeset 30 a8b7cd92f39f
parent 29 8636719a30f6
child 31 c2e6e24b83d9
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
analyzeall.py
batchquery.py
pgtoolkit/toolbase.py
--- a/analyzeall.py	Tue Feb 14 18:15:56 2012 +0100
+++ b/analyzeall.py	Mon Feb 27 15:12:40 2012 +0100
@@ -1,4 +1,12 @@
 #!/usr/bin/env python3.2
+"""
+analyzeall
+
+Analyze/vacuum all tables in selected schemas.
+See also "VACUUM ANALYZE VERBOSE;" query.
+Unlike that, this program skips pg_catalog etc.
+
+"""
 
 from pgtoolkit import pgbrowser, toolbase
 from pgtoolkit.highlight import highlight
@@ -10,6 +18,7 @@
         self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
         self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
+        self.target_isolation_level = 'autocommit'
         self.init()
 
     def main(self):
--- a/batchquery.py	Tue Feb 14 18:15:56 2012 +0100
+++ b/batchquery.py	Mon Feb 27 15:12:40 2012 +0100
@@ -11,6 +11,7 @@
         self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
         self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
         self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
+        self.parser.add_argument('--outputfunc', dest='outputfunc', type=str, help='Python function which will format results (format_row(args, rows)).')
         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.')
         self.init()
 
@@ -46,11 +47,21 @@
             curs.connection.commit()
         # write results to output file
         if self.args.output:
+            format_row = None
+            if self.args.outputfunc:
+                with open(self.args.outputfunc, 'r', encoding='utf8') as f:
+                    d = dict()
+                    exec(f.read(), d)
+                    format_row = d['format_row']
+            
             with open(self.args.output, 'w', encoding='utf8') as f:
                 for args, rows in results:
-                    f.write(repr(args))
-                    f.write(' -> ')
-                    f.write(repr(rows))
+                    if format_row:
+                        f.write(format_row(args, rows))
+                    else:
+                        f.write(repr(args))
+                        f.write(' -> ')
+                        f.write(repr(rows))
                     f.write('\n')
 
 
--- a/pgtoolkit/toolbase.py	Tue Feb 14 18:15:56 2012 +0100
+++ b/pgtoolkit/toolbase.py	Mon Feb 27 15:12:40 2012 +0100
@@ -22,6 +22,7 @@
         self.config.add_argument('meta_query')
         
         self.pgm = pgmanager.get_instance()
+        self.target_isolation_level = None
         
     def init(self, *args):
         self.config.load('pgtoolkit.conf')
@@ -59,7 +60,9 @@
             row = curs.fetchone_dict()
             curs.connection.commit()
             if row:
-                self.pgm.create_conn(name=name, **row)
+                self.pgm.create_conn(name=name,
+                    isolation_level=self.target_isolation_level,
+                    **row)
                 return True
 
     def prepare_conn_from_config(self, name, lookup_name):
@@ -67,13 +70,15 @@
         if self.config.databases:
             if lookup_name in self.config.databases:
                 dsn = self.config.databases[lookup_name]
-                self.pgm.create_conn(name=name, dsn=dsn)
+                self.pgm.create_conn(name=name,
+                    isolation_level=self.target_isolation_level,
+                    dsn=dsn)
                 return True
 
     def prepare_conns(self, *pgm_names):
         if self.config.meta_db:
             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
-            
+        
         for name in pgm_names:
             lookup_name = self.args.__dict__[name]
             found = self.prepare_conn_from_config(name, lookup_name)