Code

Ticket #4896: executemany_oracle_sqlite.patch

File executemany_oracle_sqlite.patch, 3.8 KB (added by jdetaeye@…, 7 years ago)

Patch

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

     
    430430    """ 
    431431    charset = 'utf-8' 
    432432 
    433     def _rewrite_args(self, query, params=None): 
     433    def _format_params(self, params): 
     434        if isinstance(params, dict): 
     435            result = {} 
     436            charset = self.charset 
     437            for key, value in params.items(): 
     438                result[smart_str(key, charset)] = smart_str(value, charset) 
     439            return result 
     440        else: 
     441            return tuple([smart_str(p, self.charset, True) for p in params]) 
     442 
     443    def execute(self, query, params=None): 
    434444        if params is None: 
    435445            params = [] 
    436446        else: 
    437447            params = self._format_params(params) 
    438448        args = [(':arg%d' % i) for i in range(len(params))] 
    439         query = smart_str(query, self.charset) % tuple(args) 
    440449        # cx_Oracle wants no trailing ';' for SQL statements.  For PL/SQL, it 
    441450        # it does want a trailing ';' but not a trailing '/'.  However, these 
    442451        # characters must be included in the original query in case the query 
    443452        # is being passed to SQL*Plus. 
    444453        if query.endswith(';') or query.endswith('/'): 
    445454            query = query[:-1] 
    446         return query, params 
    447  
    448     def _format_params(self, params): 
    449         if isinstance(params, dict): 
    450             result = {} 
    451             charset = self.charset 
    452             for key, value in params.items(): 
    453                 result[smart_str(key, charset)] = smart_str(value, charset) 
    454             return result 
    455         else: 
    456             return tuple([smart_str(p, self.charset, True) for p in params]) 
    457  
    458     def execute(self, query, params=None): 
    459         query, params = self._rewrite_args(query, params) 
     455        query = smart_str(query, self.charset) % tuple(args) 
    460456        return Database.Cursor.execute(self, query, params) 
    461457 
    462458    def executemany(self, query, params=None): 
    463         query, params = self._rewrite_args(query, params) 
    464         return Database.Cursor.executemany(self, query, params) 
     459        try: 
     460          args = [(':arg%d' % i) for i in range(len(params[0]))] 
     461        except (IndexError, TypeError): 
     462          # No params given, nothing to do 
     463          return None 
     464        # cx_Oracle wants no trailing ';' for SQL statements.  For PL/SQL, it 
     465        # it does want a trailing ';' but not a trailing '/'.  However, these 
     466        # characters must be included in the original query in case the query 
     467        # is being passed to SQL*Plus. 
     468        if query.endswith(';') or query.endswith('/'): 
     469            query = query[:-1] 
     470        query = smart_str(query, self.charset) % tuple(args) 
     471        new_param_list = [self._format_params(i) for i in params] 
     472        return Database.Cursor.executemany(self, query, new_param_list) 
    465473 
    466474    def fetchone(self): 
    467475        return to_unicode(Database.Cursor.fetchone(self)) 
  • django/db/backends/sqlite3/base.py

     
    133133        return Database.Cursor.execute(self, query, params) 
    134134 
    135135    def executemany(self, query, param_list): 
    136         query = self.convert_query(query, len(param_list[0])) 
    137         return Database.Cursor.executemany(self, query, param_list) 
     136        try: 
     137          query = self.convert_query(query, len(param_list[0])) 
     138          return Database.Cursor.executemany(self, query, param_list) 
     139        except (IndexError,TypeError): 
     140          # No parameter list provided 
     141          return None 
    138142 
    139143    def convert_query(self, query, num_params): 
    140144        return query % tuple("?" * num_params)