ó ·\c@@sddlmZmZddlmZddlZddlZddlmZm Z m Z ddl m Z ej ddd ejejBƒZd efd „ƒYZd efd „ƒYZdeefd„ƒYZdefd„ƒYZdeefd„ƒYZdS(i(tprint_functiontabsolute_import(tpartialNi(t range_typet text_typetPY2(terrs'\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)s6(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))s(\s*(?:ON DUPLICATE.*)?);?\s*\ZtCursorcB@s|eZdZdZeZd„Zd„Zd„Zd„Z d„Z d„Z d„Z d „Z d „Zed „Zd „Zdd „Zd„Zdd„Zdd„Zd„Zd„Zdd„Zd„Zdd„Zd„Zdd„Zd„Zd„Zd„Zd„Z 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,RS( s  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. i cC@sU||_d|_d|_d|_d|_d|_d|_d|_t |_ dS(Niiÿÿÿÿi( t connectiontNonet descriptiont rownumbertrowcountt arraysizet _executedt_resultt_rowstFalset_warnings_handled(tselfR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt__init__(s        cC@sA|j}|dkrdSzx|jƒr.qWWdd|_XdS(sD Closing a cursor just exhausts all remaining data. N(RR tnextset(Rtconn((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytclose3s  cC@s|S(N((R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt __enter__@scG@s~|jƒdS(N(R(Rtexc_info((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt__exit__CscC@s"|jstjdƒ‚n|jS(Ns Cursor closed(RRtProgrammingError(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt_get_dbGs cC@s|jstjdƒ‚ndS(Nsexecute() first(RRR(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt_check_executedLs cC@s|S(N((Rtrow((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt _conv_rowPscG@sdS(s!Does nothing, required by DB API.N((Rtargs((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt setinputsizesSscG@sdS(s!Does nothing, required by DB API.N((RR ((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytsetoutputsizesVscC@s…|jƒ}|j}|r(|jƒn|dksC||jk rGdS|jsTdSd|_|jƒ|jd|ƒ|jƒtS(sGet the next query sett unbufferedN( RRt_show_warningsR thas_nextt _clear_resultt next_resultt_do_get_resulttTrue(RR#Rtcurrent_result((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt_nextsetYs       cC@s |jtƒS(N(R+R(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRjsc@sbt|tƒr!|jˆƒ}n=t|ttfƒr^t|ƒ‡‡fd†|Dƒƒ}n|S(Nc3@s$|]}ˆj|dˆƒVqdS(tencodingN(t _ensure_bytes(t.0tv(R,R(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys qs(t isinstanceRtencodettupletlistttype(RtxR,((R,Rs0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR-ms (c@sÙt|jdˆjƒ‰t|ttfƒretrKttˆ|ƒƒ}nt‡fd†|DƒƒSt|tƒr³tr™‡fd†|j ƒDƒ}n‡fd†|j ƒDƒStrȈ|ƒ}nˆj |ƒSdS(NR,c3@s|]}ˆj|ƒVqdS(N(tliteral(R.targ(R(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys zsc@s+i|]!\}}ˆ|ƒˆ|ƒ“qS(((R.tkeytval(t ensure_bytes(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys }s c@s(i|]\}}ˆj|ƒ|“qS((R6(R.R8R9(R(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys s ( RR-R,R0R2R3Rtmaptdicttitemstescape(RR R((RR:s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt _escape_argsts cC@sV|jƒ}tr-|j|d|jƒ}n|dk rR||j||ƒ}n|S(s 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(RRR-R,R R?(RtqueryR R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytmogrify‡s   cC@sAx|jƒrqW|j||ƒ}|j|ƒ}||_|S(sŠExecute 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. (RRAt_queryR(RR@R tresult((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytexecute—s  c@sÓ|s dStjˆƒ}|rª|jdƒd }|jdƒjƒ}|jdƒpYd}|ddkr||dd ks‚t‚ˆj||||ˆjˆjƒjƒSt ‡‡fd †|Dƒƒˆ_ ˆj S( s€Run 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(). Niiitit(iÿÿÿÿt)c3@s!|]}ˆjˆ|ƒVqdS(N(RD(R.R7(R@R(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys Çs(( tRE_INSERT_VALUEStmatchtgrouptrstriptAssertionErrort_do_execute_manytmax_stmt_lengthRR,tsumR (RR@R tmtq_prefixtq_valuest q_postfix((R@Rs0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt executemany®s &"c C@sã|jƒ}|j}t|tƒr6|j|ƒ}ntr]t|tƒr]|j|ƒ}nt|tƒr~|j|ƒ}nt|ƒ} t|ƒ}||t|ƒ|ƒ} t| tƒrëtrÖ| j|ƒ} që| j|dƒ} n| | 7} d} xÁ|D]¹} ||| |ƒ} t| tƒrWtrB| j|ƒ} qW| j|dƒ} nt | ƒt | ƒt |ƒd|kr§| |j | |ƒ7} t|ƒ} n | d7} | | 7} qW| |j | |ƒ7} | |_ | S(Ntsurrogateescapeiit,( RR?R0RR1Rt bytearraytitertnexttlenRDR ( RtprefixtvaluestpostfixR RNR,RR>tsqlR/trowsR7((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRMÊs>      *  c@s½|jƒ‰|radj|ƒ‰|jddj‡‡fd†t|ƒDƒƒƒ|jƒnd|djgtt|ƒƒD]}d||f^q€ƒf}|j|ƒ||_|S(sExecute 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. s @_{0}_%d=%ssSET %sRVc3@s.|]$\}}ˆ|ˆj|ƒfVqdS(N(R>(R.tindexR7(Rtfmt(s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pys ss CALL %s(%s)s@_%s_%d( RtformatRBtjoint enumerateRRRZR(RtprocnameR titq((RRas0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytcallprocîs   3  cC@sX|jƒ|jdks1|jt|jƒkr5dS|j|j}|jd7_|S(sFetch the next rowiN(RRR R RZ(RRC((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytfetchones  'cC@se|jƒ|jdkrdS|j|p/|j}|j|j|!}t|t|jƒƒ|_|S(sFetch several rowsN((RRR R R tminRZ(RtsizetendRC((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt fetchmany!s cC@sX|jƒ|jdkrdS|jr9|j|j}n |j}t|jƒ|_|S(sFetch all the rowsN((RRR R RZ(RRC((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytfetchall+s   trelativecC@s|jƒ|dkr&|j|}n(|dkr;|}ntjd|ƒ‚d|kont|jƒkns‚tdƒ‚n||_dS(NRotabsolutesunknown scroll mode %sis out of range(RR RRRZRt IndexError(Rtvaluetmodetr((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pytscroll7s    %cC@s=|jƒ}||_|jƒ|j|ƒ|jƒ|jS(N(Rt_last_executedR&R@R(R (RRgR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRBDs      cC@s:d|_d|_d|_d|_d|_d|_dS(Ni(R R RR R t lastrowidR(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR&Ls      cC@so|jƒ}|j|_}|j|_|j|_|j|_|j|_t |_ |j sk|j ƒndS(N( RRt affected_rowsR R t insert_idRwR_RRRt_defer_warningsR$(RRRC((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR(Us       cC@sÍ|jr dSt|_|jr<|jjs8|jj r<dS|jƒjƒ}|dkr^dSxh|D]`}|d}tr¢t |t ƒr¢|j ddƒ}q¢nt j tj|dd!ŒddƒqeWdS(Niÿÿÿÿsutf-8treplaceiit stackleveli(RR)RR%t warning_countRt show_warningsR RR0tunicodeR1twarningstwarnRtWarning(Rtwstwtmsg((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR$cs  "   cC@st|jdƒS(N(RXRiR (R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt__iter__ssN((-t__name__t __module__t__doc__RNRRzRRRRRRRR!R"R+RR R-R?RARDRTRMRhRiRmRnRuRBR&R(R$R†RR‚tErrortInterfaceErrort DatabaseErrort DataErrortOperationalErrortIntegrityErrort InternalErrorRtNotSupportedError(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRsP                $ *             tDictCursorMixincB@s eZeZd„Zd„ZRS(cC@s¹tt|ƒjƒg}|jr{xJ|jjD]<}|j}||kr^|jd|}n|j|ƒq/W||_ n|rµ|j rµg|j D]}|j |ƒ^q”|_ ndS(Nt.( tsuperR’R(R Rtfieldstnamet table_nametappendt_fieldsRR(RR•tfR–Rt((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR(†s    cC@s)|dkrdS|jt|j|ƒƒS(N(R t dict_typetzipR™(RR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR”s (R‡RˆR<R›R(R(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR’‚s t DictCursorcB@seZdZRS(s.A cursor which returns results as a dictionary(R‡RˆR‰(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRšstSSCursorcB@sƒeZdZeZd„Zd„ZeZd„Zd„Z d„Z d„Z d„Z d„Z d „Zd d „Zd d „ZRS(sà 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. cC@s|S(N((RR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR°scC@sr|j}|dkrdS|jdk rJ|j|jkrJ|jjƒnzx|jƒr_qPWWdd|_XdS(N(RR Rt_finish_unbuffered_queryR(RR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR³s  !cC@sC|jƒ}||_|jƒ|j|dtƒ|jƒ|jS(NR#(RRvR&R@R)R(R (RRgR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRBÃs     cC@s|jdtƒS(NR#(R+R)(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRËscC@s|j|jjƒƒS(s Read next row(RRt_read_rowdata_packet_unbuffered(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyt read_nextÎscC@sC|jƒ|jƒ}|dkr0|jƒdS|jd7_|S(sFetch next rowiN(RR¡R R$R (RR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRiÒs    cC@st|jƒƒS(sÈ 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. (R3tfetchall_unbuffered(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRnÜscC@st|jdƒS(sÛ 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. N(RXRiR (R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR¢äscC@s |jƒS(N(R¢(R((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR†ìscC@s…|jƒ|dkr"|j}ng}xVt|ƒD]H}|jƒ}|dkra|jƒPn|j|ƒ|jd7_q5W|S(s Fetch manyiN(RR R RR¡R$R˜R (RRkR_RfR((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRmïs       RocC@så|jƒ|dkrg|dkr4tjdƒ‚nxt|ƒD]}|jƒqAW|j|7_nz|dkrÎ||jkr”tjdƒ‚n||j}xt|ƒD]}|jƒq®W||_ntjd|ƒ‚dS(NRois0Backwards scrolling not supported by this cursorRpsunknown scroll mode %s(RRR‘RR¡R R(RRrRst_Rl((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRuÿs"        N(R‡RˆR‰R)RzRRt__del__RBRR¡RiRnR¢R†R RmRu(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyRžžs         t SSDictCursorcB@seZdZRS(s;An unbuffered cursor, which returns results as a dictionary(R‡RˆR‰(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyR¥s(t __future__RRt functoolsRtreR€t_compatRRRRERtcompilet IGNORECASEtDOTALLRHtobjectRR’RRžR¥(((s0/tmp/pip-build-7KIPgS/PyMySQL/pymysql/cursors.pyts  ÿny