pgtoolkit/pgbrowser.py
changeset 40 922d7fb63384
parent 35 e7f79c4a27ce
child 52 26121a8fe78b
equal deleted inserted replaced
39:0cef3540b69f 40:922d7fb63384
    40         self.default = default
    40         self.default = default
    41         self.description = description
    41         self.description = description
    42         
    42         
    43 
    43 
    44 class Constraint:
    44 class Constraint:
    45     def __init__(self, browser, table, name, type, definition):
    45     def __init__(self, browser, table, name, type, fname, fschema, definition):
    46         self.browser = browser
    46         self.browser = browser
    47         self.table = table
    47         self.table = table
    48         self.name = name
    48         self.name = name
    49         self.type = type
    49         self.type = type
       
    50         self.fname = fname  # foreign table name
       
    51         self.fschema = fschema  # foreign table schema
    50         self.definition = definition
    52         self.definition = definition
    51 
    53 
    52 
    54 
    53 class Index:
    55 class Index:
    54     def __init__(self, browser, table,
    56     def __init__(self, browser, table,
   209             ''', [schema])
   211             ''', [schema])
   210 
   212 
   211     def list_columns(self, table, schema='public', order=2):
   213     def list_columns(self, table, schema='public', order=2):
   212         return self._query('''
   214         return self._query('''
   213             SELECT
   215             SELECT
   214                 a.attrelid,
   216                 --a.attrelid,
   215                 a.attname as "name",
   217                 a.attname as "name",
   216                 format_type(a.atttypid, a.atttypmod) AS "type",
   218                 format_type(a.atttypid, a.atttypmod) AS "type",
   217                 a.attnotnull as "notnull",
   219                 a.attnotnull as "notnull",
   218                 a.atthasdef as "hasdefault",
   220                 a.atthasdef as "hasdefault",
   219                 d.adsrc as "default",
   221                 d.adsrc as "default",
   259             JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
   261             JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
   260             WHERE n.nspname = %(schema)s AND c.relname = %(table)s
   262             WHERE n.nspname = %(schema)s AND c.relname = %(table)s
   261             ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname
   263             ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname
   262             ''', {'schema': schema, 'table': table})
   264             ''', {'schema': schema, 'table': table})
   263 
   265 
       
   266     def list_sequences(self, schema=None):
       
   267         '''List sequences in schema.'''
       
   268         return self._query('''
       
   269             SELECT
       
   270                 nc.nspname AS "sequence_schema",
       
   271                 c.relname AS "sequence_name",
       
   272                 t.relname AS "related_table",
       
   273                 a.attname AS "related_column",
       
   274                 format_type(a.atttypid, a.atttypmod) AS "related_column_type"
       
   275             FROM pg_class c
       
   276             JOIN pg_namespace nc ON nc.oid = c.relnamespace
       
   277             JOIN pg_depend d ON d.objid = c.oid
       
   278             JOIN pg_class t ON d.refobjid = t.oid
       
   279             JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
       
   280             WHERE c.relkind = 'S' AND NOT pg_is_other_temp_schema(nc.oid)
       
   281             ''' + (schema and ' AND nc.nspname = %(schema)s' or '') + '''
       
   282         ''', {'schema': schema})
       
   283 
   264     def list_column_usage(self, table, column, schema='public'):
   284     def list_column_usage(self, table, column, schema='public'):
   265         '''List objects using the column.
   285         '''List objects using the column.
   266         
   286         
   267         Currently shows views and constraints which use the column.
   287         Currently shows views and constraints which use the column.
   268         
   288