
 Xj6                 @   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
 Gd 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 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   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          	      sX   |  j  j   4 } | j d  | j   d |  _ | j d  Wd  QXt t |   j   S)NzPRAGMA foreign_keysr   zPRAGMA foreign_keys = 0)
connectioncursorexecuteZfetchone_initial_pragma_fksuperr   	__enter__)selfc)	__class__ O/home/ubuntu/projects/ifolica/build/django/django/db/backends/sqlite3/schema.pyr      s
    zDatabaseSchemaEditor.__enter__c                sR   t  t |   j | | |  |  j j   ! } | j d t |  j   Wd  QXd  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 |  } Wn& t k
 r3 Yn | j k
 rG Yn Xt | t d   rm t t |   St | t t	 f  r t |  St | t
 j  r t |  St | t
 j  r d t
 j |  j d d  S| d  k r d St | t t t
 j f  r@t |  } t j d  } | |  \ } } d | j d	  St 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                s   f d d       f d d    j  j D }  f d d    j  j D } i   d }	 t d d   | D  s t d	 d   | D  r x[ t | j    D]D \ }
 } | j r d
 | _ | }	 | j r | |
 =| | j =q q q Wn  xP | D]H } | | | j <| j	 r | j
 r  j  j |   | | j <q q Wx | D] \ } } | j | j d  | j | j d  | | | j <| j r| j rd i  j | j  d 6 j  j |   d 6} | | | j <n  j | j  | | j <| j  | j <qFWxP | D]H } | | j =| | j =| j	 r| j j j  j r j | j j  SqWt   } t j |  } | d k r f d d    j  j D } n  | d k r f d d    j  j D } n  i  j  j d 6 j  j d 6| d 6| d 6| d 6} 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
 Wd QXx  j" D] }  j$ |  qjWg   _" |	 rd! |	 _ n  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                s4   i  |  ]* }   |  r$ | j    n | | j  q Sr   )clonename).0r0   )r1   r   r   
<dictcomp>X   s   	z6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>c                s(   i  |  ] }   j  | j  | j  q Sr   )
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 Sr   )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 Sr   )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 Sr   )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    2			 

	

		z"DatabaseSchemaEditor._remake_tableTc                sM   | r t  t |   j |  n* |  j |  j i |  j | j j  d 6 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   sH   | j  r. | j j j j r. |  j | j j  S|  j | d | g 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   sq   | j  r4 | j j j j rm |  j | j j  qm n9 | j d |  j  d d k rW d S|  j | d | g d S)z
        Removes a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        r   r   Nra   )	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  | d | | f g 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 r |  j | j  j d | j  j j j | j    | j  j j j | j    f g d | j   | j   f 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)__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   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   