o {nhVC@sddlmZmZddlmZddlZddlZddlmZm Z m Z ddl m Z e dejejBZGdd d eZGd d d eZGd d d eeZGdddeZGdddeeZdS))print_functionabsolute_import)partialN) range_type text_typePY2)errz|\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?);?\s*\Zc@s:eZdZdZdZdZddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZd=ddZddZd>ddZddZd>dd Zd>d!d"Zd#d$Zd%d&Zd?d(d)Zd*d+Zd>d,d-Zd.d/Zd@d1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z e!j"Z"e!j#Z#e!j$Z$e!j%Z%e!j&Z&e!j'Z'e!j(Z(e!j)Z)e!j*Z*e!j+Z+dS)ACursora  This is the object you use to interact with the database. Do not create an instance of a Cursor yourself. Call connections.Connection.cursor(). See `Cursor `_ in the specification. iFcCs:||_d|_d|_d|_d|_d|_d|_d|_d|_dS)NrrF) connection description rownumberrowcount arraysize _executed_result_rows_warnings_handled)selfr r:/usr/local/lib/python3.10/dist-packages/pymysql/cursors.py__init__(s zCursor.__init__cCsF|j}|dur dSz|r |sWd|_dSWd|_dSd|_w)zD Closing a cursor just exhausts all remaining data. N)r nextsetrconnrrrclose3s z Cursor.closecCs|SNrrrrr __enter__@zCursor.__enter__cGs~|dSr)r)rexc_inforrr__exit__Cs zCursor.__exit__cCs|jstd|jS)Nz Cursor closed)r r ProgrammingErrorrrrr_get_dbGs zCursor._get_dbcCs|jstddS)Nzexecute() first)rr r#rrrr_check_executedLs zCursor._check_executedcC|Srrrrowrrr _conv_rowPr zCursor._conv_rowcGdSz!Does nothing, required by DB API.Nrrargsrrr setinputsizesSzCursor.setinputsizescGr*r+rr,rrrsetoutputsizesVr/zCursor.setoutputsizescCs`|}|j}|r ||dus||jurdS|jsdSd|_||j|d|dS)zGet the next query setN unbufferedT)r$r_show_warningshas_next _clear_result next_result_do_get_result)rr2rcurrent_resultrrr_nextsetYs zCursor._nextsetcCs |dSNFr9rrrrrjs zCursor.nextsetNcsFt|tr |}|St|ttfr!t|fdd|D}|S)Nc3s|] }j|dVqdS)encodingN) _ensure_bytes).0vr=rrr qsz'Cursor._ensure_bytes..) isinstancerencodetuplelisttype)rxr=rrArr>ms  zCursor._ensure_bytescst|jjdt|ttfr#trtt|}tfdd|DSt|tr@tr5fdd| D}fdd| DStrF|} |S)Nr<c3s|]}|VqdSrliteralr?argrrrrBzsz&Cursor._escape_args..csi|] \}}||qSrrr?keyval) ensure_bytesrr }sz'Cursor._escape_args..csi|] \}}||qSrrIrNrMrrrRs) rr>r=rCrErFrmapdictitemsescape)rr-rr)rrQr _escape_argsts   zCursor._escape_argscCs8|}tr|j||jd}|dur||||}|S)z Returns the exact string that is sent to the database by calling the execute() method. This method follows the extension to the DB API 2.0 followed by Psycopg. r<N)r$rr>r=rW)rqueryr-rrrrmogrifys zCursor.mogrifycCs2|r |s|||}||}||_|S)aExecute a query :param str query: Query to execute. :param args: parameters used with query. (optional) :type args: tuple, list or dict :return: Number of affected rows :rtype: int If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query. )rrY_queryr)rrXr-resultrrrexecutes  zCursor.executec s|sdSt}|r<|dd}|d}|dpd}|ddkr,|d d ks.J||||jjStfd d |D_ j S) aRun several data against one query :param query: query to execute on server :param args: Sequence of sequences or mappings. It is used as parameter. :return: Number of rows affected, if any. This method improves performance on multiple-row INSERT and REPLACE. Otherwise it is equivalent to looping over args with execute(). Nrrr(r )c3s|] }|VqdSr)r\rKrXrrrrBsz%Cursor.executemany..) RE_INSERT_VALUESmatchgrouprstrip_do_execute_manymax_stmt_lengthr$r=sumr)rrXr-mq_prefixq_values q_postfixrrbr executemanys   zCursor.executemanyc CsH|}|j}t|tr||}trt|tr||}t|tr'||}t|} t|}||t||} t| trKtrE| |} n| |d} | | 7} d} |D]B} ||| |} t| trotri| |} n| |d} t | t | t |d|kr| | | |7} t|} n| d7} | | 7} qS| | | |7} | |_ | S)Nsurrogateescaperr,) r$rWrCrrDr bytearrayiternextlenr\r) rprefixvaluespostfixr-rhr=rrVsqlr@rowsrLrrrrgs>              zCursor._do_execute_manyrcs||r#d|ddfddt|D|ddfddtt|Df}||||_|S) aExecute stored procedure procname with args procname -- string, name of procedure to execute on server args -- Sequence of parameters to use with procedure Returns the original args. Compatibility warning: PEP-249 specifies that any modified parameters must be returned. This is currently impossible as they are only available by storing them in a server variable and then retrieved by a query. Since stored procedures return zero or more result sets, there is no reliable way to get at OUT or INOUT parameters via callproc. The server variables are named @_procname_n, where procname is the parameter above and n is the position of the parameter (from zero). Once all result sets generated by the procedure have been fetched, you can issue a SELECT @_procname_0, ... query using .execute() to get any OUT or INOUT values. Compatibility warning: The act of calling a stored procedure itself creates an empty result set. This appears after any result sets generated by the procedure. This is non-standard behavior with respect to the DB-API. Be sure to use nextset() to advance through all result sets; otherwise you may get disconnected. z @_{0}_%d=%szSET %s,c3s&|]\}}||fVqdSr)rV)r?indexrL)rfmtrrrB sz"Cursor.callproc..z CALL %s(%s)csg|]}d|fqS)z@_%s_%dr)r?i)procnamerr sz#Cursor.callproc..) r$formatrZjoin enumeraterrrtr)rr~r-qr)rr|r~rcallprocs    zCursor.callproccCsD||jdus|jt|jkrdS|j|j}|jd7_|S)zFetch the next rowNrr%rrrtrr[rrrfetchones  zCursor.fetchonecCsL||jdur dS|j|p|j}|j|j|}t|t|j|_|S)zFetch several rowsNr)r%rrrminrt)rsizeendr[rrr fetchmany!s zCursor.fetchmanycCsD||jdur dS|jr|j|jd}n|j}t|j|_|S)zFetch all the rowsNrrrrrrfetchall+s  zCursor.fetchallrelativecCsl||dkr|j|}n|dkr|}ntd|d|kr,t|jks1tdtd||_dS)Nrabsoluteunknown scroll mode %srz out of range)r%rr r#rtr IndexError)rvaluemoderrrrscroll7s  z Cursor.scrollcCs.|}||_|||||jSrr$_last_executedr5rXr7rrrrrrrrZDs  z Cursor._querycCs(d|_d|_d|_d|_d|_d|_dS)Nr)rrrr lastrowidrrrrrr5Ls  zCursor._clear_resultcCsP|}|j|_}|j|_|j|_|j|_|j|_d|_ |j s&| dSdSr:) r$r affected_rowsrr insert_idrryrr_defer_warningsr3)rrr[rrrr7Us  zCursor._do_get_resultcCs|jrdSd|_|jr|jjs|jjsdS|}|dur!dS|D]!}|d}tr6t|tr6| dd}t j t j |ddddq#dS) NTr zutf-8replacerr^) stacklevel)rrr4 warning_countr$ show_warningsrrCunicoderDwarningswarnr Warning)rwswmsgrrrr3cs   zCursor._show_warningscCs t|jdSrrrrrrrr__iter__s zCursor.__iter__)Fr)rr),__name__ __module__ __qualname____doc__rhrrrrr"r$r%r)r.r0r9rr>rWrYr\rnrgrrrrrrZr5r7r3rr rErrorInterfaceError DatabaseError DataErrorOperationalErrorIntegrityError InternalErrorr#NotSupportedErrorrrrrr sR       $*     r cs(eZdZeZfddZddZZS)DictCursorMixincs~ttg}jr)jjD]}|j}||vr |jd|}||q|_ |r;j r=fddj D_ dSdSdS)N.csg|]}|qSr)r))r?rrrrrsz2DictCursorMixin._do_get_result..) superrr7r rfieldsname table_nameappend_fieldsr)rrfr __class__rrr7s   zDictCursorMixin._do_get_resultcCs|durdS|t|j|Sr) dict_typeziprr'rrrr)szDictCursorMixin._conv_row)rrrrTrr7r) __classcell__rrrrrs rc@eZdZdZdS) DictCursorz.A cursor which returns results as a dictionaryNrrrrrrrrrrc@steZdZdZdZddZddZeZddZd d Z d d Z d dZ ddZ ddZ ddZdddZdddZdS)SSCursora Unbuffered Cursor, mainly useful for queries that return a lot of data, or for connections to remote servers over a slow network. Instead of copying every row of data into a buffer, this will fetch rows as needed. The upside of this is the client uses much less memory, and rows are returned much faster when traveling over a slow network or if the result set is very big. There are limitations, though. The MySQL protocol doesn't support returning the total number of rows, so the only way to tell how many rows there are is to iterate over every row returned. Also, it currently isn't possible to scroll backwards, as only the current row is held in memory. TcCr&rrr'rrrr)r zSSCursor._conv_rowcCsf|j}|dur dS|jdur|j|jur|jz|r) |sWd|_dSWd|_dSd|_wr)r r_finish_unbuffered_queryrrrrrrs  zSSCursor.closecCs2|}||_||j|dd||jSNTr1rrrrrrZs zSSCursor._querycCs |jddSrr;rrrrrrzSSCursor.nextsetcCs||jS)z Read next row)r)r_read_rowdata_packet_unbufferedrrrr read_nextszSSCursor.read_nextcCs6||}|dur|dS|jd7_|S)zFetch next rowNr)r%rr3rr'rrrrszSSCursor.fetchonecCs t|S)z Fetch all, as per MySQLdb. Pretty useless for large queries, as it is buffered. See fetchall_unbuffered(), if you want an unbuffered generator version of this method. )rFfetchall_unbufferedrrrrr zSSCursor.fetchallcCs t|jdS)z Fetch all, implemented as a generator, which isn't to standard, however, it doesn't make sense to return everything in a list, as that would use ridiculous memory for large result sets. NrrrrrrrzSSCursor.fetchall_unbufferedcCs|Sr)rrrrrrszSSCursor.__iter__NcCsb||dur |j}g}t|D]}|}|dur"||S|||jd7_q|S)z Fetch manyNr)r%rrrr3rr)rrryr}r(rrrrs  zSSCursor.fetchmanyrcCs||dkr%|dkrtdt|D]}|q|j|7_dS|dkrH||jkr3td||j}t|D]}|q<||_dStd|)Nrrz0Backwards scrolling not supported by this cursorrr)r%r rrrrr#)rrr_rrrrrs&       zSSCursor.scrollrr)rrrrrr)r__del__rZrrrrrrrrrrrrrs  rc@r) SSDictCursorz;An unbuffered cursor, which returns results as a dictionaryNrrrrrrrr) __future__rr functoolsrrer_compatrrrr_r compile IGNORECASEDOTALLrcobjectr rrrrrrrrs"   oy