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
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
from django.conf import settings
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.base import BaseDatabaseWrapper
|
|
|
|
from django.db.backends.base.validation import BaseDatabaseValidation
|
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
|
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:
|
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
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
# Some of these import psycopg2, so import them after checking if it's installed.
|
|
|
|
from .client import DatabaseClient
|
|
|
|
from .creation import DatabaseCreation
|
|
|
|
from .features import DatabaseFeatures
|
|
|
|
from .introspection import DatabaseIntrospection
|
|
|
|
from .operations import DatabaseOperations
|
|
|
|
from .schema import DatabaseSchemaEditor
|
|
|
|
from .utils import utc_tzinfo_factory
|
|
|
|
from .version import get_version
|
|
|
|
|
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)
|
2013-09-07 08:43:41 +08:00
|
|
|
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
|
|
|
|
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',
|
|
|
|
'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
|
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
|
|
|
|
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)
|
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
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
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'])
|
|
|
|
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:
|
2014-01-13 03:27:08 +08:00
|
|
|
cursor = self.connection.cursor()
|
2014-01-09 23:05:15 +08:00
|
|
|
try:
|
|
|
|
cursor.execute(self.ops.set_time_zone_sql(), [tz])
|
|
|
|
finally:
|
|
|
|
cursor.close()
|
2014-01-13 01:26:20 +08:00
|
|
|
# Commit after setting the time zone (see #17062)
|
2014-01-13 03:27:08 +08:00
|
|
|
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 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)
|
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:
|
|
|
|
if self.psycopg2_version >= (2, 4, 2):
|
|
|
|
self.connection.autocommit = autocommit
|
2013-03-02 23:57:56 +08:00
|
|
|
else:
|
2014-03-24 06:09:26 +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):
|
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
|
|
|
|
2013-03-02 23:57:56 +08:00
|
|
|
@cached_property
|
|
|
|
def psycopg2_version(self):
|
|
|
|
version = psycopg2.__version__.split(' ', 1)[0]
|
2014-11-29 22:36:36 +08:00
|
|
|
return tuple(int(v) for v in version.split('.') if v.isdigit())
|
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)
|