pydbkit/tools/listtables.py
changeset 105 10551741f61f
parent 104 d8ff52a0390f
equal deleted inserted replaced
104:d8ff52a0390f 105:10551741f61f
       
     1 from pydbkit.toolbase import SimpleTool
       
     2 from pydbkit import pgbrowser
       
     3 
       
     4 
       
     5 class ListTablesTool(SimpleTool):
       
     6 
       
     7     """
       
     8     List tables in database.
       
     9 
       
    10     Allows filtering by combination of conditions.
       
    11     (Currently only by options.)
       
    12 
       
    13     """
       
    14 
       
    15     def __init__(self):
       
    16         SimpleTool.__init__(self, name='listtables')
       
    17 
       
    18     def specify_args(self):
       
    19         SimpleTool.specify_args(self)
       
    20         self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
       
    21 
       
    22     def main(self):
       
    23         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
       
    24 
       
    25         # scan all tables from all shemas, remember names and sizes
       
    26         tables = []
       
    27         for schema in browser.schemas.values():
       
    28             for table in schema.tables.values():
       
    29                 for option in self.args.options:
       
    30                     if option in table.options:
       
    31                         tables.append(table)
       
    32 
       
    33         # print result
       
    34         if len(tables):
       
    35             print('Found %d tables:' % len(tables))
       
    36         else:
       
    37             print('No table meets the conditions.')
       
    38         for table in tables:
       
    39             table_name = '%s.%s' % (table.schema.name, table.name)
       
    40             print(' ', table_name)
       
    41 
       
    42 
       
    43 cls = ListTablesTool
       
    44