o
    ÕèFhÌ)  ã                   @   sT   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 G dd„ deƒZ
dS )é    N)ÚDatabaseError)ÚBaseDatabaseSchemaEditorÚ_related_non_m2m_objects)Úduration_iso_stringc                       sÌ   e Zd ZdZdZdZdZdZdZeZ	dZ
dZd	Zd
Zdd„ Z‡ fdd„Z‡ fdd„Zd)‡ fdd„	Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡ fdd„Zd d!„ Zd"d#„ Zd$d%„ Zd*‡ fd'd(„	Z‡  ZS )+ÚDatabaseSchemaEditorz3ALTER TABLE %(table)s ADD %(column)s %(definition)sz'MODIFY %(column)s %(type)s%(collation)szMODIFY %(column)s NULLzMODIFY %(column)s NOT NULLz%MODIFY %(column)s DEFAULT %(default)szMODIFY %(column)s DEFAULT NULLz,ALTER TABLE %(table)s DROP COLUMN %(column)szHCONSTRAINT %(name)s REFERENCES %(to_table)s(%(to_column)s)%(deferrable)sz(DROP TABLE %(table)s CASCADE CONSTRAINTSz9CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)sc                 C   sŠ   t |tjtjtjfƒrd| S t |tjƒrdt|ƒ S t |tƒr(d| dd¡ S t |tt	t
fƒr6d| ¡  S t |tƒrA|r?dS dS t|ƒS )Nz'%s'ú'z''Ú1Ú0)Ú
isinstanceÚdatetimeÚdateÚtimeÚ	timedeltar   ÚstrÚreplaceÚbytesÚ	bytearrayÚ
memoryviewÚhexÚbool©ÚselfÚvalue© r   úY/var/www/html/loop/nvenv/lib/python3.10/site-packages/django/db/backends/oracle/schema.pyÚquote_value   s   

z DatabaseSchemaEditor.quote_valuec                    s6   |   |jj|j¡r|  |jj|j¡ tƒ  ||¡ d S ©N)Ú_is_identity_columnÚ_metaÚdb_tableÚcolumnÚ_drop_identityÚsuperÚremove_field)r   ÚmodelÚfield©Ú	__class__r   r   r#   +   s   z!DatabaseSchemaEditor.remove_fieldc                    s0   t ƒ  |¡ |  dd| jj |jj¡i ¡ d S )NaK  
            DECLARE
                i INTEGER;
            BEGIN
                SELECT COUNT(1) INTO i FROM USER_SEQUENCES
                    WHERE SEQUENCE_NAME = '%(sq_name)s';
                IF i = 1 THEN
                    EXECUTE IMMEDIATE 'DROP SEQUENCE "%(sq_name)s"';
                END IF;
            END;
        /Úsq_name)r"   Údelete_modelÚexecuteÚ
connectionÚopsÚ_get_no_autofield_sequence_namer   r   )r   r$   r&   r   r   r)   2   s   
ÿÿõÿz!DatabaseSchemaEditor.delete_modelFc              
      s2  zt ƒ  ||||¡ W d S  ty˜ } z€t|ƒ}d|v s d|v r(|  |||¡ nPd|v r>|  |jj|j¡ |  ||||¡ nAd|v rT|j	rT| j
|dd� |  |||¡ n2d|v rw|j	rw| j
|dd� |  ||||¡ |j	rv|  |  ||¡¡ n‚ W Y d }~d S W Y d }~d S W Y d }~d S W Y d }~d S d }~ww )Nz	ORA-22858z	ORA-22859z	ORA-30675z	ORA-30673T)Ústrictz	ORA-43923)r"   Úalter_fieldr   r   Ú_alter_field_type_workaroundr!   r   r   r    Úprimary_keyÚ_delete_primary_keyr*   Ú_create_primary_key_sql)r   r$   Ú	old_fieldÚ	new_fieldr.   ÚeÚdescriptionr&   r   r   r/   I   s0   €ì€ñz DatabaseSchemaEditor.alter_fieldc                    sd  t  |¡}| ¡ dv|_|  |j¡|_|  ||¡ |  |j¡}| | j	¡}t
 d|¡r1d| }d}t
 d|¡rY| ¡ }|dkrDd| }n|dkrMd	| }n|d
krYd| }d	| }|  d|  |jj¡|  |j¡|f ¡ |  ||¡ tƒ  |||¡ | | j	¡}|jr†|jsŒ|jr¬|jr®||krªt||ƒD ]\}	}
|
jjr©|  |  |
j|
jd¡¡ q•dS dS dS dS )aÁ  
        Oracle refuses to change from some type to other type.
        What we need to do instead is:
        - Add a nullable version of the desired field with a temporary name. If
          the new column is an auto field, then the temporary column can't be
          nullable.
        - Update the table to transfer values from old to new
        - Drop old column
        - Rename the new column and possibly drop the nullable property
        )Ú	AutoFieldÚBigAutoFieldÚSmallAutoFieldz^N?CLOBzTO_CHAR(%s)ÚVARCHAR2z^N?VARCHAR2Ú	DateFieldzTO_DATE(%s, 'YYYY-MM-DD')ÚDateTimeFieldz,TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')Ú	TimeFieldzCONCAT('1900-01-01 ', %s)zUPDATE %s set %s=%sÚ_fkN)ÚcopyÚdeepcopyÚget_internal_typeÚnullÚ_generate_temp_namer    Ú	add_fieldÚ
