[IMP] remove usage of psycopg1.cursor, implement dictfetch* on openerp cursor

This commit is contained in:
Xavier Morel 2014-06-20 07:54:44 +02:00
parent 981362b2d8
commit 64ca79cc4c
1 changed files with 13 additions and 4 deletions

View File

@ -30,12 +30,11 @@ the ORM does, in fact.
from contextlib import contextmanager from contextlib import contextmanager
from functools import wraps from functools import wraps
import logging import logging
import time
import uuid import uuid
import psycopg2.extras
import psycopg2.extensions import psycopg2.extensions
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_REPEATABLE_READ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_REPEATABLE_READ
from psycopg2.pool import PoolError from psycopg2.pool import PoolError
from psycopg2.psycopg1 import cursor as psycopg1cursor
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
@ -76,7 +75,7 @@ sql_counter = 0
class Cursor(object): class Cursor(object):
"""Represents an open transaction to the PostgreSQL DB backend, """Represents an open transaction to the PostgreSQL DB backend,
acting as a lightweight wrapper around psycopg2's acting as a lightweight wrapper around psycopg2's
``psycopg1cursor`` objects. ``cursor`` objects.
``Cursor`` is the object behind the ``cr`` variable used all ``Cursor`` is the object behind the ``cr`` variable used all
over the OpenERP code. over the OpenERP code.
@ -175,7 +174,7 @@ class Cursor(object):
self._serialized = serialized self._serialized = serialized
self._cnx = pool.borrow(dsn(dbname)) self._cnx = pool.borrow(dsn(dbname))
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) self._obj = self._cnx.cursor()
if self.sql_log: if self.sql_log:
self.__caller = frame_codeinfo(currentframe(),2) self.__caller = frame_codeinfo(currentframe(),2)
else: else:
@ -188,6 +187,16 @@ class Cursor(object):
self.cache = {} 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): def __del__(self):
if not self._closed and not self._cnx.closed: if not self._closed and not self._cnx.closed:
# Oops. 'self' has not been closed explicitly. # Oops. 'self' has not been closed explicitly.