
    ug                        d dl mZmZmZmZmZmZ d dlmZ d dl	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 d dlZd dlZd dlmZmZ d d	lmZ d d
lmZmZ d dlm Z   G d de
      Z! G d dee      Z" G d de      Z# e       Z$ G d dejJ                        Z&	 ddejN                  jP                  de)dee)eedf   f   fdZ*y)    )AnyOptionalSequenceTupleTypeUnion)TracebackType)ProtocolSelfLiteral)ABCabstractmethod)local)overrideEnforceOverridesN)System	Component)UUID)islicecount)SeqIdc            	           e Zd ZdZddedeeedf      defdZ	dedefd	Z
	 ddedeeeedf         defd
Zdeedf   fdZdeeedf      fdZy)CursorzFReifies methods we use from a DBAPI2 Cursor since DBAPI2 is not typed.Nsqlparams.returnc                      y N selfr   r   s      E/var/www/openai/venv/lib/python3.12/site-packages/chromadb/db/base.pyexecutezCursor.execute           scriptc                      y r   r   )r!   r&   s     r"   executescriptzCursor.executescript   r$   r%   c                      y r   r   r    s      r"   executemanyzCursor.executemany   s     	r%   c                      y r   r   r!   s    r"   fetchonezCursor.fetchone   r$   r%   c                      y r   r   r,   s    r"   fetchallzCursor.fetchall    r$   r%   r   )__name__
__module____qualname____doc__strr   r   r   r   r#   r(   r   r*   r-   r/   r   r%   r"   r   r      s    P3 sCx(A T C D  GK (%S/)B C	
%S/ (5c?3 r%   r   c            
       f    e Zd ZdZedefd       Zedeee	      dee	   dee
   ded   fd       Zy	)
	TxWrapperaF  Wrapper class for DBAPI 2.0 Connection objects, with which clients can implement transactions.
    Makes two guarantees that basic DBAPI 2.0 connections do not:

    - __enter__ returns a Cursor object consistently (instead of a Connection like some do)
    - Always re-raises an exception if one was thrown from the body
    r   c                      y r   r   r,   s    r"   	__enter__zTxWrapper.__enter__,   s    r%   exc_type	exc_value	tracebackFc                      y r   r   )r!   r9   r:   r;   s       r"   __exit__zTxWrapper.__exit__0        	r%   N)r0   r1   r2   r3   r   r   r8   r   r   BaseExceptionr	   r   r=   r   r%   r"   r6   r6   $   st     6   4./ M* M*	
 
 r%   r6   c                       e Zd ZdZdef fdZedefd       Ze	ede
ej                     fd              Ze	edefd              Ze	edee   dee   fd	              Ze	ed
ee   dee   fd              Ze	ede
e   fd              Zdedej0                  fdZe	deeef   defd       Ze	dedefd       Z xZS )SqlDBzQDBAPI 2.0 interface wrapper to ensure consistent behavior between implementationssystemc                 $    t         |   |       y r   )super__init__)r!   rB   	__class__s     r"   rE   zSqlDB.__init__=   s     r%   r   c                      y)zReturn a transaction wrapperNr   r,   s    r"   txzSqlDB.tx@   s     	r%   c                       y)zReturn a PyPika Query builder of an appropriate subtype for this database
        implementation (see
        https://pypika.readthedocs.io/en/latest/3_advanced.html#handling-different-database-platforms)
        Nr   r   r%   r"   querybuilderzSqlDB.querybuilderE   r>   r%   c                       y)zReturn the appropriate parameter format for this database implementation.
        Will be called with str.format(i) where i is the numeric index of the parameter.
        Nr   r   r%   r"   parameter_formatzSqlDB.parameter_formatN   s     	r%   uuidc                      y)z=Convert a UUID to a value that can be passed to the DB driverNr   )rM   s    r"   
uuid_to_dbzSqlDB.uuid_to_dbV        	r%   valuec                      y)z,Convert a value from the DB driver to a UUIDNr   rQ   s    r"   uuid_from_dbzSqlDB.uuid_from_db\   rP   r%   c                       y)zYReturn the exception type that the DB raises when a unique constraint is
        violatedNr   r   r%   r"   unique_constraint_errorzSqlDB.unique_constraint_errorb   s    
 	r%   idxc                 f    t        j                  | j                         j                  |            S )z4Return a PyPika Parameter object for the given index)pypika	ParameterrL   format)r!   rW   s     r"   paramzSqlDB.parami   s'     5 5 7 > >s CDDr%   seq_id_bytesc                     t        | t              r| S t        |       dk(  rt        j                  | d      S t        |       dk(  rt        j                  | d      S t	        dt        |              )z Decode a byte array into a SeqID   big   zUnknown SeqID type with length )
