Add listtables tool.
authorRadek Brich <radek.brich@devl.cz>
Tue, 26 Feb 2013 14:40:39 +0100
changeset 68 b0d972be2631
parent 67 78478a49c0bd
child 69 1bf7d2f5e00c
Add listtables tool.
.hgignore
listtables.py
pgtoolkit/pgbrowser.py
--- a/.hgignore	Fri Feb 15 16:38:23 2013 +0100
+++ b/.hgignore	Tue Feb 26 14:40:39 2013 +0100
@@ -3,5 +3,7 @@
 ^.pydevproject
 ^.settings/org.eclipse.core.resources.prefs
 ^pgtoolkit\.conf$
-^build
+^build/
+^dist/
+^MANIFEST$
 ^tests\.conf$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/listtables.py	Tue Feb 26 14:40:39 2013 +0100
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3.2
+
+from pgtoolkit import pgbrowser, toolbase, prettysize
+
+
+class ListTablesTool(toolbase.SimpleTool):
+    def __init__(self):
+        toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
+        self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
+        self.init()
+
+    def main(self):
+        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
+
+        # scan all tables from all shemas, remember names and sizes
+        tables = []
+        for schema in browser.schemas.values():
+            for table in schema.tables.values():
+                for option in self.args.options:
+                    if option in table.options:
+                        tables.append(table)
+
+        # print result
+        if len(tables):
+            print('Found %d tables:' % len(tables))
+        else:
+            print('No table meets the conditions.')
+        for table in tables:
+            table_name = '%s.%s' % (table.schema.name, table.name)
+            print(' ', table_name)
+
+
+tool = ListTablesTool()
+tool.main()
+
--- a/pgtoolkit/pgbrowser.py	Fri Feb 15 16:38:23 2013 +0100
+++ b/pgtoolkit/pgbrowser.py	Tue Feb 26 14:40:39 2013 +0100
@@ -66,7 +66,7 @@
 
 
 class Table:
-    def __init__(self, browser, schema, name, owner, size, description):
+    def __init__(self, browser, schema, name, owner, size, description, options):
         self._columns = None
         self._constraints = None
         self._indexes = None
@@ -76,6 +76,7 @@
         self.owner = owner
         self.size = size
         self.description = description
+        self.options = options or []
 
     def refresh(self):
         self.refresh_columns()
@@ -269,7 +270,8 @@
                 c.relname as "name",
                 pg_catalog.pg_get_userbyid(c.relowner) as "owner",
                 pg_catalog.pg_relation_size(c.oid) as "size",
-                pg_catalog.obj_description(c.oid, 'pg_class') as "description"
+                pg_catalog.obj_description(c.oid, 'pg_class') as "description",
+                c.reloptions as "options"
             FROM pg_catalog.pg_class c
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             WHERE n.nspname = %s AND c.relkind IN ('r','s','')