a
    Lcòf¶%  ã                   @   s@   d dl mZ d dlmZmZmZ d dlmZ G dd„ deƒZdS )é    )ÚBaseDatabaseSchemaEditor)ÚNOT_PROVIDEDÚFÚUniqueConstraint)Ú
LOOKUP_SEPc                       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ZdZdZdZdZdZedd„ ƒZe‡ fdd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZ‡ fdd „Z‡ fd!d"„Z‡ fd#d$„Z‡ fd%d&„Z‡ fd'd(„Zdd)œd*d+„Z ‡ fd,d-„Z!d.d/„ Z"‡ fd0d1„Z#‡ fd2d3„Z$d4d5„ Z%‡ fd6d7„Z&‡  Z'S )8ÚDatabaseSchemaEditorz+RENAME TABLE %(old_table)s TO %(new_table)szMODIFY %(column)s %(type)s NULLz#MODIFY %(column)s %(type)s NOT NULLz2MODIFY %(column)s %(type)s%(collation)s%(comment)sz(ALTER COLUMN %(column)s SET DEFAULT NULLz,ALTER TABLE %(table)s DROP COLUMN %(column)sz)ALTER TABLE %(table)s DROP INDEX %(name)szY, ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) REFERENCES %(to_table)s(%(to_column)s)z/ALTER TABLE %(table)s DROP FOREIGN KEY %(name)sz DROP INDEX %(name)s ON %(table)sz?ALTER TABLE %(table)s RENAME INDEX %(old_name)s TO %(new_name)szGALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)z&ALTER TABLE %(table)s DROP PRIMARY KEYz9CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)sz+ALTER TABLE %(table)s COMMENT = %(comment)sNc                 C   s   | j jrdS dS )Nz8ALTER TABLE %(table)s DROP CONSTRAINT IF EXISTS %(name)sz)ALTER TABLE %(table)s DROP CHECK %(name)s)Ú
connectionÚmysql_is_mariadb©Úself© r   ú\/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/db/backends/mysql/schema.pyÚsql_delete_check%   s    z%DatabaseSchemaEditor.sql_delete_checkc                    s6   | j jr| j jdkr2tƒ jS n| j jdkr2tƒ jS dS )N)é
   é   é   )é   r   é   zCALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s)r   r	   Úmysql_versionÚsuperÚsql_rename_columnr
   ©Ú	__class__r   r   r   .   s    
z&DatabaseSchemaEditor.sql_rename_columnc                 C   sV   | j  ¡  t|tƒr | dd¡}| j j  || j j j¡}t|tƒrRt|tƒrR| ¡ }|S )Nú%z%%)	r   Zensure_connectionÚ
isinstanceÚstrÚreplaceÚescapeÚencodersÚbytesÚdecode)r   ÚvalueÚquotedr   r   r   Úquote_value9   s    


ÿz DatabaseSchemaEditor.quote_valuec                 C   s$   |  | j¡}|d uo"| ¡ | jjv S ©N)Údb_typer   ÚlowerZ_limited_data_types)r   Úfieldr%   r   r   r   Ú_is_limited_data_typeE   s    þz*DatabaseSchemaEditor._is_limited_data_typec                 C   s   | j s|  |¡S dS )NF)Ú$_supports_limited_data_type_defaultsr(   ©r   r'   r   r   r   Úskip_defaultL   s    
z!DatabaseSchemaEditor.skip_defaultc                 C   s   |   |¡r| jjsdS dS )NTF)r(   r   r	   r*   r   r   r   Úskip_default_on_alterQ   s    z*DatabaseSchemaEditor.skip_default_on_alterc                 C   s   | j jrdS | j jdkS )NT)r   r   é   )r   r	   r   r
   r   r   r   r)   X   s    z9DatabaseSchemaEditor._supports_limited_data_type_defaultsc                    s(   | j js| jr|  |¡rdS tƒ  |¡S )Nz(%s))r   r	   r)   r(   r   Ú_column_default_sqlr*   r   r   r   r.   _   s    ÿþýz(DatabaseSchemaEditor._column_default_sqlc                    s^   t ƒ  ||¡ |  |¡rZ|jd tfvrZ|  |¡}|  d|  |jj	¡|  |j
¡dœ |g¡ d S )Nz%UPDATE %(table)s SET %(column)s = %%s)ÚtableÚcolumn)r   Ú	add_fieldr+   Údefaultr   Úeffective_defaultÚexecuteZ
quote_nameÚ_metaÚdb_tabler0   )r   Úmodelr'   r3   r   r   r   r1   j   s    

