3
(hj6                 @   s\   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)Decimal)Apps)BaseDatabaseSchemaEditor)sixc                   s   e Zd ZdZdZdZdZ fddZ fddZd	d
 Z	g g g ddfddZ
d fdd	Zdd Zdd ZdddZdd Zdd Zdd Z  ZS ) DatabaseSchemaEditorzDROP TABLE %(table)sz'REFERENCES %(to_table)s (%(to_column)s)z7CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)zDROP INDEX %(name)sc          
      sF   | j j (}|jd |j d | _|jd W d Q R X tt| j S )NzPRAGMA foreign_keysr   zPRAGMA foreign_keys = 0)
connectioncursorexecuteZfetchone_initial_pragma_fksuperr   	__enter__)selfc)	__class__ E/tmp/pip-install-q3hcpn_q/Django/django/db/backends/sqlite3/schema.pyr      s
    
zDatabaseSchemaEditor.__enter__c                sB   t t| j||| | jj }|jdt| j  W d Q R X d S )NzPRAGMA foreign_keys = %s)r   r   __exit__r   r   r	   intr
   )r   exc_type	exc_value	tracebackr   )r   r   r   r      s    zDatabaseSchemaEditor.__exit__c             C   s  ydd l }|j|}W n( tk
r*   Y n |jk
r>   Y nX t|tdrZtt|S t|tt	frpt|S t|t
jrt|S t|t
jrdt
j|jdd S |d krdS t|ttt
jfrt|}tjd}||\}}d|jd	 S td
|t|f d S )Nr   Tz'%s''z''ZNULL	hex_codeczX'%s'asciiz*Cannot quote parameter value %r of type %s)sqlite3ZadaptImportErrorZProgrammingError
isinstancetypestrr   r   floatr   integer_typesstring_types	text_typereplacebytes	bytearray
memoryviewcodecs
getencoderdecode
ValueError)r   valuer   Zhex_encoderZ	value_hex_lengthr   r   r   quote_value#   s.    
z DatabaseSchemaEditor.quote_valueNc                sN  fdd  fddj jD }fddj jD }i d}	tdd |D sdtd	d |D rx<t|j D ],\}
}|jrrd
|_|}	|jrr||
= ||j= qrW x:|D ]2}|||j< |j	 r|j
rjj|||j< qW x|D ]\}}|j|jd |j|jd |||j< |jrR|j rRdj|jjj|d }|||j< nj|j||j< |j|j< qW xB|D ]:}||j= ||j= |j	rz|jjj jrzj|jjS qzW t }tj|}|dkrfddj jD }|dkrfddj jD }j jj j|||d}tdt |}||d< j|d< tj jj|tj dd }|j jd  j!j jj j fddj"D _"j# t|j }j$djj jdj%fdd|D dj%dd |D jj jf  jd
d W dQ R X xj"D ]}j$| q$W g _"|	rJd|	_dS )a  
        Shortcut to transform a model from old_model into new_model

        The essential steps are:
          1. rename the model's existing table, e.g. "app_model" to "app_model__old"
          2. create a table with the updated definition called "app_model"
          3. copy the data from the old renamed table to the new table
          4. delete the "app_model__old" table
        c                s   | j o| jj kS )N)Zis_relationremote_fieldmodel)f)r/   r   r   is_self_referentialT   s    z?DatabaseSchemaEditor._remake_table.<locals>.is_self_referentialc                s$   i | ]} |r|j  n||jqS r   )clonename).0r0   )r1   r   r   
