Ticket #9633: 9633-adaptor-query-params-sqlite3-v2.diff
File 9633-adaptor-query-params-sqlite3-v2.diff, 2.6 KB (added by , 16 years ago) |
---|
-
django/db/backends/sqlite3/base.py
13 13 from django.db.backends.sqlite3.creation import DatabaseCreation 14 14 from django.db.backends.sqlite3.introspection import DatabaseIntrospection 15 15 from django.utils.safestring import SafeString 16 from django.utils.encoding import force_unicode 16 17 17 18 try: 18 19 try: … … 190 191 """ 191 192 def execute(self, query, params=()): 192 193 query = self.convert_query(query, len(params)) 193 return Database.Cursor.execute(self, query, params) 194 return Database.Cursor.execute(self, query, 195 [self.convert_param(p) for p in params]) 194 196 195 197 def executemany(self, query, param_list): 196 198 try: 197 199 query = self.convert_query(query, len(param_list[0])) 198 return Database.Cursor.executemany(self, query, param_list) 200 return Database.Cursor.executemany(self, query, 201 [[self.convert_param(p) for p in params] for params in param_list]) 199 202 except (IndexError,TypeError): 200 203 # No parameter list provided 201 204 return None … … 203 206 def convert_query(self, query, num_params): 204 207 return query % tuple("?" * num_params) 205 208 209 def convert_param(self, param): 210 # These are the types that sqlite3 can handle directly 211 if param is None or isinstance(param, (int,long,float,str,unicode,buffer,bool)): 212 return param 213 # The rest we must convert to unicode first 214 return force_unicode(param) 215 206 216 def _sqlite_extract(lookup_type, dt): 207 217 if dt is None: 208 218 return None -
tests/regressiontests/backends/models.py
15 15 def __unicode__(self): 16 16 return u'%s %s' % (self.first_name, self.last_name) 17 17 18 class Adaptor(object): 19 def __init__(self, value): 20 self.value = value 21 22 def __str__(self): 23 return self.value 24 18 25 qn = connection.ops.quote_name 19 26 20 27 __test__ = {'API_TESTS': """ … … 56 63 >>> list(cursor.fetchall()) 57 64 [(u'Mary', u'Agnelline'), (u'Peter', u'Parker')] 58 65 66 #9633: Using adaptor objects as query parameters with sqlite3 67 >>> query = 'SELECT %s' 68 >>> param = Adaptor(u'Arthur') 69 >>> foo = cursor.execute(query, (param,)) 70 >>> cursor.fetchone() 71 (u'Arthur',) 59 72 """}