
    NgN                     R    d dl mZmZmZmZ d dlmZ d dlmZ  G d de          Z	dS )    )DictListOptionalcast)Document)
BaseLoaderc                       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Z	dee
         f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                 ^    || _         || _        || _        |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   s          n/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/duckdb_loader.py__init__zDuckDBLoader.__init__   s:    . 
 "l$8! 0    returnc                 x   	 dd l }n# t          $ r t          d          w xY wg }|                    | j        | j        | j                  5 }|                    | j                  }|                                }t          t          |j                  }d |D             | j        }n| j        }| j        g }n| j        }|D ]Yd                    fd|D                       }	fd|D             }
t          |	|
          }|                    |           Z	 d d d            n# 1 swxY w Y   |S )	Nr   zTCould not import duckdb python package. Please install it with `pip install duckdb`.)r   r   r   c                     g | ]
}|d          S )r    ).0cs     r   
<listcomp>z%DuckDBLoader.load.<locals>.<listcomp>?   s    555A1Q4555r   
c              3   X   K   | ]$}| d                      |                    V  %dS )z: Nindexr   columnfield_namesresults     r   	<genexpr>z$DuckDBLoader.load.<locals>.<genexpr>L   sX       ) ) DD(9(9&(A(A!BDD) ) ) ) ) )r   c                 H    i | ]}|                     |                   S r   r    r"   s     r   
<dictcomp>z%DuckDBLoader.load.<locals>.<dictcomp>Q   s>        F;#4#4V#<#<=  r   )page_contentmetadata)duckdbImportErrorconnectr   r   r   executer   fetchallr   listdescriptionr   r   joinr   append)r   r+   docsconquery_resultresultsr1   r   r   r)   r*   docr$   r%   s               @@r   loadzDuckDBLoader.load/   s   	MMMM 	 	 	?  	 ^^]dnT[  
 
 	!;;tz22L"++--Gt\%=>>K55555K(0'2$$'+'@$$,#%  #'#8 ! ! !#yy ) ) ) ) )"6) ) )    
    "2  
 L8LLLC    !%	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!@ s   	 #CD//D36D3)r   FNNN)__name__
__module____qualname____doc__strboolr   r   r   r   r   r9   r   r   r   r
   r
      s          #+/48041 11 1 	1
 c3h(1 'tCy11 #49-1 1 1 1<*d8n * * * * * *r   r
   N)
typingr   r   r   r   langchain_core.documentsr   )langchain_community.document_loaders.baser   r
   r   r   r   <module>rC      s    - - - - - - - - - - - - - - - - - - @ @ @ @ @ @Q Q Q Q Q: Q Q Q Q Qr   