
    g/7                       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ee#         Z$ G d dee#         Z% G d de%e#         Z& G d dee#   e$e#         Z' G d dee#   e%e#         Z( G d dee#   e$e#         Z) G d dee#         Z*y)    )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)
SyncClientget_origin_and_cast_ReturnTc                  8    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZy)SyncQueryRequestBuilderc                X    || _         || _        || _        || _        || _        || _        y Nsessionpathhttp_methodheadersparamsjsonselfr$   r%   r&   r'   r(   r)   s          T/var/www/openai/venv/lib/python3.12/site-packages/postgrest/_sync/request_builder.py__init__z SyncQueryRequestBuilder.__init__   /     	&	    c                   | j                   j                  | j                  | j                  | j                  | j
                  | j                        }	 |j                  r| j                  dk7  r|j                  }| j                  j                  d      dk(  r|S | j                  j                  d      r<d| j                  j                  d      v rd| j                  j                  d      vr|S t        t           j                  |      S t        |j	                               # t        $ r}t        |j	                               |d}~wt        $ r t        t!        |            w xY w)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'   HEADAccepttext/csvzapplication/vnd.pgrst.planz+jsonN)r$   requestr&   r%   r)   r(   r'   
is_successtextgetr   r   from_http_request_responser   r   r   r   )r+   rbodyes       r,   executezSyncQueryRequestBuilder.execute.   s,    LL  II;;LL ! 
	>||##v-66D||''1Z?#||'' 6$,,:J:J8:TT"$,,*:*:8*DD#'K"8,GGJJqvvx(( 	,1668$!+ 	>9!<==	>s,   AD' AD' 2D' D' '	E+0E

!E+N)r$   r   r%   strr&   r>   r'   r
   r(   r   r)   Union[dict, list]returnNone)r@   zAPIResponse[_ReturnT]__name__
__module____qualname__r-   r=    r/   r,   r    r       sP      	
     
 $>r/   r    c                  8    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZy)SyncSingleRequestBuilderc                X    || _         || _        || _        || _        || _        || _        y r"   r#   r*   s          r,   r-   z!SyncSingleRequestBuilder.__init__V   r.   r/   c                   | j                   j                  | j                  | j                  | j                  | j
                  | j                        }	 d|j                  cxk  rdk  rn nt        t           j                  |      S t        |j	                               # t        $ r}t        |j	                               |d}~wt        $ r t        t        |            w xY w)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.
        r1      i+  N)r$   r5   r&   r%   r)   r(   r'   status_coder   r   r9   r   r   r   r   r+   r:   r<   s      r,   r=   z SyncSingleRequestBuilder.executef   s     LL  II;;LL ! 

	>q}}++(2MMaPPqvvx(( 	,1668$!+ 	>9!<==	>s   3B! B! !	C%*C!C%Nr$   r   r%   r>   r&   r>   r'   r
   r(   r   r)   dictr@   rA   )r@   zSingleAPIResponse[_ReturnT]rB   rF   r/   r,   rH   rH   U   sP      	
    
 >r/   rH   c                      e Zd ZddZy)SyncMaybeSingleRequestBuilderc                    d }	 t         t           j                  |       }|st        ddddd      |S # t        $ r)}|j                  rd|j                  v rY d }~y Y d }~Ad }~ww xY w)NzThe result contains 0 rowszMissing response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)rH   r   r=   r   rW   rM   s      r,   r=   z%SyncMaybeSingleRequestBuilder.execute   sw    	(2::4@A 1!@  r	    	yy9QYYF	s   4 	A&A!!A&N)r@   z%Optional[SingleAPIResponse[_ReturnT]])rC   rD   rE   r=   rF   r/   r,   rQ   rQ      s    r/   rQ   c                  0    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)SyncFilterRequestBuilderc           	         t        t        t                 j                  | |||       t        t        t                 j                  | ||||||       y r"   )r   r   r   r-   r    r*   s          r,   r-   z!SyncFilterRequestBuilder.__init__   P     	4X>?HH'7F	
 	3H=>GG'4gvt	
r/   NrN   rC   rD   rE   r-   rF   r/   r,   rY   rY      sK    

 
 	

 
 
 
 

r/   rY   c                  0    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)SyncRPCFilterRequestBuilderc           	         t        t        t                 j                  | |||       t        t        t                 j                  | ||||||       y r"   )r   r   r   r-   rH   r*   s          r,   r-   z$SyncRPCFilterRequestBuilder.__init__   sP     	4X>?HH'7F	
 	4X>?HH'4gvt	
r/   NrN   r\   rF   r/   r,   r^   r^      sK    

 
 	

 
 
 
 

r/   r^   c                  b    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZd	dZi f	 	 	 	 	 	 	 d
dZddZy)SyncSelectRequestBuilderc           	         t        t        t                 j                  | |||       t        t        t                 j                  | ||||||       y r"   )r   r   r   r-   r    r*   s          r,   r-   z!SyncSelectRequestBuilder.__init__   r[   r/   c                    d| j                   d<   t        t           | j                   | j                  | j                  | j
                  | j                  | j                        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+jsonr3   r'   r&   r)   r(   r%   r$   )r'   rH   r   r&   r)   r(   r%   r$   r+   s    r,   singlezSyncSelectRequestBuilder.single   sQ     "EX'1LL((;;LL
 	
r/   c                    d| j                   d<   t        t           | j                   | j                  | j                  | j
                  | j                  | j                        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.rd   r3   re   )r'   rQ   r   r&   r)   r(   r%   r$   rf   s    r,   maybe_singlez%SyncSelectRequestBuilder.maybe_single   sN    !DX,X6LL((;;LL
 	
