
    	ixT                         d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlZdZdZdZdZd ZdZdZd	ZdZdZdZd
ZdZdZdZdZdZ G d de j        j                  ZdS )    N)Number)CURSORSTATUS
CAPABILITY	INDICATOR)Sequence         r                  c                   J    e Zd ZdZd Z fdZd Zd Zd Zd%de	d	e
fd
Zd&de	f fdZ fdZd'de	d	e
fdZd Z fdZ fdZd Zd(def fdZ fdZd Zd)defdZdefdZdefdZd Zd  Ze fd!            Zed"             Zed#             Zed$             Z  xZ!S )*Cursorz0
    MariaDB Connector/Python Cursor Object
    c                     | j         | j        j        k    rt          j        d          | j        rt          j        d          | j                                         d S )NzGCursor cannot be used anymore (the connection aborted and reconnected).z=Cursor cannot be used anymore (it was already closed before).)
_thread_id
connection	thread_idmariadbProgrammingError_closed_connection_check_closedselfs    G/srv/django_bis/venv311/lib/python3.11/site-packages/mariadb/cursors.pycheck_closedzCursor.check_closed8   s^    ?do777*+uvvv< 	l*+jkkk&&(((((    c                    d| _         d| _        d| _        || _        t          | _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        |j        | _        d| _        |st%          j        d          |r|                    dd          }|rt*          | _        n$|                    dd          }|rt,          | _        |                    dd          }|| _        |                    d	d          | _        |                    d
d          | _
        |                    dd          | _         t1                      j        |fi | dS )z 
        initialization
        FNr   Tz!Invalid or no connection providednamed_tuple
dictionarybufferedpreparedbinarycursor_type)_bulk_dictionary_named_tupler   RESULT_TUPLE_resulttype_description_transformed_statement	_prepared
_prev_stmt_force_binary	_rowcountr&   
_parseinfo_datar   r   r   r   r   popRESULT_NAMEDTUPLERESULT_DICTIONARY_cursor_typesuper__init__)r   r   kwargsrtyper&   	__class__s        r    r<   zCursor.__init__?   so    
 !%' &*#!
$. 	P*+NOOO  	=JJ}e44E 9#4  

<77 9'8D$zz*d33H$DM#ZZ
E::DN!'He!<!<D &

=! < <D 	..v.....r"   c                    | j                             d          }d}| j        rt          dt	          | j                            D ]}d}| j        t          k    r| j        | j        |                  }n| j        |         }|d}nt          |t          j                  r%|t          j        k    rd}|t          j        k    rd}nt          |t                    r|                                }nt          |t           t"          f          r2d| j                            |                    d                    z  }nad| j                            |                                          z  }t	          |                    d	                    t	          |          z
  }| j        |         |z   }|d|         |                                                    d          z   ||d
z   d         z   }|t	          |          d
z
  |z   z  }|S )z
        Internal use only.

        When running in text protocol, this method will replace placeholders
        by supplied values.

        For values which aren't numbers, strings or bytes string representation
        will be used.
        utf8r   NNULLDEFAULTz'%s'latin1)encodingzutf-8r	   )	statementencode
_paramlistrangelen_paramstylePARAMSTYLE_PYFORMATr6   _keys
isinstancer   MrdbIndicatorrB   rC   r   __str__bytes	bytearrayr   escape_stringdecode)r   new_stmtreplace_diffiextra_bytesvalreplaceofss           r    _substitute_parameterszCursor._substitute_parametersi   s    >((00? 	?1c$/2233 ? ?#':::*TZ]3CC*Q-C;$GG!#y'>?? -).00&,G)"333&/G#C00 
-"%++--%cE9+=>> -&,t/L/L #

H
 = =0? 0? '?GG '-t/L/L #0/ 0/ '/G*-gnnW.E.E*F*F #G+-Koa(<7#DSD>GOO,=,=,D,DV,L,LLSUVV$%Gq 0; >>r"   c                 P   t          |t                    r6t          |          dv r%t          j        dt          |           d          t          |t
          j                  r@|                                dv r*t          j        d|                                 d          dS )a)  
        Internal use only

        Checks for unsupported parameters.
        The following parameters are not supported by MariaDB/MySQL (but also
        not part of the SQL Standard)

        - float("nan" | "inf" | "-inf")
        - Decimal("NaN" | "sNaN" | "Infinity" | "-Infinity")
        )naninfz-infzFloat value 'z' is not supported.)NaNsNaNInfinityz	-InfinityzDecimal value 'N)rN   floatreprr   NotSupportedErrordecimalDecimalrP   )r   rY   s     r    _check_decimal_parameterzCursor._check_decimal_parameter   s     c5!! 	bd3ii3I&I&I+,ZDII,Z,Z,Z[[[W_-- 	b#++--Ck2k2k+,`ckkmm,`,`,`aaatr"   c                    d}| j         t          t          fv r<t          | j        t
          t          f          st          j        d          | j        }| j         t          k    rt          | j        t                    st          j        d          t          dt          | j                            D ]8}| j        |         | j        vr"t          j        d| j        |         z            9| j                                        }n| j        r| j        st          | j                  dk    s*t          | j                  t          | j                  k    r?t          j        dt          | j                  dt          | j                  d          t          |          rI|D ]H}t          |t                     st          |t"          j                  r|                     |           Gd S d S )	N z#Data argument must be Tuple or Listz Data argument must be Dictionaryr   z#Dictionary doesn't contain key '%s'zstatement (z-) doesn't match the number of data elements (z).)rK   PARAMSTYLE_QMARKPARAMSTYLE_FORMATrN   r6   tuplelistr   r   rL   dictrI   rJ   rM   valuesrH   rc   rf   rg   rh   )r   rp   rW   rY   s       r    _check_execute_paramszCursor._check_execute_params   s    02CDDDdj5$-88 @. 0? @ @ @ZF222dj$// =. 0< = = =1c$*oo.. P P:a=
