U
    R+|jß)  ã                   @   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ƒr6dt|ƒ S t |tƒrXd| dd¡ dd¡ S t |tt	t
fƒrtd| ¡  S t |tƒrŠ|r†dS dS t|ƒS d S )Nz'%s'ú'z''ú%z%%Ú1Ú0)Ú
isinstanceÚdatetimeÚdateÚtimeÚ	timedeltar   ÚstrÚreplaceÚbytesÚ	bytearrayÚ
memoryviewÚhexÚbool©ÚselfÚvalue© r   úhc:\users\snmko\desktop\web_content\django_cbt\venv\Lib\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;
        /Zsq_name)r#   Údelete_modelÚexecuteÚ
connectionÚopsZ_get_no_autofield_sequence_namer   r    )r   r%   r'   r   r   r)   2   s     ÿÿõÿz!DatabaseSchemaEditor.delete_modelFc              
      s  zt ƒ  ||||¡ W nì tk
�r } zÌt|ƒ}d|ksBd|krR|  |||¡ n d|kr~|  |jj|j¡ |  ||||¡ ntd|krª|j	rª| j
|dd� |  |||¡ nHd|krð|j	rð| j
|dd� |  ||||¡ |j	rò|  |  ||¡¡ n‚ W 5 d }~X Y nX d S )Nz	ORA-22858z	ORA-22859z	ORA-30675z	ORA-30673T)Ústrictz	ORA-43923)r#   Úalter_fieldr   r   Ú_alter_field_type_workaroundr"   r   r    r!   Úprimary_keyZ_delete_primary_keyr*   Z_create_primary_key_sql)r   r%   Ú	old_fieldÚ	new_fieldr-   ÚeÚdescriptionr'   r   r   r.   I   s$    z DatabaseSchemaEditor.alter_fieldc                    sf  t  |¡}| ¡ dk|_|  |j¡|_|  ||¡ |  |j¡}| | j	¡}t
 d|¡rbd| }d}t
 d|¡r²| ¡ }|dkrˆd| }n*|dkršd	| }n|d
kr²d| }d	| }|  d|  |jj¡|  |j¡|f ¡ |  ||¡ tƒ  |||¡ | | j	¡}|j�r|j�s |j�rb|j�rb||k�rbt||ƒD ],\}	}
|
jj�r4|  |  |
j|
jd¡¡ �q4d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)ZVARCHAR2z^N?VARCHAR2Z	DateFieldzTO_DATE(%s, 'YYYY-MM-DD')ZDateTimeFieldz,TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')Z	TimeFieldzCONCAT('1900-01-01 ', %s)zUPDATE %s set %s=%sZ_fkN)ÚcopyÚdeepcopyÚget_internal_typeÚnullÚ_generate_temp_namer!   Z	add_fieldÚ
quote_nameÚdb_typer+   ÚreÚmatchr*   r   r    r$   r#   r.   r0   Úuniquer   r&   Zdb_constraintZ_create_fk_sqlZrelated_model)r   r%   r1   r2   Znew_temp_fieldÚ	new_valueZold_typeZnew_internal_typeÚnew_typeÚ_Úrelr'   r   r   r/   g   sX    



ýÿÿ	ÿÿþþý
ÿz1DatabaseSchemaEditor._alter_field_type_workaroundc                    s\   dddh}|  ¡ |krF|  ¡ |krF|  |jj|j¡rF|  |jj|j¡ tƒ  ||||||¡S )Nr5   r6   r7   )r:   r   r   r    r!   r"   r#   Ú_alter_column_type_sql)r   r%   r1   r2   rC   Úold_collationZnew_collationZauto_field_typesr'   r   r   rF   ª   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   ú"éÿÿÿÿé   )r=   )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.rJ   NrD   )r   ÚhashÚupperrM   )r   Z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 k	r6| ¡ | jjkr6dS |S )NF)r#   Ú_field_should_be_indexedr>   r+   ÚlowerZ_limited_data_types)r   r%   r&   Zcreate_indexr>   r'   r   r   rR   Ë   s    ÿþz-DatabaseSchemaEditor._field_should_be_indexedc              
   C   sV   | j  ¡ �B}| d|  |¡|  |¡g¡ | ¡ }|r<|d ndW  5 Q R £ S Q R X 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*   rM   Zfetchone)r   Ú
table_nameÚcolumn_namerU   Ú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*   r=   )r   rV   rW   r   r   r   r"   ä   s    þÿÿz#DatabaseSchemaEditor._drop_identityc              
   C   sB   | j  ¡ �.}| d|  |¡g¡ | ¡ d W  5 Q R £ S Q R X d S )Nza
                SELECT default_collation FROM user_tables WHERE table_name = %s
                r   rT   )r   rV   rU   r   r   r   Ú_get_default_collationí   s    
üz+DatabaseSchemaEditor._get_default_collationNc                    s*   |d kr|d k	r|   |¡}tƒ  |||¡S r   )rZ   r#   Ú_collate_sql)r   Z	collationrG   rV   r'   r   r   r[   ÷   s    
z!DatabaseSchemaEditor._collate_sql)F)NN)Ú__name__Ú
__module__Ú__qualname__Zsql_create_columnZsql_alter_column_typeZsql_alter_column_nullZsql_alter_column_not_nullZsql_alter_column_defaultZsql_alter_column_no_defaultZ sql_alter_column_no_default_nullZsql_delete_columnZsql_create_column_inline_fkZsql_delete_tableZsql_create_indexr   r$   r)   r.   r/   rF   rM   r<   rQ   rR   r   r"   rZ   r[   Ú__classcell__r   r   r'   r   r      s4   ÿC

	
r   )r8   r   r?   Z	django.dbr   Zdjango.db.backends.base.schemar   r   Zdjango.utils.durationr   r   r   r   r   r   Ú<module>   s   