Changeset 733 for trunk/pg.py


Ignore:
Timestamp:
Jan 13, 2016, 10:12:48 AM (4 years ago)
Author:
cito
Message:

Assume select privilege in insert/update()

Don't query the database whether we have select privilege on the
table on every call of insert/update(). We assume that we can
update/insert() anyway, and it's pretty sane then to assume we can
select, too. This spares one database request per call that is
superfluous under all normal circumstances. In the theoretically
possible but rare case that you can insert/update and not select,
a normal query() is always possible instead.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r732 r733  
    685685        is updated from the keywords.
    686686
    687         The dictionary is then, if possible, reloaded with the values actually
    688         inserted in order to pick up values modified by rules, triggers, etc.
     687        The dictionary is then reloaded with the values actually inserted in
     688        order to pick up values modified by rules, triggers, etc.
    689689
    690690        Note: The method currently doesn't support insert into views
     
    706706                values.append(param(d[n], attnames[n]))
    707707        names, values = ', '.join(names), ', '.join(values)
    708         selectable = self.has_table_privilege(cl)
    709         if selectable:
    710             ret = ' RETURNING %s*' % ('oid, ' if 'oid' in attnames else '')
    711         else:
    712             ret = ''
    713         q = 'INSERT INTO %s (%s) VALUES (%s)%s' % (
     708        ret = 'oid, *' if 'oid' in attnames else '*'
     709        q = 'INSERT INTO %s (%s) VALUES (%s) RETURNING %s' % (
    714710            self._escape_qualified_name(cl), names, values, ret)
    715711        self._do_debug(q, params)
    716712        res = self.db.query(q, params)
    717         if ret:
    718             res = res.dictresult()[0]
    719             for n, value in res.items():
    720                 if n == 'oid':
    721                     n = qoid
    722                 elif attnames.get(n) == 'bytea' and value is not None:
    723                     value = self.unescape_bytea(value)
    724                 d[n] = value
    725         elif isinstance(res, int):
    726             d[qoid] = res
    727             if selectable:
    728                 self.get(cl, d, 'oid')
    729         elif selectable:
    730             if qoid in d:
    731                 self.get(cl, d, 'oid')
    732             else:
    733                 try:
    734                     self.get(cl, d)
    735                 except ProgrammingError:
    736                     pass  # table has no primary key
     713        res = res.dictresult()[0]
     714        for n, value in res.items():
     715            if n == 'oid':
     716                n = qoid
     717            elif attnames.get(n) == 'bytea' and value is not None:
     718                value = self.unescape_bytea(value)
     719            d[n] = value
    737720        return d
    738721
     
    742725        Similar to insert but updates an existing row.  The update is based
    743726        on the OID value as munged by get or passed as keyword, or on the
    744         primary key of the table.  The dictionary is modified, if possible,
    745         to reflect any changes caused by the update due to triggers, rules,
    746         default values, etc.
     727        primary key of the table.  The dictionary is modified to reflect
     728        any changes caused by the update due to triggers, rules, default
     729        values, etc.
    747730
    748731        """
     
    783766            return d
    784767        values = ', '.join(values)
    785         selectable = self.has_table_privilege(cl)
    786         if selectable:
    787             ret = ' RETURNING %s*' % ('oid, ' if 'oid' in attnames else '')
    788         else:
    789             ret = ''
    790         q = 'UPDATE %s SET %s WHERE %s%s' % (
     768        ret = 'oid, *' if 'oid' in attnames else '*'
     769        q = 'UPDATE %s SET %s WHERE %s RETURNING %s' % (
    791770            self._escape_qualified_name(cl), values, where, ret)
    792771        self._do_debug(q, params)
    793772        res = self.db.query(q, params)
    794         if ret:
    795             res = res.dictresult()[0]
    796             for n, value in res.items():
    797                 if n == 'oid':
    798                     n = qoid
    799                 elif attnames.get(n) == 'bytea' and value is not None:
    800                     value = self.unescape_bytea(value)
    801                 d[n] = value
    802         else:
    803             if selectable:
    804                 if qoid in d:
    805                     self.get(cl, d, 'oid')
    806                 else:
    807                     self.get(cl, d)
     773        res = res.dictresult()[0]
     774        for n, value in res.items():
     775            if n == 'oid':
     776                n = qoid
     777            elif attnames.get(n) == 'bytea' and value is not None:
     778                value = self.unescape_bytea(value)
     779            d[n] = value
    808780        return d
    809781
Note: See TracChangeset for help on using the changeset viewer.