pgtoolkit/pgbrowser.py
changeset 85 11a282e23e0d
parent 68 b0d972be2631
child 86 b61b54aa9f96
equal deleted inserted replaced
84:3b5dd9efba35 85:11a282e23e0d
   159         if not self._definition:
   159         if not self._definition:
   160             self._definition = self.browser.get_function_definition(self.oid)
   160             self._definition = self.browser.get_function_definition(self.oid)
   161         return self._definition
   161         return self._definition
   162 
   162 
   163 
   163 
       
   164 class Type:
       
   165     def __init__(self, browser, schema, name, type, elements, description):
       
   166         self.browser = browser
       
   167         self.schema = schema
       
   168         self.name = name
       
   169         self.type = type
       
   170         self.elements = elements
       
   171         self.description = description
       
   172 
       
   173 
   164 class Schema:
   174 class Schema:
   165     def __init__(self, browser, name, owner, acl, description, system):
   175     def __init__(self, browser, name, owner, acl, description, system):
   166         self._tables = None
   176         self._tables = None
   167         self._functions = None
   177         self._functions = None
       
   178         self._types = None
   168         self.browser = browser
   179         self.browser = browser
   169         self.name = name
   180         self.name = name
   170         self.owner = owner
   181         self.owner = owner
   171         self.acl = acl
   182         self.acl = acl
   172         self.description = description
   183         self.description = description
   182 
   193 
   183     def refresh_functions(self):
   194     def refresh_functions(self):
   184         rows = self.browser.list_functions(self.name)
   195         rows = self.browser.list_functions(self.name)
   185         self._functions = OrderedDict([(x['name'], Function(self.browser, self, **x)) for x in rows])
   196         self._functions = OrderedDict([(x['name'], Function(self.browser, self, **x)) for x in rows])
   186 
   197 
       
   198     def refresh_types(self):
       
   199         rows = self.browser.list_types(self.name)
       
   200         self._types = OrderedDict([(x['name'], Type(self.browser, self, **x)) for x in rows])
       
   201 
   187     @property
   202     @property
   188     def tables(self):
   203     def tables(self):
   189         if self._tables is None:
   204         if self._tables is None:
   190             self.refresh_tables()
   205             self.refresh_tables()
   191         return self._tables
   206         return self._tables
   193     @property
   208     @property
   194     def functions(self):
   209     def functions(self):
   195         if self._functions is None:
   210         if self._functions is None:
   196             self.refresh_functions()
   211             self.refresh_functions()
   197         return self._functions
   212         return self._functions
       
   213 
       
   214     @property
       
   215     def types(self):
       
   216         if self._types is None:
       
   217             self.refresh_types()
       
   218         return self._types
   198 
   219 
   199 
   220 
   200 class PgBrowser:
   221 class PgBrowser:
   201     def __init__(self, conn=None):
   222     def __init__(self, conn=None):
   202         self._schemas = None
   223         self._schemas = None
   393             oid: function oid from pg_catalog.pg_proc (returned by list_functions)
   414             oid: function oid from pg_catalog.pg_proc (returned by list_functions)
   394 
   415 
   395         """
   416         """
   396         return self._query('''select pg_get_functiondef(%s);''', [oid])
   417         return self._query('''select pg_get_functiondef(%s);''', [oid])
   397 
   418 
       
   419     def list_types(self, schema='public'):
       
   420         """List types in schema."""
       
   421         return self._query('''
       
   422             SELECT
       
   423                 t.typname AS "name",
       
   424                 CASE
       
   425                     WHEN t.typtype = 'b' THEN 'base'::text
       
   426                     WHEN t.typtype = 'c' THEN 'composite'::text
       
   427                     WHEN t.typtype = 'd' THEN 'domain'::text
       
   428                     WHEN t.typtype = 'e' THEN 'enum'::text
       
   429                     WHEN t.typtype = 'p' THEN 'pseudo'::text
       
   430                 END AS "type",
       
   431                 ARRAY(
       
   432                       SELECT e.enumlabel
       
   433                       FROM pg_catalog.pg_enum e
       
   434                       WHERE e.enumtypid = t.oid
       
   435                       ORDER BY e.oid
       
   436                 ) AS "elements",
       
   437                 pg_catalog.obj_description(t.oid, 'pg_type') AS "description"
       
   438             FROM pg_catalog.pg_type t
       
   439             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
       
   440             WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
       
   441               AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
       
   442                   AND n.nspname <> 'pg_catalog'
       
   443                   AND n.nspname <> 'information_schema'
       
   444               AND n.nspname = %(schema)s
       
   445             ORDER BY 1, 2;
       
   446         ''', {'schema': schema})
       
   447 
   398     def list_sequences(self, schema=None):
   448     def list_sequences(self, schema=None):
   399         '''List sequences in schema.'''
   449         '''List sequences in schema.'''
   400         return self._query('''
   450         return self._query('''
   401             SELECT
   451             SELECT
   402                 nc.nspname AS "sequence_schema",
   452                 nc.nspname AS "sequence_schema",