Django

Code

Changeset 4419

Show
Ignore:
Timestamp:
01/24/07 13:57:41 (2 years ago)
Author:
ubernostrum
Message:

0.91-bufixes: Backport [4244] for those using legacy Django with psycopg1

Files:

Legend:

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

    r2579 r4419  
    1616    # Import copy of _thread_local.py from python 2.4 
    1717    from django.utils._threading_local import local 
     18 
     19def smart_basestring(s, charset): 
     20    if isinstance(s, unicode): 
     21        return s.encode(charset) 
     22    return s 
     23 
     24class UnicodeCursorWrapper(object): 
     25    """ 
     26    A thin wrapper around psycopg cursors that allows them to accept Unicode 
     27    strings as params. 
     28 
     29    This is necessary because psycopg doesn't apply any DB quoting to 
     30    parameters that are Unicode strings. If a param is Unicode, this will 
     31    convert it to a bytestring using DEFAULT_CHARSET before passing it to 
     32    psycopg. 
     33    """ 
     34    def __init__(self, cursor, charset): 
     35        self.cursor = cursor 
     36        self.charset = charset 
     37 
     38    def execute(self, sql, params=()): 
     39        return self.cursor.execute(sql, [smart_basestring(p, self.charset) for p in params]) 
     40 
     41    def executemany(self, sql, param_list): 
     42        new_param_list = [tuple([smart_basestring(p, self.charset) for p in params]) for params in param_list] 
     43        return self.cursor.executemany(sql, new_param_list) 
     44 
     45    def __getattr__(self, attr): 
     46        if self.__dict__.has_key(attr): 
     47            return self.__dict__[attr] 
     48        else: 
     49            return getattr(self.cursor, attr) 
    1850 
    1951class DatabaseWrapper(local): 
     
    4173        cursor = self.connection.cursor() 
    4274        cursor.execute("SET TIME ZONE %s", [TIME_ZONE]) 
     75        cursor = UnicodeCursorWrapper(cursor, settings.DEFAULT_CHARSET) 
    4376        if DEBUG: 
    4477            return base.CursorDebugWrapper(cursor, self)