
    dh                     ~    S SK JrJrJrJrJrJrJrJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S SKJr   " S S\5      rg	)
    )AnyCallableDictIteratorListOptionalSequenceUnion)
RowMapping)Select)Document)
BaseLoader)SQLDatabasec                   &   \ rS rSrSrSSSSSSS.S\\\4   S\S\	\
\\4      S	\	\S
\4      S\	\S
\
\\4   4      S\	\\      S\S\4S jjrS\\   4S jr\ SS\S\	\\      S\4S jj5       r\ SS\S\	\\      S\
\\4   4S jj5       rSrg)SQLDatabaseLoader   z
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                    Xl         X l        U=(       d    0 U l        U=(       d    U R                  U l        U=(       d    U R
                  U l        X`l        Xpl        Xl	        g)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	            i/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/sql_database.py__init__SQLDatabaseLoader.__init__   sW    > 
!$*C4#C#C 	   /N$2N2N,,H)+F(    returnc              #   *  #     SS K n[        U R                  UR                  5      (       af  U R
                  R                  U R                  SU R                  S9n[        U R                  R                  U R
                  R                  S95      nO[        U R                  [        5      (       aK  U R
                  R                  UR                  U R                  5      SU R                  S9nU R                  nO[        SU R                   35      e[        UR                  5       5       H  u  pEU R                  U5      nU R!                  U5      nU R"                  (       a  XGS'   U R$                  (       a  X7S'   / nUR'                  5        H:  u  pU R(                  (       d  M  XR(                  ;   d  M)  UR+                  U
5        M<     U(       a  S	R-                  U5      US
'   [/        XgS9v   M     g ! [         a    [        S5      ef = f7f)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_loadSQLDatabaseLoader.lazy_load?   s    	# djj"--00WW%%

(t & F DJJ..DGGOO.DEI

C((WW%%

#8 & F 

IG

|TUU   12FA33C8L++C0H00"#//$-!M!$&&&65H5H+H!((/ "- %(XXm%<"HH! 31  	C 	s)   HG: FH)H:A H:HHr(   column_namesc                    ^ Tc  [        U R                  5       5      mSR                  U4S jU R                  5        5       5      $ )zQ
A reasonable default function to convert a record into a "page content" string.

c              3   D   >#    U H  u  pUT;   d  M  U S U 3v   M     g7f)z: N ).0rA   rB   rE   s      r   	<genexpr>@SQLDatabaseLoader.page_content_default_mapper.<locals>.<genexpr>u   s/      
!,% !vhb !,s     )listkeysr;   r9   )r(   rE   s    `r   r   -SQLDatabaseLoader.page_content_default_mapperl   s?     
+Lyy 
!$
 
 	
r"   c                 \    Uc  0 $ 0 nU R                  5        H  u  p4X1;   d  M  XBU'   M     U$ )zQ
A reasonable default function to convert a record into a "metadata" dictionary.
)r9   )r(   rE   r,   rA   rB   s        r   r   )SQLDatabaseLoader.metadata_default_mapper{   s;     I#% YY[MF%#(  ) r"   )r   r   r   r   r   r   r   r   )N)__name__
__module____qualname____firstlineno____doc__r
   r2   r   r   r   r   r   r   r	   boolr    r   r   rC   staticmethodr   r   r   r   __static_attributes__rI   r"   r   r   r      sH    04<@CG26-2,1(GS&[!(G (G
 T#s(^,(G &hsCx&89(G "(3S#X+>"?@(G !#/(G '+(G &*(GT+I8H- +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   rI   r"   r   <module>r`      s+    Q Q Q ( , : @ B~
 ~r"   