22!2 4?AEA4O P P P 3 Z&&((FF  HJH#&t#7#7!#;#;DJ3t#7#777.. #DO 4 4 4 4c$*ooooGH H H
 v;; 	7 7 7c5)) 7ZW_-M-M 711#666	7 	77 7r"   rj   spdatac                 6   | j         j        r| j         j        | _        |                                  |                                  d}|r)t          |          rdt          |          z  dd         }d|d|d}d| _        |                     ||           dS )	a  
        Executes a stored procedure sp. The data sequence must contain an
        entry for each parameter the procedure expects.

        Input/Output or Output parameters have to be retrieved by .fetch
        methods, the .sp_outparams attribute indicates if the result set
        contains output parameters.

        Arguments:
            - sp: Name of stored procedure.
            - data: Optional sequence containing data for placeholder
                    substitution.
         z?,Nr   zCALL ()r   )	r   auto_reconnectr   r   r!   _resetrJ   r4   execute)r   rr   rs   paramsrF   s        r    callproczCursor.callproc   s     ?) 	7!_6DO  	-CII 	-SYY&,F%'RR0	Y%%%%%r"   FrF   c                 P   |st          j        d          | j        |k    s	|r7| j        s0t	                                          |           || _        d| _        nd| _        | j        | _        | j	        t          j        k    rd| _        || _        |                                  dS )zW
        For internal use

        Parses SQL statement and checks parameters.
        zempty statementTFN)r   r   rF   r*   r;   _parser2   
