Changeset 857 for trunk/pg.py


Ignore:
Timestamp:
Mar 18, 2016, 8:22:21 AM (3 years ago)
Author:
cito
Message:

Add system parameter to get_relations()

Also fix a regression in the 4.x branch when using temporary tables,
related to filtering system tables (as discussed on the mailing list).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r849 r857  
    17521752            self.db.query('SELECT datname FROM pg_database').getresult()]
    17531753
    1754     def get_relations(self, kinds=None):
     1754    def get_relations(self, kinds=None, system=False):
    17551755        """Get list of relations in connected database of specified kinds.
    17561756
     
    17581758        Otherwise kinds can be a string or sequence of type letters
    17591759        specifying which kind of relations you want to list.
    1760         """
    1761         where = " AND r.relkind IN (%s)" % ','.join(
    1762             ["'%s'" % k for k in kinds]) if kinds else ''
     1760
     1761        Set the system flag if you want to get the system relations as well.
     1762        """
     1763        where = []
     1764        if kinds:
     1765            where.append("r.relkind IN (%s)" %
     1766                ','.join("'%s'" % k for k in kinds))
     1767        if not system:
     1768            where.append("s.nspname NOT SIMILAR"
     1769                " TO 'pg/_%|information/_schema' ESCAPE '/'")
     1770        where = " WHERE %s" % ' AND '.join(where) if where else ''
    17631771        q = ("SELECT quote_ident(s.nspname)||'.'||quote_ident(r.relname)"
    17641772            " FROM pg_class r"
    1765             " JOIN pg_namespace s ON s.oid = r.relnamespace"
    1766             " WHERE s.nspname NOT SIMILAR"
    1767             " TO 'pg/_%%|information/_schema' ESCAPE '/' %s"
     1773            " JOIN pg_namespace s ON s.oid = r.relnamespace%s"
    17681774            " ORDER BY s.nspname, r.relname") % where
    17691775        return [r[0] for r in self.db.query(q).getresult()]
    17701776
    1771     def get_tables(self):
    1772         """Return list of tables in connected database."""
    1773         return self.get_relations('r')
     1777    def get_tables(self, system=False):
     1778        """Return list of tables in connected database.
     1779
     1780        Set the system flag if you want to get the system tables as well.
     1781        """
     1782        return self.get_relations('r', system)
    17741783
    17751784    def get_attnames(self, table, with_oid=True, flush=False):
Note: See TracChangeset for help on using the changeset viewer.