o
    uvXj  ã                   @   sD   d dl Z d dlZd dlZd dlmZ d dlmZ G dd„ deƒZdS )é    N)ÚBaseDatabaseSchemaEditor)ÚDatabaseErrorc                       s®   e 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‡ fdd„Zd#‡ fdd„	Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡ fdd„Z‡ fdd„Zdd „ Z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,ALTER TABLE %(table)s DROP COLUMN %(column)sz(DROP TABLE %(table)s CASCADE CONSTRAINTSz9CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)sc                 C   sz   t |tjtjtjfƒrd| S t |tƒr d| dd¡ dd¡ S t |tttfƒr.d| 	¡  S t |t
ƒr9|r7dS dS t|ƒS )Nz'%s'ú'z''ú%z%%Ú1Ú0)Ú
isinstanceÚdatetimeÚdateÚtimeÚstrÚreplaceÚbytesÚ	bytearrayÚ
memoryviewÚhexÚbool©ÚselfÚvalue© r   ú]/var/www/html/myproject/venv/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'   (   s   

öz!DatabaseSchemaEditor.delete_modelFc              
      sÞ   zt ƒ  ||||¡ W d S  tyn } zVt|ƒ}d|v s d|v r(|  |||¡ n-d|v r>|  |jj|j¡ |  ||||¡ nd|v rT|j	rT| j
|dd� |  |||¡ n‚ 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)Ústrict)r    Úalter_fieldr   r   Ú_alter_field_type_workaroundr   r   r   r   Úprimary_keyÚ_delete_primary_key)r   r"   Ú	old_fieldÚ	new_fieldr,   ÚeÚdescriptionr$   r   r   r-   8   s$   ô€ñz DatabaseSchemaEditor.alter_fieldc                    sð   t  |¡}| ¡ dv|_|  |j¡|_|  ||¡ |  |j¡}| | j	¡}t
 d|¡r1d| }d}t
 d|¡rU| ¡ }|dkrDd| }n|dkrMd	| }n|d
krUd| }|  d|  |jj¡|  |j¡|f ¡ |  ||¡ tƒ  |||¡ 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ÚBigAutoFieldz^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')Ú	TimeFieldzCTO_TIMESTAMP(CONCAT('1900-01-01 ', %s), 'YYYY-MM-DD HH24:MI:SS.FF')zUPDATE %s set %s=%sN)ÚcopyÚdeepcopyÚget_internal_typeÚnullÚ_generate_temp_namer   Ú	add_fieldÚ
quote_nameÚdb_typer)   ÚreÚmatchr(   r   r   r!   r    r-   )r   r"   r1   r2   Únew_temp_fieldÚ	new_valueÚold_typeÚnew_internal_typer$   r   r   r.   N   s0   



ýz1DatabaseSchemaEditor._alter_field_type_workaroundc                 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   ú"éÿÿÿÿé   )rA   )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.rK   NÚ_)r   ÚhashÚupperrN   )r   Úfor_nameÚsuffixr   r   r   r?   †   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_indexedrB   r)   ÚlowerÚ_limited_data_types)r   r"   r#   Úcreate_indexrB   r$   r   r   rU   Ž   s
   z-DatabaseSchemaEditor._field_should_be_indexedc                    s   t ƒ  ||¡o|  ||¡ S r   )r    Ú_unique_should_be_addedÚ_field_became_primary_key)r   r1   r2   r$   r   r   rY   •   s   þz,DatabaseSchemaEditor._unique_should_be_addedc                 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(   rN   Úfetchone)r   Ú
table_nameÚcolumn_namer[   Ú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(   rA   )r   r]   r^   r   r   r   r   §   s   þz#DatabaseSchemaEditor._drop_identity)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_delete_columnÚsql_delete_tableÚsql_create_indexr   r!   r'   r-   r.   rN   r?   rT   rU   rY   r   r   Ú__classcell__r   r   r$   r   r   	   s,    .
r   )r;   r
   rC   Údjango.db.backends.base.schemar   Údjango.db.utilsr   r   r   r   r   r   Ú<module>   s    