
    afN                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ  G d	 d
e          ZdS )    N)Decimal)Apps)NotSupportedError)BaseDatabaseSchemaEditor)	Statement)strip_quotes)NOT_PROVIDEDUniqueConstraintc                        e Zd ZdZdZdZeZdZdZdZ	dZ
dZ fdZ fdZd	 Zd
 Z	 ddZd fd	Z fdZ fdZ	 ddZd Z fdZ fdZd Z xZS )DatabaseSchemaEditorzDROP TABLE %(table)sNzEREFERENCES %(to_table)s (%(to_column)s) DEFERRABLE INITIALLY DEFERREDz,ALTER TABLE %(table)s DROP COLUMN %(column)sz7CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)zDROP INDEX %(name)sc                     | j                                         st          d          t                                                      S )NzSQLite schema editor cannot be used while foreign key constraint checks are enabled. Make sure to disable them before entering a transaction.atomic() context because SQLite does not support disabling them in the middle of a multi-statement transaction.)
connectiondisable_constraint_checkingr   super	__enter__)self	__class__s    S/var/www/html/env/lib/python3.11/site-packages/django/db/backends/sqlite3/schema.pyr   zDatabaseSchemaEditor.__enter__   sJ     ::<< 	#1   ww  """    c                     | j                                          t                                          |||           | j                                          d S N)r   check_constraintsr   __exit__enable_constraint_checking)r   exc_type	exc_value	tracebackr   s       r   r   zDatabaseSchemaEditor.__exit__&   sM    ))+++9i8882244444r   c                 L   	 dd l }|                    |          }n# t          $ r Y n|j        $ r Y nw xY wt	          |t
                    rt          t          |                    S t	          |t          t          t          f          rt          |          S t	          |t                    rd|
                    dd          z  S |dS t	          |t          t          t          f          rd|                                z  S t          d|dt!          |                    )	Nr   z'%s''z''NULLzX'%s'zCannot quote parameter value z	 of type )sqlite3adaptImportErrorProgrammingError
isinstanceboolstrintr   floatreplacebytes	bytearray
memoryviewhex
ValueErrortype)r   valuer!   s      r   quote_valuez DatabaseSchemaEditor.quote_value+   s3   	NNNMM%((EE 	 	 	D' 	 	 	D	 eT"" 	s5zz??"455 	u::s## 	EMM#t4444]6y*=>> 	 UYY[[((*@EtE{{{S  s    
4	44c                 ,    |                      |          S r   )r2   )r   r1   s     r   prepare_defaultz$DatabaseSchemaEditor.prepare_defaultJ   s    &&&r   c                 T
    fdfdj         j        D             } fdj         j        D             }i d}|pg }t          |dd          st          d |D                       rdt	          |                                          D ]B\  }|j        r6t          fd|D                       sd|_        |}|j        r|= ||j        = C|r]|||j	        <   |j
        t          u rE|j        s>|j        s7|j        r0                                          |                    ||j        <   |D ]}	|	\  }
}|                    |
j	        d           |                    |
j        d           |||j	        <   |
j        r|j        sz|j
        t          u r)                                          |                    }n                     |          \  }}d	                     |
