2006-05-17 07:27:07 +08:00
|
|
|
"""
|
|
|
|
PostgreSQL database backend for Django.
|
|
|
|
|
|
|
|
Requires psycopg 2: http://initd.org/projects/psycopg2
|
|
|
|
"""
|
|
|
|
|
2009-03-11 15:06:50 +08:00
|
|
|
from django.conf import settings
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends import *
|
2007-10-24 03:00:31 +08:00
|
|
|
from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.postgresql.client import DatabaseClient
|
|
|
|
from django.db.backends.postgresql.creation import DatabaseCreation
|
2008-08-12 14:31:29 +08:00
|
|
|
from django.db.backends.postgresql.version import get_version
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
|
2008-08-25 12:13:27 +08:00
|
|
|
from django.utils.safestring import SafeUnicode, SafeString
|
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
|
2006-05-27 02:58:46 +08:00
|
|
|
except ImportError, e:
|
|
|
|
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)
|
2008-08-25 12:13:27 +08:00
|
|
|
psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString)
|
2007-12-02 06:26:24 +08:00
|
|
|
psycopg2.extensions.register_adapter(SafeUnicode, 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
|
|
|
|
2007-08-20 10:20:33 +08:00
|
|
|
class DatabaseFeatures(BaseDatabaseFeatures):
|
|
|
|
needs_datetime_string_cast = False
|
2009-03-11 15:06:50 +08:00
|
|
|
can_return_id_from_insert = True
|
2007-08-20 10:20:33 +08:00
|
|
|
|
2007-10-24 03:00:31 +08:00
|
|
|
class DatabaseOperations(PostgresqlDatabaseOperations):
|
|
|
|
def last_executed_query(self, cursor, sql, params):
|
|
|
|
# With psycopg2, cursor objects have a "query" attribute that is the
|
|
|
|
# exact query sent to the database. See docs here:
|
|
|
|
# http://www.initd.org/tracker/psycopg/wiki/psycopg2_documentation#postgresql-status-message-and-executed-query
|
|
|
|
return cursor.query
|
|
|
|
|
2009-03-11 15:06:50 +08:00
|
|
|
def return_insert_id(self):
|
|
|
|
return "RETURNING %s"
|
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
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
|
|
|
|
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
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
self.features = DatabaseFeatures()
|
2009-03-12 13:30:51 +08:00
|
|
|
autocommit = self.settings_dict["DATABASE_OPTIONS"].get('autocommit', False)
|
|
|
|
self.features.uses_autocommit = autocommit
|
|
|
|
self._set_isolation_level(int(not autocommit))
|
2008-08-11 20:11:25 +08:00
|
|
|
self.ops = DatabaseOperations()
|
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)
|
|
|
|
self.validation = BaseDatabaseValidation()
|
|
|
|
|
2009-03-11 11:39:34 +08:00
|
|
|
def _cursor(self):
|
2007-02-26 00:18:46 +08:00
|
|
|
set_tz = False
|
2009-03-11 11:39:34 +08:00
|
|
|
settings_dict = self.settings_dict
|
2006-05-17 07:27:07 +08:00
|
|
|
if self.connection is None:
|
2007-02-26 00:18:46 +08:00
|
|
|
set_tz = True
|
2009-03-11 11:39:34 +08:00
|
|
|
if settings_dict['DATABASE_NAME'] == '':
|
2006-05-17 07:27:07 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2007-08-20 11:32:06 +08:00
|
|
|
raise ImproperlyConfigured("You need to specify DATABASE_NAME in your Django settings file.")
|
2009-03-06 10:36:56 +08:00
|
|
|
conn_params = {
|
2009-03-11 11:39:34 +08:00
|
|
|
'database': settings_dict['DATABASE_NAME'],
|
2009-03-06 10:36:56 +08:00
|
|
|
}
|
2009-03-11 11:39:34 +08:00
|
|
|
conn_params.update(settings_dict['DATABASE_OPTIONS'])
|
2009-03-11 15:06:50 +08:00
|
|
|
if 'autocommit' in conn_params:
|
|
|
|
del conn_params['autocommit']
|
2009-03-11 11:39:34 +08:00
|
|
|
if settings_dict['DATABASE_USER']:
|
|
|
|
conn_params['user'] = settings_dict['DATABASE_USER']
|
|
|
|
if settings_dict['DATABASE_PASSWORD']:
|
|
|
|
conn_params['password'] = settings_dict['DATABASE_PASSWORD']
|
|
|
|
if settings_dict['DATABASE_HOST']:
|
|
|
|
conn_params['host'] = settings_dict['DATABASE_HOST']
|
|
|
|
if settings_dict['DATABASE_PORT']:
|
|
|
|
conn_params['port'] = settings_dict['DATABASE_PORT']
|
2009-03-06 10:36:56 +08:00
|
|
|
self.connection = Database.connect(**conn_params)
|
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
|
|
|
self.connection.set_client_encoding('UTF8')
|
2006-05-17 07:27:07 +08:00
|
|
|
cursor = self.connection.cursor()
|
2006-08-29 04:00:47 +08:00
|
|
|
cursor.tzinfo_factory = None
|
2007-02-26 00:18:46 +08:00
|
|
|
if set_tz:
|
2009-03-11 11:39:34 +08:00
|
|
|
cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
|
2008-08-12 14:31:29 +08:00
|
|
|
if not hasattr(self, '_version'):
|
2008-08-28 14:49:00 +08:00
|
|
|
self.__class__._version = get_version(cursor)
|
|
|
|
if self._version < (8, 0):
|
|
|
|
# No savepoint support for earlier version of PostgreSQL.
|
|
|
|
self.features.uses_savepoints = False
|
2009-03-12 13:32:00 +08:00
|
|
|
if self._version < (8, 2):
|
|
|
|
# Cannot return the insert ID as part of an INSERT statement
|
|
|
|
# prior to version 8.2.
|
|
|
|
self.features.can_return_id_from_insert = False
|
|
|
|
if self.features.uses_autocommit:
|
|
|
|
# FIXME: Needs extra code to do reliable model insert
|
|
|
|
# handling, so we forbid it for now.
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
raise ImproperlyConfigured("You cannot use autocommit=True with PostgreSQL prior to 8.2 at the moment.")
|
2006-05-17 07:27:07 +08:00
|
|
|
return cursor
|
2009-03-11 15:06:50 +08:00
|
|
|
|
|
|
|
def _enter_transaction_management(self, managed):
|
|
|
|
"""
|
|
|
|
Switch the isolation level when needing transaction support, so that
|
|
|
|
the same transaction is visible across all the queries.
|
|
|
|
"""
|
|
|
|
if self.features.uses_autocommit and managed and not self.isolation_level:
|
2009-03-12 13:30:51 +08:00
|
|
|
self._set_isolation_level(1)
|
2009-03-11 15:06:50 +08:00
|
|
|
|
|
|
|
def _leave_transaction_management(self, managed):
|
|
|
|
"""
|
|
|
|
If the normal operating mode is "autocommit", switch back to that when
|
|
|
|
leaving transaction management.
|
|
|
|
"""
|
|
|
|
if self.features.uses_autocommit and not managed and self.isolation_level:
|
2009-03-12 13:30:51 +08:00
|
|
|
self._set_isolation_level(0)
|
2009-03-11 15:06:50 +08:00
|
|
|
|
2009-03-12 13:30:51 +08:00
|
|
|
def _set_isolation_level(self, level):
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2009-03-12 13:30:51 +08:00
|
|
|
Do all the related feature configurations for changing isolation
|
|
|
|
levels. This doesn't touch the uses_autocommit feature, since that
|
|
|
|
controls the movement *between* isolation levels.
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2009-03-12 13:30:51 +08:00
|
|
|
assert level in (0, 1)
|
2009-03-11 15:06:50 +08:00
|
|
|
try:
|
|
|
|
if self.connection is not None:
|
2009-03-12 13:30:51 +08:00
|
|
|
self.connection.set_isolation_level(level)
|
2009-03-11 15:06:50 +08:00
|
|
|
finally:
|
2009-03-12 13:30:51 +08:00
|
|
|
self.isolation_level = level
|
|
|
|
self.features.uses_savepoints = bool(level)
|
2009-03-11 15:06:50 +08:00
|
|
|
|