[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 psycopg2.pool import PoolError
|
||||||
from werkzeug import urls
|
from werkzeug import urls
|
||||||
|
|
||||||
from odoo.api import Environment
|
|
||||||
|
|
||||||
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
@@ -59,6 +57,25 @@ from datetime import timedelta
|
|||||||
import threading
|
import threading
|
||||||
from inspect import currentframe
|
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
|
import re
|
||||||
re_from = re.compile('.* from "?([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]+)"? .*$')
|
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$')
|
||||||
@@ -369,10 +386,9 @@ class Cursor(object):
|
|||||||
def commit(self):
|
def commit(self):
|
||||||
""" Perform an SQL `COMMIT`
|
""" Perform an SQL `COMMIT`
|
||||||
"""
|
"""
|
||||||
for env in Environment.envs:
|
env = get_env(currentframe(), 2)
|
||||||
if env.cr is self:
|
if env is not None:
|
||||||
env['base'].flush()
|
env['base'].flush()
|
||||||
break
|
|
||||||
result = self._cnx.commit()
|
result = self._cnx.commit()
|
||||||
for func in self._pop_event_handlers()['commit']:
|
for func in self._pop_event_handlers()['commit']:
|
||||||
func()
|
func()
|
||||||
@@ -382,10 +398,9 @@ class Cursor(object):
|
|||||||
def rollback(self):
|
def rollback(self):
|
||||||
""" Perform an SQL `ROLLBACK`
|
""" Perform an SQL `ROLLBACK`
|
||||||
"""
|
"""
|
||||||
for env in Environment.envs:
|
env = get_env(currentframe(), 2)
|
||||||
if env.cr is self:
|
if env is not None:
|
||||||
env.clear()
|
env.clear()
|
||||||
break
|
|
||||||
result = self._cnx.rollback()
|
result = self._cnx.rollback()
|
||||||
for func in self._pop_event_handlers()['rollback']:
|
for func in self._pop_event_handlers()['rollback']:
|
||||||
func()
|
func()
|
||||||
@@ -473,17 +488,15 @@ class TestCursor(object):
|
|||||||
_logger.debug("TestCursor.autocommit(%r) does nothing", on)
|
_logger.debug("TestCursor.autocommit(%r) does nothing", on)
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
for env in Environment.envs:
|
env = get_env(currentframe(), 1)
|
||||||
if env.cr is self:
|
if env is not None:
|
||||||
env['base'].flush()
|
env['base'].flush()
|
||||||
break
|
|
||||||
self._cursor.execute('SAVEPOINT "%s"' % self._savepoint)
|
self._cursor.execute('SAVEPOINT "%s"' % self._savepoint)
|
||||||
|
|
||||||
def rollback(self):
|
def rollback(self):
|
||||||
for env in Environment.envs:
|
env = get_env(currentframe(), 1)
|
||||||
if env.cr is self:
|
if env is not None:
|
||||||
env.clear()
|
env.clear()
|
||||||
break
|
|
||||||
self._cursor.execute('ROLLBACK TO SAVEPOINT "%s"' % self._savepoint)
|
self._cursor.execute('ROLLBACK TO SAVEPOINT "%s"' % self._savepoint)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user