o
    ×ŸŽi8  ã                   @   sD   d dl mZ d dlmZ d dlmZ d dlmZ G dd„ deƒZdS )é    )ÚBaseDatabaseSchemaEditor)ÚIndexColumns)Úsql)Ú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d2‡ fd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dd„ Z‡ fdd„Zd d!„ Z	"d3‡ fd#d$„	Z‡ fd%d&„Zd3d'd(„Zd3d)d*„Zd4‡ fd,d-„	Zd+d+d.d.d+dd+dd+d"d+d+d/œ‡ fd0d1„
Z‡  ZS )5Ú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)© c                    s8   |d u rt ƒ  ||¡S | jj t|ƒ|¡}t ƒ  |d ¡S ©N)ÚsuperÚexecuteÚ
connectionÚopsZcompose_sqlÚstr)Úselfr   Úparams©Ú	__class__r   únC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\django/db/backends/postgresql/schema.pyr
   *   s   zDatabaseSchemaEditor.executezRALTER 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j¡S )Nú%z%%)Ú
isinstancer   Úreplacer   Úquoter   )r   Úvaluer   r   r   Úquote_value:   s   
z DatabaseSchemaEditor.quote_valuec                    s0   t ƒ  ||¡}|  ||¡}|d ur| |¡ |S r   )r	   Ú_field_indexes_sqlÚ_create_like_index_sqlÚappend)r   ÚmodelÚfieldÚoutputÚlike_index_statementr   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_typer   Ú
data_typesÚgetÚget_internal_typeÚdb_type©r   r   r   r   r   Ú_field_data_typeF   s   
þz%DatabaseSchemaEditor._field_data_typec                 c   s8   � |j  ¡ dkr|  |j ¡E d H  d S |  |j ¡V  d S )NÚ
ArrayField)Z
base_fieldr"   Ú_field_base_data_typesr%   r$   r   r   r   r'   N   s   €z+DatabaseSchemaEditor._field_base_data_typesc                 C   s‚   |j | jd�}|dur?|js|jr?d|v rdS t|ddƒrdS | d¡r/| j||gddgd�S | d	¡r?| 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ú[Zdb_collationÚvarcharÚ_likeZvarchar_pattern_ops)ÚfieldsÚsuffixÚ	opclassesÚtextZtext_pattern_ops)r#   r   Údb_indexÚuniqueÚgetattrÚ
startswithÚ_create_index_sql)r   r   r   r#   r   r   r   r   U   s*   
ü
üz+DatabaseSchemaEditor._create_like_index_sqlc                 C   sd   d}|  ¡ }|  ¡ }|dkr$||kr$t|  |¡ƒt|  |¡ƒkr"|S dS |  |¡|  |¡kr0|S dS )Nz USING %(column)s::%(type)sr&   Ú )r"   Úlistr'   r%   )r   Ú	new_fieldÚ	old_fieldÚ	using_sqlÚnew_internal_typeÚold_internal_typer   r   r   Ú
_using_sqly   s   ÿþzDatabaseSchemaEditor._using_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   ÚcursorZintrospectionZget_sequences)r   Útabler=   r?   Úsequencer   r   r   Ú_get_sequence_name‡   s   
ýÿ
ÿüz'DatabaseSchemaEditor._get_sequence_namec                    sT  |j | jd�}|d }|js|jrD| d¡r| d¡r/| d¡r%| d¡r/| d¡rD| d¡sD| j|jj|jgdd�}	|  	|  
||	¡¡ d| _|  ||¡ }
rV|  j|
7  _| ¡ }| ¡ }t|jjƒ}h d	£}||v }||v }|r˜|s˜t|jƒ}| j|  |¡|d
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�rtƒ  ||||||¡\}}t|jƒ}ddddœ}g }|  ||¡ }�r| j|  |¡|| dœ g fg}||fS tƒ  ||||||¡S )Nr(   Útyper*   r/   Zcitextr+   ©r-   z2ALTER COLUMN %(column)s TYPE %(type)s%(collation)s>   ÚBigAutoFieldÚ	AutoFieldÚSmallAutoFieldr5   ©r=   rC   Z	collation)r@   r=   rA   ÚintegerZbigintZsmallint)rF   rE   rG   )rA   rC   )Zdb_parametersr   r0   r1   r3   Ú_create_index_nameÚ_metaÚdb_tabler=   r
   Ú_delete_index_sqlZsql_alter_column_typer<   r"   r   Ú
