ó
ø¢TQc           @  sß   d  d l  m Z 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  d l m Z d „  Z d	 „  Z e e d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   settings(   t   CommandError(   t   models(   t
   get_models(   t   upathc         C  sì  | j  d d k r* t d d d ƒ ‚ n  t j |  d t ƒ} g  } | j j ƒ  } t g  | j j | ƒ D] } | | k rj | ^ qj ƒ } i  } xÉ | D]Á } | j	 j
 | | | ƒ \ }	 }
 | j |	 ƒ xa |
 j ƒ  D]S \ } } | j | g  ƒ j | ƒ | | k rÖ | j | j	 j | | | ƒ ƒ qÖ qÖ W| j | j	 j | | | ƒ ƒ | j | ƒ q˜ Wt | j ƒ  ƒ } | règ  } xD | D]< } | j g  | j	 j | | | ƒ D] } d | ^ q§ƒ q‚W| rè| j d ƒ | j | ƒ qèn  | S(	   uD   Returns a list of the CREATE TABLE SQL statements for the given app.u   ENGINEu   django.db.backends.dummyuA   Django doesn't know which syntax to use for your SQL statements,
uL   because you haven't properly specified the ENGINE setting for the database.
uB   see: https://docs.djangoproject.com/en/dev/ref/settings/#databasest   include_auto_createdu   -- uN   -- The following references should be added but depend on non-existent tables:(   t   settings_dictR   R   R   t   Truet   introspectiont   table_namest   sett   installed_modelst   creationt   sql_create_modelt   extendt   itemst
   setdefaultt   sql_for_pending_referencest   addt   keyst   append(   t   appt   stylet
   connectiont
   app_modelst   final_outputt   tablest   modelt   known_modelst   pending_referencest   outputt
   referencest   reftot   refst   not_installed_modelst	   alter_sqlt   sql(    (    s3   ../Django//lib/python/django/core/management/sql.pyt
   sql_create   s8    7&	1c         C  s  y | j  ƒ  } Wn d } n X| r; | j j | ƒ } n g  } g  } t ƒ  } i  } t j |  d t ƒ} x¢ | D]š }	 | rr | j j |	 j	 j
 ƒ | k rr |	 j	 }
 xT |
 j D]I } | j r¯ | j j | k r¯ | j | j j g  ƒ j |	 | f ƒ q¯ q¯ W| j |	 ƒ qr qr WxN | D]F }	 | j j |	 j	 j
 ƒ | k r| j | j j |	 | | ƒ ƒ qqW| r~| j ƒ  | j ƒ  n  | d d d … S(   uB   Returns a list of the DROP TABLE SQL statements for the given app.R   Niÿÿÿÿ(   t   cursort   NoneR	   R
   R   R   R   R   t   table_name_convertert   _metat   db_tablet   local_fieldst   relt   toR   R   R   R   R   t   sql_destroy_modelt   close(   R   R   R   R'   R
   R   t	   to_deletet   references_to_deleteR   R   t   optst   f(    (    s3   ../Django//lib/python/django/core/management/sql.pyt
   sql_delete<   s2    
	$	,&
c         C  sd   | r | j  j d t ƒ } n | j  j ƒ  } | rB | j  j ƒ  n d } | j j |  | | ƒ } | S(   uÐ   
    Returns a list of the SQL statements used to flush the database.

    If only_django is True, then only table names that have associated Django
    models and are in INSTALLED_APPS will be included.
    t   only_existing(    (   R	   t   django_table_namesR   R
   t   sequence_listt   opst	   sql_flush(   R   R   t   only_djangot   reset_sequencesR   t   seqst
   statements(    (    s3   ../Django//lib/python/django/core/management/sql.pyR:   i   s    c         C  s@   g  } t  |  ƒ } x' | D] } | j t | | | ƒ ƒ q W| S(   uN   Returns a list of the custom table modifying SQL statements for the given app.(   R   R   t   custom_sql_for_model(   R   R   R   R   R   R   (    (    s3   ../Django//lib/python/django/core/management/sql.pyt
   sql_customy   s
    c         C  s@   g  } x3 t  j |  ƒ D]" } | j | j j | | ƒ ƒ q W| S(   uR   Returns a list of the CREATE INDEX SQL statements for all models in the given app.(   R   R   R   R   t   sql_indexes_for_model(   R   R   R   R   R   (    (    s3   ../Django//lib/python/django/core/management/sql.pyt   sql_indexes…   s     c         C  s0   t  |  | | ƒ t |  | | ƒ t |  | | ƒ S(   ud   Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module.(   R&   R@   RB   (   R   R   R   (    (    s3   ../Django//lib/python/django/core/management/sql.pyt   sql_all   s    c         C  s˜   t  j d ƒ } g  } g  } xv |  j d ƒ D]e } | j d | ƒ j ƒ  } | sU q+ n  | j | ƒ | j d ƒ r+ | j d j | ƒ ƒ g  } q+ q+ W| S(   Nu   ^((?:'[^']*'|[^'])*?)--.*$u   
u   \1u   ;u    (   t   ret   compilet   splitt   subt   stripR   t   endswitht   join(   t   contentt
   comment_reR>   t	   statementt   linet   cleaned_line(    (    s3   ../Django//lib/python/django/core/management/sql.pyt   _split_statements’   s    c         C  s†  |  j  } t j j t j j t j j t t j |  j  j	 ƒ j
 ƒ ƒ d ƒ ƒ } g  } | j r» g  | j D] } t | d ƒ rg | ^ qg } x0 | D]% } | j | j | |  j  j ƒ ƒ q Wn  | j d j d ƒ d } t j j | d | j j ƒ  | f ƒ t j j | d | j j ƒ  ƒ g }	 x` |	 D]X }
 t j j |
 ƒ r&t j |
 d d	 t j ƒ  } | j t | j ƒ  ƒ ƒ Wd  QXq&q&W| S(
   Nu   sqlu   post_create_sqlu   ENGINEu   .iÿÿÿÿu	   %s.%s.sqlu   %s.sqlu   Ut   encoding(   R*   t   ost   patht   normpathRJ   t   dirnameR   R   t   get_appt	   app_labelt   __file__t   managedR,   t   hasattrR   t   post_create_sqlR+   R   RF   t   object_namet   lowert   existst   codecst   openR   t   FILE_CHARSETRP   t   read(   R   R   R   R3   t   app_dirR   R4   t   post_sql_fieldst   backend_namet	   sql_filest   sql_filet   fp(    (    s3   ../Django//lib/python/django/core/management/sql.pyR?   ¡   s    	E	+&%%&c         C  s}   xv t  j ƒ  D]h } | j j d ƒ d } | d k rA d | GHn  t  j j j d | d | d |  d | d	 | d
 | ƒ q Wd  S(   Nu   .iþÿÿÿi   u-   Running post-sync handlers for application %st   senderR   t   created_modelst	   verbosityt   interactivet   db(   R   t   get_appst   __name__RF   t   signalst   post_syncdbt   send(   Rj   Rk   Rl   Rm   R   t   app_name(    (    s3   ../Django//lib/python/django/core/management/sql.pyt   emit_post_sync_signal»   s    (   t
   __future__R    R_   RR   RD   t   django.confR   t   django.core.management.baseR   t	   django.dbR   t   django.db.modelsR   t   django.utils._osR   R&   R5   t   FalseR   R:   R@   RB   RC   RP   R?   Rt   (    (    (    s3   ../Django//lib/python/django/core/management/sql.pyt   <module>   s"   	.	-					