Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Malcolm Tredinnick authored June 25, 2007
16  django/db/backends/postgresql/base.py
@@ -6,7 +6,6 @@
6 6
 
7 7
 from django.utils.encoding import smart_str, smart_unicode
8 8
 from django.db.backends import util
9  
-from django.db.backends.postgresql.encodings import ENCODING_MAP
10 9
 try:
11 10
     import psycopg as Database
12 11
 except ImportError, e:
@@ -64,7 +63,6 @@ def __getattr__(self, attr):
64 63
             return getattr(self.cursor, attr)
65 64
 
66 65
 postgres_version = None
67  
-client_encoding = None
68 66
 
69 67
 class DatabaseWrapper(local):
70 68
     def __init__(self, **kwargs):
@@ -94,15 +92,8 @@ def cursor(self):
94 92
         cursor = self.connection.cursor()
95 93
         if set_tz:
96 94
             cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
97  
-        cursor.execute("SHOW client_encoding")
98  
-        encoding = ENCODING_MAP[cursor.fetchone()[0]]
99  
-        cursor = UnicodeCursorWrapper(cursor, encoding)
100  
-        global client_encoding
101  
-        if not client_encoding:
102  
-            # We assume the client encoding isn't going to change for random
103  
-            # reasons.
104  
-            Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string))
105  
-            client_encoding = encoding
  95
+        cursor.execute("SET client_encoding to 'UNICODE'")
  96
+        cursor = UnicodeCursorWrapper(cursor, 'utf-8')
106 97
         global postgres_version
107 98
         if not postgres_version:
108 99
             cursor.execute("SELECT version()")
@@ -289,7 +280,7 @@ def typecast_string(s):
289 280
     """
290 281
     if not s:
291 282
         return s
292  
-    return smart_unicode(s, client_encoding)
  283
+    return smart_unicode(s)
293 284
 
294 285
 # Register these custom typecasts, because Django expects dates/times to be
295 286
 # in Python's native (standard-library) datetime/time format, whereas psycopg
@@ -302,6 +293,7 @@ def typecast_string(s):
302 293
 Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp))
303 294
 Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean))
304 295
 Database.register_type(Database.new_type((1700,), "NUMERIC", util.typecast_decimal))
  296
+Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string))
305 297
 
306 298
 OPERATOR_MAPPING = {
307 299
     'exact': '= %s',
48  django/db/backends/postgresql/encodings.py
... ...
@@ -1,48 +0,0 @@
1  
-# Mapping between PostgreSQL encodings and Python codec names. This mapping
2  
-# doesn't exist in psycopg, so we have to maintain it by hand (using
3  
-# information from section 21.2.1 in the PostgreSQL manual).
4  
-ENCODING_MAP = {
5  
-    "ALT": 'cp866',
6  
-    "BIG5": 'big5-tw',
7  
-    "EUC_CN": 'gb2312',
8  
-    "EUC_JP": 'euc_jp',
9  
-    "EUC_KR": 'euc_kr',
10  
-    "GB18030": 'gb18030',
11  
-    "GBK": 'gbk',
12  
-    "ISO_8859_5": 'iso8859_5',
13  
-    "ISO_8859_6": 'iso8859_6',
14  
-    "ISO_8859_7": 'iso8859_7',
15  
-    "ISO_8859_8": 'iso8859_8',
16  
-    "JOHAB": 'johab',
17  
-    "KOI8": 'koi18_r',
18  
-    "KOI18R": 'koi18_r',
19  
-    "LATIN1": 'latin_1',
20  
-    "LATIN2": 'iso8859_2',
21  
-    "LATIN3": 'iso8859_3',
22  
-    "LATIN4": 'iso8859_4',
23  
-    "LATIN5": 'iso8859_9',
24  
-    "LATIN6": 'iso8859_10',
25  
-    "LATIN7": 'iso8859_13',
26  
-    "LATIN8": 'iso8859_14',
27  
-    "LATIN9": 'iso8859_15',
28  
-    "SJIS": 'shift_jis',
29  
-    "SQL_ASCII": 'ascii',
30  
-    "TCVN": 'cp1258',
31  
-    "UHC": 'cp949',
32  
-    "UNICODE": 'utf-8',
33  
-    "UTF8": 'utf-8',
34  
-    "WIN": 'cp1251',
35  
-    "WIN866": 'cp866',
36  
-    "WIN874": 'cp874',
37  
-    "WIN1250": 'cp1250',
38  
-    "WIN1251": 'cp1251',
39  
-    "WIN1252": 'cp1252',
40  
-    "WIN1256": 'cp1256',
41  
-    "WIN1258": 'cp1258',
42  
-
43  
-    # Unsupported (no equivalents in codecs module):
44  
-    # EUC_TW
45  
-    # LATIN10
46  
-    # MULE_INTERNAL
47  
-}
48  
-

0 notes on commit dbb785c

Please sign in to comment.
Something went wrong with that request. Please try again.