<dictcomp>W   s   z6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>c                s   i | ]} j |j|jqS r   )
quote_namecolumn)r4   r0   )r   r   r   r5   ]   s    Nc             s   s   | ]}|j V  qd S )N)primary_key)r4   r0   r   r   r   	<genexpr>c   s    z5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>c             s   s   | ]\}}|j V  qd S )N)r8   )r4   onr   r   r   r9   c   s    Fzcoalesce(%(col)s, %(default)s))coldefaultc                s   g | ]} fd d|D qS )c                s   g | ]} j ||qS r   )get)r4   r;   )rename_mappingr   r   
<listcomp>   s    zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>r   )r4   unique)r?   r   r   r@      s   z6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>c                s   g | ]} fd d|D qS )c                s   g | ]} j ||qS r   )r>   )r4   r;   )r?   r   r   r@      s    zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>r   )r4   index)r?   r   r   r@      s   )	app_labeldb_tableunique_togetherindex_togetherappsZMeta
__module__c             s   s"   | j j}|| j _d V  || j _d S )N)_metarD   )r/   Ztemporary_table_nameZoriginal_table_namer   r   r   altered_table_name   s    z>DatabaseSchemaEditor._remake_table.<locals>.altered_table_nameZ__oldc                s   g | ]} j j|kr|qS r   )rI   rD   )r4   x)
temp_modelr   r   r@      s    z%INSERT INTO %s (%s) SELECT %s FROM %sz, c             3   s   | ]\}} j |V  qd S )N)r6   )r4   rK   y)r   r   r   r9      s    c             s   s   | ]\}}|V  qd S )Nr   )r4   rK   rM   r   r   r   r9      s    )handle_autom2mT)&rI   Zlocal_concrete_fieldsanylistitemsr8   auto_createdr7   r3   many_to_manyZconcreter-   Zeffective_defaultpopnullr6   r.   throughdelete_modelr   copydeepcopyrE   rF   rC   rD   r   tuplerH   Zobject_name	__bases__
contextlibcontextmanagerZalter_db_tableZdeferred_sqlcreate_modelr	   join)r   r/   create_fieldsdelete_fieldsalter_fieldsoverride_uniquesoverride_indexesbodymappingZrestore_pk_fieldr3   field	old_field	new_fieldZcase_sqlrG   Zmeta_contentsmetarJ   Z
field_mapsZsqlr   )r1   r/   r?   r   rL   r   _remake_tableF   s    
$











z"DatabaseSchemaEditor._remake_tableTc                s8   |rt t| j| n| j| jd| j|jji  d S )Ntable)r   r   rW   r	   sql_delete_tabler6   rI   rD   )r   r/   rN   )r   r   r   rW      s    z!DatabaseSchemaEditor.delete_modelc             C   s4   |j r |jjjjr | j|jjS | j||gd dS )z
        Creates a field on a model.
        Usually involves adding a column, but may involve adding a
        table instead (for M2M fields)
        )r`   N)rS   r.   rV   rI   rR   r^   rk   )r   r/   rg   r   r   r   	add_field   s    zDatabaseSchemaEditor.add_fieldc             C   sP   |j r"|jjjjrL| j|jj n*|j| jdd dkr<dS | j||gd dS )z
        Removes a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        )r   r   N)ra   )	rS   r.   rV   rI   rR   rW   Zdb_parametersr   rk   )r   r/   rg   r   r   r   remove_field   s    z!DatabaseSchemaEditor.remove_fieldFc	       	      C   s   | j |||fgd dS )z<Actually perform a "physical" (non-ManyToMany) field update.)rb   N)rk   )	r   r/   rh   ri   Zold_typeZnew_typeZold_db_paramsZnew_db_paramsstrictr   r   r   _alter_field   s    z!DatabaseSchemaEditor._alter_fieldc             C   s   | j ||d dS )z
        Deals with a model changing its index_together.
        Note: The input index_togethers must be doubly-nested, not the single-
        nested ["foo", "bar"] format.
        )rd   N)rk   )r   r/   Zold_index_togetherZnew_index_togetherr   r   r   alter_index_together  s    z)DatabaseSchemaEditor.alter_index_togetherc             C   s   | j ||d dS )z
        Deals with a model changing its unique_together.
        Note: The input unique_togethers must be doubly-nested, not the single-
        nested ["foo", "bar"] format.
        )rc   N)rk   )r   r/   Zold_unique_togetherZnew_unique_togetherr   r   r   alter_unique_together	  s    z*DatabaseSchemaEditor.alter_unique_togetherc             C   s   |j jjj|j jjjkrb| j|j j|j jjj|j |j jjj|j fg|j |j fd dS | j|j j | j	d| j
|j jjjdjd|j |j gdjd|j |j g| j
|j jjjf  | j|j j dS )z=
        Alters M2Ms to repoint their to= endpoints.
        )rb   rc   Nz%INSERT INTO %s (%s) SELECT %s FROM %sz, id)r.   rV   rI   rD   rk   	get_fieldZm2m_reverse_field_nameZm2m_field_namer^   r	   r6   r_   Zm2m_column_nameZm2m_reverse_namerW   )r   r/   rh   ri   rp   r   r   r   _alter_many_to_many  s(    

z(DatabaseSchemaEditor._alter_many_to_many)T)F)__name__rH   __qualname__rm   Zsql_create_inline_fkZsql_create_uniqueZsql_delete_uniquer   r   r-   rk   rW   rn   ro   rq   rr   rs   rv   __classcell__r   r   )r   r   r      s"   # 	
r   )r'   r\   rX   decimalr   Zdjango.apps.registryr   Zdjango.db.backends.base.schemar   Zdjango.utilsr   r   r   r   r   r   <module>   s   