
TQc           @   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
 d Z d e f d     YZ d S(	   iN(   t   settings(   t   load_backend(   t   force_bytes(   t   inputt   test_t   BaseDatabaseCreationc           B   s   e  Z d  Z i  Z d   Z d   Z e   d  Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d e d  Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    This class encapsulates all backend-specific differences that pertain to
    database *creation*, such as the column types to use for particular Django
    Fields, the SQL used to create and destroy tables, and the creation and
    destruction of test databases.
    c         C   s   | |  _  d  S(   N(   t
   connection(   t   selfR   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   __init__   s    c         G   s>   t  j   } x! | D] } | j t |   q W| j   d  S(   sx   
        Generates a 32-bit digest of a set of arguments that can be used to
        shorten identifying names.
        i   (   t   hashlibt   md5t   updateR   t	   hexdigest(   R   t   argst   ht   arg(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   _digest   s    c         C   s  | j  } | j s% | j s% | j r/ g  i  f Sg  } g  } i  } |  j j j } x| j D]}	 |	 j d |  j  }
 |	 j	 p | j	 } |
 d k r qZ n  | j | |	 j   | j |
  g } |	 j } |	 j r |	 j r |  j j j r t } n  | s| j | j d   n  |	 j r5| j | j d   n" |	 j rW| j | j d   n  | r|	 j r|  j j j | d t } | r| j |  qn  |	 j r|  j |	 | |  \ } } | r| j |	 j j g   j | |	 f  q| j |  n  | j d j |   qZ Wxf | j D][ } | j | j d  d d j g  | D]' }	 | j | | j |	  j   ^ qH  q W| j d	  d | j  | | j!   d
 g } xM t" |  D]? \ } } | j d | | t# |  d k  rd pd f  qW| j d  | j	 rF|  j j j | j	  } | rF| j |  qFn  | j d  | j d j |   | j$ r| j% j& p| j% j' } |  j j j( | j! |  } | rx | D] } | j |  qWqn  | | f S(   s   
        Returns the SQL required to create a single model, as a tuple of:
            (list_of_sql, pending_references_dict)
        R   s   NOT NULLs   PRIMARY KEYt   UNIQUEt   inlinet    s    (%s)s   , s   CREATE TABLEs    (s       %s%si   t   ,t    t   )t   ;s   
