
    h                     ~    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y	)
    )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y)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                    || _         || _        |xs i | _        |xs | j                  | _        |xs | j
                  | _        || _        || _        || _	        y)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	            o/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/document_loaders/sql_database.py__init__zSQLDatabaseLoader.__init__   sa    > 
!$*C4#C#C 	   /N$2N2N,,H)+F(    returnc              #      K   	 dd l }t        | j                  |j                        rl| j
                  j                  | j                  d| j                        }t        | j                  j                  | j
                  j                              }nt        | j                  t              rN| j
                  j                  |j                  | j                        d| j                        }| j                  }nt        d| j                         t        |j                               D ]  \  }}| j                  |      }| j!                  |      }| j"                  r||d<   | j$                  r||d<   g }|j'                         D ]2  \  }	}
| j(                  s|	| j(                  v s"|j+                  |
       4 |rd	j-                  |      |d
<   t/        ||        y # t        $ r t        d      w xY ww)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    	# djj"--0WW%%

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

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

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

IG

|TUU   12 	IFAs33C8L++C0H00"#//$-!M!$ 0&&6T5H5H+H!((/0 %(XXm%<"xHH!	I1  	C 	s(   G>G& FG>G>+;G>&G;;G>r&   column_namesc                     t        | j                               dj                  fd| j                         D              S )za
        A reasonable default function to convert a record into a "page content" string.
        
c              3   :   K   | ]  \  }}|v r	| d |   yw)z: N ).0r?   r@   rB   s      r   	<genexpr>z@SQLDatabaseLoader.page_content_default_mapper.<locals>.<genexpr>u   s0      
% hb 
s   )listkeysr9   r7   )r&   rB   s    `r   r   z-SQLDatabaseLoader.page_content_default_mapperl   s?     
+Lyy 
!$
 
 	
r    c                 V    |i S i }| j                         D ]  \  }}||v s|||<    |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{   sF     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      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   rF   r    r   <module>rX      s+    Q Q Q ( , : @ B~
 ~r    