o
    `^h:                     @   sl   d dl Z d dlmZmZ d dlmZmZ ddlmZ dd Z	G dd	 d	eZ
G d
d dZG dd dZdS )    N)MappingSequence)
expressionsqltypes   )excc                    s"   t | |||}| I d H  |S N)ResultProxy_prepare)
connectioncursordialect
result_mapresult_proxy r   P/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/aiomysql/sa/result.pycreate_result_proxy   s   r   c                   @   sd   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dZ
dd Zdd Zdd Zdd ZdS )RowProxy_result_proxy_row_processors_keymapc                 C   s   || _ || _|| _|| _dS )z8RowProxy objects are constructed by ResultProxy objects.Nr   )selfr   row
processorskeymapr   r   r   __init__   s   
zRowProxy.__init__c                 C   s   t | jjS r   )iterr   keysr   r   r   r   __iter__      zRowProxy.__iter__c                 C   s
   t | jS r   )lenr   r    r   r   r   __len__       
zRowProxy.__len__c                 C   sp   z
| j | \}}}W n ty   | j|\}}}Y nw |d u r(td| |d ur3|| j| S | j| S )NzVAmbiguous column name '%s' in result set! try 'use_labels' option on select statement.)r   KeyErrorr   _key_fallbackr   InvalidRequestErrorr   )r   key	processorobjindexr   r   r   __getitem__#   s   
zRowProxy.__getitem__c              
   C   s2   z| | W S  t y } zt|jd d }~ww )Nr   )r&   AttributeErrorargs)r   nameer   r   r   __getattr__>   s   
zRowProxy.__getattr__c                 C   s   | j | j|S r   )r   _has_keyr   )r   r)   r   r   r   __contains__D   s   zRowProxy.__contains__Nc                 C   s4   t |tr|  | kS t |tr|  |kS tS r   )
isinstancer   as_tupler   NotImplementedr   otherr   r   r   __eq__I   s
   

zRowProxy.__eq__c                 C   s
   | |k S r   r   r8   r   r   r   __ne__Q   r%   zRowProxy.__ne__c                    s   t  fdd D S )Nc                 3   s    | ]} | V  qd S r   r   ).0kr    r   r   	<genexpr>U   s    z$RowProxy.as_tuple.<locals>.<genexpr>)tupler    r   r    r   r6   T   s   zRowProxy.as_tuplec                 C   s   t |  S r   )reprr6   r    r   r   r   __repr__W   r"   zRowProxy.__repr__)__name__
__module____qualname__	__slots__r   r!   r$   r-   r2   r4   __hash__r:   r;   r6   rA   r   r   r   r   r      s    r   c                   @   s*   e Zd ZdZdd Zd
ddZdd Zd	S )ResultMetaDatazRHandle cursor.description, applying additional info from an execution
    context.c              
   C   s$  g  | _ }i }|jrdd |jD }i  | _}g | _|j}t|di }|js*J di }|jr3J dt|D ]S\}	}
|
d }|
d }|d |	||	|t
j}}}|||}|| |||	f}
|
||	< |||
|
uryd |d f ||< }
| j| |r|D ]}|
||< qq7|| d S )Nc                 S   s   i | ]	}|d  |d qS )r      r   )r<   elemr   r   r   
<dictcomp>e   s    z+ResultMetaData.__init__.<locals>.<dictcomp>dbapi_type_mapz4Doesn't support case insensitive database connectionz#psycopg in py3k should not use thisr   r   )r   _result_mapr   r   r   getattrcase_sensitivedescription_encoding	enumerategetr   NULLTYPE_cached_result_processorappend
setdefaultupdate)r   r   metadatar   r   r   r   typemapprimary_keymapireccolnamecoltyper0   r+   type_r*   or   r   r   r   _   sN   





zResultMetaData.__init__Tc                 C   s   | j }d }t|tr||}n=t|tjrM|jr$|j|v r$||j }nt|dr3|j|v r3||j }|d urM|d d urM|d D ]	}|	|rJ nqAd }|d u r_|r]t
dt| d S |||< |S )Nr0   r   z.Could not locate column in row for column '%s')r   r5   strrQ   r   ColumnElement_labelhasattrr0   _compare_name_for_resultr   NoSuchColumnError_string_or_unprintable)r   r)   raiseerrmapresultr+   r   r   r   r'      s4   


zResultMetaData._key_fallbackc                 C   s   || j v rdS | |dd uS )NTF)r   r'   )r   r   r)   r   r   r   r3      s   
zResultMetaData._has_keyN)T)rB   rC   rD   __doc__r   r'   r3   r   r   r   r   rG   [   s
    
N%rG   c                   @   s   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	edd Z
edd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zd)dd Zd!d" Zd#d$ Zd%d& Zd'd( ZdS )*r	   ac  Wraps a DB-API cursor object to provide easier access to row columns.

    Individual columns may be accessed by their integer position,
    case-insensitive column name, or by sqlalchemy schema.Column
    object. e.g.:

      row = fetchone()

      col1 = row[0]    # access via integer position

      col2 = row['col2']   # access via name

      col3 = row[mytable.c.mycol] # access via Column object.

    ResultProxy also handles post-processing of result column
    data using sqlalchemy TypeEngine objects, which are referenced from
    the originating SQL statement that produced this result set.
    c                 C   s2   || _ d| _|| _|| _|j| _|j| _|| _d S )NF)	_dialect_closed_cursor_connectionrowcount	_rowcount	lastrowid
_lastrowidrL   )r   r   r   r   r   r   r   r   r      s   
zResultProxy.__init__c                    sh   | j jj| j  jd ur%t|  j| _ fdd}t| || _	d S d | _| 
 I d H  d | _	d S )Nc                    s       d S r   )create_taskclose)wrr   loopr   r   callback   s   z&ResultProxy._prepare.<locals>.callback)rn   r   rw   rm   descriptionrG   	_metadataweakrefref_weakrt   )r   rx   r   rv   r   r
      s   


zResultProxy._preparec                 C      | j S )zSQLAlchemy dialect.)rk   r    r   r   r   r     s   zResultProxy.dialectc                 C   r~   r   )rm   r    r   r   r   r   	     zResultProxy.cursorc                 C   s   | j r	t| j jS dS )z/Return the current set of string keys for rows.r   )rz   r?   r   r    r   r   r   r     s   zResultProxy.keysc                 C   r~   )aE  Return the 'rowcount' for this result.

        The 'rowcount' reports the number of rows *matched*
        by the WHERE criterion of an UPDATE or DELETE statement.

        .. note::

           Notes regarding .rowcount:


           * This attribute returns the number of rows *matched*,
             which is not necessarily the same as the number of rows
             that were actually *modified* - an UPDATE statement, for example,
             may have no net change on a given row if the SET values
             given are the same as those present in the row already.
             Such a row would be matched but not modified.

           * .rowcount is *only* useful in conjunction
             with an UPDATE or DELETE statement.  Contrary to what the Python
             DBAPI says, it does *not* return the
             number of rows available from the results of a SELECT statement
             as DBAPIs cannot support this functionality when rows are
             unbuffered.

           * Statements that use RETURNING may not return a correct
             rowcount.
        )rp   r    r   r   r   ro     s   zResultProxy.rowcountc                 C   r~   )zReturns the 'lastrowid' accessor on the DBAPI cursor.

        This is a DBAPI specific method and is only functional
        for those backends which support it, for statements
        where it is appropriate.
        )rr   r    r   r   r   rq   3  s   zResultProxy.lastrowidc                 C   s
   | j duS )zTrue if this ResultProxy returns rows.

        I.e. if it is legal to call the methods .fetchone(),
        .fetchmany() and .fetchall()`.
        N)rz   r    r   r   r   returns_rows=  s   
