a
    bgf(                     @   sh  d dl 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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 d dlmZmZmZ d dlm Z m!Z!m"Z"m#Z# zd d	lm$Z$ W n  e%y   e&d
e&fi Z$Y n0 e" Z'z d dlm(Z( G dd de'Z)W n0 e%y8   d dlm*Z* G dd de'Z)Y n0 eee+dddZ,G dd dee-e.f Z/dS )    N)Path)AnyAsyncGeneratorAsyncIteratorDict	GeneratorIteratorListOptionalSequenceTupleUnioncast)	BaseStore)LargeBinaryTextand_create_enginedeleteselect)Engine)AsyncEngineAsyncSessioncreate_async_engine)MappedSessiondeclarative_basesessionmaker)async_sessionmakerr   )mapped_columnc                   @   sX   e Zd ZU dZdZeddddZee e	d< eddddZ
ee e	d< eedddZd	S )
LangchainKeyValueStoresTable used to save values.langchain_key_value_storesTFZprimary_keyindexnullable	namespacekeyr$   r%   N)__name__
__module____qualname____doc____tablename__r   r&   r   str__annotations__r'   r   value r1   r1   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/storage/sql.pyr    3   s   
r    )Columnc                   @   sF   e Zd ZdZdZee ddddZee ddddZee	dddZ
dS )r    r!   r"   TFr#   r(   N)r)   r*   r+   r,   r-   r3   r   r&   r'   r   r0   r1   r1   r1   r2   r    F   s
   )xyreturnc                 C   s   | |kS Nr1   )r4   r5   r1   r1   r2   items_equalT   s    r8   c                   @   s  e Zd ZdZdddddeeeeef  eeee	f  ee
eef  ee dddZdddd	Zddd
dZddddZee eee  dddZee eee  dddZeeeef  ddddZeeeef  ddddZee eddddZee eddddZee ddddZee dddd Zdd!ee ee d"d#d$Z dd!ee e!e d"d%d&Z"e#j$e%eddf dd'd(Z&e#j'e(edf dd)d*Z)dS )+SQLStorea  BaseStore interface that works on an SQL database.

    Examples:
        Create a SQLStore instance and perform operations on it:

        .. code-block:: python

            from langchain_community.storage import SQLStore

            # Instantiate the SQLStore with the root path
            sql_store = SQLStore(namespace="test", db_url="sqlite://:memory:")

            # Set values for keys
            sql_store.mset([("key1", b"value1"), ("key2", b"value2")])

            # Get values for keys
            values = sql_store.mget(["key1", "key2"])  # Returns [b"value1", b"value2"]

            # Delete keys
            sql_store.mdelete(["key1"])

            # Iterate over keys
            for key in sql_store.yield_keys():
                print(key)

    N)db_urlengineengine_kwargs
async_mode)r&   r:   r;   r<   r=   c                C   s   |d u r|d u rt d|d ur0|d ur0t d|r||d u r@d}|r`tf dt|i|pXi }qtf dt|i|pti }n|r|}ntdt|trd| _t|d}nd| _t	|d}|| _
|jj| _|| _|| _d S )Nz$Must specify either db_url or enginez.Must specify either db_url or engine, not bothFurlz2Something went wrong with configuration of engine.Tbind)
ValueErrorr   r.   r   AssertionError
isinstancer   r=   r   r   r;   dialectnamesession_makerr&   )selfr&   r:   r;   r<   r=   Z_engineZ_session_makerr1   r1   r2   __init__v   s4    	


zSQLStore.__init__)r6   c                 C   s   t j| j d S r7   )Basemetadata
create_allr;   rG   r1   r1   r2   create_schema   s    zSQLStore.create_schemac              	      sf   t | jtsJ | j 4 I d H *}|tjjI d H  W d   I d H  qb1 I d H sX0    Y  d S r7   )rC   r;   r   beginZrun_syncrI   rJ   rK   rG   sessionr1   r1   r2   acreate_schema   s    zSQLStore.acreate_schemac                 C   s   t jj| j d d S )Nr?   )rI   rJ   Zdrop_allr;   connectrL   r1   r1   r2   drop   s    zSQLStore.drop)keysr6   c              	      s   t | jtsJ i  |  4 I d H \}ttttj	|tj
| j
k}||I d H D ]}|j |j< qZW d   I d H  q1 I d H s0    Y   fdd|D S )Nc                    s   g | ]}  |qS r1   get.0r'   resultr1   r2   
<listcomp>       z"SQLStore.amget.<locals>.<listcomp>)rC   r;   r   _make_async_sessionr   r    filterr   r'   in_r&   scalarsr0   rG   rT   rP   stmtvr1   rY   r2   amget   s    

8zSQLStore.amgetc                    s~   i  |   P}ttttj|tj| jk}||D ]}|j	 |j< q<W d    n1 sb0    Y   fdd|D S )Nc                    s   g | ]}  |qS r1   rU   rW   rY   r1   r2   r[      r\   z!SQLStore.mget.<locals>.<listcomp>)
_make_sync_sessionr   r    r^   r   r'   r_   r&   r`   r0   ra   r1   rY   r2   mget   s    


