U
    HaS+                     @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd deZdS )    N)BaseDatabaseSchemaEditor)IndexColumns)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dZdd Z fddZdd Zdd Zdd Z fddZd) fdd	Z fddZd*ddZd+dd Zd, fd"d#	Zd!d!d$d$d!d%d!d%d!dd!d!d& fd'd(
Z  ZS )-DatabaseSchemaEditorzCREATE SEQUENCE %(sequence)sz,DROP SEQUENCE IF EXISTS %(sequence)s CASCADEz=SELECT setval('%(sequence)s', MAX(%(column)s)) FROM %(table)sz9ALTER SEQUENCE %(sequence)s OWNED BY %(table)s.%(column)szZCREATE 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                 C   s>   t |tr|dd}tj|}t|dr2d|_| 	 S )N%z%%encodingutf8)

isinstancestrreplacepsycopg2
extensionsZadapthasattrr   Z	getquoteddecode)selfvalueZadapted r   H/tmp/pip-unpacked-wheel-3jxiddxt/django/db/backends/postgresql/schema.pyquote_value&   s    

z DatabaseSchemaEditor.quote_valuec                    s0   t  ||}| ||}|d k	r,|| |S N)super_field_indexes_sql_create_like_index_sqlappend)r   modelfieldoutputlike_index_statement	__class__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_type
connection
data_typesgetget_internal_typedb_typer   r   r   r   r   _field_data_type6   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(   >   s    z+DatabaseSchemaEditor._field_base_data_typesc                 C   sr   |j | jd}|dk	rn|js"|jrnd|kr.dS |drN| j||gddgdS |drn| 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[varchar_likeZvarchar_pattern_ops)fieldssuffix	opclassestextZtext_pattern_ops)r$   r    db_indexunique
startswith_create_index_sql)r   r   r   r$   r   r   r   r   E   s&    

z+DatabaseSchemaEditor._create_like_index_sqlc                    s  d| _ d}| }| }|dkrV||krVt| |t| |krx|  j |7  _ n"| || |krx|  j |7  _ t|jj}dddd}	| |	krt|j	}
d||
f }| j | 
|
|	|  d	 g f| jd
| 
|i g f| jd
| 
|i g f| j| 
|| j| 
|
d| 
| d d g f| j| 
|| 
|
| 
|d g f| j| 
|| 
|
| 
|d g fgfS |j| jdd |	krt|j	}
d||
f }t ||||\}}|| jd
| 
|i g fgfS t ||||S d S )Nz%ALTER COLUMN %(column)s TYPE %(type)sz USING %(column)s::%(type)sr'   ZbigintintegerZsmallint)Z	bigserialserialZsmallserialz	%s_%s_seq)columntypesequenceznextval('%s'))r7   default)tablechanges)r;   r7   r9   r)   r8   )Zsql_alter_column_typer#   listr(   r&   r   _metadb_tablelowerr7   
quote_namesql_delete_sequencesql_create_sequenceZsql_alter_columnZsql_alter_column_defaultsql_set_sequence_maxsql_set_sequence_ownerZdb_parametersr    r   _alter_column_type_sql)r   r   	old_field	new_fieldnew_typeZ	using_sqlZnew_internal_typeZold_internal_typer;   Zserial_fields_mapr7   Zsequence_namefragment_r   r   r   rF   e   s    

	  	1
 	z+DatabaseSchemaEditor._alter_column_type_sqlFc	              
      s  |j s|jrr|dr |drH|dr4|drH|drr|dsr| j|jj|jgdd}	| | ||	 t	 
|||||||| |j s|js|j s|js|jr| ||}
|
d k	r| |
 |jr
|j s
|js
| j|jj|jgdd}| | || d S )Nr+   r0   Zcitextr,   )r.   )r1   r2   r3   Z_create_index_namer>   r?   r7   execute_delete_index_sqlr   _alter_fieldr   )r   r   rG   rH   Zold_typerI   Zold_db_paramsZnew_db_paramsstrictZ
index_namer   Zindex_to_remover   r   r   rN      sH          
z!DatabaseSchemaEditor._alter_fieldc                    s*   |rt ||| j||dS t ||||S )N)col_suffixesr/   )r   rA   r   _index_columns)r   r;   columnsrP   r/   r   r   r   rQ      s    z#DatabaseSchemaEditor._index_columnsc                 C   s   | j |j|| |dd d d S )Nconcurrently)params)rL   Z
create_sqlr   r   indexrT   r   r   r   	add_index   s    zDatabaseSchemaEditor.add_indexc                 C   s   |  |j|| |d d S )NrS   )rL   Z
remove_sqlrV   r   r   r   remove_index   s    z!DatabaseSchemaEditor.remove_indexNc                    s    |r
| j n| j}t |||S r   )sql_delete_index_concurrentlysql_delete_indexr   rM   )r   r   namesqlrT   r   r   r   rM      s    z&DatabaseSchemaEditor._delete_index_sql r   )r-   r\   r.   usingdb_tablespacerP   r]   r/   	conditionrT   includeexpressionsc                   s4   |s
| j n| j}t j|||||||||	|
||dS )N)r-   r\   r.   r_   r`   rP   r]   r/   ra   rb   rc   )sql_create_indexsql_create_index_concurrentlyr   r4   )r   r   r-   r\   r.   r_   r`   rP   r]   r/   ra   rT   rb   rc   r   r   r   r4      s            z&DatabaseSchemaEditor._create_index_sql)F)F)F)NF)__name__
__module____qualname__rC   rB   rD   rE   rd   re   r[   rZ   Zsql_create_column_inline_fkZsql_delete_fkZsql_delete_procedurer   r   r&   r(   r   rF   rN   rQ   rX   rY   rM   r4   __classcell__r   r   r   r   r      sN   	 T 

         r   )r   Zdjango.db.backends.base.schemar   Z!django.db.backends.ddl_referencesr   Zdjango.db.backends.utilsr   r   r   r   r   r   <module>   s   