o
    wiG                     @  s  U d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dl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 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%m&Z& d	dl'm(Z( d	dl)m*Z* d	dl+m,Z, d	dl-m.Z. e/e0Z1de2d< G dd dZ3e3 Z4ddd(ddZ5e	d ed!Z6G d"d# d#ee6 Z7G d$d% d%e7e Z8G d&d' d'e7e  Z9dS ))    )annotationsN)TracebackType)AnyGenericTypeVaroverload)Path)	timedelta)SelfLiteral)	BaseModel   )
Datasource
HttpConfigPrismaMethodMetricsFormatTransactionIdDatasourceOverride)SyncQueryEngineAsyncQueryEngineBaseAbstractEngineSyncAbstractEngineAsyncAbstractEngine)ClientNotConnectedErrorClientNotRegisteredError)model_parseremoveprefix)QueryBuilder)Metrics)
get_client)
EngineTypezlogging.Loggerlogc                   @  s   e Zd ZdZdS )UseClientDefaulta1  For certain parameters such as `timeout=...` we can make our intent more clear
    by typing the parameter with this class rather than using None, for example:

    ```py
    def connect(timeout: Union[int, timedelta, UseClientDefault] = UseClientDefault()) -> None: ...
    ```

    relays the intention more clearly than:

    ```py
    def connect(timeout: Union[int, timedelta, None] = None) -> None: ...
    ```

    This solution also allows us to indicate an "unset" state that is uniquely distinct
    from `None` which may be useful in the future.
    N)__name__
__module____qualname____doc__ r'   r'   J/home/app/Keep/.python/lib/python3.10/site-packages/prisma/_base_client.pyr"      s    r"   F)overrider)   boolkwargsr   returnNonec                 K  s4   ddl m} |dd| i| |dd| i| dS )	zLoad environemntal variables from dotenv files

    Loads from the following files relative to the current
    working directory:

    - .env
    - prisma/.env
    r   )load_dotenv.envr)   prisma/.envN)r/   )r0   )Zdotenvr.   )r)   r+   r.   r'   r'   r(   load_env5   s   	r1   _EngineT)boundc                   @  s(  e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ded< dZd]d#d$Zd^d-d.Zed_d0d1Zd`d2d3Z	d`d4d5Z
