Do not use savepoints with PostgreSQL prior to 8.0.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8317 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-12 06:31:29 +00:00
parent 97da73a98c
commit 6bebb23318
3 changed files with 32 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.introspection import DatabaseIntrospection from django.db.backends.postgresql.introspection import DatabaseIntrospection
from django.db.backends.postgresql.operations import DatabaseOperations from django.db.backends.postgresql.operations import DatabaseOperations
from django.db.backends.postgresql.version import get_version
from django.utils.encoding import smart_str, smart_unicode from django.utils.encoding import smart_str, smart_unicode
try: try:
@ -115,6 +116,12 @@ class DatabaseWrapper(BaseDatabaseWrapper):
cursor = self.connection.cursor() cursor = self.connection.cursor()
if set_tz: if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if not hasattr(self, '_version'):
version = get_version(cursor)
self.__class__._version = version
if version < (8, 0):
# No savepoint support for earlier version of PostgreSQL.
self.features.uses_savepoints = False
cursor.execute("SET client_encoding to 'UNICODE'") cursor.execute("SET client_encoding to 'UNICODE'")
cursor = UnicodeCursorWrapper(cursor, 'utf-8') cursor = UnicodeCursorWrapper(cursor, 'utf-8')
return cursor return cursor

View File

@ -0,0 +1,18 @@
"""
Extracts the version of the PostgreSQL server.
"""
import re
VERSION_RE = re.compile(r'PostgreSQL (\d+)\.(\d+)\.')
def get_version(cursor):
"""
Returns a tuple representing the major and minor version number of the
server. For example, (7, 4) or (8, 3).
"""
cursor.execute("SELECT version()")
version = cursor.fetchone()[0]
major, minor = VERSION_RE.search(version).groups()
return int(major), int(minor)

View File

@ -8,6 +8,7 @@ from django.db.backends import *
from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
from django.db.backends.postgresql.client import DatabaseClient from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.version import get_version
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
from django.utils.safestring import SafeUnicode from django.utils.safestring import SafeUnicode
@ -86,4 +87,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
cursor.tzinfo_factory = None cursor.tzinfo_factory = None
if set_tz: if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if not hasattr(self, '_version'):
version = get_version(cursor)
self.__class__._version = version
if version < (8, 0):
# No savepoint support for earlier version of PostgreSQL.
self.features.uses_savepoints = False
return cursor return cursor