o
    HYc1                     @   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Zdd Z fddZ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 ).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)zRALTER 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|}t|drd|_| 	 S )N%z%%encodingutf8)

isinstancestrreplacepsycopg2
extensionsadapthasattrr   	getquoteddecode)selfvalueadapted r   Y/var/www/jaguar/venv/lib/python3.10/site-packages/django/db/backends/postgresql/schema.pyquote_value4   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   )is_relationrel_db_type
connection
data_typesgetget_internal_typedb_typer   r   r   r   r   _field_data_typeD   s   
z%DatabaseSchemaEditor._field_data_typec                 c   s8    |j  dkr| |j E d H  d S | |j V  d S )N
ArrayField)
base_fieldr(   _field_base_data_typesr+   r*   r   r   r   r.   L   s   z+DatabaseSchemaEditor._field_base_data_typesc                 C   sr   |j | jd}|dur7|js|jr7d|v rdS |dr'| j||gddgdS |dr7| 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_likevarchar_pattern_ops)fieldssuffix	opclassestexttext_pattern_ops)r)   r%   db_indexunique
startswith_create_index_sql)r   r   r   r)   r   r   r   r   S   s&   

z+DatabaseSchemaEditor._create_like_index_sqlc                 C   sl   | j  '}| j j||D ]}|d |kr#|d   W  d    S qW d    d S 1 s/w   Y  d S )Ncolumnname)r%   cursorintrospectionget_sequences)r   tabler<   r>   sequencer   r   r   _get_sequence_names   s   

z'DatabaseSchemaEditor._get_sequence_namec                    s  d| _ d}| }| }|dkr+||kr+t| |t| |kr*|  j |7  _ n| || |kr<|  j |7  _ t|jj}h d}	||	v }
||	v }|ru|
sut|j}| j | 	||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 |r|
r||krt ||||\}}t|j}dd	d
d}g }| || }r| j| 	||| d g fg}||fS t ||||S )Nz%ALTER COLUMN %(column)s TYPE %(type)sz USING %(column)s::%(type)sr,   >   	AutoFieldBigAutoFieldSmallAutoField)r<   type)rA   r<   rB   integerbigintsmallint)rD   rE   rF   )rB   rG   )sql_alter_column_typer(   listr.   r+   r   _metadb_tabler<   
quote_namesql_add_identityexecutesql_drop_indentityr   _alter_column_type_sqlrC   sql_delete_sequencesql_alter_sequence_type)r   r   	old_field	new_fieldnew_type	using_sqlnew_internal_typeold_internal_typerA   auto_field_typesold_is_autonew_is_autor<   fragment_other_actionssequence_namedb_typesr!   r   r   rS   z   s   



	

z+DatabaseSchemaEditor._alter_column_type_sqlFc	              
      s  |j s|jr9|dr|dr$|dr|dr$|dr9|ds9| j|jj|jgdd}	| | ||	 t	 
|||||||| |j sO|jsO|j sU|jsd|jrd| ||}
|
d urd| |
 |jr|j s|js| j|jj|jgdd}| | || d S d S d S d S )Nr0   r6   citextr1   )r4   )r8   r9   r:   _create_index_namerM   rN   r<   rQ   _delete_index_sqlr   _alter_fieldr   )r   r   rV   rW   old_typerX   old_db_paramsnew_db_paramsstrict
index_namer    index_to_remover!   r   r   rg      sR   
z!DatabaseSchemaEditor._alter_fieldc                    s*   |rt ||| j||dS t ||||S )N)col_suffixesr5   )r   rO   r   _index_columns)r   rA   columnsrn   r5   r!   r   r   ro     s   z#DatabaseSchemaEditor._index_columnsc                 C   s   | j |j|| |dd d d S )Nconcurrently)params)rQ   
create_sqlr   r   indexrr   r   r   r   	add_index  s   
zDatabaseSchemaEditor.add_indexc                 C   s   |  |j|| |d d S )Nrq   )rQ   
remove_sqlru   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   r=   sqlrr   r!   r   r   rf   "  s
   z&DatabaseSchemaEditor._delete_index_sql r   )r3   r=   r4   usingdb_tablespacern   r|   r5   	conditionrr   includeexpressionsc                   s4   |s| j n| j}t j|||||||||	|
||dS )N)r3   r=   r4   r~   r   rn   r|   r5   r   r   r   )sql_create_indexsql_create_index_concurrentlyr   r;   )r   r   r3   r=   r4   r~   r   rn   r|   r5   r   rr   r   r   r!   r   r   r;   *  s$   z&DatabaseSchemaEditor._create_index_sql)F)NF)__name__
__module____qualname__sql_update_with_defaultrU   rT   r   r   r{   rz   sql_create_column_inline_fksql_delete_fksql_delete_procedurerP   rR   r   r   r+   r.   r   rC   rS   rg   ro   rw   ry   rf   r;   __classcell__r   r   r!   r   r      s\    	 n0

r   )r   django.db.backends.base.schemar   !django.db.backends.ddl_referencesr   django.db.backends.utilsr   r   r   r   r   r   <module>   s
    