o
    ˜¨Êhä)  ã                   @   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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‡ fd'd(„Z‡  ZS )*ÚDatabaseSchemaEditorz3ALTER TABLE %(table)s ADD %(column)s %(definition)szMODIFY %(column)s %(type)szMODIFY %(column)s NULLzMODIFY %(column)s NOT NULLz%MODIFY %(column)s DEFAULT %(default)szMODIFY %(column)s DEFAULT NULLz'MODIFY %(column)s %(type)s%(collation)sz,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¡ dd¡ S t |tt	t
fƒr:d| ¡  S t |tƒrE|rCdS dS t|ƒS )Nz'%s'ú'z''ú%z%%Ú1Ú0)Ú
isinstanceÚdatetimeÚdateÚtimeÚ	timedeltar   ÚstrÚreplaceÚbytesÚ	bytearrayÚ
memoryviewÚhexÚbool©ÚselfÚvalue© r   úR/var/www/html/env/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*   4   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   r0   K   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#   r0   r2   Úuniquer   r&   Údb_constraintÚ_create_fk_sqlÚrelated_model)r   r%   r5   r6   Únew_temp_fieldÚ	new_valueÚold_typeÚnew_internal_typeÚnew_typeÚ_Úrelr'   r   r   r1   i   s^   



ýÿÿ	ÿÿþþÿ€ýýz1DatabaseSchemaEditor._alter_field_type_workaroundc                    sV   h d£}|  ¡ |v r"|  ¡ |vr"|  |jj|j¡r"|  |jj|j¡ tƒ  ||||¡S )N>   r9   r:   r;   )rC   r   r   r    r!   r"   r#   Ú_alter_column_type_sql)r   r%   r5   r6   rS   Úauto_field_typesr'   r   r   rV   ¬   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   ú"éÿÿÿÿé   )rG   )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.rZ   NrT   )r   ÚhashÚupperr]   )r   Úfor_nameÚsuffixr   r   r   rE   Á   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_indexedrH   r,   ÚlowerÚ_limited_data_types)r   r%   r&   Úcreate_indexrH   r'   r   r   rc   É   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_namerh   Ú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+   rG   )r   rj   rk   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   rg   )r   rj   rh   r   r   r   Ú_get_default_collationë   s   
ü
$ùz+DatabaseSchemaEditor._get_default_collationc                    s(   |d u r|   |jj¡}tƒ  ||||¡S r   )rn   r   r    r#   Ú_alter_column_collation_sql)r   r%   r6   rS   Únew_collationr'   r   r   ro   õ   s
   ÿz0DatabaseSchemaEditor._alter_column_collation_sql)F)Ú__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_alter_column_collateÚsql_delete_columnÚsql_create_column_inline_fkÚsql_delete_tableÚsql_create_indexr   r$   r*   r0   r1   rV   r]   rE   rb   rc   r   r"   rn   ro   Ú__classcell__r   r   r'   r   r      s8    ÿC

	
r   )rA   r   rI   Ú	django.dbr   Údjango.db.backends.base.schemar   r   Údjango.utils.durationr   r   r   r   r   r   Ú<module>   s    