Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #4664 -- Forced the client character set encoding to UTF-8 for …

…PostgreSQL

(via the psycopg backend). The previous version was causing problems on some
setups, particularly PostgreSQL 7.x. Current code should work with 7.x and 8.x,
no matter what the default client encoding is.


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5535 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dbb785c0b4da1103368cc2d1e368d91063117ec6 1 parent 03ec642
@malcolmt malcolmt authored
View
16 django/db/backends/postgresql/base.py
@@ -6,7 +6,6 @@
from django.utils.encoding import smart_str, smart_unicode
from django.db.backends import util
-from django.db.backends.postgresql.encodings import ENCODING_MAP
try:
import psycopg as Database
except ImportError, e:
@@ -64,7 +63,6 @@ def __getattr__(self, attr):
return getattr(self.cursor, attr)
postgres_version = None
-client_encoding = None
class DatabaseWrapper(local):
def __init__(self, **kwargs):
@@ -94,15 +92,8 @@ def cursor(self):
cursor = self.connection.cursor()
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
- cursor.execute("SHOW client_encoding")
- encoding = ENCODING_MAP[cursor.fetchone()[0]]
- cursor = UnicodeCursorWrapper(cursor, encoding)
- global client_encoding
- if not client_encoding:
- # We assume the client encoding isn't going to change for random
- # reasons.
- Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string))
- client_encoding = encoding
+ cursor.execute("SET client_encoding to 'UNICODE'")
+ cursor = UnicodeCursorWrapper(cursor, 'utf-8')
global postgres_version
if not postgres_version:
cursor.execute("SELECT version()")
@@ -289,7 +280,7 @@ def typecast_string(s):
"""
if not s:
return s
- return smart_unicode(s, client_encoding)
+ return smart_unicode(s)
# Register these custom typecasts, because Django expects dates/times to be
# in Python's native (standard-library) datetime/time format, whereas psycopg
@@ -302,6 +293,7 @@ def typecast_string(s):
Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp))
Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean))
Database.register_type(Database.new_type((1700,), "NUMERIC", util.typecast_decimal))
+Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string))
OPERATOR_MAPPING = {
'exact': '= %s',
View
48 django/db/backends/postgresql/encodings.py
@@ -1,48 +0,0 @@
-# Mapping between PostgreSQL encodings and Python codec names. This mapping
-# doesn't exist in psycopg, so we have to maintain it by hand (using
-# information from section 21.2.1 in the PostgreSQL manual).
-ENCODING_MAP = {
- "ALT": 'cp866',
- "BIG5": 'big5-tw',
- "EUC_CN": 'gb2312',
- "EUC_JP": 'euc_jp',
- "EUC_KR": 'euc_kr',
- "GB18030": 'gb18030',
- "GBK": 'gbk',
- "ISO_8859_5": 'iso8859_5',
- "ISO_8859_6": 'iso8859_6',
- "ISO_8859_7": 'iso8859_7',
- "ISO_8859_8": 'iso8859_8',
- "JOHAB": 'johab',
- "KOI8": 'koi18_r',
- "KOI18R": 'koi18_r',
- "LATIN1": 'latin_1',
- "LATIN2": 'iso8859_2',
- "LATIN3": 'iso8859_3',
- "LATIN4": 'iso8859_4',
- "LATIN5": 'iso8859_9',
- "LATIN6": 'iso8859_10',
- "LATIN7": 'iso8859_13',
- "LATIN8": 'iso8859_14',
- "LATIN9": 'iso8859_15',
- "SJIS": 'shift_jis',
- "SQL_ASCII": 'ascii',
- "TCVN": 'cp1258',
- "UHC": 'cp949',
- "UNICODE": 'utf-8',
- "UTF8": 'utf-8',
- "WIN": 'cp1251',
- "WIN866": 'cp866',
- "WIN874": 'cp874',
- "WIN1250": 'cp1250',
- "WIN1251": 'cp1251',
- "WIN1252": 'cp1252',
- "WIN1256": 'cp1256',
- "WIN1258": 'cp1258',
-
- # Unsupported (no equivalents in codecs module):
- # EUC_TW
- # LATIN10
- # MULE_INTERNAL
-}
-
Please sign in to comment.
Something went wrong with that request. Please try again.