U
    f8                     @   sD   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dS )    )BaseDatabaseSchemaEditor)IndexColumns)sql)strip_quotesc                       s  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zd2 fdd	ZdZdZdd Z fddZdd Zdd Zdd Zdd Zdd Z fddZd d! Zd3 fd#d$	Z fd%d&Zd4d'd(Zd5d)d*Zd6 fd,d-	Zd+d+d.d.d+dd+dd+d"d+d+d/ fd0d1
Z  ZS )7DatabaseSchemaEditorzeUPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULL; SET CONSTRAINTS ALL IMMEDIATEz1ALTER SEQUENCE IF EXISTS %(sequence)s AS %(type)sz,DROP SEQUENCE IF EXISTS %(sequence)s CASCADEzZCREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(include)s%(extra)s%(condition)szgCREATE INDEX CONCURRENTLY %(name)s ON %(table)s%(using)s (%(columns)s)%(include)s%(extra)s%(condition)szDROP INDEX IF EXISTS %(name)sz*DROP INDEX CONCURRENTLY IF EXISTS %(name)szyCONSTRAINT %(name)s REFERENCES %(to_table)s(%(to_column)s)%(deferrable)s; SET CONSTRAINTS %(namespace)s%(name)s IMMEDIATEzRSET CONSTRAINTS %(name)s IMMEDIATE; ALTER TABLE %(table)s DROP CONSTRAINT %(name)sz,DROP FUNCTION %(procedure)s(%(param_types)s) c                    s8   |d krt  ||S | jjt||}t  |d S N)superexecute
connectionopsZcompose_sqlstr)selfr   params	__class__r   H/tmp/pip-unpacked-wheel-_jot26k8/django/db/backends/postgresql/schema.pyr
   *   s    zDatabaseSchemaEditor.executezRALTER TABLE %(table)s ALTER COLUMN %(column)s ADD GENERATED BY DEFAULT AS IDENTITYzEALTER TABLE %(table)s ALTER COLUMN %(column)s DROP IDENTITY IF EXISTSc                 C   s&   t |tr|dd}t|| jjS )N%z%%)
isinstancer   replacer   quoter   )r   valuer   r   r   quote_value:   s    
z DatabaseSchemaEditor.quote_valuec                    s0   t  ||}| ||}|d k	r,|| |S r   )r	   _field_indexes_sql_create_like_index_sqlappend)r   modelfieldoutputlike_index_statementr   r   r   r   ?   s
    
z'DatabaseSchemaEditor._field_indexes_sqlc                 C   s.   |j r|| jS | jj| || jS r   )Zis_relationZrel_db_typer   
data_typesgetget_internal_typedb_typer   r   r   r   r   _field_data_typeF   s    
z%DatabaseSchemaEditor._field_data_typec                 c   s4   |j  dkr"| |j E d H  n| |j V  d S )N
ArrayField)Z
base_fieldr"   _field_base_data_typesr%   r$   r   r   r   r'   N   s    z+DatabaseSchemaEditor._field_base_data_typesc                 C   s   |j | jd}|dk	r~|js"|jr~d|kr.dS t|ddr>dS |dr^| j||gddgdS |d	r~| j||gdd
gdS dS )z
        Return the statement to create an index with varchar operator pattern
        when the column type is 'varchar' or 'text', otherwise return None.
        r   N[Zdb_collationvarchar_likeZvarchar_pattern_ops)fieldssuffix	opclassestextZtext_pattern_ops)r#   r   db_indexuniquegetattr
startswith_create_index_sql)r   r   r   r#   r   r   r   r   U   s*    

z+DatabaseSchemaEditor._create_like_index_sqlc                 C   sb   d}|  }|  }|dkrF||krFt| |t| |kr^|S n| || |kr^|S dS )Nz USING %(column)s::%(type)sr&    )r"   listr'   r%   )r   	new_field	old_field	using_sqlnew_internal_typeold_internal_typer   r   r   
_using_sqly   s    zDatabaseSchemaEditor._using_sqlc              
   C   sT   | j  @}| j j||D ](}|d |kr|d   W  5 Q R  S qW 5 Q R X d S )Ncolumnname)r   cursorZintrospectionZget_sequences)r   tabler=   r?   sequencer   r   r   _get_sequence_name   s
    $z'DatabaseSchemaEditor._get_sequence_namec                    sd  |j | jd}|d }|js"|jr|dr6|dr^|drJ|dr^|dr|ds| j|jj|jgdd}	| 	| 
