o
    Zh                     @   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deeef dedee	ee
f  deed	ef  d
eed	e	ee
f f  deee  dedefddZdee fddZe	ddedeee  defddZe	ddedeee  de	ee
f f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   h/var/www/html/lang_env/lib/python3.10/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w t| j|jr1| jj| jd| jd}t| jj	| jj
d}n t| jtrI| jj|| jd| jd}| j}ntd| j t| D ]D\}}| |}| |}| jrl||d< | jrs||d< g }| D ]\}	}
| jr|	| jv r||
 qy|rd	||d
< t||dV  qWd 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?   sF   




zSQLDatabaseLoader.lazy_loadr"   column_namesc                    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)__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      sX    

	

*-


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   ( 