1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-06-25 14:40:08 +00:00
parent 03ec6423c4
commit dbb785c0b4
2 changed files with 4 additions and 60 deletions

View File

@ -6,7 +6,6 @@ Requires psycopg 1: http://initd.org/projects/psycopg1
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 @@ class UnicodeCursorWrapper(object):
return getattr(self.cursor, attr)
postgres_version = None
client_encoding = None
class DatabaseWrapper(local):
def __init__(self, **kwargs):
@ -94,15 +92,8 @@ class DatabaseWrapper(local):
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 @@ Database.register_type(Database.new_type((1083,1266), "TIME", util.typecast_time
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 File

@ -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
}