[REF] netrpc: removed.
[odoo/odoo.git] / openerp / sql_db.py
index 7b6f446..337964f 100644 (file)
@@ -74,8 +74,8 @@ import threading
 from inspect import currentframe
 
 import re
-re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
-re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');
+re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$')
+re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$')
 
 sql_counter = 0
 
@@ -138,6 +138,16 @@ class Cursor(object):
         sure you use psycopg2 v2.4.2 or newer if you use PostgreSQL 9.1 and
         the performance hit is a concern for you.
 
+        .. attribute:: cache
+
+            Cache dictionary with a "request" (-ish) lifecycle, only lives as
+            long as the cursor itself does and proactively cleared when the
+            cursor is closed.
+
+            This cache should *only* be used to store repeatable reads as it
+            ignores rollbacks and savepoints, it should not be used to store
+            *any* data which may be modified during the life of the cursor.
+
     """
     IN_MAX = 1000 # decent limit on size of IN queries - guideline = Oracle limit
 
@@ -182,6 +192,8 @@ class Cursor(object):
 
         self._default_log_exceptions = True
 
+        self.cache = {}
+
     def __del__(self):
         if not self.__closed and not self._cnx.closed:
             # Oops. 'self' has not been closed explicitly.
@@ -203,6 +215,9 @@ class Cursor(object):
             _logger.warning(query)
             _logger.warning("SQL queries cannot contain %d or %f anymore. "
                          "Use only %s")
+        if params and not isinstance(params, (tuple, list, dict)):
+            _logger.error("SQL query parameters should be a tuple, list or dict; got %r", params)
+            raise ValueError("SQL query parameters should be a tuple, list or dict; got %r" % (params,))
 
         if self.sql_log:
             now = mdt.now()
@@ -211,11 +226,11 @@ class Cursor(object):
             params = params or None
             res = self._obj.execute(query, params)
         except psycopg2.ProgrammingError, pe:
-            if (self._default_log_exceptions if log_exceptions is None else log_exceptions):
+            if self._default_log_exceptions if log_exceptions is None else log_exceptions:
                 _logger.error("Programming error: %s, in query %s", pe, query)
             raise
         except Exception:
-            if (self._default_log_exceptions if log_exceptions is None else log_exceptions):
+            if self._default_log_exceptions if log_exceptions is None else log_exceptions:
                 _logger.exception("bad query: %s", self._obj.query or query)
             raise
 
@@ -279,6 +294,8 @@ class Cursor(object):
         if not self._obj:
             return
 
+        del self.cache
+
         if self.sql_log:
             self.__closer = frame_codeinfo(currentframe(),3)
         self.print_log()
@@ -340,11 +357,6 @@ class Cursor(object):
     def __getattr__(self, name):
         return getattr(self._obj, name)
 
-        """ Set the mode of postgres operations for all cursors
-        """
-        """Obtain the mode of postgres operations for all cursors
-        """
-
 class PsycoConnection(psycopg2.extensions.connection):
     pass
 
@@ -504,8 +516,8 @@ def db_connect(db_name):
     return Connection(_Pool, db_name)
 
 def close_db(db_name):
-    global _Pool
     """ You might want to call openerp.modules.registry.RegistryManager.delete(db_name) along this function."""
+    global _Pool
     if _Pool:
         _Pool.close_all(dsn(db_name))
     ct = currentThread()