j                  |d
z  }|||j        <   n"                     |
j                  ||j        <   |j	        |
j	        <   r_|j	        = |                    j        d           j        r5j        j        j         j        r                     j        j                  S t1                      }fdj         j        D             }fdj         j        D             }j         j        }rfd|D             }t	          j         j                  }t;          j        |          }j         j        j         j         |||||d}tC          dd|          }||d<   j"        |d<   tC          j         j#        j$        |           t;          j        |          }j         j        dtK          j         j                   z  |||||d}tC          dd|          }||d<   j"        |d<   tC          dj         j#        z  j$        |          } &                    |            '                    d                     |j         j                   dd(                     fd|D                       dd(                    |)                                          d                     j         j                                                   d            *                    ||j         j         j         j                     j+        D ]} '                    |           g  _+        |r	d|_        dS dS )a|  
        Shortcut to transform a model from old_model into new_model

        This follows the correct procedure to perform non-rename or column
        addition operations based on SQLite's documentation

        https://www.sqlite.org/lang_altertable.html#caution

        The essential steps are:
          1. Create a table with the updated definition called "new__app_model"
          2. Copy the data from the existing "app_model" table to the new table
          3. Drop the "app_model" table
          4. Rename the "new__app_model" table to "app_model"
          5. Restore any index of the previous "app_model" table.
        c                 .    | j         o| j        j        u S r   )is_relationremote_fieldmodel)fr9   s    r   is_self_referentialz?DatabaseSchemaEditor._remake_table.<locals>.is_self_referentialc   s    =BQ^%9U%BBr   c                 ^    i | ])}|j          |          r|                                n|*S  )nameclone).0r:   r;   s     r   
<dictcomp>z6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>g   sJ     
 
 
 F!4!4Q!7!7>AGGIIIQ
 
 
r   c                 b    i | ]+}|j         d u |j                            |j                  ,S F)	generatedcolumn
quote_name)r@   r:   r   s     r   rA   z6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>m   sB     
 
 
{e## Hdooah//###r   Nprimary_keyFc              3   >   K   | ]\  }}t          |d d          V  dS )rG   FN)getattr)r@   _	new_fields      r   	<genexpr>z5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>x   sC       >
 >
9EIGI}e44>
 >
 >
 >
 >
 >
r   c              3   4   K   | ]\  }}|j         k    V  d S r   )r>   )r@   rJ   rK   r>   s      r   rL   z5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>|   sD       1 1 %9 IN*1 1 1 1 1 1r   zcoalesce(%(col)s, %(default)s))coldefaultc                 ,    g | ]}fd |D             S )c                 <    g | ]}                     ||          S r=   getr@   nrename_mappings     r   
<listcomp>zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>   s)    666!^1%%666r   r=   )r@   uniquerV   s     r   rW   z6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>   s=     
 
 
 7666v666
 
 
r   c                 ,    g | ]}fd |D             S )c                 <    g | ]}                     ||          S r=   rR   rT   s     r   rW   zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>   s)    555!^1%%555r   r=   )r@   indexrV   s     r   rW   z6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>   s=     
 
 
 6555u555
 
 
r   c                 0    g | ]}j         |j        v|S r=   )r>   fields)r@   r[   delete_fields     r   rW   z6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>   s/       l.?u|.S.S.S.S.Sr   )	app_labeldb_tableunique_togetherindex_togetherindexesconstraintsappsMetar=   
__module__znew__%szNew%sINSERT INTO  (, c              3   B   K   | ]}                     |          V  d S r   )rF   )r@   xr   s     r   rL   z5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>   s/      >>$//!,,>>>>>>r   	) SELECT  FROM )handle_autom2mT),_metalocal_concrete_fieldsrI   anylistitemsrG   auto_createdrE   r>   
db_defaultr	   many_to_manyrD   concreter4   effective_defaultpopnulldb_default_sqlrF   r8   throughdelete_modelr   ra   rb   rc   rd   copydeepcopyr_   r`   r0   rg   object_name	__bases__r   create_modelexecutejoinvaluesalter_db_tabledeferred_sql)r   r9   create_fieldr^   alter_fieldsbodymappingrestore_pk_fieldfieldalter_field	old_fieldrK   rO   rJ   case_sqlre   ra   rb   rc   rd   	body_copymeta_contentsmeta	new_modelsqlr;   r>   rV   s   `` `                     @@@r   _remake_tablez"DatabaseSchemaEditor._remake_tableM   sx   ,	C 	C 	C 	C 	C
 
 
 
