
 XQ                 @   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 d  d l m Z Gd d	   d	 e  Z d
 S)    )settings)caches)BaseDatabaseCache)BaseCommandCommandError)DEFAULT_DB_ALIASconnectionsmodelsroutertransaction)DatabaseError)
force_textc               @   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   sg   | j  d d d d d d d | j  d d	 d
 d d d t d d | j  d d	 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actionstoredestdatabasedefaultzgNominates a database onto which the cache tables will be installed. Defaults to the "default" database.z	--dry-run
store_truedry_runzADoes not create the table, just prints the SQL that would be run.)add_argumentr   )selfparser r   ^/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/createcachetable.pyadd_arguments   s    zCommand.add_argumentsc             O   s   | d } | d |  _  | d } t |  rT xj | D] } |  j | | |  q4 WnF xC t j D]8 } t | } t | t  r^ |  j | | j |  q^ q^ Wd  S)Nr   	verbosityr   )	r!   lencreate_tabler   ZCACHESr   
isinstancer   Z_table)r   Z
tablenamesoptionsdbr   	tablenameZcache_aliascacher   r   r   handle!   s    


zCommand.handlec             C   sU  t  | i   } t j | | j  s( d  St | } | | j j   k rq |  j d k rm |  j j	 d |  n  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 r1| j d  n |
 j rJ| j d  n  |
 j r|
 j rbd n d } | j d | |	 d | |
 j f  |	 |  |	 |
 j  f  n  | 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 |  } | r~|  j j	 |  x | D] } |  j j	 |  q`Wd  St j d | d | j j   | j   z } y | j |  WnA t  k
 r } z! t! d | t" |  f   WYd  d  } ~ Xn Xx | D] } | j |  qWWd  QXWd  QX|  j d k rQ|  j j	 d  |  n  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.   r2   join	enumerater"   r   ZatomicfeaturesZcan_rollback_ddlcursorexecuter   r   r   )r   r   r'   r   r(   r3   fieldsZtable_outputZindex_outputqnfZfield_outputr.   Zfull_statementiline	statementZcurser   r   r   r#   /   sd    
!			9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   Zdjango.utils.encodingr   r   r   r   r   r   <module>   s   (