mirror of
https://github.com/django/django.git
synced 2025-09-11 04:56:51 +00:00
Refactored all database backends to inherit from a common base class to remove quite a bit of duplicated code. Thanks for the patch, Brian Harring. Refs #5106
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5949 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
77a9b0cb1d
commit
7c41b19c8a
8 changed files with 76 additions and 221 deletions
|
@ -4,7 +4,7 @@ MySQL database backend for Django.
|
|||
Requires MySQLdb: http://sourceforge.net/projects/mysql-python
|
||||
"""
|
||||
|
||||
from django.db.backends import util
|
||||
from django.db.backends import BaseDatabaseWrapper, util
|
||||
try:
|
||||
import MySQLdb as Database
|
||||
except ImportError, e:
|
||||
|
@ -53,19 +53,10 @@ server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
|
|||
# standard util.CursorDebugWrapper can be used. Also, using sql_mode
|
||||
# TRADITIONAL will automatically cause most warnings to be treated as errors.
|
||||
|
||||
try:
|
||||
# Only exists in Python 2.4+
|
||||
from threading import local
|
||||
except ImportError:
|
||||
# Import copy of _thread_local.py from Python 2.4
|
||||
from django.utils._threading_local import local
|
||||
|
||||
class DatabaseWrapper(local):
|
||||
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||
def __init__(self, **kwargs):
|
||||
self.connection = None
|
||||
self.queries = []
|
||||
super(DatabaseWrapper, self).__init__(**kwargs)
|
||||
self.server_version = None
|
||||
self.options = kwargs
|
||||
|
||||
def _valid_connection(self):
|
||||
if self.connection is not None:
|
||||
|
@ -77,8 +68,7 @@ class DatabaseWrapper(local):
|
|||
self.connection = None
|
||||
return False
|
||||
|
||||
def cursor(self):
|
||||
from django.conf import settings
|
||||
def _cursor(self, settings):
|
||||
from warnings import filterwarnings
|
||||
if not self._valid_connection():
|
||||
kwargs = {
|
||||
|
@ -100,29 +90,16 @@ class DatabaseWrapper(local):
|
|||
kwargs['port'] = int(settings.DATABASE_PORT)
|
||||
kwargs.update(self.options)
|
||||
self.connection = Database.connect(**kwargs)
|
||||
cursor = self.connection.cursor()
|
||||
else:
|
||||
cursor = self.connection.cursor()
|
||||
cursor = self.connection.cursor()
|
||||
if settings.DEBUG:
|
||||
filterwarnings("error", category=Database.Warning)
|
||||
return util.CursorDebugWrapper(cursor, self)
|
||||
return cursor
|
||||
|
||||
def _commit(self):
|
||||
if self.connection is not None:
|
||||
self.connection.commit()
|
||||
|
||||
def _rollback(self):
|
||||
if self.connection is not None:
|
||||
try:
|
||||
self.connection.rollback()
|
||||
except Database.NotSupportedError:
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
if self.connection is not None:
|
||||
self.connection.close()
|
||||
self.connection = None
|
||||
try:
|
||||
BaseDatabaseWrapper._rollback(self)
|
||||
except Database.NotSupportedError:
|
||||
pass
|
||||
|
||||
def get_server_version(self):
|
||||
if not self.server_version:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue