o
    ZhN                     @   sD   d dl mZmZmZmZ d dlmZ d dlmZ G dd deZ	dS )    )DictListOptionalcast)Document)
BaseLoaderc                   @   sl   e Zd ZdZ					ddedededeeeef  d	eee  d
eee  fddZ	dee
 fddZdS )DuckDBLoaderaK  Load from `DuckDB`.

    Each document represents one row of the result. The `page_content_columns`
    are written into the `page_content` of the document. The `metadata_columns`
    are written into the `metadata` of the document. By default, all columns
    are written into the `page_content` and none into the `metadata`.
    :memory:FNquerydatabase	read_onlyconfigpage_content_columnsmetadata_columnsc                 C   s,   || _ || _|| _|pi | _|| _|| _dS )aF  

        Args:
            query: The query to execute.
            database: The database to connect to. Defaults to ":memory:".
            read_only: Whether to open the database in read-only mode.
              Defaults to False.
            config: A dictionary of configuration options to pass to the database.
              Optional.
            page_content_columns: The columns to write into the `page_content`
              of the document. Optional.
            metadata_columns: The columns to write into the `metadata` of the document.
              Optional.
        N)r
   r   r   r   r   r   )selfr
   r   r   r   r   r    r   i/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/duckdb_loader.py__init__   s   

zDuckDBLoader.__init__returnc                    s  zdd l }W n ty   tdw g }|j| j| j| jd]}|| j}| }t	t
|j}dd |D  | jd u r? }n| j}| jd u rJg }n| j}|D ]$d fdd|D }	 fd	d
|D }
t|	|
d}|| qOW d    |S 1 sw   Y  |S )Nr   zTCould not import duckdb python package. Please install it with `pip install duckdb`.)r   r   r   c                 S   s   g | ]}|d  qS )r   r   ).0cr   r   r   
<listcomp>?   s    z%DuckDBLoader.load.<locals>.<listcomp>
c                 3   s(    | ]}| d   |  V  qdS )z: Nindexr   columnfield_namesresultr   r   	<genexpr>L   s
    
z$DuckDBLoader.load.<locals>.<genexpr>c                    s   i | ]
}|  | qS r   r   r   r   r   r   
<dictcomp>Q   s    z%DuckDBLoader.load.<locals>.<dictcomp>)page_contentmetadata)duckdbImportErrorconnectr   r   r   executer
   Zfetchallr   listdescriptionr   r   joinr   append)r   r$   docsconZquery_resultresultsr)   r   r   r"   r#   docr   r   r   load/   sH   


  zDuckDBLoader.load)r	   FNNN)__name__
__module____qualname____doc__strboolr   r   r   r   r   r0   r   r   r   r   r      s*    


r   N)
typingr   r   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r   r   r   r   <module>   s    