_repreparer0   r:   r   	READ_ONLY_textr6   rq   )r   rF   rs   is_bulkr?   s       r    _parse_executezCursor._parse_execute   s      	>*+<=== >Y&&'&$*&GGNN9%%%'DO"DOO#DO&*n# 000DJ
""$$$$$r"   c                 l    |                                   t                                                      S )z
        Will make the cursor skip to the next available result set,
        discarding any remaining rows from the current set.
        )r!   r;   _nextsetr   r?   s    r    nextsetzCursor.nextset  s-     	ww!!!r"   Nc                    | j         j        r| j         j        | _        |                                  | j        s|                                  || j         _        d}d| _        ||| _	        | j
        r|                                  | j        r| j        r	| j        }d}|r|                     ||           d| _        |pd}t          |          r|| _        n)d| _        | j        t$          k    r| j        dk    rd| _        | j        rd| _        |r|                                 dk    rd| _        | j        r`| j        dk    r|                                 | _        n| j        | _        |                     | j                   |                                  n|| _        |                                  |                                  d| _        dS )a  
        Prepare and execute a SQL statement.

        Parameters may be provided as sequence or mapping and will be bound
        to variables in the operation. Variables are specified as question
        marks (paramstyle ='qmark'), however for compatibility reasons MariaDB
        Connector/Python also supports the 'format' and 'pyformat' paramstyles
        with the restriction, that different paramstyles can't be mixed within
        a statement.

        A reference to the operation will be retained by the cursor.
        If the cursor was created with attribute prepared =True the statement
        string for following execute operations will be ignored.
        This is most effective for algorithms where the same operation is used,
        but different parameters are bound to it (many times).

        By default execute() method generates an buffered result unless the
        optional parameter buffered was set to False or the cursor was
        generated as an unbuffered cursor.
        Tr   NFrj   )r   rx   r   r   r!   r1   ry   _last_executed_statementr4   r&   field_count_clear_resultrF   r   r/   rJ   r6   _commandSQL_CALLr:   r   r3   _check_text_types
paramcountr\   r0   _execute_text_readresponse_execute_binary_initresultr*   )r   rF   rs   r&   do_parses        r    rz   zCursor.execute  s   * ?) 	7!_6DO~ 	KKMMM3<0 $DM  	!    > 	dn 	IH  	3	D222  zrt99 	"DJJDJ }((T->!-C-C!
 	DJ  	D**,,44DJ: 	# !##.2.I.I.K.K++.2n+t:;;;    DJ  """


r"   c                 \   | j         j        r| j         j        | _        |                                  |                                  |rt          |          st          j        d          || j         _	        | j
        r|                                  | j         j        t          j        dz	  z  s0d}|D ]"}|                     ||           || j        z  }#|| _        dS |                     ||d         d           || _        d| _        d| _        |                                  d| _        dS )	a!  
        Prepare a database operation (INSERT,UPDATE,REPLACE or DELETE
        statement) and execute it against all parameter found in sequence.

        Exactly behaves like .execute() but accepts a list of tuples, where
        each tuple represents data of a row within a table.
        .executemany() only supports DML (insert, update, delete) statements.

        If the SQL statement contains a RETURNING clause, executemany()
        returns a result set containing the values for columns listed in the
        RETURNING clause.
        zNo data provided    r   T)r   Fr	   N)r   rx   r   r   r!   ry   rJ   r   r   r   r   r   extended_server_capabilitiesr   BULK_OPERATIONSrz   rowcountr4   r   r6   is_text_execute_bulkr*   )r   rF   
parameterscountrows        r    executemanyzCursor.executemanyg  s?    ?) 	7!_6DO 	?Z 	?*+=>>>3<0  	!    <+r13 	E! ' 'Y,,,&"DNNN 	:a=$GGG#DJ DLDN   DJJJr"   c                     | j         s|                                  | j        st          j        d          t                                                      S )zl
        Internal use only

        fetches row and converts values, if connection has a converter.
         Cursor doesn't have a result set)r&   r!   r   r   r   r;   fetchoner   s    r    
_fetch_rowzCursor._fetch_row  sW     } 	   	O*+MNNNww!!!r"   c                     | j         r| ` | j        j        s t                                                       d| _        dS )ap  
        Closes the cursor.

        If the cursor has pending or unread results, .close() will cancel them
        so that further operations using the same connection can be executed.

        The cursor will be unusable from this point forward; an Error
        (or subclass) exception will be raised if any operation is attempted
        with the cursor."
        TN)r6   r   r   r;   closer   s    r    r   zCursor.close  s=     : 	
& 	GGMMOOOr"   c                 r    | j         r| j        s|                                  |                                 }|S )a  
        Fetch the next row of a query result set, returning a single sequence,
        or None if no more data is available.

        An exception will be raised if the previous call to execute() didn't
        produce a result set or execute() wasn't called before.
        )r&   r   r!   r   )r   r   s     r    r   zCursor.fetchone  s=      	 $* 	 oo
r"   r   sizec                     | j         r| j        s|                                  |dk    r| j        }t	                                          |          S )a  
        Fetch the next set of rows of a query result, returning a sequence
        of sequences (e.g. a list of tuples). An empty sequence is returned
        when no more rows are available.

        The number of rows to fetch per call is specified by the parameter.
        If it is not given, the cursor's arraysize determines the number
        of rows to be fetched. The method should try to fetch as many rows
        as indicated by the size parameter.
        If this is not possible due to the specified number of rows not being
        available, fewer rows may be returned.

        An exception will be raised if the previous call to execute() didn't
        produce a result set or execute() wasn't called before.
        r   )r&   r   r!   	arraysizer;   	fetchrows)r   r   r?   s     r    	fetchmanyzCursor.fetchmany  sR       	 $* 	 199>Dww  &&&r"   c                     | j         r| j        s|                                  t                                          t
                    S )a  
        Fetch all remaining rows of a query result, returning them as a
        sequence of sequences (e.g. a list of tuples).

        An exception will be raised if the previous call to execute() didn't
        produce a result set or execute() wasn't called before.
        )r&   r   r!   r;   r   ROWS_EOFr   s    r    fetchallzCursor.fetchall  sB      	 $* 	 ww  ***r"   c                 ,    t          | j        d           S )N)iterr   r   s    r    __iter__zCursor.__iter__  s    DM4(((r"   relativevaluec                 P   | j         r| j        s|                                  | j        dk    rt	          j        d          | j         st	          j        d          |dk    r|dk    rt	          j        d          |dk    r|dk    rt	          j        d          |dk    r@| j        |z   dk     s| j        |z   | j        k    rt	          j        d          | j        |z   }n'|dk     s|| j        k    rt	          j        d          |}|                     |           || _	        d	S )
