ó
øĒTQc           @   sv   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 e
 f d     YZ d S(	   iĸĸĸĸN(   t   settings(   t	   get_cache(   t   BaseDatabaseCache(   t   ImproperlyConfigured(   t   DatabaseCreationt   SpatiaLiteCreationc           B   s2   e  Z d  e d  Z d   Z d   Z d   Z RS(   i   c      
   C   s  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 <|  j j j	   |  j
   | 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 |	 |  r4| d |	 j d |  j j q4q4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.

        This method is overloaded to load up the SpatiaLite initialization
        SQL prior to calling the `syncdb` command.
        iĸĸĸĸ(   t   call_commandi   t    i   s    ('%s')s*   Creating test database for alias '%s'%s...t   NAMEt   syncdbt	   verbosityi    t   interactivet   databaset   load_initial_datat   flush(   R   (   R   t   createcachetable(   t   django.core.managementR   t   _get_test_db_namet
   connectiont   aliast   _create_test_dbt   closet   settings_dictt   opst#   confirm_spatial_components_versionst   load_spatialite_sqlt   maxt   Falset   django.core.cacheR   t   django.core.cache.backends.dbR   R    t   CACHESt
   isinstancet   _tablet   cursor(   t   selfR
   t   autoclobberR   t   test_database_namet   test_db_reprR   R   t   cache_aliast   cacheR!   (    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyt   create_test_db
   s:    	
		#c   	      C   s  d d l  m } t t |   j | | |  } t | |  r|  j j j } |  j j j	 } | j
 j } | j | j d  | j d  d | j | |   d | j | | j   d | j t | j   d | j | | j   d | j t | j   d | j t t | j    d  | j r| j | j d  | j d  d | j | |   d | j | | j   d  qn  | S(	   s4   Return any spatial index creation SQL for the field.iĸĸĸĸ(   t   GeometryFields   SELECT t   AddGeometryColumnt   (s   , s   );t   CreateSpatialIndex(   t#   django.contrib.gis.db.models.fieldsR)   t   superR   t   sql_indexes_for_fieldR   R   R   t   geo_quote_namet
   quote_namet   _metat   db_tablet   appendt   SQL_KEYWORDt	   SQL_TABLEt	   SQL_FIELDt   columnt   strt   sridt   SQL_COLTYPEt	   geom_typet   dimt   intt   nullt   spatial_index(	   R"   t   modelt   ft   styleR)   t   outputt   gqnt   qnR3   (    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyR/   F   s    đ	Xc         C   s§   |  j  j j d  d k r8 |  j  j   } | j d  nk |  j   } t j j |  si t	 d |   n  t
 | d  ) } |  j  j   } | j | j    Wd QXd S(   s@   
        This routine loads up the SpatiaLite SQL file.
        i   i   s   SELECT InitSpatialMetaData()sZ   Could not find the required SpatiaLite initialization SQL file (necessary for testing): %st   rN(   i   i   (   R   R   t   spatial_versiont   _cursort   executet   spatialite_init_filet   ost   patht   isfileR   t   opent   executescriptt   read(   R"   t   curt   spatialite_sqlt   sql_fh(    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyR   c   s    c         C   s!   t  t d d |  j j j d   S(   Nt   SPATIALITE_SQLs   init_spatialite-%s.%s.sqli   (   t   getattrR    R   R   RH   (   R"   (    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyRK   }   s    	(   t   __name__t
   __module__R   R(   R/   R   RK   (    (    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyR      s   <		(   RL   t   django.confR    R   R   R   R   t   django.core.exceptionsR   t#   django.db.backends.sqlite3.creationR   R   (    (    (    sK   ../Django//lib/python/django/contrib/gis/db/backends/spatialite/creation.pyt   <module>   s   