Author: cito
Date: Wed Jan 13 10:12:48 2016
New Revision: 733
Log:
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.
Modified:
trunk/docs/contents/pg/db_wrapper.rst
trunk/pg.py
Modified: trunk/docs/contents/pg/db_wrapper.rst
==============================================================================
--- trunk/docs/contents/pg/db_wrapper.rst Wed Jan 13 07:45:34 2016
(r732)
+++ trunk/docs/contents/pg/db_wrapper.rst Wed Jan 13 10:12:48 2016
(r733)
@@ -181,8 +181,8 @@
pairs. If a dictionary is supplied then any keywords provided will be
added to or replace the entry in the dictionary.
-The dictionary is then, if possible, reloaded with the values actually
-inserted in order to pick up values modified by rules, triggers, etc.
+The dictionary is then reloaded with the values actually inserted in order
+to pick up values modified by rules, triggers, etc.
Note: The method currently doesn't support insert into views
although PostgreSQL does.
@@ -201,8 +201,8 @@
Similar to insert but updates an existing row. The update is based on the
OID value as munged by get or passed as keyword, or on the primary key of
-the table. The dictionary is modified, if possible, to reflect any changes
-caused by the update due to triggers, rules, default values, etc.
+the table. The dictionary is modified to reflect any changes caused by the
+update due to triggers, rules, default values, etc.
Like insert, the dictionary is optional and updates will be performed
on the fields in the keywords. There must be an OID or primary key
Modified: trunk/pg.py
==============================================================================
--- trunk/pg.py Wed Jan 13 07:45:34 2016 (r732)
+++ trunk/pg.py Wed Jan 13 10:12:48 2016 (r733)
@@ -684,8 +684,8 @@
that. Otherwise it uses a blank dictionary. Either way the dictionary
is updated from the keywords.
- The dictionary is then, if possible, reloaded with the values actually
- inserted in order to pick up values modified by rules, triggers, etc.
+ The dictionary is then reloaded with the values actually inserted in
+ order to pick up values modified by rules, triggers, etc.
Note: The method currently doesn't support insert into views
although PostgreSQL does.
@@ -705,35 +705,18 @@
names.append(col(n))
values.append(param(d[n], attnames[n]))
names, values = ', '.join(names), ', '.join(values)
- selectable = self.has_table_privilege(cl)
- if selectable:
- ret = ' RETURNING %s*' % ('oid, ' if 'oid' in attnames else '')
- else:
- ret = ''
- q = 'INSERT INTO %s (%s) VALUES (%s)%s' % (
+ ret = 'oid, *' if 'oid' in attnames else '*'
+ q = 'INSERT INTO %s (%s) VALUES (%s) RETURNING %s' % (
self._escape_qualified_name(cl), names, values, ret)
self._do_debug(q, params)
res = self.db.query(q, params)
- if ret:
- res = res.dictresult()[0]
- for n, value in res.items():
- if n == 'oid':
- n = qoid
- elif attnames.get(n) == 'bytea' and value is not None:
- value = self.unescape_bytea(value)
- d[n] = value
- elif isinstance(res, int):
- d[qoid] = res
- if selectable:
- self.get(cl, d, 'oid')
- elif selectable:
- if qoid in d:
- self.get(cl, d, 'oid')
- else:
- try:
- self.get(cl, d)
- except ProgrammingError:
- pass # table has no primary key
+ res = res.dictresult()[0]
+ for n, value in res.items():
+ if n == 'oid':
+ n = qoid
+ elif attnames.get(n) == 'bytea' and value is not None:
+ value = self.unescape_bytea(value)
+ d[n] = value
return d
def update(self, cl, d=None, **kw):
@@ -741,9 +724,9 @@
Similar to insert but updates an existing row. The update is based
on the OID value as munged by get or passed as keyword, or on the
- primary key of the table. The dictionary is modified, if possible,
- to reflect any changes caused by the update due to triggers, rules,
- default values, etc.
+ primary key of the table. The dictionary is modified to reflect
+ any changes caused by the update due to triggers, rules, default
+ values, etc.
"""
# Update always works on the oid which get returns if available,
@@ -782,29 +765,18 @@
if not values:
return d
values = ', '.join(values)
- selectable = self.has_table_privilege(cl)
- if selectable:
- ret = ' RETURNING %s*' % ('oid, ' if 'oid' in attnames else '')
- else:
- ret = ''
- q = 'UPDATE %s SET %s WHERE %s%s' % (
+ ret = 'oid, *' if 'oid' in attnames else '*'
+ q = 'UPDATE %s SET %s WHERE %s RETURNING %s' % (
self._escape_qualified_name(cl), values, where, ret)
self._do_debug(q, params)
res = self.db.query(q, params)
- if ret:
- res = res.dictresult()[0]
- for n, value in res.items():
- if n == 'oid':
- n = qoid
- elif attnames.get(n) == 'bytea' and value is not None:
- value = self.unescape_bytea(value)
- d[n] = value
- else:
- if selectable:
- if qoid in d:
- self.get(cl, d, 'oid')
- else:
- self.get(cl, d)
+ res = res.dictresult()[0]
+ for n, value in res.items():
+ if n == 'oid':
+ n = qoid
+ elif attnames.get(n) == 'bytea' and value is not None:
+ value = self.unescape_bytea(value)
+ d[n] = value
return d
def clear(self, cl, a=None):
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql