a
    bg                     @   sx   d dl 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 d dlmZ d dlmZ d dlmZ G dd deZd	S )
    )AnyCallableDictIteratorListOptionalSequenceUnion)
RowMapping)Select)Document)
BaseLoader)SQLDatabasec                   @   s   e Zd ZdZdddddddeeef eee	ee
f  eedef  eede	ee
f f  eee  eedddZee d	d
dZedeeee  edddZedeeee  e	ee
f dddZdS )SQLDatabaseLoadera  
    Load documents by querying database tables supported by SQLAlchemy.

    For talking to the database, the document loader uses the `SQLDatabase`
    utility from the LangChain integration toolkit.

    Each document represents one row of the result.
    NF)
parameterspage_content_mappermetadata_mappersource_columnsinclude_rownum_into_metadatainclude_query_into_metadata.)querydbr   r   r   r   r   r   c          	      C   sD   || _ || _|pi | _|p| j| _|p*| j| _|| _|| _|| _	dS )a  
        Args:
            query: The query to execute.
            db: A LangChain `SQLDatabase`, wrapping an SQLAlchemy engine.
            sqlalchemy_kwargs: More keyword arguments for SQLAlchemy's `create_engine`.
            parameters: Optional. Parameters to pass to the query.
            page_content_mapper: Optional. Function to convert a row into a string
              to use as the `page_content` of the document. By default, the loader
              serializes the whole row into a string, including all columns.
            metadata_mapper: Optional. Function to convert a row into a dictionary
              to use as the `metadata` of the document. By default, no columns are
              selected into the metadata dictionary.
            source_columns: Optional. The names of the columns to use as the `source`
              within the metadata dictionary.
            include_rownum_into_metadata: Optional. Whether to include the row number
              into the metadata dictionary. Default: False.
            include_query_into_metadata: Optional. Whether to include the query
              expression into the metadata dictionary. Default: False.
        N)
r   r   r   page_content_default_mapperr   metadata_default_mapperr   r   r   r   )	selfr   r   r   r   r   r   r   r    r   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/sql_database.py__init__   s    
zSQLDatabaseLoader.__init__)returnc                 c   s@  zdd l }W n ty&   tdY n0 t| j|jrd| jj| jd| jd}t| jj	| jj
d}n@t| jtr| jj|| jd| jd}| j}ntd| j t| D ]\}}| |}| |}| jr||d< | jr||d< g }| D ]"\}	}
| jr|	| jv r||
 q|r,d	||d
< t||dV  qd S )Nr   z\Could not import sqlalchemy python package. Please install it with `pip install sqlalchemy`.cursor)fetchr   )bindz)Unable to process query of unknown type: rowr   ,source)page_contentmetadata)Z
sqlalchemyImportError
isinstancer   Z
SelectBaser   Z_executer   strcompileZ_enginetext	TypeError	enumerateZmappingsr   r   r   r   itemsr   appendjoinr   )r   saresultZ	query_sqlir"   r%   r&   Zsource_valuescolumnvaluer   r   r   	lazy_load?   s>    
	


zSQLDatabaseLoader.lazy_load)r"   column_namesr   c                    s0    du rt |   d fdd|  D S )za
        A reasonable default function to convert a record into a "page content" string.
        N
c                 3   s(   | ] \}}| v r| d | V  qdS )z: Nr   ).0r4   r5   r7   r   r   	<genexpr>u   s   z@SQLDatabaseLoader.page_content_default_mapper.<locals>.<genexpr>)listkeysr0   r.   )r"   r7   r   r:   r   r   l   s
    z-SQLDatabaseLoader.page_content_default_mapperc                 C   s6   |du ri S i }|   D ]\}}||v r|||< q|S )za
        A reasonable default function to convert a record into a "metadata" dictionary.
        N)r.   )r"   r7   r&   r4   r5   r   r   r   r   {   s    
z)SQLDatabaseLoader.metadata_default_mapper)N)N)__name__
__module____qualname____doc__r	   r)   r   r   r   r   r   r   r   boolr   r   r   r6   staticmethodr
   r   r   r   r   r   r   r   r      s<   

*-  
r   N)typingr   r   r   r   r   r   r   r	   Zsqlalchemy.enginer
   Zsqlalchemy.sql.expressionr   Z%langchain_community.docstore.documentr   Z)langchain_community.document_loaders.baser   Z*langchain_community.utilities.sql_databaser   r   r   r   r   r   <module>   s   (