Commit 7845f37c authored by Grégory Wisniewski's avatar Grégory Wisniewski

Move under_transaction flag from mysqldb to database API.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1379 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent af30f5c2
......@@ -16,13 +16,49 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from neo import util
from neo.exception import DatabaseFailure
class DatabaseManager(object):
"""This class only describes an interface for database managers."""
def __init__(self, **kwargs):
"""Initialize the object."""
pass
"""
Initialize the object.
"""
self._under_transaction = False
def connect(self):
"""
Establish the connection to the database
"""
self._connect()
self._under_transaction = False
def begin(self):
"""
Begin a transaction
"""
if self._under_transaction:
raise DatabaseFailure('A transaction as already began')
self._begin()
self._under_transaction = True
def commit(self):
"""
Commit the current transaction
"""
if not self._under_transaction:
raise DatabaseFailure('The transaction as not began')
self._commit()
self._under_transaction = False
def rollback(self):
"""
Rollback the current transaction
"""
# XXX: don't care if not in a transaction ?
self._rollback()
self._under_transaction = False
def setup(self, reset = 0):
"""Set up a database. If reset is true, existing data must be
......@@ -33,6 +69,12 @@ class DatabaseManager(object):
""" close the database connection """
raise NotImplementedError
def _connect(self):
raise NotImplementedError
def _begin(self):
raise NotImplementedError
def getConfiguration(self, key):
"""
Return a configuration value, returns None if not found or not set
......
......@@ -51,7 +51,7 @@ class MySQLDatabaseManager(DatabaseManager):
def close(self):
self.conn.close()
def connect(self):
def _connect(self):
kwd = {'db' : self.db, 'user' : self.user}
if self.passwd is not None:
kwd['passwd'] = self.passwd
......@@ -59,25 +59,15 @@ class MySQLDatabaseManager(DatabaseManager):
self.db, self.user)
self.conn = MySQLdb.connect(**kwd)
self.conn.autocommit(False)
self.under_transaction = False
def begin(self):
if self.under_transaction:
try:
self.commit()
except:
# Ignore any error for this implicit commit.
pass
def _begin(self):
self.query("""BEGIN""")
self.under_transaction = True
def commit(self):
def _commit(self):
self.conn.commit()
self.under_transaction = False
def rollback(self):
def _rollback(self):
self.conn.rollback()
self.under_transaction = False
def query(self, query):
"""Query data from a database."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment