summaryrefslogtreecommitdiff
path: root/odoo/tools
diff options
context:
space:
mode:
Diffstat (limited to 'odoo/tools')
-rw-r--r--odoo/tools/sql.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py
index 69968f78de8..fc3d6f5524e 100644
--- a/odoo/tools/sql.py
+++ b/odoo/tools/sql.py
@@ -253,3 +253,27 @@ def reverse_order(order):
direction = 'asc' if item[1:] == ['desc'] else 'desc'
items.append('%s %s' % (item[0], direction))
return ', '.join(items)
+
+
+def increment_field_skiplock(record, field):
+ """
+ Increment 'friendly' the [field] of the current [record](s)
+ If record is locked, we just skip the update.
+ It doesn't invalidate the cache since the update is not critical.
+
+ :rtype: bool - if field has been incremented or not
+ """
+ if not record:
+ return False
+
+ assert record._fields[field].type == 'integer'
+
+ cr = record._cr
+ query = """
+ UPDATE {table} SET {field} = {field} + 1 WHERE id IN (
+ SELECT id from {table} WHERE id in %(ids)s FOR UPDATE SKIP LOCKED
+ ) RETURNING id
+ """.format(table=record._table, field=field)
+ cr.execute(query, {'ids': tuple(record.ids)})
+
+ return bool(cr.fetchone())