d`d6d7Zdad8d9Zedbd;d<Zejdcd>d<Zddd@dAZdedCdDZdEdFdfdJdKZedLdgdPdQZdhd[d\ZdES )i
BasePrismar*   _log_queriesDatasourceOverride | None_datasourceint | timedelta_connect_timeoutzTransactionId | None_tx_idr   _http_configz_EngineT | None_internal_engine_copiedr   _schema_pathset[str]_prisma_models_packaged_schema_pathr    _engine_type_preview_featuresstr_default_datasource_namedict[str, dict[str, str]]_relational_field_mappings)r=   r:   r7   r5   r;   r>   rB   r@   _active_providerr9   r<   rA   rC   rE   rG   
use_dotenvlog_queries
datasourceconnect_timeouthttpHttpConfig | Noner,   r-   c                C  sh   d | _ || _|| _t|trd}tj|tdd t|d}|| _	|p#i | _
d | _d| _|r2t  d S d S )NzPassing an int as `connect_timeout` argument is deprecated and will be removed in the next major release. Use a `datetime.timedelta` instance instead.   
stacklevelsecondsF)r<   r5   r7   
isinstanceintwarningswarnDeprecationWarningr	   r9   r;   r:   r=   r1   )selfrI   rJ   rK   rL   rM   messager'   r'   r(   __init__k   s   



zBasePrisma.__init__schema_pathengine_typepackaged_schema_pathactive_providerprisma_modelspreview_featuresrelational_field_mappingsdefault_datasource_namec          	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _dS )zWe pass through generated metadata using this method
        instead of the `__init__()` because that causes weirdness
        for our `_copy()` method as this base class has arguments
        that the subclasses do not.
        N)r>   rB   r@   rH   rA   rC   rG   rE   )	rY   r\   r]   r^   r_   r`   ra   rb   rc   r'   r'   r(   _set_generated_properties   s   
z$BasePrisma._set_generated_propertiesr   c                 C  s   t d)Nz9`_default_datasource` should be implemented in a subclass)NotImplementedErrorrY   r'   r'   r(   _default_datasource   s   zBasePrisma._default_datasourcec                 C  s"   zt  | u W S  ty   Y dS w )z2Returns True if this client instance is registeredF)r   r   rf   r'   r'   r(   is_registered   s
   zBasePrisma.is_registeredc                 C  
   | j duS )z:Returns True if the client is wrapped within a transactionN)r:   rf   r'   r'   r(   is_transaction      
zBasePrisma.is_transactionc                 C  ri   )zMReturns True if the client is connected to the query engine, False otherwise.Nr<   rf   r'   r'   r(   is_connected   rk   zBasePrisma.is_connectedc                 C  s:   | j d ur| jstd | j }d | _ |  d S d S d S )Nz!unclosed client - stopping engine)r<   r=   r!   debugstoprY   enginer'   r'   r(   __del__   s   
zBasePrisma.__del__r2   c                 C  s   | j }|d u r
t |S N)r<   r   rp   r'   r'   r(   _engine   s   zBasePrisma._enginerq   c                 C  s
   || _ d S rs   rl   rp   r'   r'   r(   rt      rk   r
   c                 C  s8   | j d| j| j| j| jd}d|_| jdur| j|_|S )zReturn a new Prisma instance using the same engine process (if connected).

        This is only intended for private usage, there are no guarantees around this API.
        F)rI   rM   rK   rJ   rL   TN)	__class__r;   r7   r5   r9   r=   r<   rt   )rY   newr'   r'   r(   _copy   s   
zBasePrisma._copyr   c                 C  s:   | j d}|rt|j}| j d | j| j d |ddS )zOverride the default SQLite path to protect against
        https://github.com/RobertCraigie/prisma-client-py/issues/409
        source_file_pathnameurlrelative_to)ry   rz   )rg   getr   parent_make_sqlite_url)rY   rx   r'   r'   r(   _make_sqlite_datasource   s   
z"BasePrisma._make_sqlite_datasourceNr{   rz   r|   Path | str | Nonec                C  sb   t t |dd}||kr|S t| r|S |d u r| jj}t|tr't|}d||  S )Nzfile:zsqlite:)	r   r   is_absoluter>   r~   rT   rD   joinpathresolve)rY   rz   r|   Zurl_pathr'   r'   r(   r      s   
zBasePrisma._make_sqlite_urltimeoutr   "int | timedelta | UseClientDefault1tuple[timedelta, list[DatasourceOverride] | None]c                C  s   t |tr| j}t |trd}tj|tdd t|d}d}| jdur3| j	 }|
d| j |g}n| jdkrBtd |  g}td	| ||fS )
zIReturns (timeout, datasources) to be passed to `AbstractEngine.connect()`zPassing an int as `timeout` argument is deprecated and will be removed in the next major release. Use a `datetime.timedelta` instance instead.rO   rP   rR   Nry   Zsqlitez)overriding default SQLite datasource pathzdatasources: %s)rT   r"   r9   rU   rV   rW   rX   r	   r7   copy
setdefaultrE   rH   r!   rn   r   )rY   r   rZ   datasourcesZdsr'   r'   r(   _prepare_connect_args  s"   







z BasePrisma._prepare_connect_argsmethodr   	argumentsdict[str, Any]modeltype[BaseModel] | Noneroot_selectionlist[str] | Noner   c                C  s   t ||||| j| jdS )N)r   r   r   r   r`   rb   )r   r@   rG   )rY   r   r   r   r   r'   r'   r(   _make_query_builder!  s   zBasePrisma._make_query_builder)rI   r*   rJ   r*   rK   r6   rL   r8   rM   rN   r,   r-   )r\   r   r]   r    r^   r   r_   rD   r`   r?   ra   r?   rb   rF   rc   rD   r,   r-   )r,   r   )r,   r*   )r,   r-   )r,   r2   )rq   r2   r,   r-   r,   r
   )r,   r   )rz   rD   r|   r   r,   rD   )r   r   r,   r   )
r   r   r   r   r   r   r   r   r,   r   )r#   r$   r%   __annotations__	__slots__r[   rd   propertyrg   rh   rj   rm   rr   rt   setterrw   r   r   USE_CLIENT_DEFAULTr   r   r'   r'   r'   r(   r4   G   sD   
 

 





 r4   c                   @  s   e Zd ZdZefd9ddZd:d;d
dZd<ddZd=ddZe		d>ddd?ddZ
e	ddd@d"dZ
	d>dddAd%dZ
