
    hN                     J    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	y)    )DictListOptionalcast)Document)
BaseLoaderc                   v    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y)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`.
    Nquerydatabase	read_onlyconfigpage_content_columnsmetadata_columnsc                 `    || _         || _        || _        |xs i | _        || _        || _        y)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          p/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/document_loaders/duckdb_loader.py__init__zDuckDBLoader.__init__   s4    . 
 "l$8! 0    returnc           
         	 dd l }g }|j                  | j                  | j                  | j
                        5 }|j                  | j                        }|j                         }t        t        |j                        }|D cg c]  }|d   	 c}| j                  }n| j                  }| j                  g }	n| j                  }	|D ]]  dj                  fd|D              }
|	D ci c]  }|j                  |          }}t!        |
|      }|j#                  |       _ 	 d d d        |S # t        $ r t        d      w xY wc c}w c c}w # 1 sw Y   |S xY w)Nr   zTCould not import duckdb python package. Please install it with `pip install duckdb`.)r   r   r   
c              3   P   K   | ]  }| d j                  |            yw)z: N)index).0columnfield_namesresults     r   	<genexpr>z$DuckDBLoader.load.<locals>.<genexpr>L   s5      ) hb(9(9&(A!B CD)s   #&)page_contentmetadata)duckdbImportErrorconnectr   r   r   executer   fetchallr   listdescriptionr   r   joinr   r   append)r   r"   docsconquery_resultresultsr(   cr   r   r    r   r!   docr   r   s                 @@r   loadzDuckDBLoader.load/   s   	 ^^]]dnnT[[  
 	!;;tzz2L"++-Gt\%=%=>K)45A1Q45K((0'2$'+'@'@$$$,#% #'#8#8 ! !#yy )"6)   #3 F;#4#4V#<== 
 L8LC !%	!@ O  	? 	 6$1	!@ s6   D8 A
EEAE/E"E8E
EE$)z:memory:FNNN)__name__
__module____qualname____doc__strboolr   r   r   r   r   r1    r   r   r
   r
      s     #+/480411 1 	1
 c3h(1 'tCy11 #49-1<*d8n *r   r
   N)
typingr   r   r   r   langchain_core.documentsr   )langchain_community.document_loaders.baser   r
   r8   r   r   <module>r<      s    - - - @Q: Qr   