[IMP] remove usage of psycopg1.cursor, implement dictfetch* on openerp cursor
authorXavier Morel <xmo@openerp.com>
Fri, 20 Jun 2014 05:54:44 +0000 (07:54 +0200)
committerXavier Morel <xmo@openerp.com>
Fri, 20 Jun 2014 05:54:44 +0000 (07:54 +0200)
openerp/sql_db.py

index 3e40134..add6cc9 100644 (file)
@@ -30,12 +30,11 @@ the ORM does, in fact.
 from contextlib import contextmanager
 from functools import wraps
 import logging
-import time
 import uuid
+import psycopg2.extras
 import psycopg2.extensions
 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_REPEATABLE_READ
 from psycopg2.pool import PoolError
-from psycopg2.psycopg1 import cursor as psycopg1cursor
 
 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
 
@@ -76,7 +75,7 @@ sql_counter = 0
 class Cursor(object):
     """Represents an open transaction to the PostgreSQL DB backend,
        acting as a lightweight wrapper around psycopg2's
-       ``psycopg1cursor`` objects.
+       ``cursor`` objects.
 
         ``Cursor`` is the object behind the ``cr`` variable used all
         over the OpenERP code.
@@ -175,7 +174,7 @@ class Cursor(object):
         self._serialized = serialized
 
         self._cnx = pool.borrow(dsn(dbname))
-        self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
+        self._obj = self._cnx.cursor()
         if self.sql_log:
             self.__caller = frame_codeinfo(currentframe(),2)
         else:
@@ -188,6 +187,16 @@ class Cursor(object):
 
         self.cache = {}
 
+    def __build_dict(self, row):
+        return { d.name: row[i] for i, d in enumerate(self._obj.description) }
+    def dictfetchone(self):
+        row = self._obj.fetchone()
+        return row and self.__build_dict(row)
+    def dictfetchmany(self, size):
+        return map(self.__build_dict, self._obj.fetchmany(size))
+    def dictfetchall(self):
+        return map(self.__build_dict, self._obj.fetchall())
+
     def __del__(self):
         if not self._closed and not self._cnx.closed:
             # Oops. 'self' has not been closed explicitly.