B
    CVvgS+                 @   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-install-o3oxmrkh/Django/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 )N)Zis_relationZrel_db_type
connectionZ
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   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'))r2   default)tableZchanges)r6   r2   r4   )r   r3   )Zsql_alter_column_typer    listr$   r"   r   _metadb_tablelowerr2   
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_typer6   Zserial_fields_mapr2   Zsequence_namefragment_)r   r   r   r@   e   sb    



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&   r+   Zcitextr'   )r)   )r,   r-   r.   Z_create_index_namer8   r9   r2   execute_delete_index_sqlr   _alter_fieldr   )r   r   rA   rB   Zold_typerC   Zold_db_paramsZnew_db_paramsstrictZ
index_namer   Zindex_to_remove)r   r   r   rH      s"    
z!DatabaseSchemaEditor._alter_fieldc                s*   |rt ||| j||dS t ||||S )N)col_suffixesr*   )r   r;   r   _index_columns)r   r6   columnsrJ   r*   )r   r   r   rK      s    z#DatabaseSchemaEditor._index_columnsc             C   s   | j |j|| |dd d d S )N)concurrently)params)rF   Z
create_sql)r   r   indexrM   r   r   r   	add_index   s    zDatabaseSchemaEditor.add_indexc             C   s   |  |j|| |d d S )N)rM   )rF   Z
remove_sql)r   r   rO   rM   r   r   r   remove_index   s    z!DatabaseSchemaEditor.remove_indexNc                s    |r
| j n| j}t |||S )N)sql_delete_index_concurrentlysql_delete_indexr   rG   )r   r   namesqlrM   )r   r   r   rG      s    z&DatabaseSchemaEditor._delete_index_sql r   )r(   rT   r)   usingdb_tablespacerJ   rU   r*   	conditionrM   includeexpressionsc               s4   |s
| j n| j}t j|||||||||	|
||dS )N)r(   rT   r)   rW   rX   rJ   rU   r*   rY   rZ   r[   )sql_create_indexsql_create_index_concurrentlyr   r/   )r   r   r(   rT   r)   rW   rX   rJ   rU   r*   rY   rM   rZ   r[   )r   r   r   r/      s    
z&DatabaseSchemaEditor._create_index_sql)F)F)F)NF)__name__
__module____qualname__r=   r<   r>   r?   r\   r]   rS   rR   Zsql_create_column_inline_fkZsql_delete_fkZsql_delete_procedurer   r   r"   r$   r   r@   rH   rK   rP   rQ   rG   r/   __classcell__r   r   )r   r   r      s2   	 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   