Changeset 722


Ignore:
Timestamp:
Jan 12, 2016, 8:28:43 AM (4 years ago)
Author:
cito
Message:

Back port some code changes from trunk to 4.x

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.x/pg.py

    r719 r722  
    4848
    4949
    50 # Auxiliary functions that are independent of a DB connection:
     50# Auxiliary functions that are independent from a DB connection:
    5151
    5252def _is_quoted(s):
     
    305305
    306306    def __getattr__(self, name):
    307         # All undefined members are same as in underlying pg connection:
     307        # All undefined members are same as in underlying connection:
    308308        if self.db:
    309309            return getattr(self.db, name)
     
    412412        """
    413413        s = _split_parts(cl)
    414         if len(s) > 1:  # name already qualfied?
     414        if len(s) > 1:  # name already qualified?
    415415            # should be database.schema.table or schema.table
    416416            if len(s) > 3:
     
    529529        If the query is an update or delete statement, or an insert statement
    530530        that did not insert exactly one row in a table with OIDs, then the
    531         numer of rows affected is returned as a string. If it is a statement
     531        number of rows affected is returned as a string. If it is a statement
    532532        that returns rows as a result (usually a select statement, but maybe
    533533        also an "insert/update ... returning" statement), this method returns
     
    582582            if self.server_version >= 80200:
    583583                # the ANY syntax works correctly only with PostgreSQL >= 8.2
    584                 any_indkey = "= ANY (pg_index.indkey)"
     584                any_indkey = "= ANY (i.indkey)"
    585585            else:
    586586                any_indkey = "IN (%s)" % ', '.join(
    587                     ['pg_index.indkey[%d]' % i for i in range(16)])
    588             for r in self.db.query(
    589                 "SELECT pg_namespace.nspname, pg_class.relname,"
    590                     " pg_attribute.attname FROM pg_class"
    591                 " JOIN pg_namespace"
    592                     " ON pg_namespace.oid = pg_class.relnamespace"
    593                     " AND pg_namespace.nspname"
    594                     " NOT SIMILAR TO 'pg/_%|information/_schema' ESCAPE '/'"
    595                 " JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid"
    596                     " AND pg_attribute.attisdropped = 'f'"
    597                 " JOIN pg_index ON pg_index.indrelid = pg_class.oid"
    598                     " AND pg_index.indisprimary = 't'"
    599                     " AND pg_attribute.attnum " + any_indkey).getresult():
     587                    ['i.indkey[%d]' % i for i in range(16)])
     588            q = ("SELECT s.nspname, r.relname, a.attname"
     589                " FROM pg_class r"
     590                " JOIN pg_namespace s ON s.oid = r.relnamespace"
     591                " AND s.nspname NOT SIMILAR"
     592                " TO 'pg/_%|information/_schema' ESCAPE '/'"
     593                " JOIN pg_attribute a ON a.attrelid = r.oid"
     594                " AND NOT a.attisdropped"
     595                " JOIN pg_index i ON i.indrelid = r.oid"
     596                " AND i.indisprimary AND a.attnum " + any_indkey)
     597            for r in self.db.query(q).getresult():
    600598                cl, pkey = _join_parts(r[:2]), r[2]
    601599                self._pkeys.setdefault(cl, []).append(pkey)
     
    621619
    622620        """
    623         where = kinds and "pg_class.relkind IN (%s) AND" % ','.join(
    624             ["'%s'" % x for x in kinds]) or ''
    625         return [_join_parts(x) for x in self.db.query(
    626             "SELECT pg_namespace.nspname, pg_class.relname"
    627             " FROM pg_class "
    628             " JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace "
    629             " WHERE %s pg_namespace.nspname"
    630             " NOT SIMILAR TO 'pg/_%%|information/_schema' ESCAPE '/'"
    631             " ORDER BY 1, 2" % where).getresult()]
     621        where = kinds and " AND r.relkind IN (%s)" % ','.join(
     622            ["'%s'" % k for k in kinds]) or ''
     623        q = ("SELECT s.nspname, r.relname"
     624            " FROM pg_class r"
     625            " JOIN pg_namespace s ON s.oid = r.relnamespace"
     626            " WHERE s.nspname NOT SIMILAR"
     627            " TO 'pg/_%%|information/_schema' ESCAPE '/' %s"
     628            " ORDER BY 1, 2") % where
     629        return [_join_parts(r) for r in self.db.query(q).getresult()]
    632630
    633631    def get_tables(self):
     
    660658            raise _prg_error('Class %s does not exist' % qcl)
    661659
    662         q = "SELECT pg_attribute.attname, pg_type.typname"
    663         if self._regtypes:
    664             q += "::regtype"
    665         q += (" FROM pg_class"
    666             " JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid"
    667             " JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid"
    668             " JOIN pg_type ON pg_type.oid = pg_attribute.atttypid"
    669             " WHERE pg_namespace.nspname = '%s' AND pg_class.relname = '%s'"
    670             " AND (pg_attribute.attnum > 0 OR pg_attribute.attname = 'oid')"
    671             " AND pg_attribute.attisdropped = 'f'") % cl
    672         q = self.db.query(q).getresult()
     660        q = ("SELECT a.attname, t.typname%s"
     661            " FROM pg_class r"
     662            " JOIN pg_namespace s ON r.relnamespace = s.oid"
     663            " JOIN pg_attribute a ON a.attrelid = r.oid"
     664            " JOIN pg_type t ON t.oid = a.atttypid"
     665            " WHERE s.nspname = $1 AND r.relname = $2"
     666            " AND (a.attnum > 0 OR a.attname = 'oid')"
     667            " AND NOT a.attisdropped") % (
     668                self._regtypes and '::regtype' or '',)
     669        q = self.db.query(q, cl).getresult()
    673670
    674671        if self._regtypes:
     
    722719            return self._privileges[(qcl, privilege)]
    723720        except KeyError:
    724             q = "SELECT has_table_privilege('%s', '%s')" % (qcl, privilege)
    725             ret = self.db.query(q).getresult()[0][0] == self._make_bool(True)
     721            q = "SELECT has_table_privilege($1, $2)"
     722            q = self.db.query(q, (qcl, privilege))
     723            ret = q.getresult()[0][0] == self._make_bool(True)
    726724            self._privileges[(qcl, privilege)] = ret
    727725            return ret
  • trunk/pg.py

    r721 r722  
    4747
    4848
    49 # Auxiliary functions which are independent from a DB connection:
     49# Auxiliary functions that are independent from a DB connection:
    5050
    5151def _is_quoted(s):
     
    622622        where = " AND r.relkind IN (%s)" % ','.join(
    623623            ["'%s'" % k for k in kinds]) if kinds else ''
    624         return [_join_parts(r) for r in self.db.query(
    625             "SELECT s.nspname, r.relname"
     624        q = ("SELECT s.nspname, r.relname"
    626625            " FROM pg_class r"
    627626            " JOIN pg_namespace s ON s.oid = r.relnamespace"
    628627            " WHERE s.nspname NOT SIMILAR"
    629628            " TO 'pg/_%%|information/_schema' ESCAPE '/' %s"
    630             " ORDER BY 1, 2" % where).getresult()]
     629            " ORDER BY 1, 2") % where
     630        return [_join_parts(r) for r in self.db.query(q).getresult()]
    631631
    632632    def get_tables(self):
Note: See TracChangeset for help on using the changeset viewer.