pydbkit/tools/listtables.py
changeset 105 10551741f61f
parent 104 d8ff52a0390f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pydbkit/tools/listtables.py	Wed Jul 09 18:04:11 2014 +0200
@@ -0,0 +1,44 @@
+from pydbkit.toolbase import SimpleTool
+from pydbkit import pgbrowser
+
+
+class ListTablesTool(SimpleTool):
+
+    """
+    List tables in database.
+
+    Allows filtering by combination of conditions.
+    (Currently only by options.)
+
+    """
+
+    def __init__(self):
+        SimpleTool.__init__(self, name='listtables')
+
+    def specify_args(self):
+        SimpleTool.specify_args(self)
+        self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
+
+    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)
+
+
+cls = ListTablesTool
+