quote_nameÚdb_typer+   ÚreÚmatchr*   r   r   r#   r"   r/   r1   Úuniquer   r%   Údb_constraintÚ_create_fk_sqlÚrelated_model)r   r$   r4   r5   Únew_temp_fieldÚ	new_valueÚold_typeÚnew_internal_typeÚnew_typeÚ_Úrelr&   r   r   r0   g   s^   



ýÿÿ	ÿÿþþÿ€ýýz1DatabaseSchemaEditor._alter_field_type_workaroundc                    sZ   h d£}|  ¡ |v r"|  ¡ |vr"|  |jj|j¡r"|  |jj|j¡ tƒ  ||||||¡S )N>   r8   r9   r:   )rB   r   r   r   r    r!   r"   Ú_alter_column_type_sql)r   r$   r4   r5   rR   Úold_collationÚnew_collationÚauto_field_typesr&   r   r   rU   ª   s   ÿÿz+DatabaseSchemaEditor._alter_column_type_sqlc                 C   s2   |   |¡}|d dkr|d dkr|dd… }|S )z‚
        Get the properly shortened and uppercased identifier as returned by
        quote_name() but without the quotes.
        r   ú"éÿÿÿÿé   )rF   )r   ÚnameÚnnr   r   r   Únormalize_name¹   s   
z#DatabaseSchemaEditor.normalize_namec                 C   s*   t t|ƒƒ ¡ dd… }|  |d | ¡S )z@Generate temporary names for workarounds that need temp columns.r[   NrS   )r   ÚhashÚupperr^   )r   Úfor_nameÚsuffixr   r   r   rD   Ã   s   z(DatabaseSchemaEditor._generate_temp_namec                 C   s
   |   |¡S r   )r   r   r   r   r   Úprepare_defaultÈ   s   
z$DatabaseSchemaEditor.prepare_defaultc                    s:   t ƒ  ||¡}| | j¡}|d ur| ¡ | jjv rdS |S )NF)r"   Ú_field_should_be_indexedrG   r+   ÚlowerÚ_limited_data_types)r   r$   r%   Úcreate_indexrG   r&   r   r   rd   Ë   s   z-DatabaseSchemaEditor._field_should_be_indexedc                 C   sb   | j  ¡ �"}| d|  |¡|  |¡g¡ | ¡ }|r|d ndW  d   ƒ S 1 s*w   Y  d S )Nzä
                SELECT
                    CASE WHEN identity_column = 'YES' THEN 1 ELSE 0 END
                FROM user_tab_cols
                WHERE table_name = %s AND
                      column_name = %s
                r   F©r+   Úcursorr*   r^   Úfetchone)r   Ú
table_nameÚcolumn_nameri   Úrowr   r   r   r   Õ   s   ø
$ôz(DatabaseSchemaEditor._is_identity_columnc                 C   s$   |   d|  |¡|  |¡dœ ¡ d S )Nz5ALTER TABLE %(table)s MODIFY %(column)s DROP IDENTITY)Útabler    )r*   rF   )r   rk   rl   r   r   r   r!   ä   s   þÿÿz#DatabaseSchemaEditor._drop_identityc                 C   sN   | j  ¡ �}| d|  |¡g¡ | ¡ d W  d   ƒ S 1 s w   Y  d S )Nza
                SELECT default_collation FROM user_tables WHERE table_name = %s
                r   rh   )r   rk   ri   r   r   r   Ú_get_default_collationí   s   
ü
$ùz+DatabaseSchemaEditor._get_default_collationNc                    s*   |d u r|d ur|   |¡}tƒ  |||¡S r   )ro   r"   Ú_collate_sql)r   Ú	collationrV   rk   r&   r   r   rp   ÷   s   
z!DatabaseSchemaEditor._collate_sql)F)NN)Ú__name__Ú
__module__Ú__qualname__Úsql_create_columnÚsql_alter_column_typeÚsql_alter_column_nullÚsql_alter_column_not_nullÚsql_alter_column_defaultÚsql_alter_column_no_defaultÚ sql_alter_column_no_default_nullÚsql_delete_columnÚsql_create_column_inline_fkÚsql_delete_tableÚsql_create_indexr   r#   r)   r/   r0   rU   r^   rD   rc   rd   r   r!   ro   rp   Ú__classcell__r   r   r&   r   r      s6    ÿC

	
r   )r@   r   rH   Ú	django.dbr   Údjango.db.backends.base.schemar   r   Údjango.utils.durationr   r   r   r   r   r   Ú<module>   s    