66"""
77
88import re
9- insert_values = re .compile (r' \svalues\s*(\(.+\))' , re .IGNORECASE )
9+ insert_values = re .compile (r" \svalues\s*(\(((?<!\\)'.*?\).*(?<!\\)?'|.)+?\))" , re .IGNORECASE )
1010from _mysql_exceptions import Warning , Error , InterfaceError , DataError , \
1111 DatabaseError , OperationalError , IntegrityError , InternalError , \
1212 NotSupportedError , ProgrammingError
@@ -189,20 +189,20 @@ def executemany(self, query, args):
189189 del self .messages [:]
190190 db = self ._get_db ()
191191 if not args : return
192+ charset = db .character_set_name ()
193+ if isinstance (query , unicode ): query = query .encode (charset )
192194 m = insert_values .search (query )
193195 if not m :
194196 r = 0
195197 for a in args :
196198 r = r + self .execute (query , a )
197199 return r
198200 p = m .start (1 )
199- charset = db .character_set_name ()
200- query = query .encode (charset )
201- qv = query [p :]
201+ e = m .end (1 )
202+ qv = m .group (1 )
202203 qargs = db .literal (args )
203204 try :
204- q = [ query % qargs [0 ] ]
205- q .extend ([ qv % a for a in qargs [1 :] ])
205+ q = [ qv % a for a in qargs ]
206206 except TypeError , msg :
207207 if msg .args [0 ] in ("not enough arguments for format string" ,
208208 "not all arguments converted" ):
@@ -216,7 +216,7 @@ def executemany(self, query, args):
216216 exc , value , tb = exc_info ()
217217 del tb
218218 self .errorhandler (self , exc , value )
219- r = self ._query (', \n ' .join (q ))
219+ r = self ._query ('\n ' . join ([ query [: p ], ', \n ' .join (q ), query [ e :]] ))
220220 if not self ._defer_warnings : self ._warning_check ()
221221 return r
222222
0 commit comments