N()   t   _metat   managedt   proxyt   swappedR   t   opst
   quote_namet   local_fieldst   db_typet   db_tablespacet   Nonet	   SQL_FIELDt   columnt   SQL_COLTYPEt   nullt   empty_strings_allowedt   primary_keyt   featurest!   interprets_empty_strings_as_nullst   Truet   appendt   SQL_KEYWORDt   uniquet   tablespace_sqlt   relt%   sql_for_inline_foreign_key_referencest
   setdefaultt   tot   extendt   joint   unique_togethert	   get_fieldt	   SQL_TABLEt   db_tablet	   enumeratet   lent   has_auto_fieldt
   auto_fieldt	   db_columnt   namet   autoinc_sql(   R   t   modelt   stylet   known_modelst   optst   final_outputt   table_outputt   pending_referencest   qnt   ft   col_typet
   tablespacet   field_outputR%   R.   t
   ref_outputt   pendingt   field_constraintst   full_statementt   it   linet   auto_columnR?   t   stmt(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   sql_create_model%   s|    	
					;01			c         C   s   |  j  j j } | j j | k r | j d  d | j | | j j j j   d | j	 | | j j j j
 | j j  j   d |  j  j j   g } t } n g  } t } | | f S(   sX   
        Return the SQL snippet defining the foreign key reference for a field.
        t
   REFERENCESR   s    (R   (   R   R   R   R/   R2   R,   R7   R   R8   R"   R6   t
   field_nameR#   t   deferrable_sqlt   FalseR*   (   R   t   fieldRB   RA   RG   t   outputRM   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR0   w   s    e	c         C   sM  d d l  m } | j } | j s5 | j s5 | j r9 g  S|  j j j } g  } | | k rIx | | D] \ } }	 | j }
 |
 j	 } |	 j
 } | j	 } | j |	 j j  j
 } d | | |  j | |  f } | j | j d  d | |  | | | |  j j j     | |  | |  | |  |  j j j   f  qe W| | =n  | S(   sW   
        Returns any ALTER TABLE statements to add constraints after the fact.
        i(   t   truncate_names   %s_refs_%s_%ss   ALTER TABLEs<    %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;(   t   django.db.backends.utilR[   R   R   R   R   R   R   R   R8   R#   R6   R/   RV   R   R+   R,   t   max_name_lengthRW   (   R   R@   RA   RF   R[   RC   RG   RD   t	   rel_classRH   t   rel_optst   r_tablet   r_colt   tablet   colt   r_name(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   sql_for_pending_references   s.    					
c         C   s   | j  j s% | j  j s% | j  j r) g  Sg  } x0 | j  j D]" } | j |  j | | |   q< WxY | j  j D]K } g  | D] } | j  j |  d ^ q| } | j |  j	 | | |   qo W| S(   sM   
        Returns the CREATE INDEX SQL statements for a single model.
        i    (
   R   R   R   R   R   R3   t   sql_indexes_for_fieldt   index_togethert   get_field_by_namet   sql_indexes_for_fields(   R   R@   RA   RZ   RH   t   fst   fields(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   sql_indexes_for_model   s    % ) c         C   s1   | j  r) | j r) |  j | | g |  Sg  Sd S(   sR   
        Return the CREATE INDEX SQL statements for a single model field.
        N(   t   db_indexR-   Ri   (   R   R@   RH   RA   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyRf      s    c   
      C   s  d d l  m } t |  d k rN | d j rN |  j j j | d j  } n0 | j j rx |  j j j | j j  } n d } | r d | } n  g  } |  j j j } x- | D]% } | j	 | j
 | | j    q Wd | j j |  j g  | D] } | j ^ q  f }	 | j d  d | j | | |	 |  j j j      d | j d	  d | j | | j j   d d
 | j
 d j |   d | g S(   Ni(   R[   i   i    R   R   s   %s_%ss   CREATE INDEXt   ONs   (%s)s   , s   %s;(   R\   R[   R:   R    R   R   R.   R   R   R+   R"   R#   R8   R   R>   R,   R7   R]   R4   (
   R   R@   Rk   RA   R[   R.   t   field_namesRG   RH   t
   index_name(    (    s4   ../Django//lib/python/django/db/backends/creation.pyRi      s    #5c         C   s   | j  j s% | j  j s% | j  j r) g  S|  j j j } d | j d  | j | | j  j	   f g } | | k r | j
 |  j | | |   n  | j  j r |  j j j | j  j	  } | r | j |  q n  | S(   se   
        Return the DROP TABLE and restraint dropping statements for a single
        model.
        s   %s %s;s
   DROP TABLE(   R   R   R   R   R   R   R   R,   R7   R8   R3   t   sql_remove_table_constraintsR;   t   drop_sequence_sqlR+   (   R   R@   t   references_to_deleteRA   RG   RZ   t   ds(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   sql_destroy_model   s    %"c         C   s=  d d l  m } | j j s5 | j j s5 | j j r9 g  Sg  } |  j j j } x | | D] \ } } | j j	 }	 | j
 }
 | j j	 } | j j | j j  j
 } d |
 | |  j |	 |  f } | j d | j d  | j | |	   | j |  j j j    | j | | | |  j j j      f  qY W| | =| S(   Ni(   R[   s   %s_refs_%s_%ss   %s %s %s %s;s   ALTER TABLE(   R\   R[   R   R   R   R   R   R   R   R8   R#   R6   R/   RV   R   R+   R,   R7   t   drop_foreignkey_sqlR"   R]   (   R   R@   Rs   RA   R[   RZ   RG   R^   RH   Rb   Rc   R`   Ra   Rd   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyRq      s(    %		'i   c   
   
   C   so  d d l  m } |  j   } | d k r_ d } | d k rG d | } n  d |  j j | f GHn  |  j | |  |  j j   | |  j j d <| d	 d
 t | d d  d t	 d |  j j d t	 | d d
 t | d d  d t	 d |  j j d d l
 m } d d l m } xK t j D]@ } | |  }	 t |	 |  r| d |	 j d |  j j qqW|  j j   | S(   s   
        Creates a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        i(   t   call_commandi   R   i   s    ('%s')s*   Creating test database for alias '%s'%s...t   NAMEt   syncdbt	   verbosityi    t   interactivet   databaset   load_initial_datat   flush(   t	   get_cache(   t   BaseDatabaseCachet   createcachetable(   t   django.core.managementRw   t   _get_test_db_nameR   t   aliast   _create_test_dbt   closet   settings_dictt   maxRX   t   django.core.cacheR   t   django.core.cache.backends.dbR   R    t   CACHESt
   isinstancet   _tablet   cursor(
   R   Rz   t   autoclobberRw   t   test_database_namet   test_db_reprR   R   t   cache_aliast   cache(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   create_test_db  s:    		c         C   s0   |  j  j d r |  j  j d St |  j  j d S(   s  
        Internal implementation - returns the name of the test DB that will be
        created. Only useful when called from create_test_db() and
        _create_test_db() and when no external munging is done with the 'NAME'
        or 'TEST_NAME' settings.
        t	   TEST_NAMERx   (   R   R   t   TEST_DATABASE_PREFIX(   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR   ?  s    c   	      C   s\  |  j    } |  j   } |  j j j } |  j j   } |  j   y! | j d | |  | f  Wn t k
 rW} t	 j
 j d |  | s t d |  } n  | s | d k rByV | d k r d |  j j GHn  | j d | |   | j d | |  | f  WqTt k
 r>} t	 j
 j d |  t	 j d	  qTXqXd
 GHt	 j d  n X| S(   sG   
        Internal implementation - creates the test db tables.
        s   CREATE DATABASE %s %ss,   Got an error creating the test database: %s
sX   Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: t   yesi   s$   Destroying old test database '%s'...s   DROP DATABASE %ss.   Got an error recreating the test database: %s
i   s   Tests cancelled.(   t   sql_table_creation_suffixR   R   R   R   R   t   _prepare_for_test_db_ddlt   executet	   Exceptiont   syst   stderrt   writeR   R   t   exit(	   R   Rz   R   t   suffixR   RG   R   t   et   confirm(    (    s4   ../Django//lib/python/django/db/backends/creation.pyR   J  s@    
		c         C   s   |  j  j   |  j  j d } | d k r` d } | d k rH d | } n  d |  j  j | f GHn  |  j  j j   } | | d <t | d  } | j | d d	 d
 t } | j j	 | |  d S(   sv   
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Rx   i   R   i   s    ('%s')s,   Destroying test database for alias '%s'%s...t   ENGINER   t   __destroy_test_db__t   allow_thread_sharingN(
   R   R   R   R   t   copyR   t   DatabaseWrapperRX   t   creationt   _destroy_test_db(   R   t   old_database_nameRz   R   R   R   t   backendt   new_connection(    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   destroy_test_dbw  s     
	c         C   sW   |  j  j   } |  j   t j d  | j d |  j  j j |   |  j  j   d S(   sF   
        Internal implementation - remove the test db tables.
        i   s   DROP DATABASE %sN(	   R   R   R   t   timet   sleepR   R   R   R   (   R   R   Rz   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR     s    
	c         C   s   d S(   s   
        Make sure a connection is in autocommit mode. - Deprecated, not used
        anymore by Django code. Kept for compatibility with user code that
        might use it.
        N(    (   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   set_autocommit  s    c         C   s   d S(   s$  
        Internal implementation - Hook for tasks that should be performed
        before the ``CREATE DATABASE``/``DROP DATABASE`` clauses used by
        testing code to create/ destroy test databases. Needed e.g. in
        PostgreSQL to rollback and close any active transaction.
        N(    (   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR     s    c         C   s   d S(   sQ   
        SQL to append to the end of the test table creation statements.
        R   (    (   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR     s    c         C   s,   |  j  j } | d | d | d | d f S(   s   
        Returns a tuple with elements of self.connection.settings_dict (a
        DATABASES setting value) that uniquely identify a database
        accordingly to the RDBMS particularities.
        t   HOSTt   PORTR   Rx   (   R   R   (   R   R   (    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   test_db_signature  s
    (   t   __name__t
   __module__t   __doc__t
   data_typesR   R   t   setRT   R0   Re   Rl   Rf   Ri   Ru   Rq   RX   R   R   R   R   R   R   R   R   R   (    (    (    s4   ../Django//lib/python/django/db/backends/creation.pyR      s*   		
R								7		-					(   R	   R   R   t   django.confR    t   django.db.utilsR   t   django.utils.encodingR   t   django.utils.six.movesR   R   t   objectR   (    (    (    s4   ../Django//lib/python/django/db/backends/creation.pyt   <module>   s   