r/   c                   |j                  d      }d}|dk(  rd}n|dk(  rd}n|dk(  rd}|j                  d	      rd
|j                  d	       dnd}| j                  j                  || d| d|       | _        t        t           | j
                  | j                  | j                  | j                  | j                  | j                        S )Ntype plainplphraseph
web_searchwconfig()fts.re   )
r8   r(   addr    r   r'   r&   r)   r%   r$   )r+   columnqueryoptionstype_	type_partconfig_parts          r,   text_searchz$SyncSelectRequestBuilder.text_search   s     F#	GIhIl"I6=kk(6K'++h/02QSkkoof3{m1UG.TU&x0LL((;;LL
 	
r/   c                    d| j                   d<   t        t           | j                  | j                  | j
                  | j                   | j                  | j                        S )zASpecify that the query must retrieve data as a single CSV string.r4   r3   r#   )r'   rH   r>   r$   r%   r&   r(   r)   rf   s    r,   csvzSyncSelectRequestBuilder.csv  sN    !+X',LL((LL;;
 	
r/   NrN   )r@   z"SyncSingleRequestBuilder[_ReturnT])r@   z'SyncMaybeSingleRequestBuilder[_ReturnT])ry   r>   rz   r>   r{   zdict[str, Any]r@   "SyncFilterRequestBuilder[_ReturnT])r@   zSyncSingleRequestBuilder[str])rC   rD   rE   r-   rg   ri   r   r   rF   r/   r,   ra   ra      s    

 
 	

 
 
 
 

 
 

 BD

"%
0>
	+
.

r/   ra   c                     e Zd ZddZddd	 	 	 	 	 	 	 ddZdej                  ddd	 	 	 	 	 	 	 	 	 	 	 ddZdej                  dd	dd
	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdej                  d	 	 	 	 	 	 	 ddZ	dej                  d	 	 	 	 	 ddZ
y)SyncRequestBuilderc                     || _         || _        y r"   )r$   r%   )r+   r$   r%   s      r,   r-   zSyncRequestBuilder.__init__  s    	r/   Ncountheadc               z    t        |||d\  }}}}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:`SyncSelectRequestBuilder`
        r   )r   ra   r   r$   r%   )r+   r   r   columnsmethodr(   r'   r)   s           r,   selectzSyncRequestBuilder.select  sB     )3G5t(T%'1LL$))VWfd
 	
r/   FTr   	returningupsertdefault_to_nullc                   t        |||||      \  }}}}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:`SyncQueryRequestBuilder`
        r   )r   r    r   r$   r%   )	r+   r)   r   r   r   r   r   r(   r'   s	            r,   insertzSyncRequestBuilder.insert,  sO    , )3+)
% 'x0LL$))VWfd
 	
r/   rl   r   r   ignore_duplicateson_conflictr   c                   t        ||||||      \  }}}	}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:`SyncQueryRequestBuilder`
        r   )r   r    r   r$   r%   )
r+   r)   r   r   r   r   r   r   r(   r'   s
             r,   r   zSyncRequestBuilder.upsertM  sR    2 )3/#+)
% 'x0LL$))VWfd
 	
r/   r   r   c               ~    t        |||      \  }}}}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:`SyncFilterRequestBuilder`
        r   )r   rY   r   r$   r%   )r+   r)   r   r   r   r(   r'   s          r,   updatezSyncRequestBuilder.updater  sI      )3)
%
 (1LL$))VWfd
 	
r/   c               |    t        ||      \  }}}}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:`SyncFilterRequestBuilder`
        r   )r   rY   r   r$   r%   )r+   r   r   r   r(   r'   r)   s          r,   deletezSyncRequestBuilder.delete  sF     )3)
% (1LL$))VWfd
 	
r/   )r$   r   r%   r>   r@   rA   )r   r>   r   Optional[CountMethod]r   zOptional[bool]r@   z"SyncSelectRequestBuilder[_ReturnT])r)   r?   r   r   r   r   r   boolr   r   r@   !SyncQueryRequestBuilder[_ReturnT])r)   r?   r   r   r   r   r   r   r   r>   r   r   r@   r   )r)   rO   r   r   r   r   r@   r   )r   r   r   r   r@   r   )rC   rD   rE   r-   r   r   representationr   r   r   r   rF   r/   r,   r   r     sY    (,#	

 %
 	

 
,
. (,"."="= $

 %	

  
 
 
 
+
J (,"."="="' $#
#
 %	#

  #
  #
 #
 #
 
+#
R (,"."="=

 %	

  
 
,
8 (,"."="=	
 %
  	

 
,
r/   r   N)+
__future__r   r)   r   typingr   r   r   r   r	   httpxr
   r   pydanticr   base_request_builderr   r   r   r   r   r   r   r   r   r   r   
exceptionsr   r   typesr   utilsr   r   r   r    rH   rQ   rY   r^   ra   r   rF   r/   r,   <module>r      s    "   9 9 & $    B   3:5>gh/ 5>p.>wx0 .>b$<X$F *
7ACZ[cCd 
&
(#%=h%G
*N
7ACZ[cCd N
bK
* K
r/   