[FIX] sql_db: environment retrieval in cursor
This commit is contained in:
+31
-18
@@ -22,8 +22,6 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ
|
||||
from psycopg2.pool import PoolError
|
||||
from werkzeug import urls
|
||||
|
||||
from odoo.api import Environment
|
||||
|
||||
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@@ -59,6 +57,25 @@ from datetime import timedelta
|
||||
import threading
|
||||
from inspect import currentframe
|
||||
|
||||
|
||||
SENTINEL = object()
|
||||
|
||||
|
||||
def get_env(frame, back=0):
|
||||
""" Retrieve an environment from the given stack frame. """
|
||||
for i in range(back):
|
||||
frame = frame.f_back
|
||||
while frame:
|
||||
env = frame.f_locals.get('env', SENTINEL)
|
||||
if env is not SENTINEL:
|
||||
return env
|
||||
env = getattr(frame.f_locals.get('self'), 'env', SENTINEL)
|
||||
if env is not SENTINEL:
|
||||
return env
|
||||
frame = frame.f_back
|
||||
return None
|
||||
|
||||
|
||||
import re
|
||||
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$')
|
||||
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$')
|
||||
@@ -369,10 +386,9 @@ class Cursor(object):
|
||||
def commit(self):
|
||||
""" Perform an SQL `COMMIT`
|
||||
"""
|
||||
for env in Environment.envs:
|
||||
if env.cr is self:
|
||||
env['base'].flush()
|
||||
break
|
||||
env = get_env(currentframe(), 2)
|
||||
if env is not None:
|
||||
env['base'].flush()
|
||||
result = self._cnx.commit()
|
||||
for func in self._pop_event_handlers()['commit']:
|
||||
func()
|
||||
@@ -382,10 +398,9 @@ class Cursor(object):
|
||||
def rollback(self):
|
||||
""" Perform an SQL `ROLLBACK`
|
||||
"""
|
||||
for env in Environment.envs:
|
||||
if env.cr is self:
|
||||
env.clear()
|
||||
break
|
||||
env = get_env(currentframe(), 2)
|
||||
if env is not None:
|
||||
env.clear()
|
||||
result = self._cnx.rollback()
|
||||
for func in self._pop_event_handlers()['rollback']:
|
||||
func()
|
||||
@@ -473,17 +488,15 @@ class TestCursor(object):
|
||||
_logger.debug("TestCursor.autocommit(%r) does nothing", on)
|
||||
|
||||
def commit(self):
|
||||
for env in Environment.envs:
|
||||
if env.cr is self:
|
||||
env['base'].flush()
|
||||
break
|
||||
env = get_env(currentframe(), 1)
|
||||
if env is not None:
|
||||
env['base'].flush()
|
||||
self._cursor.execute('SAVEPOINT "%s"' % self._savepoint)
|
||||
|
||||
def rollback(self):
|
||||
for env in Environment.envs:
|
||||
if env.cr is self:
|
||||
env.clear()
|
||||
break
|
||||
env = get_env(currentframe(), 1)
|
||||
if env is not None:
|
||||
env.clear()
|
||||
self._cursor.execute('ROLLBACK TO SAVEPOINT "%s"' % self._savepoint)
|
||||
|
||||
def __enter__(self):
|
||||
|
||||
Reference in New Issue
Block a user