summaryrefslogtreecommitdiff
path: root/openerp/sql_db.py
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2016-02-02 14:52:15 +0100
committerChristophe Simonis <chs@odoo.com>2016-02-02 14:52:15 +0100
commit469e04e97c2aa60d9d41bd430e0aeba77ee6f9dd (patch)
tree0119f59009274d2c569920432c9a7fabafde0974 /openerp/sql_db.py
parent78d20b552b4374b94aec7097c0e6a48a3dd2d961 (diff)
parent2611a8198eaac60cf424742e4dc411700e439352 (diff)
[MERGE] forward port of branch saas-8 up to 2611a81
Diffstat (limited to 'openerp/sql_db.py')
-rw-r--r--openerp/sql_db.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/openerp/sql_db.py b/openerp/sql_db.py
index 749af2376c0..4aef9549ff4 100644
--- a/openerp/sql_db.py
+++ b/openerp/sql_db.py
@@ -456,6 +456,40 @@ class TestCursor(Cursor):
self.execute("ROLLBACK TO SAVEPOINT test_cursor")
self.execute("SAVEPOINT test_cursor")
+class LazyCursor(object):
+ """ A proxy object to a cursor. The cursor itself is allocated only if it is
+ needed. This class is useful for cached methods, that use the cursor
+ only in the case of a cache miss.
+ """
+ def __init__(self, dbname=None):
+ self._dbname = dbname
+ self._cursor = None
+ self._depth = 0
+
+ @property
+ def dbname(self):
+ return self._dbname or threading.currentThread().dbname
+
+ def __getattr__(self, name):
+ cr = self._cursor
+ if cr is None:
+ from openerp import registry
+ cr = self._cursor = registry(self.dbname).cursor()
+ for _ in xrange(self._depth):
+ cr.__enter__()
+ return getattr(cr, name)
+
+ def __enter__(self):
+ self._depth += 1
+ if self._cursor is not None:
+ self._cursor.__enter__()
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self._depth -= 1
+ if self._cursor is not None:
+ self._cursor.__exit__(exc_type, exc_value, traceback)
+
class PsycoConnection(psycopg2.extensions.connection):
pass