U
    WiC7                     @  sZ  d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
mZmZ d dlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
l m!Z!m"Z" edZ#G dd dee# Z$G dd dee# Z%G dd de%e# Z&G dd dee# e$e# Z'G dd dee# e%e# Z(G dd dee# e$e# Z)G dd dee# Z*dS )    )annotations)JSONDecodeError)AnyGenericOptionalTypeVarUnion)HeadersQueryParams)ValidationError   )APIResponseBaseFilterRequestBuilderBaseRPCRequestBuilderBaseSelectRequestBuilderCountMethodSingleAPIResponse
pre_delete
pre_insert
pre_select
pre_update
pre_upsert)APIErrorgenerate_default_error_message)ReturnMethod)AsyncClientget_origin_and_cast_ReturnTc                   @  s4   e Zd Zdddddddddd	Zd
dddZdS )AsyncQueryRequestBuilderr   strr	   r
   dictNonesessionpathhttp_methodheadersparamsjsonreturnc                 C  s(   || _ || _|| _|| _|| _|| _d S Nr#   r$   r%   r&   r'   r(   selfr#   r$   r%   r&   r'   r(    r.   f/home/afrizal/public_html/fastapi/venv/lib/python3.8/site-packages/postgrest/_async/request_builder.py__init__   s    	z!AsyncQueryRequestBuilder.__init__zAPIResponse[_ReturnT]r)   c              
     s   | j j| j| j| j| j| jdI dH }z~|jr| jdkr|j}| j	ddkrT|W S | j	drd| j	dkrd| j	dkr|W S t
t |W S t| W nN tk
r } zt| |W 5 d}~X Y n  tk
r   tt|Y nX dS )zExecute the query.

        .. tip::
            This is the last method called, after the query is built.

        Returns:
            :class:`APIResponse`

        Raises:
            :class:`APIError` If the API raised an error.
        r(   r'   r&   NHEADAccepttext/csvzapplication/vnd.pgrst.planz+json)r#   requestr%   r$   r(   r'   r&   Z
is_successtextgetr   r   from_http_request_responser   r   r   r   )r-   rbodyer.   r.   r/   execute.   s4    
z AsyncQueryRequestBuilder.executeN__name__
__module____qualname__r0   r=   r.   r.   r.   r/   r      s   r   c                   @  s4   e Zd Zdddddddddd	Zd
dddZdS )AsyncSingleRequestBuilderr   r   r	   r
   r    r!   r"   c                 C  s(   || _ || _|| _|| _|| _|| _d S r*   r+   r,   r.   r.   r/   r0   V   s    	z"AsyncSingleRequestBuilder.__init__zSingleAPIResponse[_ReturnT]r1   c              
     s   | j j| j| j| j| j| jdI dH }z:d|j  kr>dkrRn ntt	 
|W S t| W nN tk
r } zt| |W 5 d}~X Y n  tk
r   tt|Y nX dS )zExecute the query.

        .. tip::
            This is the last method called, after the query is built.

        Returns:
            :class:`SingleAPIResponse`

        Raises:
            :class:`APIError` If the API raised an error.
        r2   N   i+  )r#   r6   r%   r$   r(   r'   r&   status_coder   r   r9   r   r   r   r   r-   r:   r<   r.   r.   r/   r=   f   s&     
z!AsyncSingleRequestBuilder.executeNr>   r.   r.   r.   r/   rB   U   s   rB   c                   @  s   e Zd ZddddZdS )AsyncMaybeSingleRequestBuilderz%Optional[SingleAPIResponse[_ReturnT]]r1   c              
     st   d }zt t | I d H }W n< tk
