Ticket #9633: 9633-adaptor-query-params-sqlite3-v2.diff

File 9633-adaptor-query-params-sqlite3-v2.diff, 2.6 KB (added by jbronn, 16 years ago)

clean up of initial patch

  • django/db/backends/sqlite3/base.py

     
    1313from django.db.backends.sqlite3.creation import DatabaseCreation
    1414from django.db.backends.sqlite3.introspection import DatabaseIntrospection
    1515from django.utils.safestring import SafeString
     16from django.utils.encoding import force_unicode
    1617
    1718try:
    1819    try:
     
    190191    """
    191192    def execute(self, query, params=()):
    192193        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])
    194196
    195197    def executemany(self, query, param_list):
    196198        try:
    197199          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])
    199202        except (IndexError,TypeError):
    200203          # No parameter list provided
    201204          return None
     
    203206    def convert_query(self, query, num_params):
    204207        return query % tuple("?" * num_params)
    205208
     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
    206216def _sqlite_extract(lookup_type, dt):
    207217    if dt is None:
    208218        return None
  • tests/regressiontests/backends/models.py

     
    1515    def __unicode__(self):
    1616        return u'%s %s' % (self.first_name, self.last_name)
    1717
     18class Adaptor(object):
     19    def __init__(self, value):
     20        self.value = value
     21
     22    def __str__(self):
     23        return self.value
     24
    1825qn = connection.ops.quote_name
    1926
    2027__test__ = {'API_TESTS': """
     
    5663>>> list(cursor.fetchall())
    5764[(u'Mary', u'Agnelline'), (u'Peter', u'Parker')]
    5865
     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',)
    5972"""}
Back to Top