a
    xd'                     @   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&	 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)szOCREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s%(condition)sz\CREATE INDEX CONCURRENTLY %(name)s ON %(table)s%(using)s (%(columns)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|  S )N%z%%)
isinstancestrreplacepsycopg2
extensionsZadaptZ	getquoteddecode)selfvalue r   \/var/www/html/Ranjet/env/lib/python3.9/site-packages/django/db/backends/postgresql/schema.pyquote_value"   s    
z DatabaseSchemaEditor.quote_valuec                    s0   t  ||}| ||}|d u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_type/   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%   7   s    z+DatabaseSchemaEditor._field_base_data_typesc                 C   sr   |j | jd}|durn|js"|jrnd|v 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)suffix	opclassestextZtext_pattern_ops)r!   r   db_indexunique
startswith_create_index_sql)r   r   r   r!   r   r   r   r   >   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}	| |	v 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 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)tablechanges)r6   r2   r4   )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_ownerr   _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_namer   r   r   rA   T   sp    

	

	2z+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 u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_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_remover   r   r   rG      s<    
z!DatabaseSchemaEditor._alter_fieldc                    s*   |rt ||| j||dS t ||||S )N)col_suffixesr*   )r   r<   r   _index_columns)r   r6   columnsrI   r*   r   r   r   rJ      s    z#DatabaseSchemaEditor._index_columnsc                 C   s   | j |j|| |dd d d S )Nconcurrently)params)rE   Z
create_sqlr   r   indexrM   r   r   r   	add_index   s    zDatabaseSchemaEditor.add_indexc                 C   s   |  |j|| |d d S )NrL   )rE   Z
remove_sqlrO   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   rF   )r   r   namesqlrM   r   r   r   rF      s    z&DatabaseSchemaEditor._delete_index_sql r   )	rU   r)   usingdb_tablespacerI   rV   r*   	conditionrM   c       	            s0   |s
| j n| j}t j|||||||||	|
d
S )N)rU   r)   rX   rY   rI   rV   r*   rZ   )sql_create_indexsql_create_index_concurrentlyr   r/   )r   r   fieldsrU   r)   rX   rY   rI   rV   r*   rZ   rM   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   rG   rJ   rQ   rR   rF   r/   __classcell__r   r   r   r   r      s:   G 

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   