Ticket #463: mysql_magic-removal_rev2360-1.diff
File mysql_magic-removal_rev2360-1.diff, 3.6 KB (added by , 19 years ago) |
---|
-
django/db/backends/mysql/base.py
9 9 from MySQLdb.converters import conversions 10 10 from MySQLdb.constants import FIELD_TYPE 11 11 import types 12 import threading 12 13 13 14 DatabaseError = Database.DatabaseError 14 15 … … 46 47 else: 47 48 return getattr(self.cursor, attr) 48 49 50 class ThreadLocal: 51 def __getattr__(self, key): 52 threadkey = 'ThreadLocal_%s' % (id(self),) 53 thread = threading.currentThread() 54 try: 55 local = getattr(thread, threadkey) 56 return local[key] 57 except AttributeError: 58 raise AttributeError(key) 59 except KeyError: 60 raise AttributeError(key) 61 62 def __setattr__(self, key, value): 63 threadkey = 'ThreadLocal_%s' % (id(self),) 64 thread = threading.currentThread() 65 try: 66 local = getattr(thread, threadkey) 67 except AttributeError: 68 local = {} 69 setattr(thread, threadkey, local) 70 local[key] = value 71 72 def __delattr__(self, key): 73 threadkey = 'ThreadLocal_%s' % (id(self),) 74 thread = threading.currentThread() 75 try: 76 local = getattr(thread, threadkey) 77 del local[key] 78 except AttributeError: 79 raise AttributeError(key) 80 except KeyError: 81 raise AttributeError(key) 82 49 83 class DatabaseWrapper: 50 84 def __init__(self): 51 self.connection = None52 85 self.queries = [] 86 if hasattr(threading, 'local'): 87 # threading.local is available in Python 2.4 and later 88 self.threadlocal = threading.local() 89 else: 90 # workaround for Python 2.3 and earlier 91 self.threadlocal = ThreadLocal() 53 92 54 93 def cursor(self): 55 94 from django.conf import settings 56 if self.connection is None: 95 conn = getattr(self.threadlocal, 'connection', None) 96 if conn is None: 57 97 kwargs = { 58 98 'user': settings.DATABASE_USER, 59 99 'db': settings.DATABASE_NAME, … … 63 103 } 64 104 if settings.DATABASE_PORT: 65 105 kwargs['port'] = settings.DATABASE_PORT 66 self.connection = Database.connect(**kwargs) 67 cursor = self.connection.cursor() 68 if self.connection.get_server_info() >= '4.1': 106 conn = self.threadlocal.connection = Database.connect(**kwargs) 107 else: 108 conn.ping() 109 cursor = conn.cursor() 110 if conn.get_server_info() >= '4.1': 69 111 cursor.execute("SET NAMES utf8") 70 112 if settings.DEBUG: 71 113 return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 72 114 return cursor 73 115 74 116 def commit(self): 75 self.connection.commit() 117 conn = getattr(self.threadlocal, 'connection', None) 118 if conn is not None: 119 conn.commit() 76 120 77 121 def rollback(self): 78 if self.connection: 122 conn = getattr(self.threadlocal, 'connection', None) 123 if conn is not None: 79 124 try: 80 self.connection.rollback()125 conn.rollback() 81 126 except Database.NotSupportedError: 82 127 pass 83 128 84 129 def close(self): 85 if self.connection is not None: 86 self.connection.close() 87 self.connection = None 130 conn = getattr(self.threadlocal, 'connection', None) 131 if conn is not None: 132 conn.close() 133 self.threadlocal.connection = None 88 134 89 135 supports_constraints = True 90 136