[6
 
 

 
 
 
[6
 
 
   #)r<66 	2# >
 >
IU>
 >
 >
 ;
 ;
 	2  $DJJLL11 2 2e$ 
2S 1 1 1 1 )5	1 1 1 . . 
2 ).E%',$) 2 J#EL1 
	&2D"# '<77%2 86B6L 8 ) 8 04/C/C**<880 0+, ( 	< 	<K#. IyHHY^T***KK	($///#,D ~ Nin N'<77"2243I3I)3T3TUUGG!%!4!4Y!?!?JGQ;??9+;<<&? ?  -5	()),0OOI<L,M,M	()-6^N9>** 	L\&'KK+T222 )L -5;HL (()B)JKKKvv
 
 
 
+5
 
 

 
 
 
3
 
 

 +% 	   #*  G 5;233
 M$''	 .,.,&
 
 FB.. 	&"'"2	,U[$eoyAAA M$''	.!L1E$F$FF.,&
 
 FB.. 	&"'"2	,5;#::EOYWW	 	)$$$ 	 	 89999		>>>>g>>>>>>>		'..**++++ 4555	
 	
 	
 	%666 	O$K 	
 	
 	
 $ 	 	CLL 	0+/(((	0 	0r   Tc                    |r#t                                          |           d S |                     | j        d|                     |j        j                  iz             t          | j                  D ]P}t          |t                    r9|                    |j        j                  r| j                            |           Qd S )Ntable)r   r~   r   sql_delete_tablerF   rp   r`   rs   r   r%   r   references_tableremove)r   r9   ro   r   r   s       r   r~   z!DatabaseSchemaEditor.delete_model  s     	2GG  ''''' LL%T__U[-ABB   D-.. 2 2c9-- 2#2F2FK(3 3 2 %,,S111	2 2r   c                    ddl m} |j        r7|j        j        j        j        r!|                     |j        j                   dS |j        sF|j	        s?|j
        r8|                     |          #|j        t          ur.t          |j        |          s|                     ||           dS t!                                          ||           dS )zCreate a field on a model.r   )ValueN)r   )django.db.models.expressionsr   rw   r8   r}   rp   ru   r   rG   rX   r{   ry   rv   r	   r%   r   r   	add_field)r   r9   r   r   r   s       r   r   zDatabaseSchemaEditor.add_field$  s    666666  	,%"4"<"B"O 	,e0899999 	, |		,
 :	, %%e,,8  44"5#3U;; 5 u599999GGeU+++++r   c                    |j         r9|j        j        j        j        r!|                     |j        j                   dS dS | j        j        j        rG|j	        s@|j
        s9|j        s2|j        r|j        s$t                                          ||           dS |                    | j                  d         dS |                     ||           dS )z
        Remove a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        )r   r0   N)r^   )rw   r8   r}   rp   ru   r~   r   featurescan_alter_table_drop_columnrG   rX   db_indexdb_constraintr   remove_fielddb_parametersr   )r   r9   r   r   s      r   r   z!DatabaseSchemaEditor.remove_field@  s     	:!)/< >!!%"4"<=====> > O$@	: %		:
 L	: N	: '	: -2,?	: GG  ..... ""do">>vFNu599999r   Fc	                    |j         |j         k    r|                     ||          |                     ||          k    rQ|j        r|j        sC|j        r|j        s5|                     |                     |j        j        |||                    S |                     |||fg           |	                    d          }	|	                    d          }
|j
        r||k    s|	|