a  
        Scroll 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   r   zEThis method is available only for cursors with a buffered result set.absoluter   z)Invalid or unknown scroll mode specified.zInvalid position value 0.zPosition value is out of range.N)
r&   r   r!   r   r   r   	rownumberr   _seek
_rownumber)r   r   modenew_poss       r    scrollzCursor.scroll  sw     	 $* 	 q  *+MNNN} 	:* ,9 : : : :$*"4"4* ,= > > > A::$*,,*+FGGG:~%))~%55. 0B C C Cnu,GGqyyET]22. 0B C C CG

7!r"   c                     dS zO
        Required by PEP-249. Does nothing in MariaDB Connector/Python
        Nrj   r   r   s     r    setinputsizeszCursor.setinputsizes  	    
 	r"   c                     dS r   rj   r   s     r    setoutputsizezCursor.setoutputsize#  r   r"   c                     | S )zReturns a copy of the cursor.rj   r   s    r    	__enter__zCursor.__enter__*  s	     r"   c                 .    |                                   dS )zCloses cursor.N)r   )r   exc_typeexc_valexc_tbs       r    __exit__zCursor.__exit__/  s    

r"   c                     | j         s,	 |                                  n# t          j        $ r Y dS w xY w| j        dk    r| j        S t                      j        S )a  
        This read-only attribute specifies the number of rows that the last        execute*() produced (for DQL statements like SELECT) or affected
        (for DML statements like UPDATE or INSERT).
        The return value is -1 in case no .execute*() has been performed
        on the cursor or the rowcount of the last operation  cannot be
        determined by the interface.
        r   r   )r&   r!   r   r   r4   r;   r   r   s    r    r   zCursor.rowcount3  sr     } 	!!####+   rr >A>!wws    22c                 v    |                                   t          | j        j        t          j        z            S )z
        Indicates if the current result set contains in out or out parameter
        from a previous executed stored procedure
        )r!   boolr   server_statusr   PS_OUT_PARAMSr   s    r    sp_outparamszCursor.sp_outparamsK  s1     	DO1F4HHIIIr"   c                 L    |                                   | j        }|dk    r|S dS )a  
        Returns the ID generated by a query on a table with a column having
        the AUTO_INCREMENT attribute or the value for the last usage of
        LAST_INSERT_ID().

        If the last query wasn't an INSERT or UPDATE
        statement or if the modified table does not have a column with the
        AUTO_INCREMENT attribute and LAST_INSERT_ID was not used, the returned
        value will be None
        r   N)r!   	insert_id)r   ids     r    	lastrowidzCursor.lastrowidU  s0     	^66Itr"   c                     | j         S )z
        Read-Only attribute which returns the reference to the connection
        object on which the cursor was created.
        )r   r   s    r    r   zCursor.connectionh  s     r"   )rj   )rj   F)rj   Nr   )r   )"__name__
__module____qualname____doc__r!   r<   r\   rh   rq   strr   r|   r   r   rz   r   r   r   r   intr   r   r   r   r   r   r   r   propertyr   r   r   r   __classcell__)r?   s   @r    r   r   3   s        ) ) )(/ (/ (/ (/ (/T, , ,\  ( 7  7  7D& &3 &h & & & &8% % % % % % % %6" " " " "X X XH X X X Xt/ / /b" " " " "    *  ' 'c ' ' ' ' ' '0+ + + + +) ) )(" ("C (" (" (" ("T#    #      
           X . J J XJ   X$     X         r"   r   ) r   datetimenumbersr   mariadb.constantsr   r   r   r   typingr   rf   rk   rl   rL   ROWS_ALLr-   r8   r9   SQL_NONE
SQL_INSERT
SQL_UPDATESQL_REPLACE
SQL_DELETEr   SQL_DO
SQL_SELECT	SQL_OTHERr   _mariadbcursorr   rj   r"   r    <module>r      s
  (         C C C C C C C C C C C C             


	

	|  |  |  |  | W$ |  |  |  |  | r"   