This commit is contained in:
adustman
2007-02-10 20:09:26 +00:00
parent 0340376f05
commit be73b0aeca

View File

@ -6,7 +6,7 @@ default, MySQLdb uses the Cursor class.
""" """
import re import re
insert_values = re.compile(r'\svalues\s*(\(.+\))', re.IGNORECASE) insert_values = re.compile(r"\svalues\s*(\(((?<!\\)'.*?\).*(?<!\\)?'|.)+?\))", re.IGNORECASE)
from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \ from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
DatabaseError, OperationalError, IntegrityError, InternalError, \ DatabaseError, OperationalError, IntegrityError, InternalError, \
NotSupportedError, ProgrammingError NotSupportedError, ProgrammingError
@ -189,6 +189,8 @@ class BaseCursor(object):
del self.messages[:] del self.messages[:]
db = self._get_db() db = self._get_db()
if not args: return if not args: return
charset = db.character_set_name()
if isinstance(query, unicode): query = query.encode(charset)
m = insert_values.search(query) m = insert_values.search(query)
if not m: if not m:
r = 0 r = 0
@ -196,13 +198,11 @@ class BaseCursor(object):
r = r + self.execute(query, a) r = r + self.execute(query, a)
return r return r
p = m.start(1) p = m.start(1)
charset = db.character_set_name() e = m.end(1)
query = query.encode(charset) qv = m.group(1)
qv = query[p:]
qargs = db.literal(args) qargs = db.literal(args)
try: try:
q = [ query % qargs[0] ] q = [ qv % a for a in qargs ]
q.extend([ qv % a for a in qargs[1:] ])
except TypeError, msg: except TypeError, msg:
if msg.args[0] in ("not enough arguments for format string", if msg.args[0] in ("not enough arguments for format string",
"not all arguments converted"): "not all arguments converted"):
@ -216,7 +216,7 @@ class BaseCursor(object):
exc, value, tb = exc_info() exc, value, tb = exc_info()
del tb del tb
self.errorhandler(self, exc, value) self.errorhandler(self, exc, value)
r = self._query(',\n'.join(q)) r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]]))
if not self._defer_warnings: self._warning_check() if not self._defer_warnings: self._warning_check()
return r return r