k    rt                      }|j        j        }|j        D ]r}|j        |k    r|j        s+|j        |j        k    r|                    |j                   @|j        r+|j        j        j        r|                    |j                   s|j        rK|j        D ]C}|j        |k    r|j        j        j        j        r|                    |j        j                   D|D ]}|                     |           dS dS dS )z3Perform a "physical" (non-ManyToMany) field update.r   	collationN)rE   
column_sqlr8   r   r   _rename_field_sqlrp   r`   r   rS   rX   setr9   related_objectsrelated_modelrw   
field_namer>   addrG   r}   ru   )r   r9   r   rK   old_typenew_typeold_db_paramsnew_db_paramsstrictold_collationnew_collationrelated_modelsoptsr8   rw   r   s                   r   _alter_fieldz!DatabaseSchemaEditor._alter_field\  sQ    	 000y11T__UI5V5VVV& W + W )	 W
 + W <<&&K()Y    	5I0F/GHHH%))+66%))+66 	2  M]$B$B UUN?(D $ 4 = =-66#0 =#.).@@&**<+EFFF* =|/C/I/V ="&&|';<<<$ N$($5 N NL#1U:: #08>K N&**<+D+LMMM!/ 2 2""=1111-	2 	2$B$B(2 2r   c                    |j         j        j        j        |j         j        j        j        k    r|                     |j         j        |j         j        j                            |                                          |j         j        j                            |                                          f|j         j        j                            |                                          |j         j        j                            |                                          fg           dS |                     |j         j                   | 	                    d| 
                    |j         j        j        j                  dd                    d|                                |                                g          dd                    d|                                |                                g          d| 
                    |j         j        j        j                             |                     |j         j                   dS )	z*Alter M2Ms to repoint their to= endpoints.r   Nrh   ri   rj   idrm   rn   )r8   r}   rp   r`   r   	get_fieldm2m_reverse_field_namem2m_field_namer   r   rF   r   m2m_column_namem2m_reverse_namer~   )r   r9   r   rK   r   s        r   _alter_many_to_manyz(DatabaseSchemaEditor._alter_many_to_many  s@    "*09%-3<= =
 &. ".6<FF%<<>>  ".6<FF%<<>> 
 ".6<FF%4466  ".6<FF%4466 
    6 F 	)08999 	 6 > D MNNNN		!1133!2244    		!1133!2244    	 6 > D MNNN#	
 	
 	
, 	)0899999r   c                     t          |t                    r@|j        s|j        s|j        s|j        r$t                                          ||           d S |                     |           d S r   )	r%   r
   	conditioncontains_expressionsinclude
deferrabler   add_constraintr   r   r9   
constraintr   s      r   r   z#DatabaseSchemaEditor.add_constraint  s    j"233 	& 	&.	& !	& $		& GG""5*55555u%%%%%r   c                     t          |t                    r@|j        s|j        s|j        s|j        r$t                                          ||           d S |                     |           d S r   )	r%   r
   r   r   r   r   r   remove_constraintr   r   s      r   r   z&DatabaseSchemaEditor.remove_constraint  s    j"233 	& 	&.	& !	& $		& GG%%eZ88888u%%%%%r   c                     d|z   S )NzCOLLATE r=   )r   r   s     r   _collate_sqlz!DatabaseSchemaEditor._collate_sql  s    I%%r   )NNN)TrC   )__name__rg   __qualname__r   sql_create_fksql_create_inline_fksql_create_column_inline_fksql_delete_columnsql_create_uniquesql_delete_uniquesql_alter_table_commentsql_alter_column_commentr   r   r2   r4   r   r~   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s       -MO  #7FQ-"## # # # #5 5 5 5 5
  >' ' ' IMC0 C0 C0 C0J2 2 2 2 2 2$, , , , ,8: : : : :J 82 82 82 82t>: >: >:@	& 	& 	& 	& 	&	& 	& 	& 	& 	&& & & & & & &r   r   )r   decimalr   django.apps.registryr   	django.dbr   django.db.backends.base.schemar   !django.db.backends.ddl_referencesr   django.db.backends.utilsr   django.db.modelsr	   r
   r   r=   r   r   <module>r      s           % % % % % % ' ' ' ' ' ' C C C C C C 7 7 7 7 7 7 1 1 1 1 1 1 ; ; ; ; ; ; ; ;a& a& a& a& a&3 a& a& a& a& a&r   