î
ªÍ XA  ã               @   sP   d  d l  m Z d  d l m Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)é    )ÚOGRGeomType)ÚDatabaseIntrospectionc               @   s   e  Z d  Z d S)ÚGeoIntrospectionErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úb/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/backends/postgis/introspection.pyr      s   r   c                   sb   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	 ‡  S)ÚPostGISIntrospectionZgeography_columnsZgeometry_columnsZraster_columnsZspatial_ref_sysZraster_overviewsa\  
        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
        LEFT JOIN pg_catalog.pg_type t ON t.oid = attr.atttypid
        WHERE
            c.oid = idx.indrelid
            AND idx.indexrelid = c2.oid
            AND attr.attrelid = c.oid
            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 i d d 6f f g } i  } d } |  j  j ƒ  } zU xN | D]F } | j | | d f ƒ x& | j ƒ  D] } | d | | d <qn WqD WWd | j ƒ  X| S)
z·
        Returns a dictionary with keys that are the PostgreSQL object
        identification integers for the PostGIS geometry and/or
        geography types (if supported).
        ÚgeometryÚGeometryFieldZ	geographyTz0SELECT "oid" FROM "pg_type" WHERE "typname" = %sr   é   N)r   r   )Ú
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                sG   |  j  s. |  j ƒ  |  _  |  j j |  j  ƒ n  t t |  ƒ j | | ƒ S)N)Úpostgis_types_reverser   Zdata_types_reverseÚupdateÚsuperr
   Úget_field_type)r   Z	data_typeÚdescription)Ú	__class__r   r	   r   I   s    	z#PostGISIntrospection.get_field_typec       	      C   s  |  j  j ƒ  } zî y5 | j d | | f ƒ | j ƒ  } | sF t ‚ n  Wn4 t k
 r} | j d | | f ƒ | j ƒ  } Yn X| s t d | | f ƒ ‚ n  t | d ƒ j } | d } | d } i  } | d k rã | | d <n  | d k rü | | d	 <n  Wd
 | j ƒ  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!   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   