rX } z|jrHd|jkrHW Y d S W 5 d }~X Y nX |sptddddd|S )NzThe result contains 0 rowszMissing responseZ204z"Please check traceback of the codezPostgrest couldn't retrieve response, please check traceback of the code. Please create an issue in `supabase-community/postgrest-py` if needed.)messagecodehintdetails)rB   r   r=   r   rJ   rE   r.   r.   r/   r=      s    z&AsyncMaybeSingleRequestBuilder.executeN)r?   r@   rA   r=   r.   r.   r.   r/   rF      s   rF   c                   @  s&   e Zd Zdddddddddd	Zd
S )AsyncFilterRequestBuilderr   r   r	   r
   r    r!   r"   c              	   C  s:   t tt | ||| t tt | |||||| d S r*   )r   r   r   r0   r   r,   r.   r.   r/   r0      s    	         z"AsyncFilterRequestBuilder.__init__Nr?   r@   rA   r0   r.   r.   r.   r/   rK      s   rK   c                   @  s&   e Zd Zdddddddddd	Zd
S )AsyncRPCFilterRequestBuilderr   r   r	   r
   r    r!   r"   c              	   C  s:   t tt | ||| t tt | |||||| d S r*   )r   r   r   r0   rB   r,   r.   r.   r/   r0      s    	         z%AsyncRPCFilterRequestBuilder.__init__NrL   r.   r.   r.   r/   rM      s   rM   c                   @  sh   e Zd Zdddddddddd	Zd
dddZddddZi fdddddddZddddZdS )AsyncSelectRequestBuilderr   r   r	   r
   r    r!   r"   c              	   C  s:   t tt | ||| t tt | |||||| d S r*   )r   r   r   r0   r   r,   r.   r.   r/   r0      s    	         z"AsyncSelectRequestBuilder.__init__z#AsyncSingleRequestBuilder[_ReturnT]r1   c                 C  s.   d| j d< tt | j | j| j| j| j| jdS )zSpecify that the query will only return a single row in response.

        .. caution::
            The API will raise an error if the query returned more than one row.
        !application/vnd.pgrst.object+jsonr4   r&   r%   r(   r'   r$   r#   )r&   rB   r   r%   r(   r'   r$   r#   r-   r.   r.   r/   single   s    
z AsyncSelectRequestBuilder.singlez(AsyncMaybeSingleRequestBuilder[_ReturnT]c                 C  s.   d| j d< tt | j | j| j| j| j| jdS )zRetrieves at most one row from the result. Result must be at most one row (e.g. using `eq` on a UNIQUE column), otherwise this will result in an error.rO   r4   rP   )r&   rF   r   r%   r(   r'   r$   r#   rQ   r.   r.   r/   maybe_single   s    
z&AsyncSelectRequestBuilder.maybe_singlezdict[str, Any]#AsyncFilterRequestBuilder[_ReturnT])columnqueryoptionsr)   c                 C  s   | d}d}|dkrd}n|dkr*d}n|dkr6d}| d	rRd
| d	 dnd}| j|| d| d| | _tt | j| j| j| j| j| j	dS )Ntype plainplphrasephZ
web_searchwconfig()Zfts.rP   )
r8   r'   addr   r   r&   r%   r(   r$   r#   )r-   rU   rV   rW   type_Z	type_partZconfig_partr.   r.   r/   text_search   s$    
  z%AsyncSelectRequestBuilder.text_searchzAsyncSingleRequestBuilder[str]c                 C  s.   d| j d< tt | j| j| j| j | j| jdS )zASpecify that the query must retrieve data as a single CSV string.r5   r4   r+   )r&   rB   r   r#   r$   r%   r'   r(   rQ   r.   r.   r/   csv  s    
