o
    :Zc                     @   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
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)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   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   V/var/www/chikooza/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'   )   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-   9   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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')	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.   O   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   "   )rB   )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.rL   N_)r   hashupperrO   )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_indexedrC   r)   lower_limited_data_types)r   r"   r#   create_indexrC   r$   r   r   rV      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   rZ      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(   rO   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(   rB   )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_create_column_inline_fksql_delete_tablesql_create_indexr   r!   r'   r-   r.   rO   r@   rU   rV   rZ   r   r   __classcell__r   r   r$   r   r   	   s.    .
r   )r<   r
   rD   django.db.backends.base.schemar   django.db.utilsr   r   r   r   r   r   <module>   s    