3
u|eS+                 @   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!f d!f 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|jdd}tjj|}t|dr2d|_|j j	 S )N%z%%encodingutf8)

isinstancestrreplacepsycopg2
extensionsZadapthasattrr   Z	getquoteddecode)selfvalueZadapted r   h/var/www/html/StaffProfile/staffvenv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.pyquote_value&   s    

z DatabaseSchemaEditor.quote_valuec                s0   t  j||}| j||}|d k	r,|j| |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| jS | jjj|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 j dkr"| j|j E d H  n| j|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 |jdrN| j||gddgdS |j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}|j }|j }|dkrV||krVt| j|t| j|krx|  j |7  _ n"| j|| j|krx|  j |7  _ t|jj}dddd}	|j |	krt|j	}
d||
f }| j | j
|
|	|j  d	 g f| jd
| j
|i g f| jd
| j
|i g f| j| j
|| j| j
|
d| j
| d d g f| j| j
|| j
|
| j
|d g f| j| j
|| j
|
| j
|d g fgfS |j| jdd |	krt|j	}
d||
f }t j||||\}}|| jd
| j
|i g fgfS t j||||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)table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   rA   e   sb    



z+DatabaseSchemaEditor._alter_column_type_sqlFc	          	      s  |j s|jrx|jdr"|jd sN|jdr8|jd sN|jdrx|jd rx| j|jj|jgdd}	| j| j||	 t	 j
|||||||| |j p|j r|j s|j r|jr| j||}
|
d k	r| j|
 |jo|j p|j r| j|jj|jgdd}| j| j|| d S )Nr&   r+   Zcitextr'   )r)   )r,   r-   r.   Z_create_index_namer9   r:   r2   execute_delete_index_sqlr   _alter_fieldr   )r   r   rB   rC   Zold_typerD   Zold_db_paramsZnew_db_paramsstrictZ
index_namer   Zindex_to_remove)r   r   r   rI      s"    
z!DatabaseSchemaEditor._alter_fieldc                s*   |rt ||| j||dS t j||||S )N)col_suffixesr*   )r   r<   r   _index_columns)r   r6   columnsrK   r*   )r   r   r   rL      s    z#DatabaseSchemaEditor._index_columnsc             C   s   | j |j|| |dd d d S )N)concurrently)params)rG   Z
create_sql)r   r   indexrN   r   r   r   	add_index   s    zDatabaseSchemaEditor.add_indexc             C   s   | j |j|| |d d S )N)rN   )rG   Z
remove_sql)r   r   rP   rN   r   r   r   remove_index   s    z!DatabaseSchemaEditor.remove_indexNc                s    |r
| j n| j}t j|||S )N)sql_delete_index_concurrentlysql_delete_indexr   rH   )r   r   namesqlrN   )r   r   r   rH      s    z&DatabaseSchemaEditor._delete_index_sql )r(   rU   r)   usingdb_tablespacerK   rV   r*   	conditionrN   includeexpressionsc               s4   |s
| j n| j}t j|||||||||	|
||dS )N)r(   rU   r)   rX   rY   rK   rV   r*   rZ   r[   r\   )sql_create_indexsql_create_index_concurrentlyr   r/   )r   r   r(   rU   r)   rX   rY   rK   rV   r*   rZ   rN   r[   r\   )r   r   r   r/      s    
z&DatabaseSchemaEditor._create_index_sql)F)F)F)NF)__name__
__module____qualname__r>   r=   r?   r@   r]   r^   rT   rS   Zsql_create_column_inline_fkZsql_delete_fkZsql_delete_procedurer   r   r"   r$   r   rA   rI   rL   rQ   rR   rH   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   