2006-05-17 07:27:07 +08:00
|
|
|
"""
|
|
|
|
PostgreSQL database backend for Django.
|
|
|
|
|
|
|
|
Requires psycopg 2: http://initd.org/projects/psycopg2
|
|
|
|
"""
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
2010-01-29 23:45:55 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.db import utils
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends import *
|
2011-04-02 16:39:08 +08:00
|
|
|
from django.db.backends.postgresql_psycopg2.operations import DatabaseOperations
|
|
|
|
from django.db.backends.postgresql_psycopg2.client import DatabaseClient
|
|
|
|
from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
|
|
|
|
from django.db.backends.postgresql_psycopg2.version import get_version
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
|
2012-09-12 16:16:49 +08:00
|
|
|
from django.utils.encoding import force_str
|
2013-02-19 05:49:59 +08:00
|
|
|
from django.utils.functional import cached_property
|
2012-08-18 22:04:06 +08:00
|
|
|
from django.utils.safestring import SafeText, SafeBytes
|
2012-07-20 18:18:38 +08:00
|
|
|
from django.utils import six
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import utc
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2006-05-27 02:58:46 +08:00
|
|
|
try:
|
|
|
|
import psycopg2 as Database
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
import psycopg2.extensions
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2006-05-27 02:58:46 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2007-08-20 11:32:06 +08:00
|
|
|
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
|
2006-05-17 07:27:07 +08:00
|
|
|
|
|
|
|
DatabaseError = Database.DatabaseError
|
2007-04-25 18:18:56 +08:00
|
|
|
IntegrityError = Database.IntegrityError
|
2006-05-17 07:27:07 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
2012-08-18 22:04:06 +08:00
|
|
|
psycopg2.extensions.register_adapter(SafeBytes, psycopg2.extensions.QuotedString)
|
|
|
|
psycopg2.extensions.register_adapter(SafeText, psycopg2.extensions.QuotedString)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
logger = logging.getLogger('django.db.backends')
|
2011-08-29 23:55:06 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def utc_tzinfo_factory(offset):
|
|
|
|
if offset != 0:
|
|
|
|
raise AssertionError("database connection isn't set to UTC")
|
|
|
|
return utc
|
|
|
|
|
2007-08-20 10:20:33 +08:00
|
|
|
class DatabaseFeatures(BaseDatabaseFeatures):
|
|
|
|
needs_datetime_string_cast = False
|
2011-06-20 03:54:20 +08:00
|
|
|
can_return_id_from_insert = True
|
2010-10-11 20:55:17 +08:00
|
|
|
requires_rollback_on_dirty_transaction = True
|
|
|
|
has_real_datatype = True
|
2010-11-10 00:46:42 +08:00
|
|
|
can_defer_constraint_checks = True
|
2011-04-21 04:42:07 +08:00
|
|
|
has_select_for_update = True
|
|
|
|
has_select_for_update_nowait = True
|
2011-09-10 03:22:28 +08:00
|
|
|
has_bulk_insert = True
|
2013-03-03 22:32:14 +08:00
|
|
|
uses_savepoints = True
|
2011-10-15 05:49:43 +08:00
|
|
|
supports_tablespaces = True
|
2013-02-11 00:36:35 +08:00
|
|
|
supports_transactions = True
|
2011-12-23 04:42:40 +08:00
|
|
|
can_distinct_on_fields = True
|
2007-08-20 10:20:33 +08:00
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
2010-10-11 20:55:17 +08:00
|
|
|
vendor = 'postgresql'
|
2007-08-20 11:26:55 +08:00
|
|
|
operators = {
|
|
|
|
'exact': '= %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'iexact': '= UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
'contains': 'LIKE %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'icontains': 'LIKE UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
'regex': '~ %s',
|
|
|
|
'iregex': '~* %s',
|
|
|
|
'gt': '> %s',
|
|
|
|
'gte': '>= %s',
|
|
|
|
'lt': '< %s',
|
|
|
|
'lte': '<= %s',
|
|
|
|
'startswith': 'LIKE %s',
|
|
|
|
'endswith': 'LIKE %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'istartswith': 'LIKE UPPER(%s)',
|
|
|
|
'iendswith': 'LIKE UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
}
|
2007-08-20 06:29:57 +08:00
|
|
|
|
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
|
|
|
Database = Database
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
2009-03-11 15:06:50 +08:00
|
|
|
|
2013-03-02 23:57:56 +08:00
|
|
|
opts = self.settings_dict["OPTIONS"]
|
|
|
|
RC = psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED
|
|
|
|
self.isolation_level = opts.get('isolation_level', RC)
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
self.features = DatabaseFeatures(self)
|
2009-12-22 23:18:51 +08:00
|
|
|
self.ops = DatabaseOperations(self)
|
2009-03-11 11:39:34 +08:00
|
|
|
self.client = DatabaseClient(self)
|
2008-08-11 20:11:25 +08:00
|
|
|
self.creation = DatabaseCreation(self)
|
|
|
|
self.introspection = DatabaseIntrospection(self)
|
2009-12-22 23:18:51 +08:00
|
|
|
self.validation = BaseDatabaseValidation(self)
|
2011-06-20 02:00:09 +08:00
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def get_connection_params(self):
|
|
|
|
settings_dict = self.settings_dict
|
|
|
|
if not settings_dict['NAME']:
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"settings.DATABASES is improperly configured. "
|
|
|
|
"Please supply the NAME value.")
|
|
|
|
conn_params = {
|
|
|
|
'database': settings_dict['NAME'],
|
|
|
|
}
|
|
|
|
conn_params.update(settings_dict['OPTIONS'])
|
|
|
|
if 'autocommit' in conn_params:
|
|
|
|
del conn_params['autocommit']
|
2013-03-02 22:00:28 +08:00
|
|
|
if 'isolation_level' in conn_params:
|
|
|
|
del conn_params['isolation_level']
|
2012-11-27 04:42:27 +08:00
|
|
|
if settings_dict['USER']:
|
|
|
|
conn_params['user'] = settings_dict['USER']
|
|
|
|
if settings_dict['PASSWORD']:
|
|
|
|
conn_params['password'] = force_str(settings_dict['PASSWORD'])
|
|
|
|
if settings_dict['HOST']:
|
|
|
|
conn_params['host'] = settings_dict['HOST']
|
|
|
|
if settings_dict['PORT']:
|
|
|
|
conn_params['port'] = settings_dict['PORT']
|
|
|
|
return conn_params
|
|
|
|
|
|
|
|
def get_new_connection(self, conn_params):
|
|
|
|
return Database.connect(**conn_params)
|
|
|
|
|
|
|
|
def init_connection_state(self):
|
2009-03-11 11:39:34 +08:00
|
|
|
settings_dict = self.settings_dict
|
2012-11-27 04:42:27 +08:00
|
|
|
self.connection.set_client_encoding('UTF8')
|
|
|
|
tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
|
|
|
|
if tz:
|
|
|
|
try:
|
|
|
|
get_parameter_status = self.connection.get_parameter_status
|
|
|
|
except AttributeError:
|
|
|
|
# psycopg2 < 2.0.12 doesn't have get_parameter_status
|
|
|
|
conn_tz = None
|
|
|
|
else:
|
|
|
|
conn_tz = get_parameter_status('TimeZone')
|
|
|
|
|
|
|
|
if conn_tz != tz:
|
|
|
|
# Set the time zone in autocommit mode (see #17062)
|
2013-03-02 23:57:56 +08:00
|
|
|
self.set_autocommit(True)
|
2012-11-27 04:42:27 +08:00
|
|
|
self.connection.cursor().execute(
|
|
|
|
self.ops.set_time_zone_sql(), [tz])
|
|
|
|
self.connection.set_isolation_level(self.isolation_level)
|
|
|
|
|
2013-02-19 00:12:42 +08:00
|
|
|
def create_cursor(self):
|
2006-05-17 07:27:07 +08:00
|
|
|
cursor = self.connection.cursor()
|
2011-11-18 21:01:06 +08:00
|
|
|
cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None
|
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
|
|
|
return cursor
|
2009-03-11 15:06:50 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def close(self):
|
|
|
|
self.validate_thread_sharing()
|
|
|
|
if self.connection is None:
|
|
|
|
return
|
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
try:
|
2013-03-02 19:12:51 +08:00
|
|
|
self.connection.close()
|
|
|
|
self.connection = None
|
|
|
|
except Database.Error:
|
|
|
|
# In some cases (database restart, network connection lost etc...)
|
|
|
|
# the connection to the database is lost without giving Django a
|
|
|
|
# notification. If we don't set self.connection to None, the error
|
|
|
|
# will occur a every request.
|
|
|
|
self.connection = None
|
|
|
|
logger.warning('psycopg2 error while closing the connection.',
|
|
|
|
exc_info=sys.exc_info()
|
|
|
|
)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.set_clean()
|
2013-02-18 18:37:26 +08:00
|
|
|
|
2013-03-02 23:57:56 +08:00
|
|
|
def _set_isolation_level(self, isolation_level):
|
|
|
|
assert isolation_level in range(1, 5) # Use set_autocommit for level = 0
|
|
|
|
if self.psycopg2_version >= (2, 4, 2):
|
|
|
|
self.connection.set_session(isolation_level=isolation_level)
|
|
|
|
else:
|
|
|
|
self.connection.set_isolation_level(isolation_level)
|
2010-10-23 08:01:22 +08:00
|
|
|
|
2013-03-02 20:47:46 +08:00
|
|
|
def _set_autocommit(self, autocommit):
|
2013-03-02 23:57:56 +08:00
|
|
|
if self.psycopg2_version >= (2, 4, 2):
|
|
|
|
self.connection.autocommit = autocommit
|
2013-03-02 20:47:46 +08:00
|
|
|
else:
|
2013-03-02 23:57:56 +08:00
|
|
|
if autocommit:
|
|
|
|
level = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
|
|
|
|
else:
|
|
|
|
level = self.isolation_level
|
|
|
|
self.connection.set_isolation_level(level)
|
2013-03-02 20:47:46 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def check_constraints(self, table_names=None):
|
|
|
|
"""
|
|
|
|
To check constraints, we set constraints to immediate. Then, when, we're done we must ensure they
|
|
|
|
are returned to deferred.
|
|
|
|
"""
|
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
|
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
|
|
|
|
|
|
|
|
def is_usable(self):
|
|
|
|
try:
|
|
|
|
# Use a psycopg cursor directly, bypassing Django's utilities.
|
|
|
|
self.connection.cursor().execute("SELECT 1")
|
|
|
|
except DatabaseError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2013-03-02 23:57:56 +08:00
|
|
|
@cached_property
|
|
|
|
def psycopg2_version(self):
|
|
|
|
version = psycopg2.__version__.split(' ', 1)[0]
|
|
|
|
return tuple(int(v) for v in version.split('.'))
|
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
@cached_property
|
|
|
|
def pg_version(self):
|
|
|
|
with self.temporary_connection():
|
|
|
|
return get_version(self.connection)
|