
v^                 @   s   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z m Z d  d l	 m
 Z
 m Z m Z m Z m Z d  d l m Z Gd d   d e  Z d	 S)
    )settings)caches)BaseDatabaseCache)BaseCommandCommandError)DEFAULT_DB_ALIASconnectionsmodelsroutertransaction)DatabaseErrorc               @   s@   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 S)
Commandz7Creates the tables needed to use the SQL cache backend.Fc             C   sU   | j  d d d d d d d | j  d d	 t d d
 | j  d d d d d d  S)NargsmetavarZ
table_namenargs*helpzNOptional table names. Otherwise, settings.CACHES is used to find cache tables.z
--databasedefaultzgNominates a database onto which the cache tables will be installed. Defaults to the "default" database.z	--dry-runaction
store_truezADoes not create the table, just prints the SQL that would be run.)add_argumentr   )selfparser r   R/tmp/pip-build-8lau8j11/django/django/core/management/commands/createcachetable.pyadd_arguments   s    zCommand.add_argumentsc             O   s   | d } | d |  _  | d } | rN xg | D] } |  j | | |  q. WnC x@ t j D]5 } t | } t | t  rX |  j | | j |  qX Wd  S)Ndatabase	verbositydry_run)r   create_tabler   ZCACHESr   
isinstancer   Z_table)r   Z
tablenamesoptionsdbr   	tablenameZcache_aliascacher   r   r   handle    s    


zCommand.handlec             C   sE  t  | i   } t j | | j  s( d  St | } | | j j   k rn |  j d k rj |  j j	 d |  d  St
 j d d d d d d d	 d  t
 j d d
  t
 j d d d d  f } g  } g  } | j j }	 x | D] }
 |	 |
 j  |
 j d |  d |
 j sd n d g } |
 j r.| j d  n |
 j rD| j d  |
 j r|
 j r\d n d } | j d | |	 d | |
 j f  |	 |  |	 |
 j  f  | j d j |   q Wd |	 |  g } xM t |  D]? \ } } | j d | | t |  d k  rd n d f  qW| j d  d j |  } | ru|  j j	 |  x | D] } |  j j	 |  qWWd  St j d | d | j j   | j   t } y | j |  Wn; t  k
 r} z t! d | | f   WYd  d  } ~ Xn Xx | D] } | j |  qWWd  QRXWd  QRX|  j d k rA|  j j	 d  |  d  S)!Nr   z Cache table '%s' already exists.name	cache_key
max_length   uniqueTprimary_keyvalueexpiresdb_index
connectionz%sNULLzNOT  zPRIMARY KEYZUNIQUEzUNIQUE zCREATE %sINDEX %s ON %s (%s);z%s_%s zCREATE TABLE %s (z    %s%s   ,z);
ZusingZ	savepointz9Cache table '%s' could not be created.
The error was: %s.zCache table '%s' created.)"r   r
   Zallow_migrate_modelZcache_model_classr   ZintrospectionZtable_namesr   stdoutwriter	   Z	CharFieldZ	TextFieldZDateTimeFieldopsZ
quote_namer&   Zdb_typenullr+   appendr*   r.   join	enumeratelenr   ZatomicfeaturesZcan_rollback_ddlcursorexecuter   r   )r   r   r#   r   r$   r/   fieldsZtable_outputZindex_outputqnfZfield_outputr*   Zfull_statementilineZ	statementZcurser   r   r   r   .   sd    
!			67#zCommand.create_tableN)__name__
__module____qualname__r   Zrequires_system_checksr   r%   r   r   r   r   r   r      s
   r   N)Zdjango.confr   Zdjango.core.cacher   Zdjango.core.cache.backends.dbr   Zdjango.core.management.baser   r   Z	django.dbr   r   r	   r
   r   Zdjango.db.utilsr   r   r   r   r   r   <module>   s   (