o
    FDi/                     @   sr   d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 eddZ
e eZG dd	 d	ZG d
d deZdS )    N)
namedtuple)Optional)InterfaceErrorProgrammingError)CursorStatusColumnzAname type_code display_size internal_size precision scale null_okc                   @   sZ  e Zd ZdZdJdefddZdefddZed	d
 Z	edd Z
edefddZdd Zdd Zdd Z		dKdefddZdLddZdKddZdd Zdee fdd Zd!d" Zd#d$ Zd%d& Zd'd( ZdMd)d*Zd+d, Zd-d. Zd/d0 Zd1d2 Zed3d4 Zd5d6 Z d7d8 Z!d9d: Z"d;d< Z#d=d> Z$d?d@ Z%dAdB Z&dCdD Z'dEdF Z(dNdHdIZ)dS )OCursorNFechoc                 C   s$   || _ |   g | _|| _d| _d S )N   )_connection_reset_state_rows_echo
_arraysize)self
connectionr	    r   G/var/www/Datamplify/venv/lib/python3.10/site-packages/asynch/cursors.py__init__   s
   
zCursor.__init__returnc                 C   s<   | j j}| j}d| d| j d| j dt| dd| dS )N<z(connection=z, echo=z) object at 0xxz
; status: >)	__class____name__statusr   r   id)r   cls_namer   r   r   r   __repr__   s   zCursor.__repr__c                 C      | j S )zmThis read-only attribute return a reference to the Connection
        object on which the cursor was created.)r   r   r   r   r   r      s   zCursor.connectionc                 C   r   )zQ
        :return: the number of rows that the last .execute*() produced.
        )	_rowcountr    r   r   r   rowcount%   s   zCursor.rowcountc                 C   r   )zReturn the status of the cursor.

        :return: the Cursor object status
        :rtype: str (CursorStatus StrEnum)
        )_stater    r   r   r   r   ,   s   zCursor.statusc                 G      dS z!Does nothing, required by DB API.Nr   r   argsr   r   r   setinputsizes6       zCursor.setinputsizesc                 G   r$   r%   r   r&   r   r   r   setoutputsizes9   r)   zCursor.setoutputsizesc                    s   t j| _d S N)r   closedr#   r    r   r   r   close<   s   zCursor.closequeryc                    s~   |    |   |   | |\}}||f|dd|I d H }| |I d H  |   | jr<t| td| | j	S )NT)r'   with_column_types%r
_check_cursor_closed_check_query_executing_begin_query_prepare_process_response
_end_queryr   loggerinfor!   r   r.   r'   contextexecuteexecute_kwargsresponser   r   r   r<   ?   s   
zCursor.executec                    s   |st |tr|| _d }|sg  | _ | _| _d S | jr'| I d H }|}n|\}}|| _|rAt	| \| _| _| js@t
|| _ng  | _| _|| _d S r+   )
isinstanceintr!   _columns_typesr   _stream_resultsget_columns_with_types_columns_with_typesziplen)r   r>   executemanycolumns_with_typesrowsr   r   r   r6   T   s&   

zCursor._process_responsec                    s   |    |   |   | |\}}||fd|i|I d H }| j|ddI d H  |   | jr=t| td| | j	S )Nr'   T)rH   r0   r1   r:   r   r   r   rH   m   s   
zCursor.executemanyc                    sN   |    | jrz	| j I d H W S  ty   Y d S w | js!d S | jdS Nr   )_check_query_startedrC   r   next	Exceptionpopr    r   r   r   fetchone}   s   zCursor.fetchonesizec                    s   |    |d u r| j}|dkrg S | jr6g }| j2 z3 d H W }|| |dkr2t||kr2 |S q6 |S |dk rB| j}g | _|S | jd | }| j|d  | _|S rK   )rL   r   rC   r   appendrG   )r   rQ   rvir   r   r   	fetchmany   s.   
zCursor.fetchmanyc                    s6   |    | jrdd | j2 I d H S | j}g | _|S )Nc                    s   g | z3 d H W }|q6 S r+   r   ).0rowr   r   r   
<listcomp>   s    z#Cursor.fetchall.<locals>.<listcomp>)rL   rC   r   )r   rS   r   r   r   fetchall   s   zCursor.fetchallc                 C   sH   t j| _d| _d| _d| _d| _d| _d| _d| _	d| _
i | _d| _dS )zbReset the state of the cursor.

        Prepares a cursor object to handle another query.
        NFr    )r   readyr#   rA   rB   r   r!   rC   _max_row_buffer	_settings	_query_id_external_tables_types_checkr    r   r   r   r      s   
