
v^                 @   s:   d  d l  m Z d  d l m Z Gd d   d e  Z d S)    )DatabaseSchemaEditor)DatabaseErrorc                   s   e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d d	 d
 d g Z
   f d d   Z d d   Z d   f d d  Z d d   Z   f d d   Z   f d d   Z   f d d   Z   f d d   Z d   f d d  Z   S) SpatialiteSchemaEditorz[SELECT AddGeometryColumn(%(table)s, %(column)s, %(srid)s, %(geom_type)s, %(dim)s, %(null)s)z0SELECT CreateSpatialIndex(%(table)s, %(column)s)z#DROP TABLE idx_%(table)s_%(column)szUSELECT RecoverGeometryColumn(%(table)s, %(column)s, %(srid)s, %(geom_type)s, %(dim)s)z3SELECT DiscardGeometryColumn(%(table)s, %(column)s)z9DELETE FROM %(geom_table)s WHERE f_table_name = %(table)szYUPDATE %(geom_table)s SET f_table_name = %(new_table)s WHERE f_table_name = %(old_table)sZgeometry_columnsZgeometry_columns_authZgeometry_columns_timeZgeometry_columns_statisticsc                s    t    j | |   g  |  _ d  S)N)super__init__geometry_sql)selfargskwargs)	__class__ R/tmp/pip-build-8lau8j11/django/django/contrib/gis/db/backends/spatialite/schema.pyr      s    zSpatialiteSchemaEditor.__init__c             C   s   |  j  j j |  S)N)
connectionopsgeo_quote_name)r   namer   r   r   r   "   s    z%SpatialiteSchemaEditor.geo_quote_nameFc                s   d d l  m } t | |  s5 t   j | | |  S|  j j |  j d |  j | j	 j
  d |  j | j  d | j d |  j | j  d | j d t | j  i  | j r |  j j |  j d |  j | j	 j
  d |  j | j  i  d	 S)
Nr   )GeometryFieldtablecolumnsrid	geom_typedimnull)NN)#django.contrib.gis.db.models.fieldsr   
isinstancer   
column_sqlr   appendsql_add_geometry_columnr   _metadb_tabler   r   r   r   intr   spatial_indexsql_add_spatial_index
quote_name)r   modelfieldinclude_defaultr   )r   r   r   r   %   s"    					z!SpatialiteSchemaEditor.column_sqlc             C   sh   |  j  |  j d |  j | j j  d |  j | j  i  |  j  |  j d | j j d | j i  d  S)Nr   r   )executesql_remove_geometry_metadatar#   r   r   r   sql_drop_spatial_index)r   r$   r%   r   r   r   remove_geometry_metadata?   s    z/SpatialiteSchemaEditor.remove_geometry_metadatac                s>   t    j |  x |  j D] } |  j |  q Wg  |  _ d  S)N)r   create_modelr   r'   )r   r$   sql)r   r   r   r+   M   s    z#SpatialiteSchemaEditor.create_modelc                s   d d l  m } x3 | j j D]% } t | |  r |  j | |  q WxY |  j D]N } y3 |  j |  j d | d |  j	 | j j
  i  WqP t k
 r YqP XqP Wt   j | |  d  S)Nr   )r   
geom_tabler   )r   r   r   local_fieldsr   r*   geometry_tablesr'   sql_discard_geometry_columnsr#   r   r   r   delete_model)r   r$   r
   r   r%   r-   )r   r   r   r1   T   s    !	z#SpatialiteSchemaEditor.delete_modelc                ss   d d l  m } t | |  r\ |  j | |  x |  j D] } |  j |  q9 Wg  |  _ n t   j | |  d  S)Nr   )r   )r   r   r   r   r   r'   r   	add_field)r   r$   r%   r   r,   )r   r   r   r2   g   s    z SpatialiteSchemaEditor.add_fieldc                sL   d d l  m } t | |  r5 |  j | d | n t   j | |  d  S)Nr   )r   Zdelete_field)r   r   r   Z_remake_tabler   remove_field)r   r$   r%   r   )r   r   r   r3   r   s    z#SpatialiteSchemaEditor.remove_fieldTc                s  d d l  m } xX | j j D]J } t | |  r |  j |  j d |  j |  d |  j | j  i  q Wt	   j
 | | | |  xb |  j D]W } y< |  j |  j d | d |  j |  d |  j |  i  Wq t k
 r Yq Xq Wx | j j D] } t | |  rd|  j |  j d |  j |  d |  j | j  d | j d	 |  j | j  d
 | j i  t | d d  r |  j |  j d |  j d | | j f  d |  j d | | j f  i  q Wd  S)Nr   )r   r   r   r-   Z	old_tableZ	new_tabler   r   r   r!   Fz	idx_%s_%s)r   r   r   r.   r   r'   r(   r#   r   r   alter_db_tabler/   sql_update_geometry_columnsr   sql_recover_geometry_metadatar   r   r   r   getattrZsql_rename_table)r   r$   Zold_db_tableZnew_db_tableZdisable_constraintsr   r%   r-   )r   r   r   r4   ~   s:    		z%SpatialiteSchemaEditor.alter_db_table)__name__
__module____qualname__r   r"   r)   r6   r(   r0   r5   r/   r   r   r   r*   r+   r1   r2   r3   r4   r   r   )r   r   r      s(   	r   N)Z!django.db.backends.sqlite3.schemar   Zdjango.db.utilsr   r   r   r   r   r   <module>   s   