Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
from functools import wraps
|
2009-12-22 23:18:51 +08:00
|
|
|
import os
|
2012-10-08 03:16:01 +08:00
|
|
|
import pkgutil
|
2011-12-16 21:40:19 +08:00
|
|
|
from threading import local
|
2013-03-06 18:12:24 +08:00
|
|
|
import warnings
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-05-21 17:35:05 +08:00
|
|
|
from django.utils.functional import cached_property
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.utils.importlib import import_module
|
2013-02-03 05:58:02 +08:00
|
|
|
from django.utils.module_loading import import_by_path
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-12-09 02:31:57 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
DEFAULT_DB_ALIAS = 'default'
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
2013-02-28 02:24:23 +08:00
|
|
|
class Error(Exception if six.PY3 else StandardError):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InterfaceError(Error):
|
2010-01-29 23:45:55 +08:00
|
|
|
pass
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
|
|
|
class DatabaseError(Error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DataError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OperationalError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
class IntegrityError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
class InternalError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProgrammingError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotSupportedError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseErrorWrapper(object):
|
|
|
|
"""
|
|
|
|
Context manager and decorator that re-throws backend-specific database
|
|
|
|
exceptions using Django's common wrappers.
|
|
|
|
"""
|
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
def __init__(self, wrapper):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
"""
|
2013-02-18 18:37:26 +08:00
|
|
|
wrapper is a database wrapper.
|
|
|
|
|
|
|
|
It must have a Database attribute defining PEP-249 exceptions.
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
"""
|
2013-02-18 18:37:26 +08:00
|
|
|
self.wrapper = wrapper
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
if exc_type is None:
|
|
|
|
return
|
|
|
|
for dj_exc_type in (
|
|
|
|
DataError,
|
|
|
|
OperationalError,
|
|
|
|
IntegrityError,
|
|
|
|
InternalError,
|
|
|
|
ProgrammingError,
|
|
|
|
NotSupportedError,
|
|
|
|
DatabaseError,
|
|
|
|
InterfaceError,
|
|
|
|
Error,
|
|
|
|
):
|
2013-02-18 18:37:26 +08:00
|
|
|
db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
if issubclass(exc_type, db_exc_type):
|
2013-02-28 02:24:23 +08:00
|
|
|
# Under Python 2.6, exc_value can still be a string.
|
|
|
|
try:
|
|
|
|
args = tuple(exc_value.args)
|
|
|
|
except AttributeError:
|
|
|
|
args = (exc_value,)
|
|
|
|
dj_exc_value = dj_exc_type(*args)
|
2013-06-04 19:31:06 +08:00
|
|
|
dj_exc_value.__cause__ = exc_value
|
2013-02-18 18:37:26 +08:00
|
|
|
# Only set the 'errors_occurred' flag for errors that may make
|
|
|
|
# the connection unusable.
|
|
|
|
if dj_exc_type not in (DataError, IntegrityError):
|
|
|
|
self.wrapper.errors_occurred = True
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
six.reraise(dj_exc_type, dj_exc_value, traceback)
|
|
|
|
|
|
|
|
def __call__(self, func):
|
2013-09-15 15:12:16 +08:00
|
|
|
# Note that we are intentionally not using @wraps here for performance
|
|
|
|
# reasons. Refs #21109.
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
def inner(*args, **kwargs):
|
|
|
|
with self:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def load_backend(backend_name):
|
2011-03-31 01:59:44 +08:00
|
|
|
# Look for a fully qualified database backend name
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2011-03-31 01:59:44 +08:00
|
|
|
return import_module('.base', backend_name)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e_user:
|
2011-03-31 01:59:44 +08:00
|
|
|
# The database backend wasn't found. Display a helpful error message
|
|
|
|
# listing all possible (built-in) database backends.
|
2012-12-08 18:13:52 +08:00
|
|
|
backend_dir = os.path.join(os.path.dirname(upath(__file__)), 'backends')
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2012-10-08 03:16:01 +08:00
|
|
|
builtin_backends = [
|
|
|
|
name for _, name, ispkg in pkgutil.iter_modules([backend_dir])
|
|
|
|
if ispkg and name != 'dummy']
|
2011-03-31 01:59:44 +08:00
|
|
|
except EnvironmentError:
|
2012-10-07 06:56:28 +08:00
|
|
|
builtin_backends = []
|
|
|
|
if backend_name not in ['django.db.backends.%s' % b for b in
|
|
|
|
builtin_backends]:
|
|
|
|
backend_reprs = map(repr, sorted(builtin_backends))
|
2011-12-25 21:24:39 +08:00
|
|
|
error_msg = ("%r isn't an available database backend.\n"
|
2012-10-07 06:56:28 +08:00
|
|
|
"Try using 'django.db.backends.XXX', where XXX "
|
2011-12-25 21:24:39 +08:00
|
|
|
"is one of:\n %s\nError was: %s" %
|
|
|
|
(backend_name, ", ".join(backend_reprs), e_user))
|
|
|
|
raise ImproperlyConfigured(error_msg)
|
2011-03-31 01:59:44 +08:00
|
|
|
else:
|
2011-12-25 21:24:39 +08:00
|
|
|
# If there's some other error, this must be an error in Django
|
|
|
|
raise
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionDoesNotExist(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionHandler(object):
|
2013-05-21 17:35:05 +08:00
|
|
|
def __init__(self, databases=None):
|
|
|
|
"""
|
|
|
|
databases is an optional dictionary of database definitions (structured
|
|
|
|
like settings.DATABASES).
|
|
|
|
"""
|
|
|
|
self._databases = databases
|
|
|
|
self._connections = local()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def databases(self):
|
|
|
|
if self._databases is None:
|
|
|
|
self._databases = settings.DATABASES
|
|
|
|
if self._databases == {}:
|
|
|
|
self._databases = {
|
2012-10-29 06:02:41 +08:00
|
|
|
DEFAULT_DB_ALIAS: {
|
|
|
|
'ENGINE': 'django.db.backends.dummy',
|
|
|
|
},
|
|
|
|
}
|
2013-05-21 17:35:05 +08:00
|
|
|
if DEFAULT_DB_ALIAS not in self._databases:
|
|
|
|
raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
|
|
|
|
return self._databases
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def ensure_defaults(self, alias):
|
|
|
|
"""
|
|
|
|
Puts the defaults into the settings dictionary for a given connection
|
|
|
|
where no settings is provided.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
conn.setdefault('ATOMIC_REQUESTS', False)
|
|
|
|
if settings.TRANSACTIONS_MANAGED:
|
|
|
|
warnings.warn(
|
|
|
|
"TRANSACTIONS_MANAGED is deprecated. Use AUTOCOMMIT instead.",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
conn.setdefault('AUTOCOMMIT', False)
|
|
|
|
conn.setdefault('AUTOCOMMIT', True)
|
2009-12-24 13:57:43 +08:00
|
|
|
conn.setdefault('ENGINE', 'django.db.backends.dummy')
|
2010-01-11 08:54:27 +08:00
|
|
|
if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
|
2009-12-24 13:57:43 +08:00
|
|
|
conn['ENGINE'] = 'django.db.backends.dummy'
|
2013-05-09 21:42:14 +08:00
|
|
|
conn.setdefault('CONN_MAX_AGE', 0)
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault('OPTIONS', {})
|
2011-11-18 21:01:06 +08:00
|
|
|
conn.setdefault('TIME_ZONE', 'UTC' if settings.USE_TZ else settings.TIME_ZONE)
|
2011-03-31 01:59:44 +08:00
|
|
|
for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault(setting, '')
|
2011-03-31 01:59:44 +08:00
|
|
|
for setting in ['TEST_CHARSET', 'TEST_COLLATION', 'TEST_NAME', 'TEST_MIRROR']:
|
|
|
|
conn.setdefault(setting, None)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def __getitem__(self, alias):
|
2011-12-16 21:40:19 +08:00
|
|
|
if hasattr(self._connections, alias):
|
|
|
|
return getattr(self._connections, alias)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
self.ensure_defaults(alias)
|
|
|
|
db = self.databases[alias]
|
|
|
|
backend = load_backend(db['ENGINE'])
|
|
|
|
conn = backend.DatabaseWrapper(db, alias)
|
2011-12-16 21:40:19 +08:00
|
|
|
setattr(self._connections, alias, conn)
|
2009-12-22 23:18:51 +08:00
|
|
|
return conn
|
|
|
|
|
2011-12-16 21:40:19 +08:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
setattr(self._connections, key, value)
|
|
|
|
|
2013-03-01 00:05:25 +08:00
|
|
|
def __delitem__(self, key):
|
|
|
|
delattr(self._connections, key)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.databases)
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return [self[alias] for alias in self]
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
class ConnectionRouter(object):
|
2013-05-21 18:21:31 +08:00
|
|
|
def __init__(self, routers=None):
|
|
|
|
"""
|
|
|
|
If routers is not specified, will default to settings.DATABASE_ROUTERS.
|
|
|
|
"""
|
|
|
|
self._routers = routers
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def routers(self):
|
|
|
|
if self._routers is None:
|
|
|
|
self._routers = settings.DATABASE_ROUTERS
|
2013-05-25 02:45:03 +08:00
|
|
|
routers = []
|
2013-05-21 18:21:31 +08:00
|
|
|
for r in self._routers:
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(r, six.string_types):
|
2013-02-03 05:58:02 +08:00
|
|
|
router = import_by_path(r)()
|
2010-01-22 22:30:06 +08:00
|
|
|
else:
|
|
|
|
router = r
|
2013-05-25 02:45:03 +08:00
|
|
|
routers.append(router)
|
|
|
|
return routers
|
2010-01-22 22:30:06 +08:00
|
|
|
|
|
|
|
def _router_func(action):
|
|
|
|
def _route_db(self, model, **hints):
|
|
|
|
chosen_db = None
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = getattr(router, action)
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
2010-12-09 02:37:00 +08:00
|
|
|
chosen_db = method(model, **hints)
|
2010-12-09 02:31:57 +08:00
|
|
|
if chosen_db:
|
|
|
|
return chosen_db
|
2010-01-22 22:30:06 +08:00
|
|
|
try:
|
|
|
|
return hints['instance']._state.db or DEFAULT_DB_ALIAS
|
|
|
|
except KeyError:
|
|
|
|
return DEFAULT_DB_ALIAS
|
|
|
|
return _route_db
|
|
|
|
|
|
|
|
db_for_read = _router_func('db_for_read')
|
|
|
|
db_for_write = _router_func('db_for_write')
|
|
|
|
|
|
|
|
def allow_relation(self, obj1, obj2, **hints):
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = router.allow_relation
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
|
|
|
allow = method(obj1, obj2, **hints)
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-22 22:30:06 +08:00
|
|
|
return obj1._state.db == obj2._state.db
|
2010-01-25 20:23:30 +08:00
|
|
|
|
|
|
|
def allow_syncdb(self, db, model):
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = router.allow_syncdb
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
|
|
|
allow = method(db, model)
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-25 20:23:30 +08:00
|
|
|
return True
|