2006-05-17 07:27:07 +08:00
|
|
|
"""
|
|
|
|
PostgreSQL database backend for Django.
|
|
|
|
|
|
|
|
Requires psycopg 2: http://initd.org/projects/psycopg2
|
|
|
|
"""
|
|
|
|
|
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
|
2008-08-12 13:34:56 +08:00
|
|
|
uses_savepoints = 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
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.features = DatabaseFeatures()
|
|
|
|
self.ops = DatabaseOperations()
|
|
|
|
self.client = DatabaseClient()
|
|
|
|
self.creation = DatabaseCreation(self)
|
|
|
|
self.introspection = DatabaseIntrospection(self)
|
|
|
|
self.validation = BaseDatabaseValidation()
|
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
def _cursor(self, settings):
|
2007-02-26 00:18:46 +08:00
|
|
|
set_tz = False
|
2006-05-17 07:27:07 +08:00
|
|
|
if self.connection is None:
|
2007-02-26 00:18:46 +08:00
|
|
|
set_tz = True
|
2006-05-17 07:27:07 +08:00
|
|
|
if settings.DATABASE_NAME == '':
|
|
|
|
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.")
|
2006-05-17 07:27:07 +08:00
|
|
|
conn_string = "dbname=%s" % settings.DATABASE_NAME
|
|
|
|
if settings.DATABASE_USER:
|
|
|
|
conn_string = "user=%s %s" % (settings.DATABASE_USER, conn_string)
|
|
|
|
if settings.DATABASE_PASSWORD:
|
|
|
|
conn_string += " password='%s'" % settings.DATABASE_PASSWORD
|
|
|
|
if settings.DATABASE_HOST:
|
|
|
|
conn_string += " host=%s" % settings.DATABASE_HOST
|
|
|
|
if settings.DATABASE_PORT:
|
|
|
|
conn_string += " port=%s" % settings.DATABASE_PORT
|
2006-11-07 13:17:38 +08:00
|
|
|
self.connection = Database.connect(conn_string, **self.options)
|
2006-05-17 07:27:07 +08:00
|
|
|
self.connection.set_isolation_level(1) # make transactions transparent to all cursors
|
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:
|
|
|
|
cursor.execute("SET TIME ZONE %s", [settings.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
|
2006-05-17 07:27:07 +08:00
|
|
|
return cursor
|