ó
TR¹Nc           @   sà   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  d l m Z d  d l m Z d	 „  Z d
 „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿN(   t   settings(   t   generic(   t   CommandError(   t
   dispatcher(   t   models(   t
   get_models(   t   truncate_namec         C   sð  | j  d d k r. t d d d d ƒ ‚ n  t j |  d t ƒ} g  } | j j ƒ  } t g  | j j | ƒ D] } | | k rn | ^ qn ƒ } 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(
   sD   Returns a list of the CREATE TABLE SQL statements for the given app.t   ENGINEs   django.db.backends.dummysA   Django doesn't know which syntax to use for your SQL statements,
sC   because you haven't specified the ENGINE setting for the database.
sS   Edit your settings file and change DATBASES['default']['ENGINE'] to something like
s=   'django.db.backends.postgresql' or 'django.db.backends.mysql't   include_auto_createds   -- sN   -- 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(    (    sG   /home/panlixing/Python_Projects/gaeseries/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(   sB   Returns a list of the DROP TABLE SQL statements for the given app.R   Niÿÿÿÿ(   t   cursort   NoneR   t   get_table_listR   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(    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt
   sql_delete:   s2    
	$	,&
c         C   s    t  |  | | ƒ t |  | | ƒ S(   sV   Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module.(   R8   t   sql_all(   R   R   R   (    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt	   sql_resetf   s    c         C   sR   | r | j  j d t ƒ } n | j  j ƒ  } | j j |  | | j  j ƒ  ƒ } | S(   sÐ   
    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   opst	   sql_flusht   sequence_list(   R   R   t   only_djangoR   t
   statements(    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyR>   j   s
    !c         C   sp   g  } t  |  ƒ } t j j t j j t j j |  j ƒ d ƒ ƒ } x' | D] } | j t | | | ƒ ƒ qI W| S(   sN   Returns a list of the custom table modifying SQL statements for the given app.R'   (	   R   t   ost   patht   normpatht   joint   dirnamet   __file__R   t   custom_sql_for_model(   R   R   R   R!   R   t   app_dirR   (    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt
   sql_customx   s    0c         C   s@   g  } x3 t  j |  ƒ D]" } | j | j j | | ƒ ƒ q W| S(   sR   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   (    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt   sql_indexes„   s     c         C   s0   t  |  | | ƒ t |  | | ƒ t |  | | ƒ S(   sd   Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module.(   R(   RJ   RL   (   R   R   R   (    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyR9   ‹   s    c         C   sÒ  |  j  } t j j t j j t j j t j |  j  j ƒ j	 ƒ d ƒ ƒ } g  } | j
 rµ g  | j D] } t | d ƒ ra | ^ qa } x0 | D]% } | j | j | |  j  j ƒ ƒ q‰ Wn  t j d t j ƒ } | j d j d ƒ d }	 t j j | d | j j ƒ  |	 f ƒ t j j | d | j j ƒ  ƒ g }
 x |
 D]• } t j j | ƒ r5t | d	 ƒ } x^ | j | j ƒ  j t j ƒ ƒ D]; } t j d
 d | ƒ } | j ƒ  r~| j | d ƒ q~q~W| j  ƒ  q5q5W| S(   NR'   t   post_create_sqls   ;[ \t]*$R   t   .iÿÿÿÿs	   %s.%s.sqls   %s.sqlt   Uu   --.*([\n\Z]|$)t    u   ;(!   R-   RB   RC   RD   RE   RF   R   t   get_appt	   app_labelRG   t   managedR/   t   hasattrR   RM   R.   t   ret   compilet   MR	   t   splitt   object_namet   lowert   existst   opent   readt   decodeR    t   FILE_CHARSETt   subt   stripR   R3   (   R   R   R   R6   RI   R!   R7   t   post_sql_fieldsRA   t   backend_namet	   sql_filest   sql_filet   fpt	   statement(    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyRH      s(    	?	+&%%(c         C   s}   xv t  j ƒ  D]h } | j j d ƒ d } | d k rA d G| GHn  t  j j j d | d | d |  d | d	 | d
 | ƒ q Wd  S(   NRN   iþÿÿÿi   s*   Running post-sync handlers for applicationt   senderR   t   created_modelst	   verbosityt   interactivet   db(   R   t   get_appst   __name__RX   t   signalst   post_syncdbt   send(   Ri   Rj   Rk   Rl   R   t   app_name(    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt   emit_post_sync_signal±   s    (   RB   RU   t   django.confR    t   django.contrib.contenttypesR   t   django.core.management.baseR   t   django.dispatchR   t	   django.dbR   t   django.db.modelsR   t   django.db.backends.utilR   R(   R8   R:   t   FalseR>   RJ   RL   R9   RH   Rs   (    (    (    sG   /home/panlixing/Python_Projects/gaeseries/django/core/management/sql.pyt   <module>   s"   	.	,					"