
    Ng                         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e          Zd	S )
    )AnyCallableDictIteratorListOptionalSequenceUnion)
RowMapping)Select)Document)
BaseLoader)SQLDatabasec                   ~   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Zdee         fdZe	 ddedeee                  defd            Ze	 ddedeee                  de	ee
f         f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                    || _         || _        |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   s	            m/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/sql_database.py__init__zSQLDatabaseLoader.__init__   sa    > 
!$*C4#C 	   /N$2N,,H)+F(((    returnc              #     K   	 dd l }n# t          $ r t          d          w xY wt          | j        |j                  rZ| j                            | j        d| j                  }t          | j        	                    | j        j
                            }nst          | j        t                    rB| j                            |                    | j                  d| j                  }| j        }nt          d| j                   t          |                                          D ]\  }}|                     |          }|                     |          }| j        r||d<   | j        r||d<   g }|                                D ]*\  }	}
| j        r|	| j        v r|                    |
           +|rd	                    |          |d
<   t/          ||          V  d 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)
sqlalchemyImportError
isinstancer   
SelectBaser   _executer   strcompile_enginetext	TypeError	enumeratemappingsr   r   r   r   itemsr   appendjoinr   )r   saresult	query_sqlir&   r)   r*   source_valuescolumnvalues              r   	lazy_loadzSQLDatabaseLoader.lazy_load?   s,     	##### 	 	 	C  	 dj"-00 	VW%%
(t &  F DJ..DGO.DDEEII
C(( 	VW%%
##8 &  F 
IIT
TTUUU   1 122 	I 	IFAs33C88L++C00H0 $"#/ .$-!M!$ 0 0& 06T5H+H+H!((/// =%(XXm%<%<"xHHHHHHH!	I 	Is   	 #r&   column_namesc                     !t          |                                           d                    fd|                                 D                       S )za
        A reasonable default function to convert a record into a "page content" string.
        N
c              3   4   K   | ]\  }}|v 	| d | V  dS )z: N ).0r?   r@   rB   s      r   	<genexpr>z@SQLDatabaseLoader.page_content_default_mapper.<locals>.<genexpr>u   sL       
 
%%     %%%%
 
r    )listkeysr9   r7   )r&   rB   s    `r   r   z-SQLDatabaseLoader.page_content_default_mapperl   sd     

++Lyy 
 
 
 
!$
 
 
 
 
 	
r    c                 X    |i S i }|                                  D ]\  }}||v r|||<   |S )za
        A reasonable default function to convert a record into a "metadata" dictionary.
        )r7   )r&   rB   r*   r?   r@   s        r   r   z)SQLDatabaseLoader.metadata_default_mapper{   sJ     I#% YY[[ 	) 	)MFE%%#( r    )N)__name__
__module____qualname____doc__r
   r0   r   r   r   r   r   r   r	   boolr   r   r   rA   staticmethodr   r   r   r   rF   r    r   r   r      s         04<@CG26-2,1(G (G (GS&[!(G (G
 T#s(^,(G &hsCx&89(G "(3S#X+>"?@(G !#/(G '+(G &*(G (G (G (GT+I8H- +I +I +I +IZ =A
 

'/S	':
	
 
 
 \
 =A '/S	':	c3h   \  r    r   N)typingr   r   r   r   r   r   r	   r
   sqlalchemy.enginer   sqlalchemy.sql.expressionr   %langchain_community.docstore.documentr   )langchain_community.document_loaders.baser   *langchain_community.utilities.sql_databaser   r   rF   r    r   <module>rX      s    Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q ( ( ( ( ( ( , , , , , , : : : : : : @ @ @ @ @ @ B B B B B B~ ~ ~ ~ ~
 ~ ~ ~ ~ ~r    