B ¡ÌìW¨Oã@sPdZddlmZmZddlmZddlZddlZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZejddkZer„e ZneZe dejejB¡ZGd d „d eƒZGd d „d eƒZGd d„deƒZ Gdd„deƒZ!Gdd„deƒZ"Gdd„de"ƒZ#Gdd„dee!eƒZ$Gdd„dee"eƒZ%Gdd„de e!eƒZ&Gdd„de e"eƒZ'dS)zyMySQLdb Cursors This module implements Cursors of various types for MySQLdb. By default, MySQLdb uses the Cursor class. é)Úprint_functionÚabsolute_import)ÚpartialN)Úunicode) ÚWarningÚErrorÚInterfaceErrorÚ DataErrorÚ DatabaseErrorÚOperationalErrorÚIntegrityErrorÚ InternalErrorÚNotSupportedErrorÚProgrammingErrorézw\s*((?:INSERT|REPLACE)\s.+\sVALUES?\s+)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?)\Zc@s0eZdZdZdZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZdZdZdd„Zd d „Zd d „Zd d„Zd5dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Z d6d#d$„Z!d%d&„Z"d'd(„Z#d7d*d+„Z$d,d-„Z%d.d/„Z&d8d1d2„Z'd3d4„Z(eZeZe Z e Z e Z e Z e Z eZeZeZdS)9Ú BaseCursoraA base for Cursor classes. Useful attributes: description A tuple of DB API 7-tuples describing the columns in the last executed query; see PEP-249 for details. description_flags Tuple of column flags for last query, one entry per column in the result set. Values correspond to those in MySQLdb.constants.FLAG. See MySQL documentation (C API) for more information. Non-standard extension. arraysize default number of rows fetchmany() will fetch ir) Ú MySQLErrorrrrr r r r r rrFNcCsN||_d|_d|_d|_d|_d|_d|_g|_|j|_d|_ d|_ d|_ dS)Néÿÿÿÿé) Ú connectionÚ descriptionÚdescription_flagsÚrowcountÚ arraysizeÚ _executedÚ lastrowidÚmessagesÚ errorhandlerÚ_resultÚ _warningsÚ rownumber)Úselfr©r"úB/opt/alt/python37/lib64/python3.7/site-packages/MySQLdb/cursors.pyÚ__init__AszBaseCursor.__init__cCs:z |jdkrdSx | ¡rqWWdd|_d|_d|_XdS)z6Close the cursor. No further queries will be possible.N)rÚnextsetrr)r!r"r"r#ÚcloseOs  zBaseCursor.closecCs|S)Nr")r!r"r"r#Ú __enter__[szBaseCursor.__enter__cGs~| ¡dS)N)r&)r!Úexc_infor"r"r#Ú__exit__^szBaseCursor.__exit__csDt|tƒr| ˆ¡}n*t|ttfƒr@t|ƒ‡‡fdd„|Dƒƒ}|S)Nc3s|]}ˆj|ˆdVqdS))ÚencodingN)Ú _ensure_bytes)Ú.0Úv)r*r!r"r#ú fsz+BaseCursor._ensure_bytes..)Ú isinstanceÚ text_typeÚencodeÚtupleÚlistÚtype)r!Úxr*r")r*r!r#r+bs   zBaseCursor._ensure_bytescs¢t|jˆjd‰t|ttfƒrFtr0ttˆ|ƒƒ}t‡fdd„|DƒƒSt|tƒrˆtrnt‡fdd„|  ¡Dƒƒ}t‡fdd„|  ¡DƒƒStr”ˆ|ƒ}ˆ  |¡SdS)N)r*c3s|]}ˆ |¡VqdS)N)Úliteral)r,Úarg)Úconnr"r#r.osz*BaseCursor._escape_args..c3s"|]\}}ˆ|ƒˆ|ƒfVqdS)Nr")r,ÚkeyÚval)Ú ensure_bytesr"r#r.rsc3s |]\}}|ˆ |¡fVqdS)N)r6)r,r9r:)r8r"r#r.ts) rr+r*r/r2r3ÚPY2ÚmapÚdictÚitemsr6)r!Úargsr8r")r8r;r#Ú _escape_argsis  zBaseCursor._escape_argscCs|js| |td¡dS)Nzexecute() first)rrr)r!r"r"r#Ú_check_executed|szBaseCursor._check_executedcCsddlm}| ¡}|jdkr(| ¡|_|jrþ|j}d|_|jrv|jjrvd|f}|j |¡||  d|¡dddS|  ¡}|rÌx|D]}|j |j |f¡qˆWxZ|D]}||j |dd…Žddq¨Wn2|  ¡}|rþ|j |j |f¡||  d|¡dddS)Nr)ÚwarnzThere are %d MySQL warnings.é)Ú stacklevelr) ÚwarningsrCÚ_get_dbrÚ warning_countrZhas_nextrÚappendrZ show_warningsÚinfo)r!rCÚdbrHÚmsgrFÚwrJr"r"r#Ú_warning_check€s,        zBaseCursor._warning_checkcCsR|jr| ¡|jdd…=| ¡}| ¡}|dkr6dS| ¡| ¡| ¡dS)z`Advance to the next result set. Returns None if there are no more result sets. Nrr)rÚfetchallrrGZ next_resultÚ_do_get_resultÚ_post_get_resultrN)r!rKZnrr"r"r#r%¢s zBaseCursor.nextsetcCsdS)Nr")r!r"r"r#rQ´ózBaseCursor._post_get_resultcCsb| ¡}| ¡|_| ¡|_d|_|jr2|j ¡p4d|_|jrH|j ¡pJd|_ |  ¡|_ d|_ dS)Nr) rGÚ _get_resultrZ affected_rowsrr ZdescriberZ field_flagsrZ insert_idrr)r!rKr"r"r#rP¶s   zBaseCursor._do_get_resultcGsdS)z!Does nothing, required by DB API.Nr")r!r@r"r"r#Ú setinputsizesÀszBaseCursor.setinputsizescGsdS)z!Does nothing, required by DB API.Nr")r!r@r"r"r#ÚsetoutputsizesÃszBaseCursor.setoutputsizescCs|j}|dkrtdƒ‚|S)Nz cursor closed)rr)r!Zconr"r"r#rGÆszBaseCursor._get_dbc sRx | ¡r qW| ¡‰tr2t|tƒr2| ˆjj¡}|dk rÒt|tƒr`t‡fdd„|  ¡Dƒƒ}nt t ˆj |ƒƒ}tst|t tfƒr| ˆjj¡}y ||}Wn4tk rÐ}z| |tt|ƒ¡Wdd}~XYnXt|tƒrì| ˆjjd¡}d}y| |¡}Wn8tk r6t ¡dd…\}}| |||¡YnX||_|jsN| ¡|S)a‰Execute a query. query -- string, query to execute on server args -- optional sequence or mapping, parameters to use with query. Note: If args is a sequence, then %s must be used as the parameter placeholder in the query. If a mapping is used, %(key)s must be used as the placeholder. Returns integer represents rows affected, if any Nc3s |]\}}|ˆ |¡fVqdS)N)r6)r,r9Úitem)rKr"r#r.èsz%BaseCursor.execute..Úsurrogateescaper)r%rGr<r/rr1Úunicode_literalÚcharsetr>r?r2r=r6ÚbytesÚ bytearrayÚdecodeÚ TypeErrorrrÚstrÚ_queryÚ ExceptionÚsysr(rÚ_defer_warningsrN)r!Úqueryr@ÚmZresÚexcÚvaluer")rKr#ÚexecuteÌs6    $ zBaseCursor.executec s¤ˆjdd…=|sdSt ˆ¡}|r„| d¡d}| d¡ ¡}| d¡pJd}|ddkrd|d d ksht‚ˆ ||||ˆjˆ ¡j ¡St ‡‡fd d „|Dƒƒˆ_ ˆj S) axExecute a multi-row 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(). Nrr"rrDÚrú(rú)c3s|]}ˆ ˆ|¡VqdS)N)rg)r,r7)rcr!r"r#r.sz)BaseCursor.executemany..) rÚRE_INSERT_VALUESÚmatchÚgroupÚrstripÚAssertionErrorÚ_do_execute_manyÚmax_stmt_lengthrGr*Úsumr)r!rcr@rdZq_prefixZq_valuesZ q_postfixr")rcr!r#Ú executemanys    zBaseCursor.executemanyc CsN| ¡}|j}t|tƒr"| |¡}tr:t|tƒr:| |¡}t|tƒrN| |¡}t|ƒ} t|ƒ}||t|ƒ|ƒ} t| tƒr–trŠ|  |¡} n |  |d¡} | | 7} d} xŽ|D]†} ||| |ƒ} t| tƒràtrÔ|  |¡} n |  |d¡} t | ƒt | ƒt |ƒd|kr| |  | |¡7} t|ƒ} n| d7} | | 7} q¨W| |  | |¡7} | |_ | S)NrWrró,) rGrAr/r0r1r<r[ÚiterÚnextÚlenrgr) r!ÚprefixÚvaluesZpostfixr@rqr*r8ÚescapeZsqlr-Zrowsr7r"r"r#rps>            "  zBaseCursor._do_execute_manyr"csÂ| ¡}xTt|ƒD]H\}}dˆ|| |¡f}t|tƒrH| |jjd¡}| |¡|  ¡qWdˆd  ‡fdd„t t |ƒƒDƒ¡f}t|tƒr | |jjd¡}| |¡||_ |js¾| ¡|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. zSET @_%s_%d=%srWz CALL %s(%s)ú,csg|]}dˆ|f‘qS)z@_%s_%dr")r,Úi)Úprocnamer"r#ú isz'BaseCursor.callproc..)rGÚ enumerater6r/rr1rXrYr_r%ÚjoinÚrangerwrrbrN)r!r}r@rKÚindexr7Úqr")r}r#ÚcallprocBs$     zBaseCursor.callproccCs&| ¡}||_| |¡| ¡|jS)N)rGZ_last_executedrcrPr)r!rƒrKr"r"r#Ú _do_queryss  zBaseCursor._do_querycCs | |¡S)N)r…)r!rƒr"r"r#r_zszBaseCursor._queryrcCs|js dS|j ||j¡S)Nr")rZ fetch_rowÚ _fetch_type)r!Úsizer"r"r#Ú _fetch_row}szBaseCursor._fetch_rowcCs t|jdƒS)N)ruÚfetchone)r!r"r"r#Ú__iter__‚szBaseCursor.__iter__)N)N)r")r))Ú__name__Ú __module__Ú __qualname__Ú__doc__rqÚ_mysql_exceptionsrrrrr r r r r rrrbrr$r&r'r)r+rArBrNr%rQrPrTrUrGrgrsrpr„r…r_rˆrŠr"r"r"r#r#sJ4  "  4$ 1 rc@sTeZdZdZdd„Zdd„Zdd„Zdd „Zdd d „Zd d„Z ddd„Z dd„Z d S)ÚCursorStoreResultMixInaThis is a MixIn class which causes the entire result set to be stored on the client side, i.e. it uses mysql_store_result(). If the result set can be very large, consider adding a LIMIT clause to your query, or using CursorUseResultMixIn instead.cCs | ¡ ¡S)N)rGZ store_result)r!r"r"r#rS—sz"CursorStoreResultMixIn._get_resultcCs| |¡}| ¡|S)N)r…rQ)r!rƒrr"r"r#r_šs zCursorStoreResultMixIn._querycCs| d¡|_d|_dS)Nr)rˆÚ_rowsr)r!r"r"r#rQŸs z'CursorStoreResultMixIn._post_get_resultcCs8| ¡|jt|jƒkrdS|j|j}|jd|_|S)z]Fetches a single row from the cursor. None indicates that no more rows are available.Nr)rBr rwr‘)r!Úresultr"r"r#r‰£s   zCursorStoreResultMixIn.fetchoneNcCs>| ¡|j|p|j}|j|j|…}t|t|jƒƒ|_|S)z…Fetch up to size rows from the cursor. Result set may be smaller than size. If size is not defined, cursor.arraysize is used.)rBr rr‘Úminrw)r!r‡Úendr’r"r"r#Ú fetchmany­s z CursorStoreResultMixIn.fetchmanycCs6| ¡|jr |j|jd…}n|j}t|jƒ|_|S)z*Fetchs all available rows from the cursor.N)rBr r‘rw)r!r’r"r"r#rO¶s  zCursorStoreResultMixIn.fetchallÚrelativecCsn| ¡|dkr|j|}n$|dkr*|}n| |tdt|ƒ¡|dksV|t|jƒkrd| |td¡||_dS)aScroll the cursor in the result set to a new position according to mode. If mode is 'relative' (default), value is taken as offset to the current position in the result set, if set to 'absolute', value states an absolute target position.r–Zabsolutezunknown scroll mode %srz out of rangeN)rBr rrÚreprrwr‘Ú IndexError)r!rfÚmodeÚrr"r"r#ÚscrollÀs zCursorStoreResultMixIn.scrollcCs,| ¡|jr|j|jd…p"|j}t|ƒS)N)rBr r‘ru)r!r’r"r"r#rŠÓszCursorStoreResultMixIn.__iter__)N)r–) r‹rŒrrŽrSr_rQr‰r•rOr›rŠr"r"r"r#r‘s  rc@sJeZdZdZdZdd„Zdd„Zddd „Zd d „Zd d „Z dd„Z e Z dS)ÚCursorUseResultMixInaThis is a MixIn class which causes the result set to be stored in the server and sent row-by-row to client side, i.e. it uses mysql_use_result(). You MUST retrieve the entire result set and close() the cursor before additional queries can be performed on the connection.TcCs | ¡ ¡S)N)rGZ use_result)r!r"r"r#rSãrRz CursorUseResultMixIn._get_resultcCs6| ¡| d¡}|s"| ¡dS|jd|_|dS)z%Fetches a single row from the cursor.rNr)rBrˆrNr )r!ršr"r"r#r‰ås  zCursorUseResultMixIn.fetchoneNcCs8| ¡| |p|j¡}|jt|ƒ|_|s4| ¡|S)z…Fetch up to size rows from the cursor. Result set may be smaller than size. If size is not defined, cursor.arraysize is used.)rBrˆrr rwrN)r!r‡ršr"r"r#r•ïs zCursorUseResultMixIn.fetchmanycCs.| ¡| d¡}|jt|ƒ|_| ¡|S)z*Fetchs all available rows from the cursor.r)rBrˆr rwrN)r!ršr"r"r#rOùs  zCursorUseResultMixIn.fetchallcCs|S)Nr")r!r"r"r#rŠszCursorUseResultMixIn.__iter__cCs| ¡}|dkrt‚|S)N)r‰Ú StopIteration)r!Úrowr"r"r#rvszCursorUseResultMixIn.next)N) r‹rŒrrŽrbrSr‰r•rOrŠrvÚ__next__r"r"r"r#rœÙs rœc@seZdZdZdZdS)ÚCursorTupleRowsMixInzwThis is a MixIn class that causes all rows to be returned as tuples, which is the standard form required by DB API.rN)r‹rŒrrŽr†r"r"r"r#r  sr c@s.eZdZdZdZdd„Zd dd„Zdd „ZdS) ÚCursorDictRowsMixInznThis is a MixIn class that causes all rows to be returned as dictionaries. This is a non-standard feature.rcCs ddlm}|dtdƒ| ¡S)zgFetch a single row as a dictionary. Deprecated: Use fetchone() instead. Will be removed in 1.3.r)rCz9fetchoneDict() is non-standard and will be removed in 1.3r)rFrCÚDeprecationWarningr‰)r!rCr"r"r#Ú fetchoneDicts z CursorDictRowsMixIn.fetchoneDictNcCs"ddlm}|dtdƒ| |¡S)zrFetch several rows as a list of dictionaries. Deprecated: Use fetchmany() instead. Will be removed in 1.3.r)rCz:fetchmanyDict() is non-standard and will be removed in 1.3r)rFrCr¢r•)r!r‡rCr"r"r#Ú fetchmanyDict"s z!CursorDictRowsMixIn.fetchmanyDictcCs ddlm}|dtdƒ| ¡S)zwFetch all available rows as a list of dictionaries. Deprecated: Use fetchall() instead. Will be removed in 1.3.r)rCz9fetchallDict() is non-standard and will be removed in 1.3r)rFrCr¢rO)r!rCr"r"r#Ú fetchallDict*s z CursorDictRowsMixIn.fetchallDict)N)r‹rŒrrŽr†r£r¤r¥r"r"r"r#r¡s  r¡c@seZdZdZdZdS)ÚCursorOldDictRowsMixInzŽThis is a MixIn class that returns rows as dictionaries with the same key convention as the old Mysqldb (MySQLmodule). Don't use this.rN)r‹rŒrrŽr†r"r"r"r#r¦3sr¦c@seZdZdZdS)ÚCursorzjThis is the standard Cursor class that returns rows as tuples and stores the result set in the client.N)r‹rŒrrŽr"r"r"r#r§;sr§c@seZdZdZdS)Ú DictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the client.N)r‹rŒrrŽr"r"r"r#r¨Asr¨c@seZdZdZdS)ÚSSCursorz_This is a Cursor class that returns rows as tuples and stores the result set in the server.N)r‹rŒrrŽr"r"r"r#r©Gsr©c@seZdZdZdS)Ú SSDictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the server.N)r‹rŒrrŽr"r"r"r#rªMsrª)(rŽZ __future__rrÚ functoolsrÚreraZMySQLdb.compatrrrrrr r r r r rrÚ version_infor<r0r^ÚcompileÚ IGNORECASEÚDOTALLrkÚobjectrrrœr r¡r¦r§r¨r©rªr"r"r"r#Ús:  0pH4