Changeset 232 for trunk/module/pg.py


Ignore:
Timestamp:
Jan 22, 2006, 7:52:45 AM (14 years ago)
Author:
darcy
Message:

Change get_tables method to get_relations and allow any type of relation
to be retrieved.

Make new get_tables method that uses get_relations.

Use new method in get_attnames method to get attributes of views as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/pg.py

    r226 r232  
    66# Improved by Christoph Zwerschke
    77#
    8 # $Id: pg.py,v 1.35 2005-12-28 00:23:33 cito Exp $
     8# $Id: pg.py,v 1.36 2006-01-22 12:52:45 darcy Exp $
    99#
    1010
     
    245245                        self.db.query('SELECT datname FROM pg_database').getresult()]
    246246
     247        def get_relations(self, typ = None):
     248                """Get list of relations in connected database of specified types.
     249                        If type is None, all relations are returned.
     250                        Otherwise type can be a string or sequence of type letters."""
     251
     252                if typ:
     253                        where = "pg_class.relkind IN (%s) AND" % \
     254                                                        ','.join(["'%s'" % x for x in typ])
     255                else:
     256                        where = ''
     257
     258                return [_join_parts(s) for s in
     259                        self.db.query(
     260                                "SELECT pg_namespace.nspname, pg_class.relname "
     261                                "FROM pg_class "
     262                                "JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace "
     263                                "WHERE %s pg_class.relname !~ '^Inv' AND "
     264                                        "pg_class.relname !~ '^pg_' "
     265                                "ORDER BY 1,2" % where).getresult()]
     266
    247267        def get_tables(self):
    248                 """Get list of tables in connected database."""
    249                 return [_join_parts(s) for s in
    250                         self.db.query("SELECT pg_namespace.nspname"
    251                                 ",pg_class.relname FROM pg_class"
    252                                 " JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace"
    253                                 " WHERE pg_class.relkind='r' AND"
    254                                 " pg_class.relname!~'^Inv' AND "
    255                                 " pg_class.relname!~'^pg_' ORDER BY 1,2").getresult()]
     268                """Return list of tables in connected database."""
     269
     270                return self.get_relations('r')
    256271
    257272        def get_attnames(self, cl, newattnames = None):
     
    275290                if self.__attnames.has_key(qcl):
    276291                        return self.__attnames[qcl]
    277                 if qcl not in self.get_tables():
     292                if qcl not in self.get_relations('rv'):
    278293                        raise ProgrammingError, 'Class %s does not exist' % qcl
    279294                t = {}
     
    311326
    312327        def get(self, cl, arg, keyname = None, view = 0):
    313                 """Get a tuple from a database table.
     328                """Get a tuple from a database table or view.
    314329
    315330                This method is the basic mechanism to get a single row.  It assumes
Note: See TracChangeset for help on using the changeset viewer.