B
    ¥0H["  ã               @   s<   d dl mZ d dlmZ G dd„ deƒZG dd„ deƒZdS )é    )ÚOGRGeomType)ÚDatabaseIntrospectionc               @   s   e Zd ZdS )ÚGeoIntrospectionErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úxC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\contrib\gis\db\backends\postgis\introspection.pyr      s   r   c                   sH   e Zd Zi Zejdddddg ZdZdd„ Z‡ fd	d
„Zdd„ Z	‡  Z
S )ÚPostGISIntrospectionZgeography_columnsZgeometry_columnsZraster_columnsZspatial_ref_sysZraster_overviewsaX  
        SELECT DISTINCT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary
        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index idx,
            pg_catalog.pg_attribute attr, pg_catalog.pg_type t
        WHERE
            c.oid = idx.indrelid
            AND idx.indexrelid = c2.oid
            AND attr.attrelid = c.oid
            AND t.oid = attr.atttypid
            AND (
                attr.attnum = idx.indkey[0] OR
                (t.typname LIKE 'raster' AND idx.indkey = '0')
            )
            AND attr.attnum > 0
            AND c.relname = %sc             C   s|   dddddiffg}i }d}| j  ¡ }zFx@|D ]8}| ||d f¡ x | ¡ D ]}|d ||d < qNW q.W W d| ¡  X |S )	z¶
        Return a dictionary with keys that are the PostgreSQL object
        identification integers for the PostGIS geometry and/or
        geography types (if supported).
        )ZgeometryÚGeometryFieldZ	geographyr   Tz0SELECT "oid" FROM "pg_type" WHERE "typname" = %sr   é   N)Ú
connectionÚcursorÚexecuteZfetchallÚclose)ÚselfZfield_typesZpostgis_typesZoid_sqlr   Ú
field_typeÚresultr   r   r	   Úget_postgis_types*   s    


z&PostGISIntrospection.get_postgis_typesc                s,   | j s|  ¡ | _ | j | j ¡ tƒ  ||¡S )N)Úpostgis_types_reverser   Zdata_types_reverseÚupdateÚsuperÚget_field_type)r   Z	data_typeÚdescription)Ú	__class__r   r	   r   I   s    
z#PostGISIntrospection.get_field_typec       	      C   sÊ   | j  ¡ }z¬y$| d||f¡ | ¡ }|s.t‚W n, tk
r\   | d||f¡ | ¡ }Y nX |srtd||f ƒ‚t|d ƒj}|d }|d }i }|dkr¤||d< |dkr´||d	< W d
| ¡  X ||fS )a%  
        The geometry type OID used by PostGIS does not indicate the particular
        type of field that a geometry column is (e.g., whether it's a
        PointField or a PolygonField).  Thus, this routine queries the PostGIS
        metadata tables to determine the geometry type.
        zsSELECT "coord_dimension", "srid", "type" FROM "geometry_columns" WHERE "f_table_name"=%s AND "f_geometry_column"=%szuSELECT "coord_dimension", "srid", "type" FROM "geography_columns" WHERE "f_table_name"=%s AND "f_geography_column"=%sz;Could not find a geometry or geography column for "%s"."%s"é   r   r   iæ  ÚsridÚdimN)	r   r   r   Zfetchoner   Ú	Exceptionr   Zdjangor   )	r   Z
table_nameZgeo_colr   Úrowr   r   r   Zfield_paramsr   r   r	   Úget_geometry_typeT   s2    



z&PostGISIntrospection.get_geometry_type)r   r   r   r   r   Zignored_tablesZ_get_indexes_queryr   r   r    Ú__classcell__r   r   )r   r	   r
   	   s   r
   N)Zdjango.contrib.gis.gdalr   Z+django.db.backends.postgresql.introspectionr   r   r   r
   r   r   r   r	   Ú<module>   s   