o ct]?@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__;s zBaseCursor.__init__cCsbz)|jdurWd|_d|_dS|r! |sWd|_d|_dSWd|_d|_dSd|_d|_w)z6Close the cursor. No further queries will be possible.N)rrnextsetr!r"r"r#closeQs     zBaseCursor.closecC|SNr"r&r"r"r# __enter__\zBaseCursor.__enter__cGs~|dSr))r')r!exc_infor"r"r#__exit___s zBaseCursor.__exit__cs|j|jfddt|ttfr$tfdd|D}d|St|tr9fdd|D}d|S|}d|S)NcsHt|tr |St|trtt|St|tr"tt|S|Sr)) isinstancerencodetuplemaplist)x)encoding ensure_bytesr"r#r5gs    z-BaseCursor._escape_args..ensure_bytesc3s|] }|VqdSr)r".0argr5literalr"r# qz*BaseCursor._escape_args..cs"i|] \}}||qSr"r")r7keyvalr9r"r# ssz+BaseCursor._escape_args..)r4r:r.r0r2dictitems)r!argsconnretr")r4r5r:r# _escape_argscs     zBaseCursor._escape_argscCs|jstddS)Nzexecute() first)rrr&r"r"r#_check_executed}szBaseCursor._check_executedcCsL|jr||jdd=|}|}|dkrdS|||dS)z`Advance to the next result set. Returns None if there are no more result sets. Nrr)rfetchallr_get_db next_result_do_get_result_post_get_result)r!dbnrr"r"r#r%s  zBaseCursor.nextsetcCs\||_}|durd|_|_n ||_||_||_d|_| |_ d|_ dSNr) _get_resultrrrdescribe field_flags affected_rowsrr insert_idrr)r!rLresultr"r"r#rJs     zBaseCursor._do_get_resultcCsdSr)r"r&r"r"r#rKr+zBaseCursor._post_get_resultcGdSz!Does nothing, required by DB API.Nr"r!rBr"r"r# setinputsizeszBaseCursor.setinputsizescGrUrVr"rWr"r"r#setoutputsizesrYzBaseCursor.setoutputsizescCs|j}|dur td|S)Nz cursor closed)rr)r!conr"r"r#rHszBaseCursor._get_dbc Cs|r |s|}t|tr||j}|durbt|trAi}|D]\}}t|tr6||j}||||<q'|}nt t |j|}z||}Wnt ya}zt t |d}~wwt|ttfskJ||}|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%rHr.rr/r4r@rAr:r0r1 TypeErrorrstrbytes bytearray_query) r!queryrBrLnargsr=itemmresr"r"r#executes0         zBaseCursor.executec sjdd=|s dSt}|rB|dd}|d}|dp%d}|ddkr2|d d ks4J||||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))rfr6rar!r"r#r;r<z)BaseCursor.executemany..) rRE_INSERT_VALUESmatchgrouprstrip_do_execute_manymax_stmt_lengthrHr4sumr)r!rarBrdq_prefixq_values q_postfixr"rlr# executemanys   zBaseCursor.executemanyc Cs|}|j}t|tr||}t|tr||}t|tr%||}t|} t|}||t||} | | 7} d} |D]/} ||| |} t| t| t|d|kre| | | |7} t|} n| d7} | | 7} q>| | | |7} | |_ | S)Nrr,) rHrEr.rr/r_iternextlenrfr) r!prefixvaluespostfixrBrrr4rCescapesqlvrowsr8r"r"r#rqs.         zBaseCursor._do_execute_manyr"cs|ttrj|r1ddddfddt|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 %srxc3s&|]\}}||fVqdSr))r:)r7indexr8)rLfmtr"r#r;-sz&BaseCursor.callproc..s CALL %s(%s)csg|]}d|fqS)s@_%s_%dr")r7i)procnamer"r# 3sz'BaseCursor.callproc..) rHr.rr/r4join enumerater`r%ranger{)r!rrBqr")rLrrr#callproc s"      zBaseCursor.callproccCs<|}d|_|||||||_||_|jSr))rHrrarJrKrrr)r!rrLr"r"r#r`8s  zBaseCursor._querycCs|jsdS|j||jS)Nr")r fetch_row _fetch_type)r!sizer"r"r# _fetch_rowBszBaseCursor._fetch_rowcCs t|jdSr))ryfetchoner&r"r"r#__iter__G zBaseCursor.__iter__r))r")r)%__name__ __module__ __qualname____doc__rr _exceptionsrrrr r r r r rrrrr$r'r*r-rErFr%rJrKrXrZrHrfrwrqrr`rrr"r"r"r#rsD4   & , 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.cC |Sr))rH store_resultr&r"r"r#rO\rz"CursorStoreResultMixIn._get_resultcCs|d|_d|_dSrN)rr rr&r"r"r#rK_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)rFrr{r r!rTr"r"r#rcs   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.)rFrrr minr{)r!rendrTr"r"r# fetchmanyms z CursorStoreResultMixIn.fetchmanycCs6||jr|j|jd}n|j}t|j|_|S)*Fetchs all available rows from the cursor.N)rFrr r{rr"r"r#rGvs  zCursorStoreResultMixIn.fetchallrelativecCsb||dkr|j|}n|dkr|}ntdt||dks(|t|jkr,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.rabsolutezunknown scroll mode %srz out of rangeN)rFrrreprr{r IndexError)r!valuemoderr"r"r#scrolls  zCursorStoreResultMixIn.scrollcCs,||jr|j|jdp|j}t|Sr))rFrr ryrr"r"r#rszCursorStoreResultMixIn.__iter__r))r) rrrrrOrKrrrGrrr"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.cCrr))rH use_resultr&r"r"r#rOrz CursorUseResultMixIn._get_resultcCs.||d}|s dS|jd|_|dS)z%Fetches a single row from the cursor.rNr)rFrrr!rr"r"r#rs   zCursorUseResultMixIn.fetchoneNcCs,|||p |j}|jt||_|Sr)rFrrrr{)r!rrr"r"r#rszCursorUseResultMixIn.fetchmanycCs&||d}|jt||_|S)rr)rFrrr{rr"r"r#rGs zCursorUseResultMixIn.fetchallcCr(r)r"r&r"r"r#rr+zCursorUseResultMixIn.__iter__cCs|}|dur t|Sr))r StopIteration)r!rowr"r"r#rzszCursorUseResultMixIn.nextr)) rrrrrOrrrGrrz__next__r"r"r"r#rs rc@eZdZdZdZdS)CursorTupleRowsMixInzwThis is a MixIn class that causes all rows to be returned as tuples, which is the standard form required by DB API.rNrrrrrr"r"r"r#rrc@r)CursorDictRowsMixInznThis is a MixIn class that causes all rows to be returned as dictionaries. This is a non-standard feature.rNrr"r"r"r#rrrc@eZdZdZdS)CursorzjThis is the standard Cursor class that returns rows as tuples and stores the result set in the client.Nrrrrr"r"r"r#rrc@r) DictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the client.Nrr"r"r"r#rrrc@r)SSCursorz_This is a Cursor class that returns rows as tuples and stores the result set in the server.Nrr"r"r"r#rrrc@r) SSDictCursorzeThis is a Cursor class that returns rows as dictionaries and stores the result set in the server.Nrr"r"r"r#rrr)#r __future__rr functoolsrresyscompatrrrrr r r r r rrrcompile IGNORECASEDOTALLrmobjectrrrrrrrrrr"r"r"r#s<  0  <B/