Factored code and added a missing docstring.

This commit is contained in:
Aymeric Augustin 2013-02-21 21:26:06 +01:00
parent 3c6318e831
commit 5488437ab6
2 changed files with 39 additions and 64 deletions

View File

@ -208,6 +208,9 @@ class BaseDatabaseWrapper(object):
self.clean_savepoints() self.clean_savepoints()
def clean_savepoints(self): def clean_savepoints(self):
"""
Resets the counter used to generate unique savepoint ids in this thread.
"""
self.savepoint_state = 0 self.savepoint_state = 0
def is_managed(self): def is_managed(self):

View File

@ -24,6 +24,19 @@ class TransactionManagementError(Exception):
""" """
pass pass
################
# Private APIs #
################
def get_connection(using=None):
"""
Get a database connection by name, or the default database connection
if no name is provided.
"""
if using is None:
using = DEFAULT_DB_ALIAS
return connections[using]
def abort(using=None): def abort(using=None):
""" """
Roll back any ongoing transactions and clean the transaction management Roll back any ongoing transactions and clean the transaction management
@ -34,10 +47,7 @@ def abort(using=None):
request has finished, the transaction state isn't known, yet the connection request has finished, the transaction state isn't known, yet the connection
must be cleaned up for the next request. must be cleaned up for the next request.
""" """
if using is None: get_connection(using).abort()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.abort()
def enter_transaction_management(managed=True, using=None): def enter_transaction_management(managed=True, using=None):
""" """
@ -49,10 +59,7 @@ def enter_transaction_management(managed=True, using=None):
from the settings, if there is no surrounding block (dirty is always false from the settings, if there is no surrounding block (dirty is always false
when no current block is running). when no current block is running).
""" """
if using is None: get_connection(using).enter_transaction_management(managed)
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.enter_transaction_management(managed)
def leave_transaction_management(using=None): def leave_transaction_management(using=None):
""" """
@ -60,20 +67,14 @@ def leave_transaction_management(using=None):
over to the surrounding block, as a commit will commit all changes, even over to the surrounding block, as a commit will commit all changes, even
those from outside. (Commits are on connection level.) those from outside. (Commits are on connection level.)
""" """
if using is None: get_connection(using).leave_transaction_management()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.leave_transaction_management()
def is_dirty(using=None): def is_dirty(using=None):
""" """
Returns True if the current transaction requires a commit for changes to Returns True if the current transaction requires a commit for changes to
happen. happen.
""" """
if using is None: return get_connection(using).is_dirty()
using = DEFAULT_DB_ALIAS
connection = connections[using]
return connection.is_dirty()
def set_dirty(using=None): def set_dirty(using=None):
""" """
@ -81,10 +82,7 @@ def set_dirty(using=None):
to decide in a managed block of code to decide whether there are open to decide in a managed block of code to decide whether there are open
changes waiting for commit. changes waiting for commit.
""" """
if using is None: get_connection(using).set_dirty()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.set_dirty()
def set_clean(using=None): def set_clean(using=None):
""" """
@ -92,25 +90,19 @@ def set_clean(using=None):
to decide in a managed block of code to decide whether a commit or rollback to decide in a managed block of code to decide whether a commit or rollback
should happen. should happen.
""" """
if using is None: get_connection(using).set_clean()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.set_clean()
def clean_savepoints(using=None): def clean_savepoints(using=None):
if using is None: """
using = DEFAULT_DB_ALIAS Resets the counter used to generate unique savepoint ids in this thread.
connection = connections[using] """
connection.clean_savepoints() get_connection(using).clean_savepoints()
def is_managed(using=None): def is_managed(using=None):
""" """
Checks whether the transaction manager is in manual or in auto state. Checks whether the transaction manager is in manual or in auto state.
""" """
if using is None: return get_connection(using).is_managed()
using = DEFAULT_DB_ALIAS
connection = connections[using]
return connection.is_managed()
def managed(flag=True, using=None): def managed(flag=True, using=None):
""" """
@ -119,46 +111,35 @@ def managed(flag=True, using=None):
management and there is a pending commit/rollback, the data will be management and there is a pending commit/rollback, the data will be
commited. commited.
""" """
if using is None: get_connection(using).managed(flag)
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.managed(flag)
def commit_unless_managed(using=None): def commit_unless_managed(using=None):
""" """
Commits changes if the system is not in managed transaction mode. Commits changes if the system is not in managed transaction mode.
""" """
if using is None: get_connection(using).commit_unless_managed()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.commit_unless_managed()
def rollback_unless_managed(using=None): def rollback_unless_managed(using=None):
""" """
Rolls back changes if the system is not in managed transaction mode. Rolls back changes if the system is not in managed transaction mode.
""" """
if using is None: get_connection(using).rollback_unless_managed()
using = DEFAULT_DB_ALIAS
connection = connections[using] ###############
connection.rollback_unless_managed() # Public APIs #
###############
def commit(using=None): def commit(using=None):
""" """
Does the commit itself and resets the dirty flag. Does the commit itself and resets the dirty flag.
""" """
if using is None: get_connection(using).commit()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.commit()
def rollback(using=None): def rollback(using=None):
""" """
This function does the rollback itself and resets the dirty flag. This function does the rollback itself and resets the dirty flag.
""" """
if using is None: get_connection(using).rollback()
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.rollback()
def savepoint(using=None): def savepoint(using=None):
""" """
@ -166,30 +147,21 @@ def savepoint(using=None):
current transaction. Returns an identifier for the savepoint that will be current transaction. Returns an identifier for the savepoint that will be
used for the subsequent rollback or commit. used for the subsequent rollback or commit.
""" """
if using is None: return get_connection(using).savepoint()
using = DEFAULT_DB_ALIAS
connection = connections[using]
return connection.savepoint()
def savepoint_rollback(sid, using=None): def savepoint_rollback(sid, using=None):
""" """
Rolls back the most recent savepoint (if one exists). Does nothing if Rolls back the most recent savepoint (if one exists). Does nothing if
savepoints are not supported. savepoints are not supported.
""" """
if using is None: get_connection(using).savepoint_rollback(sid)
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.savepoint_rollback(sid)
def savepoint_commit(sid, using=None): def savepoint_commit(sid, using=None):
""" """
Commits the most recent savepoint (if one exists). Does nothing if Commits the most recent savepoint (if one exists). Does nothing if
savepoints are not supported. savepoints are not supported.
""" """
if using is None: get_connection(using).savepoint_commit(sid)
using = DEFAULT_DB_ALIAS
connection = connections[using]
connection.savepoint_commit(sid)
############## ##############
# DECORATORS # # DECORATORS #