
    rht                     B    d dl mZ d dlmZ d dlmZmZ  G d de      Zy)    )GeometryField)DatabaseSchemaEditor)ColFuncc                   v     e Zd ZdZdZdZdZdZd Z fdZ	dd	 fd

Z
 fdZ	 d fd	Zd Z fdZd Z xZS )PostGISSchemaEditorGISTGIST_GEOMETRY_OPS_NDzST_ConvexHull(%(expressions)s)zLALTER COLUMN %(column)s TYPE %(type)s USING ST_Force3D(%(column)s)::%(type)szLALTER COLUMN %(column)s TYPE %(type)s USING ST_Force2D(%(column)s)::%(type)sc                 L    | j                   j                  j                  |      S N)
connectionopsgeo_quote_name)selfnames     /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/django/contrib/gis/db/backends/postgis/schema.pyr   z"PostGISSchemaEditor.geo_quote_name   s    ""11$77    c                 @    t        |dd      ryt        | 	  ||      S )Nspatial_indexFT)getattrsuper_field_should_be_indexed)r   modelfield	__class__s      r   r   z,PostGISSchemaEditor._field_should_be_indexed   s$    5/51w/u==r   N)fieldsc                    |t        |      dk7  st        |d   d      st        |   |fd|i|S  | j                  ||d   fi |S )N   r   geodeticr   )lenhasattrr   _create_index_sql_create_spatial_index_sql)r   r   r   kwargsr   s       r   r"   z%PostGISSchemaEditor._create_index_sql   sX    >S[A-WVAY
5S7,UL6LVLL-t--eVAYI&IIr   c                 l   t        |d      rt        |d      st        | 	  ||||||      S |j                  dk(  r|j                  dk(  r| j                  }n7|j                  dk(  r|j                  dk(  r| j
                  }n| j                  }|| j                  |j                        |ddz  g fg fS )z6
        Special case when dimension changed.
        dim       )columntype	collation)	r!   r   _alter_column_type_sqlr&   sql_alter_column_to_3dsql_alter_column_to_2dsql_alter_column_type
quote_namer*   )	r   table	old_field	new_fieldnew_typeold_collationnew_collation	sql_alterr   s	           r   r-   z*PostGISSchemaEditor._alter_column_type_sql    s     y%(	50I71y)X}m  ==A)--1"433I]]aIMMQ$633I22I "ooi.>.>?$!#  
 	
r   c	           
      F   t         |   ||||||||       t        |t              xr |j                  }	t        |t              xr |j                  }
|	s$|
r"| j                  | j                  ||             y |	r%|
s"| j                  | j                  ||             y y y )N)strict)r   _alter_field
isinstancer   r   executer#   _delete_spatial_index_sql)r   r   r3   r4   old_typer5   old_db_paramsnew_db_paramsr:   old_field_spatial_indexnew_field_spatial_indexr   s              r   r;   z PostGISSchemaEditor._alter_field>   s     	 	 		
 y-0LY5L5L 	  y-0LY5L5L 	  '+BLL77yIJ$-DLL77yIJ .E$r   c                 f    | j                  |j                  j                  |j                  gd      S )N_id)_create_index_name_metadb_tabler*   )r   r   r   s      r   _create_spatial_index_namez.PostGISSchemaEditor._create_spatial_index_name_   s'    &&u{{';';ell^USSr   c                 V   d }d }|g}|j                   dk(  r$t        t        d |      | j                        }d }n(|j                  dkD  r|j
                  s| j                  g}|j                  d      x}s| j                  ||      }t        | )  |||d| j                  z  ||      S )NRASTER)templater'   r   z	 USING %s)r   r   using	opclassesexpressions)	geom_typer   r   rast_index_templater&   	geographygeom_index_ops_ndgetrI   r   r"   geom_index_type)	r   r   r   r$   rO   rN   r   r   r   s	           r   r#   z-PostGISSchemaEditor._create_spatial_index_sqlb   s    	??h& s4/$:R:RSKFYY]5??//0I

6***225%@Dw( 4 44# ) 
 	
r   c                 J    | j                  ||      }| j                  ||      S r   )rI   _delete_index_sql)r   r   r   
index_names       r   r>   z-PostGISSchemaEditor._delete_spatial_index_sqlz   s'    44UEB
%%eZ88r   )F)__name__
__module____qualname__rU   rS   rQ   r.   r/   r   r   r"   r-   r;   rI   r#   r>   __classcell__)r   s   @r   r   r      sa    O.: 	W  	W 8>
 26 J
N KBT
09r   r   N)django.contrib.gis.db.modelsr   $django.db.backends.postgresql.schemar   django.db.models.expressionsr   r   r    r   r   <module>ra      s    6 E 2v9. v9r   