8000 0.90-bugfixes: Backporting [4244] for those using legacy Django with … · django/django@78217bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 78217bf

8000 Browse files
committed
0.90-bugfixes: Backporting [4244] for those using legacy Django with psycopg1
git-svn-id: http://code.djangoproject.com/svn/django/branches/0.90-bugfixes@4418 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 56075c0 commit 78217bf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

django/core/db/backends/postgresql.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@
99

1010
DatabaseError = Database.DatabaseError
1111

12+
def smart_basestring(s, charset):
13+
if isinstance(s, unicode):
14+
return s.encode(charset)
15+
return s
16+
17+
class UnicodeCursorWrapper(object):
18+
"""
19+
A thin wrapper around psycopg cursors that allows them to accept Unicode
20+
strings as params.
21+
22+
This is necessary because psycopg doesn't apply any DB quoting to
23+
parameters that are Unicode strings. If a param is Unicode, this will
24+
convert it to a bytestring using DEFAULT_CHARSET before passing it to
25+
psycopg.
26+
"""
27+
def __init__(self, cursor, charset):
28+
self.cursor = cursor
29+
self.charset = charset
30+
31+
def execute(self, sql, params=()):
32+
return self.cursor.execute(sql, [smart_basestring(p, self.charset) for p in params])
33+
34+
def executemany(self, sql, param_list):
35+
new_param_list = [tuple([smart_basestring(p, self.charset) for p in params]) for params in param_list]
36+
return self.cursor.executemany(sql, new_param_list)
37+
38+
def __getattr__(self, attr):
39+
if self.__dict__.has_key(attr):
40+
return self.__dict__[attr]
41+
else:
42+
return getattr(self.cursor, attr)
43+
1244
class DatabaseWrapper:
1345
def __init__(self):
1446
self.connection = None
@@ -33,6 +65,7 @@ def cursor(self):
3365
self.connection.set_isolation_level(1) # make transactions transparent to all cursors
3466
cursor = self.connection.cursor()
3567
cursor.execute("SET TIME ZONE %s", [TIME_ZONE])
68+
cursor = UnicodeCursorWrapper(cursor, settings.DEFAULT_CHARSET)
3669
if DEBUG:
3770
return base.CursorDebugWrapper(cursor, self)
3871
return cursor

0 commit comments

Comments
 (0)
0