d:dBd)d*ZedCd,d-Z		dDdEd7d8ZdS )FSyncBasePrismar'   r   r   r,   r-   c                 C  s>   | j du r| j| jd| _ | j|d\}}| j j||d dS zhConnect to the Prisma query engine.

        It is required to call this before accessing data.
        N)dml_pathr   )r   r   r<   _create_enginerA   r   connectrY   r   r   r'   r'   r(   r   6  s   

zSyncBasePrisma.connectNfloat | timedelta | Nonec                 C  sb   | j dur/| j }d| _ t|ttfr!d}tj|tdd t|d}|j|d |j	|d dS dS z#Disconnect the Prisma query engine.NzPassing a number as `timeout` argument is deprecated and will be removed in the next major release. Use a `datetime.timedelta` instead.rO   rP   rR   r   )
r<   rT   rU   floatrV   rW   rX   r	   closero   rY   r   rq   rZ   r'   r'   r(   
disconnectH  s   

zSyncBasePrisma.disconnectr
   c                 C  s   |    | S rs   r   rf   r'   r'   r(   	__enter__Z  s   zSyncBasePrisma.__enter__exc_typetype[BaseException] | NoneexcBaseException | Noneexc_tbTracebackType | Nonec                 C  s   |   r
|   d S d S rs   rm   r   rY   r   r   r   r'   r'   r(   __exit__^  s   zSyncBasePrisma.__exit__jsonglobal_labelsformatLiteral['json']r   dict[str, str] | Noner   c                C     d S rs   r'   rY   r   r   r'   r'   r(   get_metricsg     zSyncBasePrisma.get_metricsLiteral['prometheus']rD   c                C  r   rs   r'   r   r'   r'   r(   r   o  r   r   str | Metricsc                C  s4   | j j||d}|dkrt|tsJ |S tt|S )  Metrics give you a detailed insight into how the Prisma Client interacts with your database.

        You can retrieve metrics in either JSON or Prometheus formats.

        For more details see https://www.prisma.io/docs/concepts/components/prisma-client/metrics.
        r   r   
prometheusrt   ZmetricsrT   rD   r   r   rY   r   r   responser'   r'   r(   r   w  s
   
r   Path | Noner   c                 C  4   | j tjkrt|p| j| j| jdS td| j  N)r   rJ   Zhttp_configzUnsupported engine type: )rB   r    binaryr   rA   r5   r;   re   rY   r   r'   r'   r(   r        zSyncBasePrisma._create_enginetype[SyncAbstractEngine]c                 C      | j tjkrtS td| j  NzUnhandled engine type: )rB   r    r   r   RuntimeErrorrf   r'   r'   r(   _engine_class     zSyncBasePrisma._engine_classr   r   r   r   r   r   r   r   r   c                 C  s(   | j ||||d}| jj| | jdS N)r   r   r   r   )Ztx_idr   rt   querybuildr:   rY   r   r   r   r   builderr'   r'   r(   _execute  s   zSyncBasePrisma._executer   r   r,   r-   rs   r   r   r,   r-   r   r   r   r   r   r   r   r,   r-   r   r   r   r   r   r,   r   r   r   r   r   r,   rD   r   r   r   r   r,   r   )r   r   r,   r   )r,   r   )NN
r   r   r   r   r   r   r   r   r,   r   )r#   r$   r%   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r   3  s2    

		
r   c                   @  s   e Zd ZdZefd:ddZd;d<d
dZd=ddZd>ddZe		d?ddd@ddZ
e	dddAd"dZ
	d?dddBd%dZ
d;dCd)d*ZedDd,d-Zddd.dEd8d9ZdS )FAsyncBasePrismar'   r   r   r,   r-   c                   sF   | j du r| j| jd| _ | j|d\}}| j j||dI dH  dS r   r   r   r'   r'   r(   r     s   
zAsyncBasePrisma.connectNr   c                   sj   | j dur3| j }d| _ t|ttfr"d}tj|tdd t|d}|j|dI dH  |j	|d dS dS r   )
r<   rT   rU   r   rV   rW   rX   r	   aclosero   r   r'   r'   r(   r     s   

zAsyncBasePrisma.disconnectr
   c                   s   |   I d H  | S rs   r   rf   r'   r'   r(   
__aenter__  s   zAsyncBasePrisma.__aenter__r   r   r   r   r   r   c                   s    |   r|  I d H  d S d S rs   r   r   r'   r'   r(   	__aexit__  s   zAsyncBasePrisma.__aexit__r   r   r   r   r   r   r   c                     d S rs   r'   r   r'   r'   r(   r        zAsyncBasePrisma.get_metricsr   rD   c                  r   rs   r'   r   r'   r'   r(   r     r   r   r   c                  s<   | j j||dI dH }|dkrt|tsJ |S tt|S )r   r   Nr   r   r   r'   r'   r(   r     s   
r   r   r   c                 C  r   r   )rB   r    r   r   rA   r5   r;   re   r   r'   r'   r(   r     r   zAsyncBasePrisma._create_enginetype[AsyncAbstractEngine]c                 C  r   r   )rB   r    r   r   r   rf   r'   r'   r(   r     r   zAsyncBasePrisma._engine_class)r   r   r   r   r   r   r   r   r   r   r   c                  s0   | j ||||d}| jj| | jdI d H S r   r   r   r'   r'   r(   r     s
   zAsyncBasePrisma._executer   rs   r   r   r   r   r   r   r   )r   r   r,   r   )r,   r   r   )r#   r$   r%   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r     s2    

		
r   )r)   r*   r+   r   r,   r-   ):
__future__r   loggingrV   typesr   typingr   r   r   r   pathlibr   datetimer	   Ztyping_extensionsr
   r   Zpydanticr   _typesr   r   r   r   r   r   rq   r   r   r   r   r   errorsr   r   _compatr   r   Z_builderr   Z_metricsr   	_registryr   Zgenerator.modelsr    	getLoggerr#   r!   r   r"   r   r1   r2   r4   r   r   r'   r'   r'   r(   <module>   s4      mw