U
    ovg                     @  s   d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G d	d
 d
Z
G dd de
ZG dd deZG dd deZdS )z?generic asyncio-adapted versions of DBAPI connection and cursor    )annotationsN   )AdaptedConnection)asyncio)await_fallback)
await_onlyc                   @  s   e Zd ZdZdZdd Zdd Zedd Zed	d
 Z	edd Z
e
jdd Z
edd Zdd Zd'ddZdd Z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S ))AsyncAdapt_dbapi_cursorF)_adapt_connection_connectionawait__cursor_rowsc                 C  s@   || _ |j| _|j| _| j }| || _| js<t | _	d S N)
r	   r
   r   cursor_aenter_cursorr   server_sidecollectionsdequer   selfZadapt_connectionr    r   A/tmp/pip-unpacked-wheel-uqd9otp_/sqlalchemy/connectors/asyncio.py__init__   s    
z AsyncAdapt_dbapi_cursor.__init__c                 C  s   |  | S r   )r   
__aenter__)r   r   r   r   r   r   *   s    z&AsyncAdapt_dbapi_cursor._aenter_cursorc                 C  s   | j jS r   )r   descriptionr   r   r   r   r   -   s    z#AsyncAdapt_dbapi_cursor.descriptionc                 C  s   | j jS r   )r   rowcountr   r   r   r   r   1   s    z AsyncAdapt_dbapi_cursor.rowcountc                 C  s   | j jS r   r   	arraysizer   r   r   r   r   5   s    z!AsyncAdapt_dbapi_cursor.arraysizec                 C  s   || j _d S r   r   r   valuer   r   r   r   9   s    c                 C  s   | j jS r   )r   	lastrowidr   r   r   r   r!   =   s    z!AsyncAdapt_dbapi_cursor.lastrowidc                 C  s   | j   d S r   )r   clearr   r   r   r   closeA   s    zAsyncAdapt_dbapi_cursor.closeNc                 C  s   |  | ||S r   )r   _execute_async)r   	operation
parametersr   r   r   executeF   s    zAsyncAdapt_dbapi_cursor.executec                 C  s   |  | ||S r   )r   _executemany_asyncr   r%   Zseq_of_parametersr   r   r   executemanyI   s    
z#AsyncAdapt_dbapi_cursor.executemanyc                   sv   | j j4 I d H V | j||p dI d H }| jjrP| jsPt| j I d H | _	|W  5 Q I d H R  S Q I d H R X d S )Nr   )
r	   _execute_mutexr   r'   r   r   r   r   fetchallr   )r   r%   r&   resultr   r   r   r$   N   s
    z&AsyncAdapt_dbapi_cursor._execute_asyncc                   sH   | j j4 I d H ( | j||I d H W  5 Q I d H R  S Q I d H R X d S r   )r	   r+   r   r*   r)   r   r   r   r(   V   s    z*AsyncAdapt_dbapi_cursor._executemany_asyncc                 C  s:   |  | j  | jjr6| js6t|  | j | _d S r   )	r   r   nextsetr   r   r   r   r,   r   r   r   r   r   r.   Z   s
    zAsyncAdapt_dbapi_cursor.nextsetc                 G  s   |  | jj| S r   )r   r   setinputsizes)r   Z
inputsizesr   r   r   r/   a   s    z%AsyncAdapt_dbapi_cursor.setinputsizesc                 c  s   | j r| j  V  q d S r   r   popleftr   r   r   r   __iter__h   s    z AsyncAdapt_dbapi_cursor.__iter__c                 C  s   | j r| j  S d S d S r   r0   r   r   r   r   fetchonel   s    
z AsyncAdapt_dbapi_cursor.fetchonec                   s4   |d kr| j }| j  fddtt|t D S )Nc                   s   g | ]}   qS r   )r1   ).0_rrr   r   
<listcomp>v   s     z5AsyncAdapt_dbapi_cursor.fetchmany.<locals>.<listcomp>)r   r   rangeminlenr   sizer   r6   r   	fetchmanyr   s    z!AsyncAdapt_dbapi_cursor.fetchmanyc                 C  s   t | j}| j  |S r   )listr   r"   )r   retvalr   r   r   r,   x   s    