quote_nameÚsql_add_identityÚsql_drop_indentityr	   Ú_alter_column_type_sqlrB   Úsql_delete_sequenceÚsql_alter_sequence_type)r   r   r8   r7   Únew_typeZold_collationÚnew_collationÚold_db_paramsÚold_typeZ
index_namer9   r:   r;   r@   Zauto_field_typesZold_is_autoZnew_is_autor=   ÚfragmentÚ_Zother_actionsZsequence_nameZdb_typesr   r   r   rQ   Ž   sº   ÿÿþþýýÿÿ
ýÿùþÿúÿöþÿÿ
ÿ
ÿÿûÿ	ÿ
ýþÿúÿ
ÿz+DatabaseSchemaEditor._alter_column_type_sqlc                 C   sL   | j }|  ||¡ }r||7 }||  |j¡||rd|  |¡ nddœ g fS )Nú r5   rH   )Zsql_alter_column_collater<   rN   r=   Z_collate_sql)r   r   r7   rT   rU   r8   r   r9   r   r   r   Ú_alter_column_collation_sql  s   
ÿûÿ÷z0DatabaseSchemaEditor._alter_column_collation_sqlFc	              
      s¢   t ƒ  ||||||||¡ |js|js|js|js+|jr+|  ||¡}	|	d ur+|  |	¡ |jrK|jsM|jsO| j|jj|j	gdd�}
|  |  
||
¡¡ d S d S d S d S )Nr+   rD   )r	   Ú_alter_fieldr0   r1   r   r
   rJ   rK   rL   r=   rM   )r   r   r8   r7   rW   rT   rV   Znew_db_paramsÚstrictr   Zindex_to_remover   r   r   r\     s0   øÿÿ
ÿüz!DatabaseSchemaEditor._alter_fieldc                    s*   |rt ||| j||d�S tƒ  ||||¡S )N)Úcol_suffixesr.   )r   rN   r	   Ú_index_columns)r   r@   Úcolumnsr^   r.   r   r   r   r_   8  s   ûz#DatabaseSchemaEditor._index_columnsc                 C   s   | j |j|| |d�d d� d S )N©Úconcurrently)r   )r
   Z
create_sql©r   r   Úindexrb   r   r   r   Ú	add_indexC  s   
ÿzDatabaseSchemaEditor.add_indexc                 C   s   |   |j|| |d�¡ d S )Nra   )r
   Z
remove_sqlrc   r   r   r   Úremove_indexH  s   z!DatabaseSchemaEditor.remove_indexNc                    s    |r| j n| j}tƒ  |||¡S r   )Úsql_delete_index_concurrentlyÚsql_delete_indexr	   rM   )r   r   r>   r   rb   r   r   r   rM   K  s
   ÿýz&DatabaseSchemaEditor._delete_index_sqlr5   )r,   r>   r-   ÚusingÚdb_tablespacer^   r   r.   Ú	conditionrb   ÚincludeÚexpressionsc                   s8   |p	|s| j n| j}tƒ j|||||||||	|
||d�S )N)r,   r>   r-   ri   rj   r^   r   r.   rk   rl   rm   )Úsql_create_indexÚsql_create_index_concurrentlyr	   r4   )r   r   r,   r>   r-   ri   rj   r^   r   r.   rk   rb   rl   rm   r   r   r   r4   S  s&   ÿýôz&DatabaseSchemaEditor._create_index_sql)r   )F)NF) Ú__name__Ú
__module__Ú__qualname__Zsql_update_with_defaultrS   rR   rn   ro   rh   rg   Zsql_create_column_inline_fkZsql_delete_fkZsql_delete_procedurer
   rO   rP   r   r   r%   r'   r   r<   rB   rQ   r[   r\   r_   re   rf   rM   r4   Ú__classcell__r   r   r   r   r      sb    ÿÿÿÿÿ	ÿÿ$s÷$

ñr   N)	Zdjango.db.backends.base.schemar   Z!django.db.backends.ddl_referencesr   Z)django.db.backends.postgresql.psycopg_anyr   Zdjango.db.backends.utilsr   r   r   r   r   r   Ú<module>   s
    