Ticket #9633: 9633-adaptor-query-params-sqlite3.diff
File 9633-adaptor-query-params-sqlite3.diff, 2.6 KB (added by , 16 years ago) |
---|
-
django/db/backends/sqlite3/base.py
10 10 from django.db.backends.sqlite3.client import DatabaseClient 11 11 from django.db.backends.sqlite3.creation import DatabaseCreation 12 12 from django.db.backends.sqlite3.introspection import DatabaseIntrospection 13 from django.utils.encoding import force_unicode 13 14 14 15 try: 15 16 try: … … 165 166 """ 166 167 def execute(self, query, params=()): 167 168 query = self.convert_query(query, len(params)) 168 return Database.Cursor.execute(self, query, params) 169 new_params = [self.convert_param(p) for p in params] 170 return Database.Cursor.execute(self, query, new_params) 169 171 170 172 def executemany(self, query, param_list): 171 173 try: 172 174 query = self.convert_query(query, len(param_list[0])) 173 return Database.Cursor.executemany(self, query, param_list) 175 new_param_list = [[self.convert_param(p) for p in params] for params in param_list] 176 return Database.Cursor.executemany(self, query, new_param_list) 174 177 except (IndexError,TypeError): 175 178 # No parameter list provided 176 179 return None … … 178 181 def convert_query(self, query, num_params): 179 182 return query % tuple("?" * num_params) 180 183 184 def convert_param(self, param): 185 # These are the types that sqlite3 can handle directly 186 if param is None or type(param) in (int,long,float,str,unicode,buffer,bool): 187 return param 188 # The rest we must convert to unicode first 189 return force_unicode(param) 190 181 191 def _sqlite_extract(lookup_type, dt): 182 192 if dt is None: 183 193 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 """}