a KK]?@s$dZddlmZmZddlmZddlZddlZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZedejejBZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeeeZGdddeeeZ GdddeeeZ!GdddeeeZ"dS)zyMySQLdb Cursors This module implements Cursors of various types for MySQLdb. By default, MySQLdb uses the Cursor class. )print_functionabsolute_import)partialN)unicode) WarningErrorInterfaceError DataError DatabaseErrorOperationalErrorIntegrityError InternalErrorNotSupportedErrorProgrammingErrorz|\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))(\s*(?:ON DUPLICATE.*)?);?\s*\Zc@seZdZdZdZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZdZddZdd Zd d Zd d ZddZddZddZddZddZddZddZddZd-ddZd d!Zd"d#Z d.d%d&Z!d'd(Z"d/d)d*Z#d+d,Z$eZeZe Z e Z e Z e Z e Z eZeZeZdS)0 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) MySQLErrorrrr r r r r rrrNcCsR||_d|_d|_d|_d|_d|_d|_d|_g|_d|_ d|_ d|_ d|_ dS)Nr) connection descriptiondescription_flagsrowcount arraysize _executed_last_executed lastrowidmessages_result _warnings rownumber_rows)selfrr"1/usr/lib/python3/dist-packages/MySQLdb/cursors.py__init__;szBaseCursor.__init__cCsJz6|jdurWd|_d|_dS|r(qWd|_d|_nd|_d|_0dS)z6Close the cursor. No further queries will be possible.N)rrnextsetr!r"r"r#closeQs zBaseCursor.closecCs|SNr"r&r"r"r# __enter__\szBaseCursor.__enter__cGs~|dSr()r')r!exc_infor"r"r#__exit___szBaseCursor.__exit__csz|j|jfddt|ttfrBtfdd|D}n0t|trffdd|D}n |}d|S)NcsHt|tr|St|tr,tt|St|trDtt|S|Sr() isinstancerencodetuplemaplist)x)encoding ensure_bytesr"r#r3gs    z-BaseCursor._escape_args..ensure_bytesc3s|]}|VqdSr(r".0argr3literalr"r# qz*BaseCursor._escape_args..cs"i|]\}}||qSr"r")r5keyvalr7r"r# ssz+BaseCursor._escape_args..)r2r8r,r.r0dictitems)r!argsconnZretr")r2r3r8r# _escape_argscs    zBaseCursor._escape_argscCs|jstddS)Nzexecute() first)rrr&r"r"r#_check_executed}szBaseCursor._check_executedcCsL|jr||jdd=|}|}|dkr6dS|||dS)z`Advance to the next result set. Returns None if there are no more result sets. Nrr)rfetchallr_get_dbZ next_result_do_get_result_post_get_result)r!dbZnrr"r"r#r%s  zBaseCursor.nextsetcCs\||_}|dur$d|_|_n||_||_||_d|_| |_ d|_ dSNr) _get_resultrrrZdescribeZ field_flagsZ affected_rowsrrZ insert_idrr)r!rHresultr"r"r#rFs    zBaseCursor._do_get_resultcCsdSr(r"r&r"r"r#rGszBaseCursor._post_get_resultcGsdSz!Does nothing, required by DB API.Nr"r!r@r"r"r# setinputsizesszBaseCursor.setinputsizescGsdSrLr"rMr"r"r#setoutputsizesszBaseCursor.setoutputsizescCs|j}|durtd|S)Nz cursor closed)rr)r!Zconr"r"r#rEszBaseCursor._get_dbc Cs|r q|}t|tr(||j}|durt|trzi}|D],\}}t|trd||j}||||<qF|}nt t |j|}z ||}Wn0t y}zt t |WYd}~n d}~00t|ttfsJ||}|S)aExecute 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 N)r%rEr,rr-r2r>r?r8r.r/ TypeErrorrstrbytes bytearray_query) r!queryr@rHnargsr;itemmresr"r"r#executes*       " zBaseCursor.executec sjdd=|sdSt}|r|dd}|d}|dpJd}|ddkrd|d d kshJ||||jjSt 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"r(r)c3s|]}|VqdSr()rZr4rUr!r"r#r9r:z)BaseCursor.executemany..) rRE_INSERT_VALUESmatchgrouprstrip_do_execute_manymax_stmt_lengthrEr2sumr)r!rUr@rXZq_prefixZq_valuesZ q_postfixr"r`r# executemanys   zBaseCursor.executemanyc Cs|}|j}t|tr"||}t|tr6||}t|trJ||}t|} t|}||t||} | | 7} d} |D]^} ||| |} t| t| t|d|kr| | | |7} t|} n| d7} | | 7} q|| | | |7} | |_ | S)Nrr,) rErBr,rr-rSiternextlenrZr) r!prefixvaluesZpostfixr@rfr2rAescapeZsqlvZrowsr6r"r"r#res.         zBaseCursor._do_execute_manyr"cs|ttrj|rbddddfddt|D}|||ddfdd t t |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. s@_s_%d=%ssSET %sric3s$|]\}}||fVqdSr()r8)r5indexr6)rHfmtr"r#r9-sz&BaseCursor.callproc..s CALL %s(%s)csg|]}d|fqS)s@_%s_%dr")r5i)procnamer"r# 3sz'BaseCursor.callproc..) rEr,rr-r2join enumeraterTr%rangerl)r!rtr@qr")rHrrrtr#callproc s"       zBaseCursor.callproccCs<|}d|_|||||||_||_|jSr()rErrUrFrGrrr)r!ryrHr"r"r#rT8s  zBaseCursor._querycCs|js dS|j||jS)Nr")rZ fetch_row _fetch_type)r!sizer"r"r# _fetch_rowBszBaseCursor._fetch_rowcCs t|jdSr()rjfetchoner&r"r"r#__iter__GszBaseCursor.__iter__)N)r")r)%__name__ __module__ __qualname____doc__rf _exceptionsrrrr r r r r rrrrr$r'r)r+rBrCr%rFrGrNrOrErZrhrerzrTr}rr"r"r"r#rsB4   & , rc@sLeZdZdZddZddZddZdd d Zd d ZdddZ ddZ dS)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 |Sr()rEZ store_resultr&r"r"r#rJ\sz"CursorStoreResultMixIn._get_resultcCs|d|_d|_dSrI)r}r rr&r"r"r#rG_s z'CursorStoreResultMixIn._post_get_resultcCs8||jt|jkrdS|j|j}|jd|_|S)z]Fetches a single row from the cursor. None indicates that no more rows are available.Nr)rCrrlr r!rKr"r"r#r~cs   zCursorStoreResultMixIn.fetchoneNcCs>||j|p|j}|j|j|}t|t|j|_|SzFetch up to size rows from the cursor. Result set may be smaller than size. If size is not defined, cursor.arraysize is used.)rCrrr minrl)r!r|endrKr"r"r# fetchmanyms z CursorStoreResultMixIn.fetchmanycCs6||jr |j|jd}n|j}t|j|_|S)*Fetchs all available rows from the cursor.N)rCrr rlrr"r"r#rDvs  zCursorStoreResultMixIn.fetchallrelativecCsb||dkr|j|}n|dkr*|}ntdt||dksP|t|jkrXtd||_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.rZabsolutezunknown scroll mode %srz out of rangeN)rCrrreprrlr IndexError)r!valuemoderr"r"r#scrolls zCursorStoreResultMixIn.scrollcCs,||jr|j|jdp"|j}t|Sr()rCrr rjrr"r"r#rszCursorStoreResultMixIn.__iter__)N)r) rrrrrJrGr~rrDrrr"r"r"r#rVs  rc@sFeZdZdZddZddZdddZd d Zd d Zd dZ 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.cCs |Sr()rEZ use_resultr&r"r"r#rJsz CursorUseResultMixIn._get_resultcCs.||d}|sdS|jd|_|dS)z%Fetches a single row from the cursor.rNr)rCr}rr!rr"r"r#r~s   zCursorUseResultMixIn.fetchoneNcCs,|||p|j}|jt||_|Sr)rCr}rrrl)r!r|rr"r"r#rszCursorUseResultMixIn.fetchmanycCs&||d}|jt||_|S)rr)rCr}rrlrr"r"r#rDs zCursorUseResultMixIn.fetchallcCs|Sr(r"r&r"r"r#rszCursorUseResultMixIn.__iter__cCs|}|durt|Sr()r~ StopIteration)r!rowr"r"r#rkszCursorUseResultMixIn.next)N) rrrrrJr~rrDrrk__next__r"r"r"r#rs rc@seZdZdZdZdS)CursorTupleRowsMixInzwThis is a MixIn class that causes all rows to be returned as tuples, which is the standard form required by DB API.rNrrrrr{r"r"r"r#rsrc@seZdZdZdZdS)CursorDictRowsMixInznThis is a MixIn class that causes all rows to be returned as dictionaries. This is a non-standard feature.rNrr"r"r"r#rsrc@seZdZdZdS)CursorzjThis is the standard Cursor class that returns rows as tuples and stores the result set in the client.Nrrrrr"r"r"r#rsrc@seZdZdZdS) DictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the client.Nrr"r"r"r#rsrc@seZdZdZdS)SSCursorz_This is a Cursor class that returns rows as tuples and stores the result set in the server.Nrr"r"r"r#rsrc@seZdZdZdS) SSDictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the server.Nrr"r"r"r#rsr)#rZ __future__rr functoolsrresyscompatrrrrr r r r r rrrcompile IGNORECASEDOTALLraobjectrrrrrrrrrr"r"r"r#s:  0  <B/