U
    Y]h,                     @  s  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 ddlmZ ddlmZ erRd 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*eed!d(d#d$d%d)d*Z+dS )+    )annotationsN)Any)Dict)Optional)TYPE_CHECKING)Union)cast)JSON)schema)sql   )alter_table)
ColumnName)format_column_name)format_table_name)RenameTable)DefaultImpl   )util)compiles)	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k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*   6/tmp/pip-unpacked-wheel-u4jwnyja/alembic/ddl/sqlite.pyrequires_recreate_in_batch0   s$    
 z%SQLiteImpl.requires_recreate_in_batchr   )constc                 C  s,   |j d k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_constraintL   s    

zSQLiteImpl.add_constraintc                 C  s   |j d krtdd S )Nr.   )r/   r0   r2   r*   r*   r+   drop_constraint]   s    
zSQLiteImpl.drop_constraintzColumn[Any]zOptional[str])inspector_columnmetadata_columnrendered_metadata_defaultrendered_inspector_defaultr#   c                 C  sP   |d k	r$t dd|}t dd|}|d k	rHt dd|}t dd|}||kS )Nz
^\((.+)\)$z\1z^\"?'(.+)'\"?$)resub)r'   r5   r6   r7   r8   r*   r*   r+   compare_server_defaulte   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)r9   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@   rA   rB   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 )NrH   rD   )superrender_ddl_sql_exprr>   )r'   r<   rH   kwZstr_expr	__class__r*   r+   rJ      s    
zSQLiteImpl.render_ddl_sql_exprz"Dict[str, Union[TypeEngine, Cast]]r   )existingexisting_transfernew_typer#   c                 C  s.   |j j|jk	r*t|ts*t|d ||d< d S )Nr<   )typeZ_type_affinityr%   r	   r   )r'   rN   rO   rP   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,   r3   r4   r;   r>   rF   rJ   rR   rS   __classcell__r*   r*   rL   r+   r   (   s    r   r    r   r   rG   )elementcompilerr#   c                 K  s"   dt || j| jt|| jd f S )Nz%s RENAME TO %s)r   
table_namer
   r   Znew_table_namerX   rY   rK   r*   r*   r+   visit_rename_table   s    r\   r   c                 K  s*   dt || j| jt|| jt|| jf S )Nz%s RENAME COLUMN %s TO %s)r   rZ   r
   r   Zcolumn_nameZnewnamer[   r*   r*   r+   visit_column_name   s
    

r]   ),
__future__r   r9   typingr   r   r   r   r   Z
sqlalchemyr   r	   r
   r   baser   r   r   r   r   implr    r   Zutil.sqla_compatr   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   r\   r]   r*   r*   r*   r+   <module>   sD    #	