a
    g	g                     @  sj  d dl mZ d dl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 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 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 er: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  d dlm!Z! d dlm"Z" d dl#m$Z$ ddl%m&Z& G dd deZ'eedd d!d"d#d$d%Z(dS )&    )annotationsN)Any)Dict)Optional)TYPE_CHECKING)Union)cast)JSON)schema)sql)compiles   )alter_table)format_table_name)RenameTable)DefaultImpl   )util)	Inspector)DDLCompiler)Cast)ClauseElement)Column)
Constraint)Table)
TypeEngine)BatchOperationsImplc                      s   e Zd ZdZdZdddddZdd	d
dZdd	ddZddddddddZdddddZ	dddddddZ
d)dddd fd d!Zdd"d#dd$d%d&Zd'd( Z  ZS )*
SQLiteImplsqliteFr   bool)batch_opreturnc                 C  s~   |j D ]r}|d dkrf|d d }t|jtjrFt|jjtjrF dS t|jtj	j
rx|jjrx dS q|d dvr dS qdS )zReturn True if the given :class:`.BatchOperationsImpl`
        would need the table to be recreated and copied in order to
        proceed.

        Normally, only returns True on SQLite when operations other
        than add_column are present.

        r   Z
add_columnr   T)Zcreate_indexZ
drop_indexFN)batch
isinstanceZserver_defaultr
   ZDefaultClauseargr   r   r   Zsqla_compatZComputedZ	persisted)selfr    opcol r(   U/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/ddl/sqlite.pyrequires_recreate_in_batch+   s"    
z%SQLiteImpl.requires_recreate_in_batchr   )constc                 C  s,   |j d u rtdn| | r(td d S )NNo support for ALTER of constraints in SQLite dialect. Please refer to the batch mode feature which allows for SQLite migrations using a copy-and-move strategy.zSkipping unsupported ALTER for creation of implicit constraint. Please refer to the batch mode feature which allows for SQLite migrations using a copy-and-move strategy.)_create_ruleNotImplementedErrorr   warnr%   r+   r(   r(   r)   add_constraintG   s    

zSQLiteImpl.add_constraintc                 C  s   |j d u rtdd S )Nr,   )r-   r.   r0   r(   r(   r)   drop_constraintX   s    
zSQLiteImpl.drop_constraintr   zOptional[str])inspector_columnmetadata_columnrendered_metadata_defaultrendered_inspector_defaultr!   c                 C  sP   |d ur$t dd|}t dd|}|d urHt dd|}t dd|}||kS )Nz
^\((.+)\)$z\1z^\"?'(.+)'\"?$)resub)r%   r3   r4   r5   r6   r(   r(   r)   compare_server_default`   s    z!SQLiteImpl.compare_server_default)exprr!   c                 C  s@   |sdS t d|rdS t d|r(dS t d|r8dS dS dS )a  Determine if a server default is a SQL expression or a constant.

        There are too many assertions that expect server defaults to round-trip
        identically without parenthesis added so we will add parens only in
        very specific cases.

        Fz	^[0-9\.]$z^'.+'$z^\(.+\)$TN)r7   match)r%   r:   r(   r(   r)   -_guess_if_default_is_unparenthesized_sql_expr|   s    
z8SQLiteImpl._guess_if_default_is_unparenthesized_sql_exprr   r   zDict[str, Any]None)	inspectortablecolumn_infor!   c                 C  s(   |  |dd r$d|d f |d< d S )Ndefault(%s))r<   get)r%   r>   r?   r@   r(   r(   r)   autogen_column_reflect   s    
z!SQLiteImpl.autogen_column_reflectr   str)r:   is_server_defaultr!   c                   s4   t  j|fd|i|}|r0| |r0d|f }|S )NrF   rB   )superrender_ddl_sql_exprr<   )r%   r:   rF   kwZstr_expr	__class__r(   r)   rH      s    
zSQLiteImpl.render_ddl_sql_exprz"Dict[str, Union[TypeEngine, Cast]]r   )existingexisting_transfernew_typer!   c                 C  s.   |j j|jur*t|ts*t|d ||d< d S )Nr:   )typeZ_type_affinityr#   r	   r   )r%   rL   rM   rN   r(   r(   r)   cast_for_batch_migrate   s    z!SQLiteImpl.cast_for_batch_migratec                 C  s   |  || d S )N)Z_skip_functional_indexes)r%   Zconn_unique_constraintsZconn_indexesZmetadata_unique_constraintsZmetadata_indexesr(   r(   r)   correct_for_autogen_constraints   s    z*SQLiteImpl.correct_for_autogen_constraints)F)__name__
__module____qualname__Z__dialect__Ztransactional_ddlr*   r1   r2   r9   r<   rD   rH   rP   rQ   __classcell__r(   r(   rJ   r)   r   #   s    r   r   r   r   rE   )elementcompilerr!   c                 K  s"   dt || j| jt|| jd f S )Nz%s RENAME TO %s)r   Z
table_namer
   r   Znew_table_name)rV   rW   rI   r(   r(   r)   visit_rename_table   s    rX   ))
__future__r   r7   typingr   r   r   r   r   Z
sqlalchemyr   r	   r
   r   Zsqlalchemy.ext.compilerr   baser   r   r   implr    r   Zsqlalchemy.engine.reflectionr   Zsqlalchemy.sql.compilerr   Zsqlalchemy.sql.elementsr   r   Zsqlalchemy.sql.schemar   r   r   Zsqlalchemy.sql.type_apir   Zoperations.batchr   r   rX   r(   r(   r(   r)   <module>   s<    &