z AsyncAdapt_dbapi_cursor.fetchall)N)N)__name__
__module____qualname__r   	__slots__r   r   propertyr   r   r   setterr!   r#   r'   r*   r$   r(   r.   r/   r2   r3   r>   r,   r   r   r   r   r      s2   






r   c                   @  sF   e Zd ZdZdZdd Zdd Zdd Zdd
dZdd Z	dd Z
d	S )AsyncAdapt_dbapi_ss_cursorr   Tc                 C  s4   || _ |j| _|j| _| j }| | | _d S r   )r	   r
   r   r   r   r   r   r   r   r   r      s
    
z#AsyncAdapt_dbapi_ss_cursor.__init__c                 C  s$   | j d k	r | | j   d | _ d S r   )r   r   r#   r   r   r   r   r#      s    
z AsyncAdapt_dbapi_ss_cursor.closec                 C  s   |  | j S r   )r   r   r3   r   r   r   r   r3      s    z#AsyncAdapt_dbapi_ss_cursor.fetchoneNc                 C  s   |  | jj|dS )N)r=   )r   r   r>   r<   r   r   r   r>      s    z$AsyncAdapt_dbapi_ss_cursor.fetchmanyc                 C  s   |  | j S r   )r   r   r,   r   r   r   r   r,      s    z#AsyncAdapt_dbapi_ss_cursor.fetchallc                 c  s>   | j  }z| | V  W q
 tk
r6   Y q:Y q
X q
d S r   )r   	__aiter__r   	__anext__StopAsyncIteration)r   iteratorr   r   r   r2      s
    
z#AsyncAdapt_dbapi_ss_cursor.__iter__)N)rA   rB   rC   rD   r   r   r#   r3   r>   r,   r2   r   r   r   r   rG   ~   s   	
rG   c                   @  s|   e Zd ZeZeZeeZ	dZ
dd Zdd Zdd Zdd	 Zed
d Zejdd ZdddZdd Zdd Zdd ZdS )AsyncAdapt_dbapi_connection)dbapir+   c                 C  s   || _ || _t | _d S r   )rM   r
   r   Lockr+   )r   rM   
connectionr   r   r   r      s    z$AsyncAdapt_dbapi_connection.__init__c                 C  s   |  | j|S r   )r   r
   ping)r   Z	reconnectr   r   r   rP      s    z AsyncAdapt_dbapi_connection.pingc                 O  s   | j j|| d S r   )r
   add_output_converter)r   argkwr   r   r   rQ      s    z0AsyncAdapt_dbapi_connection.add_output_converterc                 C  s
   | j  S r   )r
   character_set_namer   r   r   r   rT      s    z.AsyncAdapt_dbapi_connection.character_set_namec                 C  s   | j jS r   )r
   
autocommitr   r   r   r   rU      s    z&AsyncAdapt_dbapi_connection.autocommitc                 C  s   || j j_d S r   )r
   Z_connrU   r   r   r   r   rU      s    Fc                 C  s   |r|  | S | | S d S r   )_ss_cursor_cls_cursor_cls)r   r   r   r   r   r      s    
z"AsyncAdapt_dbapi_connection.cursorc                 C  s   |  | j  d S r   )r   r
   rollbackr   r   r   r   rX      s    z$AsyncAdapt_dbapi_connection.rollbackc                 C  s   |  | j  d S r   )r   r
   commitr   r   r   r   rY      s    z"AsyncAdapt_dbapi_connection.commitc                 C  s   |  | j  d S r   )r   r
   r#   r   r   r   r   r#      s    z!AsyncAdapt_dbapi_connection.closeN)F)rA   rB   rC   r   rW   rG   rV   staticmethodr   r   rD   r   rP   rQ   rT   rE   rU   rF   r   rX   rY   r#   r   r   r   r   rL      s    


rL   c                   @  s   e Zd ZdZeeZdS )#AsyncAdaptFallback_dbapi_connectionr   N)rA   rB   rC   rD   rZ   r   r   r   r   r   r   r[      s   r[   )__doc__
__future__r   r   Zenginer   Zutil.concurrencyr   r   r   r   rG   rL   r[   r   r   r   r   <module>	   s   i$0