o
    5ti                     @   sT   d dl mZ d dlmZmZmZ d dlZddlmZ ddl	m
Z
 G dd	 d	e
ZdS )
    )abspath)IOAnyUnionN   )EmptyValueError   )AbstractBinaryTableWriterc                	       s   e Zd ZdZdZedefddZdeddf fdd	Z	d
eddfddZ
ddeeef dededdfddZdeddfddZ  ZS )SqliteTableWriteraq  
    A table writer class for `SQLite <https://www.sqlite.org/index.html>`__ database.

    .. py:method:: write_table()

        Write a table to a `SQLite <https://www.sqlite.org/index.html>`__ database.

        :raises pytablewriter.EmptyTableNameError:
            If the |table_name| is empty.
        :Example:
            :ref:`example-sqlite-table-writer`
    sqlitereturnc                 C   s   | j S )N)FORMAT_NAME)self r   W/home/ubuntu/.local/lib/python3.10/site-packages/pytablewriter/writer/binary/_sqlite.pyformat_name   s   zSqliteTableWriter.format_namekwargsNc                    sR   dd l }dd l}t jdi | d| _d| _d| _d| _d| _|	|j
| _d S )Nr   FTr   )copydatapropertysuper__init__
is_paddingis_formatting_float_use_default_header_is_require_table_name_is_require_headerdeepcopyNOT_QUOTING_FLAGS_quoting_flags)r   r   r   r   	__class__r   r   r      s   zSqliteTableWriter.__init__	file_pathc                 C   s^   ddl m} |  r$| jjt|kr | jjd| jj  dS | 	  ||d| j
d| _dS )zp
        Open a SQLite database file.

        :param str file_path: SQLite database file path to open.
        r   )SimpleSQLitezdatabase already opened: Nw)max_workers)simplesqliter"   	is_openedstreamdatabase_pathr   _loggerloggerdebugcloser$   _stream)r   r!   r"   r   r   r   open.   s   zSqliteTableWriter.openToutputclose_after_writec                 K   s^   t |tstdt| | | z| jdi | W |r%|   dS dS |r.|   w w )a  Write data to the SQLite database file.

        Args:
            output (str):
                path to the output SQLite database file.
            close_after_write (bool, optional):
                Close the output after write.
                Defaults to |True|.
        zoutput must be a str: actual=Nr   )
isinstancestr	TypeErrortyper.   write_tabler,   )r   r/   r0   r   r   r   r   dump@   s   


zSqliteTableWriter.dumpc                 K   sp   z|    W n ty   | jjd Y d S w |   tj| j| j	dd | j
D | j| jd}| j| d S )Nzno tabular data foundc                 S   s   g | ]	}d d |D qS )c                 S   s   g | ]}|j qS r   )data).0value_dpr   r   r   
<listcomp>b   s    z=SqliteTableWriter._write_table.<locals>.<listcomp>.<listcomp>r   )r8   value_dp_listr   r   r   r:   a   s    z2SqliteTableWriter._write_table.<locals>.<listcomp>)
type_hintsr$   )_verify_value_matrixr   r)   r*   r+   _preprocess	tabledata	TableData
table_nameheaders_table_value_dp_matrixr<   r$   r'   create_table_from_tabledata)r   r   
table_datar   r   r   _write_tableU   s"   
zSqliteTableWriter._write_table)T)__name__
__module____qualname____doc__r   propertyr2   r   r   r   r.   r   r   boolr6   rF   __classcell__r   r   r   r   r
   
   s    $r
   )os.pathr   typingr   r   r   r?   errorr   
_interfacer	   r
   r   r   r   r   <module>   s    