Skip to content

Commit

Permalink
Fixed #4896: fixed #4765: Patch for cursor.executemany using oracle and
Browse files Browse the repository at this point in the history
sqlite3. Thanks, jdetaeye@www.frepple.com


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
nightflyerkilo committed Sep 14, 2007
1 parent aff47aa commit f180d95
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
44 changes: 26 additions & 18 deletions django/db/backends/oracle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,21 +438,6 @@ class FormatStylePlaceholderCursor(Database.Cursor):
"""
charset = 'utf-8'

def _rewrite_args(self, query, params=None):
if params is None:
params = []
else:
params = self._format_params(params)
args = [(':arg%d' % i) for i in range(len(params))]
query = smart_str(query, self.charset) % tuple(args)
# cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it
# it does want a trailing ';' but not a trailing '/'. However, these
# characters must be included in the original query in case the query
# is being passed to SQL*Plus.
if query.endswith(';') or query.endswith('/'):
query = query[:-1]
return query, params

def _format_params(self, params):
if isinstance(params, dict):
result = {}
Expand All @@ -464,12 +449,35 @@ def _format_params(self, params):
return tuple([smart_str(p, self.charset, True) for p in params])

def execute(self, query, params=None):
query, params = self._rewrite_args(query, params)
if params is None:
params = []
else:
params = self._format_params(params)
args = [(':arg%d' % i) for i in range(len(params))]
# cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it
# it does want a trailing ';' but not a trailing '/'. However, these
# characters must be included in the original query in case the query
# is being passed to SQL*Plus.
if query.endswith(';') or query.endswith('/'):
query = query[:-1]
query = smart_str(query, self.charset) % tuple(args)
return Database.Cursor.execute(self, query, params)

def executemany(self, query, params=None):
query, params = self._rewrite_args(query, params)
return Database.Cursor.executemany(self, query, params)
try:
args = [(':arg%d' % i) for i in range(len(params[0]))]
except (IndexError, TypeError):
# No params given, nothing to do
return None
# cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it
# it does want a trailing ';' but not a trailing '/'. However, these
# characters must be included in the original query in case the query
# is being passed to SQL*Plus.
if query.endswith(';') or query.endswith('/'):
query = query[:-1]
query = smart_str(query, self.charset) % tuple(args)
new_param_list = [self._format_params(i) for i in params]
return Database.Cursor.executemany(self, query, new_param_list)

def fetchone(self):
return to_unicode(Database.Cursor.fetchone(self))
Expand Down
8 changes: 6 additions & 2 deletions django/db/backends/sqlite3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ def execute(self, query, params=()):
return Database.Cursor.execute(self, query, params)

def executemany(self, query, param_list):
query = self.convert_query(query, len(param_list[0]))
return Database.Cursor.executemany(self, query, param_list)
try:
query = self.convert_query(query, len(param_list[0]))
return Database.Cursor.executemany(self, query, param_list)
except (IndexError,TypeError):
# No parameter list provided
return None

def convert_query(self, query, num_params):
return query % tuple("?" * num_params)
Expand Down

0 comments on commit f180d95

Please sign in to comment.