zResultProxy.returns_rowsc                 C   r~   r   )rl   r    r   r   r   closedF  r   zResultProxy.closedc                    s2   | j sd| _ | j I dH  d| _d| _dS dS )ae  Close this ResultProxy.

        Closes the underlying DBAPI cursor corresponding to the execution.

        Note that any data cached within this ResultProxy is still available.
        For some types of results, this may include buffered rows.

        If this ResultProxy was generated from an implicit execution,
        the underlying Connection will also be closed (returns the
        underlying DBAPI connection to the connection pool.)

        This method is called automatically when:

        * all result rows are exhausted using the fetchXXX() methods.
        * cursor.description is None.
        TN)rl   rm   rt   r}   r    r   r   r   rt   J  s   
zResultProxy.closec                 C   s   | j d u r
tdtd)NzJThis result object does not return rows. It has been closed automatically.zThis result object is closed.)rz   r   ResourceClosedErrorr    r   r   r   _non_resultk  s
   

zResultProxy._non_resultc                    s.   t | jj j fdd|D S )Nc                    s   g | ]	}| qS r   r   )r<   r   r   rW   process_rowr   r   r   
<listcomp>x  s    z-ResultProxy._process_rows.<locals>.<listcomp>)r   rz   r   r   )r   rowsr   r   r   _process_rowss  s   zResultProxy._process_rowsc                    sP   z
| j  I dH }W n ty   |   Y dS w | |}|  I dH  |S )z3Fetch all rows, just like DB-API cursor.fetchall().N)rm   fetchallr.   r   r   rt   )r   r   retr   r   r   r   {  s   
zResultProxy.fetchallc                    s^   z
| j  I dH }W n ty   |   Y dS w |dur&| |gd S |  I dH  dS )zFetch one row, just like DB-API cursor.fetchone().

        If a row is present, the cursor remains open after this is called.
        Else the cursor is automatically closed and None is returned.
        Nr   )rm   fetchoner.   r   r   rt   r   r   r   r   r   r     s   zResultProxy.fetchoneNc                    sx   z|du r| j  I dH }n	| j |I dH }W n ty'   |   Y dS w | |}t|dkr:|  I dH  |S )zFetch many rows, just like DB-API
        cursor.fetchmany(size=cursor.arraysize).

        If rows are present, the cursor remains open after this is called.
        Else the cursor is automatically closed and an empty list is returned.
        Nr   )rm   	fetchmanyr.   r   r   r#   rt   )r   sizer   r   r   r   r   r     s   
zResultProxy.fetchmanyc                    sD   | j du r
|   z|  I dH W |  I dH  S |  I dH  w )zwFetch the first row and then close the result set unconditionally.

        Returns None if no row is present.
        N)rz   r   r   rt   r    r   r   r   first  s   
 zResultProxy.firstc                    s$   |   I dH }|dur|d S dS )zwFetch the first column of the first row, and close the result set.

        Returns None if no row is present.
        Nr   )r   r   r   r   r   scalar  s
   zResultProxy.scalarc                 C   s   | S r   r   r    r   r   r   	__aiter__  s   zResultProxy.__aiter__c                    s    |   I d H }|d ur|S tr   )r   StopAsyncIteration)r   datar   r   r   	__anext__  s
   zResultProxy.__anext__r   )rB   rC   rD   rj   r   r
   propertyr   r   r   ro   rq   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r	      s6    	



	

!
r	   )r{   collections.abcr   r   sqlalchemy.sqlr   r    r   r   r   rG   r	   r   r   r   r   <module>   s   I~