þÿúzDatabaseSchemaEditor.add_fieldc                    s@   t |tƒr.| || ¡d ur.| j||j|jd� tƒ  ||¡ d S )N©ÚfieldsÚexpressions)r   r   Z
create_sqlÚ_create_missing_fk_indexr9   r:   r   Úremove_constraint)r   r7   Ú
constraintr   r   r   r<   z   s    ÿþýz&DatabaseSchemaEditor.remove_constraintc                    s0   | j |dd„ |jD ƒ|jd� tƒ  ||¡ d S )Nc                 S   s   g | ]\}}|‘qS r   r   )Ú.0Ú
field_nameÚ_r   r   r   Ú
<listcomp>‰   ó    z5DatabaseSchemaEditor.remove_index.<locals>.<listcomp>r8   )r;   Zfields_ordersr:   r   Úremove_index)r   r7   Úindexr   r   r   rC   †   s    ýz!DatabaseSchemaEditor.remove_indexc                    sV   t ƒ  ||¡sdS | jj | j ¡ |jj¡}|dkrJ| ¡ dkrJ|j	rJdS |  
|¡ S )NFZInnoDBÚ
ForeignKey)r   Ú_field_should_be_indexedr   ÚintrospectionZget_storage_engineÚcursorr5   r6   Úget_internal_typeZdb_constraintr(   )r   r7   r'   Zstorager   r   r   rF   Ž   s    ÿÿ
þýz-DatabaseSchemaEditor._field_should_be_indexed)r:   c                   sî   d}|r|d }n4|rF| j jjrFt|d tƒrFt|d jvrF|d j}|sNdS |j |¡}| 	¡ dkrê| j j
 |j¡‰ | j  ¡ �6}‡ fdd„| j j
 ||jj¡ ¡ D ƒ}W d  ƒ n1 s¼0    Y  t|ƒdkrê|  | j||gdd�¡ dS )	aŠ  
        MySQL can remove an implicit FK index on a field when that field is
        covered by another index like a unique_together. "covered" here means
        that the more complex index has the FK field as its first field (see
        https://bugs.mysql.com/bug.php?id=37910).

        Manually create an implicit FK index to make it possible to remove the
        composed index.
        Nr   rE   c                    s,   g | ]$\}}|d  r|d d ˆ kr|‘qS )rD   Úcolumnsr   r   )r>   ÚnameZinfodict©r0   r   r   rA   Ä   s   üzADatabaseSchemaEditor._create_missing_fk_index.<locals>.<listcomp>é   Ú )r9   Úsuffix)r   ÚfeaturesZsupports_expression_indexesr   r   r   rK   r5   Ú	get_fieldrI   rG   Zidentifier_converterr0   rH   Zget_constraintsr6   ÚitemsÚlenr4   Z_create_index_sql)r   r7   r9   r:   Zfirst_field_nameZfirst_fieldrH   Zconstraint_namesr   rL   r   r;       s:    
ÿþýü
ÿ
ÿþ$	ÿz-DatabaseSchemaEditor._create_missing_fk_indexc                    s$   | j ||d� tƒ j||g|¢R Ž S )N)r9   )r;   r   Ú_delete_composed_index)r   r7   r9   Úargsr   r   r   rT   Ò   s    z+DatabaseSchemaEditor._delete_composed_indexc                 C   s   |j r|d7 }n|d7 }|S )zt
        Keep the null property of the old field. If it has changed, it will be
        handled separately.
        z NULLz	 NOT NULL)Únull)r   r'   Únew_typer   r   r   Ú_set_field_new_type_null_statusÖ   s    
z4DatabaseSchemaEditor._set_field_new_type_null_statusc                    s"   |   ||¡}tƒ  ||||||¡S r$   )rX   r   Ú_alter_column_type_sql)r   r7   Ú	old_fieldÚ	new_fieldrW   Zold_collationZnew_collationr   r   r   rY   á   s    ÿz+DatabaseSchemaEditor._alter_column_type_sqlc                    s   |   ||¡}tƒ  ||||¡S r$   )rX   r   Ú_rename_field_sql)r   r/   rZ   r[   rW   r   r   r   r\   é   s    z&DatabaseSchemaEditor._rename_field_sqlc                 C   s   dg fS )NrN   r   )r   r7   r[   rW   Znew_db_commentr   r   r   Ú_alter_column_comment_sqlí   s    z.DatabaseSchemaEditor._alter_column_comment_sqlc                    s   t ƒ  |¡}d|› �S )Nz	 COMMENT )r   Ú_comment_sql)r   ÚcommentZcomment_sqlr   r   r   r^   ñ   s    z!DatabaseSchemaEditor._comment_sql)(Ú__name__Ú
__module__Ú__qualname__Zsql_rename_tableZsql_alter_column_nullZsql_alter_column_not_nullZsql_alter_column_typeZ sql_alter_column_no_default_nullZsql_delete_columnZsql_delete_uniqueZsql_create_column_inline_fkZsql_delete_fkZsql_delete_indexZsql_rename_indexZsql_create_pkZsql_delete_pkZsql_create_indexZsql_alter_table_commentZsql_alter_column_commentÚpropertyr   r   r#   r(   r+   r,   r)   r.   r1   r<   rC   rF   r;   rT   rX   rY   r\   r]   r^   Ú__classcell__r   r   r   r   r      sR   ÿÿ


û2r   N)	Zdjango.db.backends.base.schemar   Zdjango.db.modelsr   r   r   Zdjango.db.models.constantsr   r   r   r   r   r   Ú<module>   s   