isinstanceintlen
from_bytes
ValueError)r]   s    r"   decode_seq_idzSqlDB.decode_seq_idm   si     lC(|!>>,66"$>>,66>s<?P>QRSSr%   seq_idc                     | j                         dk  rt        j                  | dd      S | j                         dk  rt        j                  | dd      S t        d|        )z Encode a SeqID into a byte array@   r_   r`      ra   zUnsupported SeqID: )
bit_lengthrc   to_bytesrf   )rh   s    r"   encode_seq_idzSqlDB.encode_seq_idz   s_     "$<<511 C'<<E2226(;<<r%   ) r0   r1   r2   r3   r   rE   r   r6   rH   staticmethodr   rY   QueryrJ   r4   rL   r   r   r   rO   rT   r?   rV   rc   rZ   r\   r   bytesr   rg   rn   __classcell__)rF   s   @r"   rA   rA   :   su   [!v ! I   $v||,    c    $ HSM    HSM htn    T-%8   
E E!1!1 E 
TE%*$5 
T% 
T 
T =e = = =r%   rA   c                   6    e Zd ZdZdefdZededefd       Zy)ParameterValuez
    Wrapper class for PyPika paramters that allows the values for Parameters
    to be expressed inline while building a query. See get_sql() for
    detailed usage information.
    rQ   c                     || _         y r   rS   )r!   rQ   s     r"   rE   zParameterValue.__init__   s	    
r%   kwargsr   c                    t        | j                  t        t        f      rt        j
                  j                  | j                         t        t        j                  t        | j                              }dj                  d |D              }d| d}t        |      S t        j
                  j                  | j                         t        j                  j                  t        t        j                              }t        |      S )Nz, c              3   Z   K   | ]#  }t         j                  j                  |       % y wr   )_context	formatstrr[   ).0is     r"   	<genexpr>z)ParameterValue.get_sql.<locals>.<genexpr>   s"     $S7aX%7%7%>%>q%A7s   )+())rb   rQ   listtuplery   valuesextendr   	generatorrd   joinappendrz   r[   nextr4   )r!   rv   indexesplaceholdersvals        r"   get_sqlzParameterValue.get_sql   s    djj4-0OO""4::.X//TZZAG99$S7$SSLl^1%C
 3x OO""4::.$$++D1C1C,DEC3xr%   N)	r0   r1   r2   r3   r   rE   r   r4   r   r   r%   r"   rt   rt      s5    c  
 
 
 
r%   rt   queryrz   r   .c                     g t         _        t        d      t         _        |t         _        | j                         }t        t         j                        }||fS )af  
    Wrapper for pypika's get_sql method that allows the values for Parameters
    to be expressed inline while building a query, and that returns a tuple of the
    SQL string and parameters. This makes it easier to construct complex queries
    programmatically and automatically matches up the generated SQL with the required
    parameter vector.

    Doing so requires using the ParameterValue class defined in this module instead
    of the base pypika.Parameter class.

    Usage Example:

        q = (
            pypika.Query().from_("table")
            .select("col1")
            .where("col2"==ParameterValue("foo"))
            .where("col3"==ParameterValue("bar"))
        )

        sql, params = get_sql(q)

        cursor.execute(sql, params)

    Note how it is not necessary to construct the parameter vector manually... it
    will always be generated with the parameter values in the same order as emitted
    SQL string.

    The format string should match the parameter format for the database being used.
    It will be called with str.format(i) where i is the numeric index of the parameter.
    For example, Postgres requires parameters like `:1`, `:2`, etc. so the format string
    should be `":{}"`.

    See https://pypika.readthedocs.io/en/latest/2_tutorial.html#parametrized-queries for more
    information on parameterized queries in PyPika.
       )ry   r   r   r   rz   r   r   )r   rz   r   r   s       r"   r   r      sC    N HOqH"H
--/C8??#F;r%   )?)+typingr   r   r   r   r   r   typesr	   typing_extensionsr
   r   r   abcr   r   	threadingr   	overridesr   r   rY   pypika.querieschromadb.configr   r   rM   r   	itertoolsr   r   chromadb.typesr   r   r6   rA   ry   rZ   rt   queriesQueryBuilderr4   r   r   r%   r"   <module>r      s    > >  5 5 #  0   -  #  X *% ,H=I H=V 7V%% 2 :=,>>&&,36,
3c3h ,r%   