listtables.py
changeset 68 b0d972be2631
parent 38 d3593869d624
child 83 515fadd3d286
equal deleted inserted replaced
67:78478a49c0bd 68:b0d972be2631
       
     1 #!/usr/bin/env python3.2
       
     2 
       
     3 from pgtoolkit import pgbrowser, toolbase, prettysize
       
     4 
       
     5 
       
     6 class ListTablesTool(toolbase.SimpleTool):
       
     7     def __init__(self):
       
     8         toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
       
     9         self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
       
    10         self.init()
       
    11 
       
    12     def main(self):
       
    13         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
       
    14 
       
    15         # scan all tables from all shemas, remember names and sizes
       
    16         tables = []
       
    17         for schema in browser.schemas.values():
       
    18             for table in schema.tables.values():
       
    19                 for option in self.args.options:
       
    20                     if option in table.options:
       
    21                         tables.append(table)
       
    22 
       
    23         # print result
       
    24         if len(tables):
       
    25             print('Found %d tables:' % len(tables))
       
    26         else:
       
    27             print('No table meets the conditions.')
       
    28         for table in tables:
       
    29             table_name = '%s.%s' % (table.schema.name, table.name)
       
    30             print(' ', table_name)
       
    31 
       
    32 
       
    33 tool = ListTablesTool()
       
    34 tool.main()
       
    35