zAsyncSelectRequestBuilder.csvN)r?   r@   rA   r0   rR   rS   re   rf   r.   r.   r.   r/   rN      s   rN   c                	   @  s   e Zd ZddddddZdddd	d
dddZdejddddd	dddddddZdejdddddd	ddddddddZdejddd	ddd d!d"Z	dejdd	ddd#d$d%Z
dS )&AsyncRequestBuilderr   r   r!   )r#   r$   r)   c                 C  s   || _ || _d S r*   )r#   r$   )r-   r#   r$   r.   r.   r/   r0     s    zAsyncRequestBuilder.__init__N)countzOptional[CountMethod]z#AsyncSelectRequestBuilder[_ReturnT])columnsrh   r)   c                G  s0   t |d|i\}}}}tt | j| j||||S )zRun a SELECT query.

        Args:
            *columns: The names of the columns to fetch.
            count: The method to use to get the count of rows returned.
        Returns:
            :class:`AsyncSelectRequestBuilder`
        rh   )r   rN   r   r#   r$   )r-   rh   ri   methodr'   r&   r(   r.   r.   r/   select  s         zAsyncRequestBuilder.selectFTrh   	returningupsertdefault_to_nullzUnion[dict, list]r   boolz"AsyncQueryRequestBuilder[_ReturnT])r(   rh   rm   rn   ro   r)   c          	      C  s4   t |||||d\}}}}tt | j| j||||S )a  Run an INSERT query.

        Args:
            json: The row to be inserted.
            count: The method to use to get the count of rows returned.
            returning: Either 'minimal' or 'representation'
            upsert: Whether the query should be an upsert.
            default_to_null: Make missing fields default to `null`.
                Otherwise, use the default value for the column.
                Only applies for bulk inserts.
        Returns:
            :class:`AsyncQueryRequestBuilder`
        rl   )r   r   r   r#   r$   )	r-   r(   rh   rm   rn   ro   rj   r'   r&   r.   r.   r/   insert+  s         zAsyncRequestBuilder.insertrY   rh   rm   ignore_duplicateson_conflictro   )r(   rh   rm   rs   rt   ro   r)   c          
      C  s6   t ||||||d\}}}	}tt | j| j||	||S )a   Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.

        Args:
            json: The row to be inserted.
            count: The method to use to get the count of rows returned.
            returning: Either 'minimal' or 'representation'
            ignore_duplicates: Whether duplicate rows should be ignored.
            on_conflict: Specified columns to be made to work with UNIQUE constraint.
            default_to_null: Make missing fields default to `null`. Otherwise, use the
                default value for the column. This only applies when inserting new rows,
                not when merging with existing rows under `ignoreDuplicates: false`.
                This also only applies when doing bulk upserts.
        Returns:
            :class:`AsyncQueryRequestBuilder`
        rr   )r   r   r   r#   r$   )
r-   r(   rh   rm   rs   rt   ro   rj   r'   r&   r.   r.   r/   rn   L  s          zAsyncRequestBuilder.upsertrh   rm   r    rT   )r(   rh   rm   r)   c                C  s0   t |||d\}}}}tt | j| j||||S )a  Run an UPDATE query.

        Args:
            json: The updated fields.
            count: The method to use to get the count of rows returned.
            returning: Either 'minimal' or 'representation'
        Returns:
            :class:`AsyncFilterRequestBuilder`
        ru   )r   rK   r   r#   r$   )r-   r(   rh   rm   rj   r'   r&   r.   r.   r/   updateq  s         zAsyncRequestBuilder.update)rh   rm   r)   c                C  s.   t ||d\}}}}tt | j| j||||S )zRun a DELETE query.

        Args:
            count: The method to use to get the count of rows returned.
            returning: Either 'minimal' or 'representation'
        Returns:
            :class:`AsyncFilterRequestBuilder`
        ru   )r   rK   r   r#   r$   )r-   rh   rm   rj   r'   r&   r(   r.   r.   r/   delete  s         zAsyncRequestBuilder.delete)r?   r@   rA   r0   rk   r   Zrepresentationrq   rn   rv   rw   r.   r.   r.   r/   rg     s(   %)rg   N)+
__future__r   r(   r   typingr   r   r   r   r   Zhttpxr	   r
   Zpydanticr   Zbase_request_builderr   r   r   r   r   r   r   r   r   r   r   
exceptionsr   r   typesr   utilsr   r   r   r   rB   rF   rK   rM   rN   rg   r.   r.   r.   r/   <module>   s&   481
 Q