Django

Code

Changeset 4418

Show
Ignore:
Timestamp:
01/24/07 13:55:43 (1 year ago)
Author:
ubernostrum
Message:

0.90-bugfixes: Backporting [4244] for those using legacy Django with psycopg1

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/0.90-bugfixes/django/core/db/backends/postgresql.py

    r1236 r4418  
    99 
    1010DatabaseError = Database.DatabaseError 
     11 
     12def smart_basestring(s, charset): 
     13    if isinstance(s, unicode): 
     14        return s.encode(charset) 
     15    return s 
     16 
     17class 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) 
    1143 
    1244class DatabaseWrapper: 
     
    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)