2006-05-17 07:27:07 +08:00
|
|
|
"""
|
|
|
|
PostgreSQL database backend for Django.
|
|
|
|
|
|
|
|
Requires psycopg 2: http://initd.org/projects/psycopg2
|
|
|
|
"""
|
2014-03-24 04:45:31 +08:00
|
|
|
|
2015-05-15 01:27:31 +08:00
|
|
|
import warnings
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
from django.conf import settings
|
2015-02-17 01:11:39 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-05-15 01:27:31 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.base import BaseDatabaseWrapper
|
2015-05-15 01:27:31 +08:00
|
|
|
from django.db.utils import DatabaseError as WrappedDatabaseError
|
2016-08-15 03:11:06 +08:00
|
|
|
from django.utils import six
|
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
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.safestring import SafeBytes, SafeText
|
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
|
2014-07-15 17:35:29 +08:00
|
|
|
import psycopg2.extras
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2007-08-20 11:32:06 +08:00
|
|
|
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
|
2006-05-17 07:27:07 +08:00
|
|
|
|
2015-02-17 01:11:39 +08:00
|
|
|
|
|
|
|
def psycopg2_version():
|
|
|
|
version = psycopg2.__version__.split(' ', 1)[0]
|
|
|
|
return tuple(int(v) for v in version.split('.') if v.isdigit())
|
|
|
|
|
|
|
|
PSYCOPG2_VERSION = psycopg2_version()
|
|
|
|
|
|
|
|
if PSYCOPG2_VERSION < (2, 4, 5):
|
|
|
|
raise ImproperlyConfigured("psycopg2_version 2.4.5 or newer is required; you have %s" % psycopg2.__version__)
|
|
|
|
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
# Some of these import psycopg2, so import them after checking if it's installed.
|
2016-04-04 09:41:37 +08:00
|
|
|
from .client import DatabaseClient # NOQA isort:skip
|
|
|
|
from .creation import DatabaseCreation # NOQA isort:skip
|
|
|
|
from .features import DatabaseFeatures # NOQA isort:skip
|
|
|
|
from .introspection import DatabaseIntrospection # NOQA isort:skip
|
|
|
|
from .operations import DatabaseOperations # NOQA isort:skip
|
|
|
|
from .schema import DatabaseSchemaEditor # NOQA isort:skip
|
|
|
|
from .utils import utc_tzinfo_factory # NOQA isort:skip
|
|
|
|
from .version import get_version # NOQA isort:skip
|
2015-01-13 04:20:40 +08:00
|
|
|
|
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
|
|
|
|
2016-08-15 03:11:06 +08:00
|
|
|
if six.PY2:
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
|
2012-08-18 22:04:06 +08:00
|
|
|
psycopg2.extensions.register_adapter(SafeBytes, psycopg2.extensions.QuotedString)
|
|
|
|
psycopg2.extensions.register_adapter(SafeText, psycopg2.extensions.QuotedString)
|
2014-07-15 17:35:29 +08:00
|
|
|
psycopg2.extras.register_uuid()
|
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
|
|
|
|
2015-01-11 02:13:28 +08:00
|
|
|
# Register support for inet[] manually so we don't have to handle the Inet()
|
|
|
|
# object on load all the time.
|
|
|
|
INETARRAY_OID = 1041
|
|
|
|
INETARRAY = psycopg2.extensions.new_array_type(
|
|
|
|
(INETARRAY_OID,),
|
|
|
|
'INETARRAY',
|
|
|
|
psycopg2.extensions.UNICODE,
|
|
|
|
)
|
|
|
|
psycopg2.extensions.register_type(INETARRAY)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
2010-10-11 20:55:17 +08:00
|
|
|
vendor = 'postgresql'
|
2014-12-30 04:14:40 +08:00
|
|
|
# This dictionary maps Field objects to their associated PostgreSQL column
|
|
|
|
# types, as strings. Column-type strings can contain format strings; they'll
|
|
|
|
# be interpolated against the values of Field.__dict__ before being output.
|
|
|
|
# If a column type is set to None, it won't be included in the output.
|
|
|
|
data_types = {
|
|
|
|
'AutoField': 'serial',
|
2015-07-02 16:43:15 +08:00
|
|
|
'BigAutoField': 'bigserial',
|
2014-12-30 04:14:40 +08:00
|
|
|
'BinaryField': 'bytea',
|
|
|
|
'BooleanField': 'boolean',
|
|
|
|
'CharField': 'varchar(%(max_length)s)',
|
|
|
|
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
|
|
|
|
'DateField': 'date',
|
|
|
|
'DateTimeField': 'timestamp with time zone',
|
|
|
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
|
|
|
'DurationField': 'interval',
|
|
|
|
'FileField': 'varchar(%(max_length)s)',
|
|
|
|
'FilePathField': 'varchar(%(max_length)s)',
|
|
|
|
'FloatField': 'double precision',
|
|
|
|
'IntegerField': 'integer',
|
|
|
|
'BigIntegerField': 'bigint',
|
|
|
|
'IPAddressField': 'inet',
|
|
|
|
'GenericIPAddressField': 'inet',
|
|
|
|
'NullBooleanField': 'boolean',
|
|
|
|
'OneToOneField': 'integer',
|
|
|
|
'PositiveIntegerField': 'integer',
|
|
|
|
'PositiveSmallIntegerField': 'smallint',
|
|
|
|
'SlugField': 'varchar(%(max_length)s)',
|
|
|
|
'SmallIntegerField': 'smallint',
|
|
|
|
'TextField': 'text',
|
|
|
|
'TimeField': 'time',
|
|
|
|
'UUIDField': 'uuid',
|
|
|
|
}
|
|
|
|
data_type_check_constraints = {
|
|
|
|
'PositiveIntegerField': '"%(column)s" >= 0',
|
|
|
|
'PositiveSmallIntegerField': '"%(column)s" >= 0',
|
|
|
|
}
|
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
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
# The patterns below are used to generate SQL pattern lookup clauses when
|
|
|
|
# the right-hand side of the lookup isn't a raw string (it might be an expression
|
|
|
|
# or the result of a bilateral transformation).
|
|
|
|
# In those cases, special characters for LIKE operators (e.g. \, *, _) should be
|
|
|
|
# escaped on database side.
|
|
|
|
#
|
|
|
|
# Note: we use str.format() here for readability as '%' is used as a wildcard for
|
|
|
|
# the LIKE operator.
|
|
|
|
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
|
2014-01-18 17:09:43 +08:00
|
|
|
pattern_ops = {
|
2014-09-27 18:41:54 +08:00
|
|
|
'contains': "LIKE '%%' || {} || '%%'",
|
|
|
|
'icontains': "LIKE '%%' || UPPER({}) || '%%'",
|
|
|
|
'startswith': "LIKE {} || '%%'",
|
|
|
|
'istartswith': "LIKE UPPER({}) || '%%'",
|
|
|
|
'endswith': "LIKE '%%' || {}",
|
|
|
|
'iendswith': "LIKE '%%' || UPPER({})",
|
2014-01-18 17:09:43 +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
|
2014-09-26 01:59:03 +08:00
|
|
|
SchemaEditorClass = DatabaseSchemaEditor
|
2016-09-09 04:33:36 +08:00
|
|
|
# Classes instantiated in __init__().
|
|
|
|
client_class = DatabaseClient
|
|
|
|
creation_class = DatabaseCreation
|
|
|
|
features_class = DatabaseFeatures
|
|
|
|
introspection_class = DatabaseIntrospection
|
|
|
|
ops_class = DatabaseOperations
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def get_connection_params(self):
|
|
|
|
settings_dict = self.settings_dict
|
2013-11-09 16:41:03 +08:00
|
|
|
# None may be used to connect to the default 'postgres' db
|
|
|
|
if settings_dict['NAME'] == '':
|
2012-11-27 04:42:27 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"settings.DATABASES is improperly configured. "
|
|
|
|
"Please supply the NAME value.")
|
|
|
|
conn_params = {
|
2013-11-09 16:41:03 +08:00
|
|
|
'database': settings_dict['NAME'] or 'postgres',
|
2012-11-27 04:42:27 +08:00
|
|
|
}
|
|
|
|
conn_params.update(settings_dict['OPTIONS'])
|
2015-02-15 00:31:53 +08:00
|
|
|
conn_params.pop('isolation_level', None)
|
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):
|
2015-02-14 16:50:38 +08:00
|
|
|
connection = Database.connect(**conn_params)
|
|
|
|
|
|
|
|
# self.isolation_level must be set:
|
|
|
|
# - after connecting to the database in order to obtain the database's
|
|
|
|
# default when no value is explicitly specified in options.
|
|
|
|
# - before calling _set_autocommit() because if autocommit is on, that
|
2015-02-17 01:11:39 +08:00
|
|
|
# will set connection.isolation_level to ISOLATION_LEVEL_AUTOCOMMIT.
|
2015-02-14 16:50:38 +08:00
|
|
|
options = self.settings_dict['OPTIONS']
|
|
|
|
try:
|
|
|
|
self.isolation_level = options['isolation_level']
|
|
|
|
except KeyError:
|
|
|
|
self.isolation_level = connection.isolation_level
|
|
|
|
else:
|
2015-02-17 01:11:39 +08:00
|
|
|
# Set the isolation level to the value from OPTIONS.
|
|
|
|
if self.isolation_level != connection.isolation_level:
|
2015-02-14 16:50:38 +08:00
|
|
|
connection.set_session(isolation_level=self.isolation_level)
|
|
|
|
|
|
|
|
return connection
|
2012-11-27 04:42:27 +08:00
|
|
|
|
2016-06-06 11:46:28 +08:00
|
|
|
def ensure_timezone(self):
|
|
|
|
self.ensure_connection()
|
|
|
|
conn_timezone_name = self.connection.get_parameter_status('TimeZone')
|
|
|
|
timezone_name = self.timezone_name
|
|
|
|
if timezone_name and conn_timezone_name != timezone_name:
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
cursor.execute(self.ops.set_time_zone_sql(), [timezone_name])
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def init_connection_state(self):
|
|
|
|
self.connection.set_client_encoding('UTF8')
|
2015-02-14 16:53:17 +08:00
|
|
|
|
2016-06-06 11:46:28 +08:00
|
|
|
timezone_changed = self.ensure_timezone()
|
|
|
|
if timezone_changed:
|
2015-02-14 16:53:17 +08:00
|
|
|
# Commit after setting the time zone (see #17062)
|
|
|
|
if not self.get_autocommit():
|
|
|
|
self.connection.commit()
|
2012-11-27 04:42:27 +08:00
|
|
|
|
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 20:47:46 +08:00
|
|
|
def _set_autocommit(self, autocommit):
|
2014-03-24 06:09:26 +08:00
|
|
|
with self.wrap_database_errors:
|
2015-02-17 01:11:39 +08:00
|
|
|
self.connection.autocommit = autocommit
|
2013-03-02 20:47:46 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def check_constraints(self, table_names=None):
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2013-03-02 19:12:51 +08:00
|
|
|
To check constraints, we set constraints to immediate. Then, when, we're done we must ensure they
|
|
|
|
are returned to deferred.
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2013-03-02 19:12:51 +08:00
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
|
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
|
2010-10-23 08:01:22 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def is_usable(self):
|
|
|
|
try:
|
|
|
|
# Use a psycopg cursor directly, bypassing Django's utilities.
|
|
|
|
self.connection.cursor().execute("SELECT 1")
|
2014-04-10 04:41:33 +08:00
|
|
|
except Database.Error:
|
2013-03-02 19:12:51 +08:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2015-09-11 00:47:50 +08:00
|
|
|
@property
|
2015-05-15 01:27:31 +08:00
|
|
|
def _nodb_connection(self):
|
|
|
|
nodb_connection = super(DatabaseWrapper, self)._nodb_connection
|
|
|
|
try:
|
|
|
|
nodb_connection.ensure_connection()
|
|
|
|
except (DatabaseError, WrappedDatabaseError):
|
|
|
|
warnings.warn(
|
|
|
|
"Normally Django will use a connection to the 'postgres' database "
|
|
|
|
"to avoid running initialization queries against the production "
|
|
|
|
"database when it's not needed (for example, when running tests). "
|
|
|
|
"Django was unable to create a connection to the 'postgres' database "
|
|
|
|
"and will use the default database instead.",
|
|
|
|
RuntimeWarning
|
|
|
|
)
|
|
|
|
settings_dict = self.settings_dict.copy()
|
|
|
|
settings_dict['NAME'] = settings.DATABASES[DEFAULT_DB_ALIAS]['NAME']
|
|
|
|
nodb_connection = self.__class__(
|
|
|
|
self.settings_dict.copy(),
|
|
|
|
alias=self.alias,
|
|
|
|
allow_thread_sharing=False)
|
|
|
|
return nodb_connection
|
|
|
|
|
2013-03-02 23:57:56 +08:00
|
|
|
@cached_property
|
|
|
|
def psycopg2_version(self):
|
2015-02-17 01:11:39 +08:00
|
|
|
return PSYCOPG2_VERSION
|
2013-03-02 23:57:56 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
@cached_property
|
|
|
|
def pg_version(self):
|
|
|
|
with self.temporary_connection():
|
|
|
|
return get_version(self.connection)
|