zCursor._reset_statec                 C   s2   g }| j  D ]\}\}}||||d q|S )Nname	structuredata)r`   itemsrR   )r   tablesrc   rd   re   r   r   r   _make_external_tables   s   zCursor._make_external_tablesc           
      C   s   | dg }g }|j}|D ])}g }|jD ]}|j|j|d}	||j|	f q||j||jd d d q|| 	  |S )Nexternal_tables)type_expression
clickhousere   rb   )
gettype_compilercolumnsprocesstyperR   rc   dialect_optionsextendrh   )
r   dialectexecution_optionsri   rg   rm   tablerd   ctype_r   r   r   make_external_tables   s    
	zCursor.make_external_tablesc                 C   s   |r|j }| |j|}ni }|  pd }| jjj}|d| jp!i }|d| j| _| jr@| jjj	}|d| j
| _
| j
|d< || _|||d| j| jd}||fS )Nsettingsstream_resultsmax_block_sizetypes_check)ry   ri   r|   query_id)rt   rx   rs   rh   r   r<   rl   r^   rC   execute_iterr]   ra   r_   )r   r;   rt   ri   r<   ry   r=   r   r   r   r5      s&   


zCursor._preparec                 C   s   | S r+   r   r    r   r   r   	__aiter__   s   zCursor.__aiter__c                    s   	 |   I d H }|st|S r+   )rP   StopAsyncIteration)r   oner   r   r   	__anext__   s   zCursor.__anext__c                    s   |   I d H  d S r+   )r-   )r   exc_typeexc_valexc_tbr   r   r   	__aexit__  s   zCursor.__aexit__c                    s   | S r+   r   r    r   r   r   
__aenter__
  s   zCursor.__aenter__c                 C   s8   | j tjkrd S | jpg }| jpg }dd t||D S )Nc              
   S   s$   g | ]\}}t ||d d d d dqS )NT)r   )rV   rc   	type_coder   r   r   rX     s    z&Cursor.description.<locals>.<listcomp>)r#   r   r\   rA   rB   rF   )r   rn   typesr   r   r   description  s   

zCursor.descriptionc                 C   s   | j tjkrtd|  d S )Nzno results to fetch from the )r#   r   r\   r   r    r   r   r   rL     s   zCursor._check_query_startedc                 C   s   | j j jr	tdd S )Nz_some records have not been fetched. fetch the remaining records before executing the next query)r   is_query_executingr   r    r   r   r   r3     s
   
zCursor._check_query_executingc                 C   s    | j tjkrtd|  dd S )Nzthe z is already closed)r#   r   r,   r   r    r   r   r   r2   $  s   zCursor._check_cursor_closedc                 C      t j| _d S r+   )r   runningr#   r    r   r   r   r4   (     zCursor._begin_queryc                 C   r   r+   )r   finishedr#   r    r   r   r   r7   +  r   zCursor._end_queryc                 C   s   || _ || _dS )am  
        Toggles results streaming from server. Driver will consume
        block-by-block of `max_row_buffer` size and yield row-by-row from each
        block.

        :param stream_results: enable or disable results streaming.
        :param max_row_buffer: specifies the maximum number of rows to buffer
               at a time.
        :return: None
        N)rC   r]   )r   rz   max_row_bufferr   r   r   set_stream_results.  s   
zCursor.set_stream_resultsc                 C   
   || _ dS )z}
        Specifies settings for cursor.

        :param settings: dictionary of query settings
        :return: None
        N)r^   )r   ry   r   r   r   set_settings<     
zCursor.set_settingsc                 C   r   )z
        Toggles type checking for sequence of INSERT parameters.
        Disabled by default.

        :param types_check: new types check value.
        :return: None
        N)ra   )r   r|   r   r   r   set_types_checkE  s   
zCursor.set_types_checkc                 C   s   ||f| j |< dS )a  
        Adds external table to cursor context.

        If the same table is specified more than once the last one is used.

        :param name: name of external table
        :param structure: list of tuples (name, type) that defines table
                          structure. Example [(x, 'Int32')].
        :param data: sequence of rows of tuples or dicts for transmission.
        :return: None
        N)r`   )r   rc   rd   re   r   r   r   set_external_tableO  s   zCursor.set_external_tabler[   c                 C   r   )z
        Specifies the query identifier for cursor.

        :param query_id: the query identifier.
        :return: None
        N)r_   )r   r}   r   r   r   set_query_id]  r   zCursor.set_query_id)NF)NN)Fr+   )r[   )*r   
__module____qualname__rE   boolr   strr   propertyr   r"   r   r(   r*   r-   r<   r6   rH   rP   r   r@   rU   rY   r   rh   rx   r5   r   r   r   r   r   rL   r3   r2   r4   r7   r   r   r   r   r   r   r   r   r   r      sV    

	





	
r   c                       sV   e Zd Zdef fddZdee dee f fddZdee f fddZ	  Z
S )	
DictCursorr   c                    s8   t   I dH }| jr|rtt| j|S i S td)zFetch exactly one row from the last executed query.

        :raises AttributeError: columns mismatch

        :return: one row from the query
        :rtype: dict
        NInvalid columns.)superrP   rA   dictrF   AttributeError)r   rW   r   r   r   rP   j  s
   	zDictCursor.fetchonerQ   c                    s<   t  |I dH } jr|r fdd|D S g S td)a#  Fetch no more than `size` rows from the last executed query.

        :param size Optional[int]: fetch upt to the `size` entries or self._arraysize if None

        :raises AttributeError: columns mismatch

        :return: the list of rows from the query
        :rtype: list[dict]
        Nc                       g | ]
}t t j|qS r   r   rF   rA   rV   itemr    r   r   rX         z(DictCursor.fetchmany.<locals>.<listcomp>r   )r   rU   rA   r   )r   rQ   rJ   r   r    r   rU   x  s
   zDictCursor.fetchmanyc                    s:   t   I dH } jr|r fdd|D S g S td)zFetch all resulting rows from the last executed query.

        :raises AttributeError: columns mismatch

        :return: the list of all possible rows from the query
        :rtype: list[dict]
        Nc                    r   r   r   r   r    r   r   rX     r   z'DictCursor.fetchall.<locals>.<listcomp>r   )r   rY   rA   r   )r   rJ   r   r    r   rY     s
   	zDictCursor.fetchall)r   r   r   r   rP   r   r@   listrU   rY   __classcell__r   r   r   r   r   i  s    r   )loggingcollectionsr   typingr   asynch.errorsr   r   asynch.proto.models.enumsr   r   	getLoggerr   r8   r   r   r   r   r   r   <module>   s    

  ^