3
š(ƒhÁ  ã               @   sd   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ G dd„ deƒZdS )é    N)ÚBaseDatabaseSchemaEditor)ÚDatabaseError)Úsix)Ú
force_textc                   sv   e 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d‡ fdd„	Z‡ fdd„Z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 CONSTRAINTSc             C   s€   t |tjtjtjfƒrd| S t |tjƒr@dtj|ƒjddƒ S t |tjƒr^dt	t
j|ƒƒ S t |tƒrt|rpdS dS t|ƒS d S )Nz'%s'ú'z''Ú1Ú0)Ú
isinstanceÚdatetimeÚdateÚtimer   Ústring_typesÚ	text_typeÚreplaceZbuffer_typesr   ÚbinasciiÚhexlifyÚboolÚstr)ÚselfÚvalue© r   úD/tmp/pip-install-q3hcpn_q/Django/django/db/backends/oracle/schema.pyÚquote_value   s    
z DatabaseSchemaEditor.quote_valuec                s4   t t| ƒj|ƒ | jdd| jjj|jjƒi ƒ d S )Nab  
            DECLARE
                i INTEGER;
            BEGIN
                SELECT COUNT(*) INTO i FROM USER_CATALOG
                    WHERE TABLE_NAME = '%(sq_name)s' AND TABLE_TYPE = 'SEQUENCE';
                IF i = 1 THEN
                    EXECUTE IMMEDIATE 'DROP SEQUENCE "%(sq_name)s"';
                END IF;
            END;
        /Zsq_name)	Úsuperr   Údelete_modelÚexecuteÚ
connectionÚopsZ_get_sequence_nameÚ_metaÚdb_table)r   Úmodel)Ú	__class__r   r   r   #   s    
z!DatabaseSchemaEditor.delete_modelFc                sl   yt t| ƒj||||ƒ W nL tk
rf } z0t|ƒ}d|ksDd|krT| j|||ƒ n‚ W Y d d }~X nX d S )Nz	ORA-22858z	ORA-22859)r   r   Úalter_fieldr   r   Ú_alter_field_type_workaround)r   r!   Ú	old_fieldÚ	new_fieldÚstrictÚeÚdescription)r"   r   r   r#   3   s    z DatabaseSchemaEditor.alter_fieldc                sì   t j|ƒ}d|_| j|jƒ|_| j||ƒ | j|jƒ}|j| jƒ}t	j
d|ƒrZd| }d}t	j
d|ƒr¢|jƒ }|dkr€d| }n"|dkr’d	| }n|d
kr¢d| }| jd| j|jjƒ| j|jƒ|f ƒ | j||ƒ tt| ƒj|||ƒ 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
        - Update the table to transfer values from old to new
        - Drop old column
        - Rename the new column and possibly drop the nullable property
        Tz^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	TimeFieldzCTO_TIMESTAMP(CONCAT('1900-01-01 ', %s), 'YYYY-MM-DD HH24:MI:SS.FF')zUPDATE %s set %s=%sN)ÚcopyÚdeepcopyÚnullÚ_generate_temp_nameÚcolumnZ	add_fieldÚ
quote_nameZdb_typer   ÚreÚmatchZget_internal_typer   r   r    Zremove_fieldr   r   r#   )r   r!   r%   r&   Znew_temp_fieldZ	new_valueZold_typeZnew_internal_type)r"   r   r   r$   ?   s.    




z1DatabaseSchemaEditor._alter_field_type_workaroundc             C   s2   | j |ƒ}|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 actual quotes.
        r   ú"é   éÿÿÿÿr4   )r/   )r   ÚnameÚnnr   r   r   Únormalize_namej   s    
z#DatabaseSchemaEditor.normalize_namec             C   s*   t t|ƒƒjƒ dd… }| j|d | ƒS )zR
        Generates temporary names for workarounds that need temp columns
        r3   NÚ_)ÚhexÚhashÚupperr7   )r   Zfor_nameÚsuffixr   r   r   r-   t   s    z(DatabaseSchemaEditor._generate_temp_namec             C   s
   | j |ƒS )N)r   )r   r   r   r   r   Úprepare_default{   s    z$DatabaseSchemaEditor.prepare_default)F)Ú__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_delete_columnZsql_delete_tabler   r   r#   r$   r7   r-   r=   Ú__classcell__r   r   )r"   r   r      s   +
r   )r   r*   r   r0   Zdjango.db.backends.base.schemar   Zdjango.db.utilsr   Zdjango.utilsr   Zdjango.utils.textr   r   r   r   r   r   Ú<module>   s   