U
    dª/e  ã                   @   s\   d dl mZ d dlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZdS )é   )ÚBlockStreamProfileInfo)ÚProgressc                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚQueryResultz3
    Stores query result from multiple blocks.
    Fc                    s0   || _ || _g | _g | _|| _tt| ƒ ¡  d S ©N)Úpacket_generatorÚwith_column_typesÚdataÚcolumns_with_typesÚcolumnarÚsuperr   Ú__init__)Úselfr   r   r
   ©Ú	__class__© ú</tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/result.pyr   
   s    zQueryResult.__init__c                 C   sŒ   t |dd ƒ}|d krd S |jrz| jrh| ¡ }| jrVt|ƒD ]\}}| j|  |¡ q:qxdd„ |D ƒ| _qˆ| j | ¡ ¡ n| jsˆ|j| _d S )NÚblockc                 S   s   g | ]}t |ƒ‘qS r   )Úlist©Ú.0Úcr   r   r   Ú
<listcomp>&   s     z%QueryResult.store.<locals>.<listcomp>)	ÚgetattrZnum_rowsr
   Zget_columnsr   Ú	enumerateÚextendÚget_rowsr	   )r   Úpacketr   ÚcolumnsÚiÚcolumnr   r   r   Ústore   s    zQueryResult.storec                 C   sJ   | j D ]}|  |¡ q| j}| jr2dd„ | jD ƒ}| jrB|| jfS |S dS )z/
        :return: stored query result.
        c                 S   s   g | ]}t |ƒ‘qS r   )Útupler   r   r   r   r   7   s     z*QueryResult.get_result.<locals>.<listcomp>N)r   r    r   r
   r   r	   )r   r   r   r   r   r   Ú
get_result-   s    

zQueryResult.get_result)FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r"   Ú__classcell__r   r   r   r   r      s      þr   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡ fdd	„Z‡  ZS )
ÚProgressQueryResultzx
    Stores query result and progress information from multiple blocks.
    Provides iteration over query progress.
    c                    s   t ƒ | _tt| ƒj||Ž d S r   )r   Úprogress_totalsr   r(   r   )r   ÚargsÚkwargsr   r   r   r   E   s    zProgressQueryResult.__init__c                 C   s   | S r   r   ©r   r   r   r   Ú__iter__I   s    zProgressQueryResult.__iter__c                 C   sF   t | jƒ}t|dd ƒ}|r6| j |¡ | jj| jjfS |  |¡ q d S )NÚprogress)Únextr   r   r)   Ú	incrementZrowsZ
total_rowsr    )r   r   Zprogress_packetr   r   r   Ú__next__L   s    
 ÿzProgressQueryResult.__next__c                    s   | D ]}qt t| ƒ ¡ S r   )r   r(   r"   )r   Ú_r   r   r   r"   X   s    zProgressQueryResult.get_result)	r#   r$   r%   r&   r   r-   r1   r"   r'   r   r   r   r   r(   ?   s
   r(   c                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚIterQueryResultzP
    Provides iteration over returned data by chunks (streaming by chunks).
    Fc                    s$   || _ || _d| _tt| ƒ ¡  d S )NT)r   r   Úfirst_blockr   r3   r   )r   r   r   r   r   r   r   e   s    zIterQueryResult.__init__c                 C   s   | S r   r   r,   r   r   r   r-   n   s    zIterQueryResult.__iter__c                 C   sZ   t | jƒ}t|dd ƒ}|d kr"g S | jrN| jrNd| _|jg}| | ¡ ¡ |S | ¡ S d S )Nr   F)r/   r   r   r4   r   r	   r   r   )r   r   r   Úrvr   r   r   r1   q   s    
zIterQueryResult.__next__)F)r#   r$   r%   r&   r   r-   r1   r'   r   r   r   r   r3   `   s
    þ	r3   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú	QueryInfoc                 C   s   t ƒ | _tƒ | _d| _d S )Né    )r   Úprofile_infor   r.   Úelapsedr,   r   r   r   r      s    zQueryInfo.__init__c                 C   s
   || _ d S r   )r8   )r   r8   r   r   r   Ústore_profile†   s    zQueryInfo.store_profilec                 C   s   | j r| j  |¡ n|| _ d S r   )r.   r0   )r   r.   r   r   r   Ústore_progress‰   s    zQueryInfo.store_progressc                 C   s
   || _ d S r   )r9   )r   r9   r   r   r   Ústore_elapsed   s    zQueryInfo.store_elapsedN)r#   r$   r%   r   r:   r;   r<   r   r   r   r   r6   €   s   r6   N)	Zblockstreamprofileinfor   r.   r   Úobjectr   r(   r3   r6   r   r   r   r   Ú<module>   s
   :! 