,zSQLStore.mget)key_value_pairsr6   c              	      s      4 I d H X} dd |D |I d H  | fdd|D  | I d H  W d   I d H  q~1 I d H st0    Y  d S )Nc                 S   s   g | ]\}}|qS r1   r1   )rX   r'   _r1   r1   r2   r[      r\   z"SQLStore.amset.<locals>.<listcomp>c                    s    g | ]\}}t  j||d qS )r&   r'   r0   r    r&   rX   krc   rL   r1   r2   r[      s   )r]   	_amdeleteadd_allcommit)rG   rg   rP   r1   rL   r2   amset   s    
zSQLStore.amsetc                    sl   t |}  H} t| | | fdd| D  |  W d    n1 s^0    Y  d S )Nc                    s    g | ]\}}t  j||d qS ri   rj   rk   rL   r1   r2   r[      s   z!SQLStore.mset.<locals>.<listcomp>)dictre   _mdeletelistrT   rn   itemsro   )rG   rg   valuesrP   r1   rL   r2   mset   s    

zSQLStore.mset)rT   rP   r6   c                 C   s2   t tttj|tj| jk}|| d S r7   r   r    r^   r   r'   r_   r&   executerG   rT   rP   rb   r1   r1   r2   rr      s    

zSQLStore._mdeletec                    s8   t tttj|tj| jk}||I d H  d S r7   rw   ry   r1   r1   r2   rm      s    

zSQLStore._amdeletec                 C   s@   |   $}| || |  W d    n1 s20    Y  d S r7   )re   rr   ro   rG   rT   rP   r1   r1   r2   mdelete   s    
zSQLStore.mdeletec              	      s`   |   4 I d H 6}| ||I d H  | I d H  W d   I d H  q\1 I d H sR0    Y  d S r7   )r]   rm   ro   rz   r1   r1   r2   amdelete   s    zSQLStore.amdelete)prefix)r}   r6   c                c   sr   |   V}|ttj| jkD ]$}t|j|p6dr"t|jV  q"|  W d    n1 sd0    Y  d S N )	re   queryr    r^   r&   r.   r'   
startswithclose)rG   r}   rP   rc   r1   r1   r2   
yield_keys   s    


zSQLStore.yield_keysc             	   C  s   |   4 I d H p}tttj| jk}||I d H D ]$}t|j|pLdr8t|jV  q8|	 I d H  W d   I d H  q1 I d H s0    Y  d S r~   )
r]   r   r    r^   r&   r`   r.   r'   r   r   )rG   r}   rP   rb   rc   r1   r1   r2   ayield_keys	  s    
zSQLStore.ayield_keysc                 c   sL   | j rtdtt|  }tt|V  W d   n1 s>0    Y  dS )Make an async session.zsAttempting to use a sync method in when async mode is turned on. Please use the corresponding async method instead.N)r=   rA   r   r   rF   rO   r1   r1   r2   re     s    zSQLStore._make_sync_sessionc              	   C  s`   | j stdtt|  4 I dH "}tt|V  W d  I dH  q\1 I dH sR0    Y  dS )r   ztAttempting to use an async method in when sync mode is turned on. Please use the corresponding async method instead.N)r=   rA   r   r   rF   rO   r1   r1   r2   r]     s    zSQLStore._make_async_session)*r)   r*   r+   r,   r.   r
   r   r   r   r   r   r   boolrH   rM   rQ   rS   r   r	   bytesrd   rf   r   rp   rv   r   rr   r   rm   r{   r|   r   r   r   r   
contextlibcontextmanagerr   re   asynccontextmanagerr   r]   r1   r1   r1   r2   r9   Z   s:   -			

r9   )0r   pathlibr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   Zlangchain_core.storesr   Z
sqlalchemyr   r   r   r   r   r   Zsqlalchemy.engine.baser   Zsqlalchemy.ext.asyncior   r   r   Zsqlalchemy.ormr   r   r   r   r   ImportErrortyperI   r   r    r3   r   r8   r.   r   r9   r1   r1   r1   r2   <module>   s(   8 