||	 d| _| || }
r|  j|
7  _| }| }t|jj}d	d
dh}||k}||k}|r6|s6t|j}| j| ||dd g f| j| || |d g fgfS |r|s| 	| j| || t|jd  t|j}t ||||||\}}g }| || }r| jd| |i g fg}||fS |rJ|rJ||krJt ||||||\}}t|j}dddd}g }| || }rB| j| ||| d g fg}||fS t ||||||S d S )Nr(   typer*   r/   Zcitextr+   r-   z2ALTER COLUMN %(column)s TYPE %(type)s%(collation)s	AutoFieldBigAutoFieldSmallAutoFieldr5   r=   rC   Z	collation)r@   r=   rA   integerZbigintZsmallint)rE   rF   rG   )rA   rC   )Zdb_parametersr   r0   r1   r3   _create_index_name_metadb_tabler=   r
   _delete_index_sqlZsql_alter_column_typer<   r"   r   
quote_namesql_add_identitysql_drop_indentityr	   _alter_column_type_sqlrB   sql_delete_sequencesql_alter_sequence_type)r   r   r8   r7   new_typeZold_collationnew_collationold_db_paramsold_typeZ
index_namer9   r:   r;   r@   Zauto_field_typesZold_is_autoZnew_is_autor=   fragment_Zother_actionsZsequence_nameZdb_typesr   r   r   rQ      s      

      	     

     z+DatabaseSchemaEditor._alter_column_type_sqlc                 C   sL   | j }| || }r||7 }|| |j||r>d| | ndd g fS )N r5   rH   )Zsql_alter_column_collater<   rN   r=   Z_collate_sql)r   r   r7   rT   rU   r8   r   r9   r   r   r   _alter_column_collation_sql  s    
z0DatabaseSchemaEditor._alter_column_collation_sqlFc	              
      s   t  |||||||| |js,|js,|js8|jsV|jrV| ||}	|	d k	rV| |	 |jr|js|js| j|jj|j	gdd}
| | 
||
 d S )Nr+   rD   )r	   _alter_fieldr0   r1   r   r
   rJ   rK   rL   r=   rM   )r   r   r8   r7   rW   rT   rV   Znew_db_paramsstrictr   Zindex_to_remover   r   r   r\     s2    
  z!DatabaseSchemaEditor._alter_fieldc                    s*   |rt ||| j||dS t ||||S )N)col_suffixesr.   )r   rN   r	   _index_columns)r   r@   columnsr^   r.   r   r   r   r_   8  s    z#DatabaseSchemaEditor._index_columnsc                 C   s   | j |j|| |dd d d S )Nconcurrently)r   )r
   Z
create_sqlr   r   indexrb   r   r   r   	add_indexC  s     zDatabaseSchemaEditor.add_indexc                 C   s   |  |j|| |d d S )Nra   )r
   Z
remove_sqlrc   r   r   r   remove_indexH  s    z!DatabaseSchemaEditor.remove_indexNc                    s    |r
| j n| j}t |||S r   )sql_delete_index_concurrentlysql_delete_indexr	   rM   )r   r   r>   r   rb   r   r   r   rM   K  s
    z&DatabaseSchemaEditor._delete_index_sqlr5   )r,   r>   r-   usingdb_tablespacer^   r   r.   	conditionrb   includeexpressionsc                   s8   |p|s| j n| j}t j|||||||||	|
||dS )N)r,   r>   r-   ri   rj   r^   r   r.   rk   rl   rm   )sql_create_indexsql_create_index_concurrentlyr	   r4   )r   r   r,   r>   r-   ri   rj   r^   r   r.   rk   rb   rl   rm   r   r   r   r4   S  s&    z&DatabaseSchemaEditor._create_index_sql)r   )F)F)F)NF) __name__
__module____qualname__Zsql_update_with_defaultrS   rR   rn   ro   rh   rg   Zsql_create_column_inline_fkZsql_delete_fkZsql_delete_procedurer
   rO   rP   r   r   r%   r'   r   r<   rB   rQ   r[   r\   r_   re   rf   rM   r4   __classcell__r   r   r   r   r      s`   	$s $

r   N)	Zdjango.db.backends.base.schemar   Z!django.db.backends.ddl_referencesr   Z)django.db.backends.postgresql.psycopg_anyr   Zdjango.db.backends.utilsr   r   r   r   r   r   <module>   s   