
v^&                 @   sF   d  d l  m Z m Z m Z d  d l m Z Gd d   d e  Z d S)    )BaseDatabaseIntrospection	FieldInfo	TableInfo)Indexc            &       s   e  Z d  Z d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d d d d d  d! d" i Z g  Z   f d# d$   Z d% d&   Z d' d(   Z f  d) d*  Z d+ d,   Z	 d- d.   Z
 d/ d0   Z   S)1DatabaseIntrospection   ZBooleanField   ZBinaryField   BigIntegerField   SmallIntegerField   IntegerField   Z	TextFieldi  Z
FloatFieldi  ie  ZGenericIPAddressFieldi  Z	CharFieldi  i:  Z	DateFieldi;  Z	TimeFieldiZ  ZDateTimeFieldi  i  ZDurationFieldi  i  ZDecimalFieldi  Z	UUIDFieldc                sa   t    j | |  } | j r] d | j k r] | d k r= d S| d k rM d S| d k r] d S| S)NZnextvalr   Z	AutoFieldr
   ZBigAutoFieldr   ZSmallAutoField)superget_field_typedefault)selfZ	data_typedescriptionZ
field_type)	__class__ M/tmp/pip-build-8lau8j11/django/django/db/backends/postgresql/introspection.pyr   !   s    z$DatabaseIntrospection.get_field_typec                sH   | j  d j   j j j r! d n d     f d d   | j   D S)z>Return a list of table and view names in the current database.a  
            SELECT c.relname,
            CASE WHEN {} THEN 'p' WHEN c.relkind IN ('m', 'v') THEN 'v' ELSE 't' END
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid)
        zc.relispartitionFALSEc                s/   g  |  ]% } | d    j  k r t |    q S)r   )ignored_tablesr   ).0row)r   r   r   
<listcomp>7   s   	 z8DatabaseIntrospection.get_table_list.<locals>.<listcomp>)executeformat
connectionfeaturesZsupports_table_partitionsfetchall)r   cursorr   )r   r   get_table_list,   s    %z$DatabaseIntrospection.get_table_listc                sf   | j  d | g  d d   | j   D   | j  d |  j j j |     f d d   | j D S)zi
        Return a description of the table with the DB-API cursor.description
        interface.
        a  
            SELECT
                a.attname AS column_name,
                NOT (a.attnotnull OR (t.typtype = 'd' AND t.typnotnull)) AS is_nullable,
                pg_get_expr(ad.adbin, ad.adrelid) AS column_default
            FROM pg_attribute a
            LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
            JOIN pg_type t ON a.atttypid = t.oid
            JOIN pg_class c ON a.attrelid = c.oid
            JOIN pg_namespace n ON c.relnamespace = n.oid
            WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
                AND c.relname = %s
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid)
        c             S   s'   i  |  ] } | d  d  | d  q S)   Nr   r   )r   liner   r   r   
<dictcomp>P   s   	 z?DatabaseIntrospection.get_table_description.<locals>.<dictcomp>zSELECT * FROM %s LIMIT 1c                sG   g  |  ]= } t  | j | j | j | j | j | j   | j   q Sr   )r   nameZ	type_codeZdisplay_sizeZinternal_sizeZ	precisionZscale)r   r%   )	field_mapr   r   r   S   s   		z?DatabaseIntrospection.get_table_description.<locals>.<listcomp>)r   r!   r   opsZ
quote_namer   )r   r"   
table_namer   )r(   r   get_table_description9   s     	z+DatabaseIntrospection.get_table_descriptionc                s0   | j  d   g    f d d   | j   D S)Na  
            SELECT s.relname as sequence_name, col.attname
            FROM pg_class s
                JOIN pg_namespace sn ON sn.oid = s.relnamespace
                JOIN pg_depend d ON d.refobjid = s.oid AND d.refclassid = 'pg_class'::regclass
                JOIN pg_attrdef ad ON ad.oid = d.objid AND d.classid = 'pg_attrdef'::regclass
                JOIN pg_attribute col ON col.attrelid = ad.adrelid AND col.attnum = ad.adnum
                JOIN pg_class tbl ON tbl.oid = ad.adrelid
            WHERE s.relkind = 'S'
              AND d.deptype in ('a', 'n')
              AND pg_catalog.pg_table_is_visible(tbl.oid)
              AND tbl.relname = %s
        c          	      s0   g  |  ]& } d  | d d   d | d i  q S)r'   r   tablecolumnr$   r   )r   r   )r*   r   r   r   n   s   	z7DatabaseIntrospection.get_sequences.<locals>.<listcomp>)r   r!   )r   r"   r*   Ztable_fieldsr   )r*   r   get_sequences_   s    z#DatabaseIntrospection.get_sequencesc             C   s   d d   |  j  | |  D S)z
        Return a dictionary of {field_name: (field_name_other_table, other_table)}
        representing all relationships to the given table.
        c             S   s+   i  |  ]! } | d  | d f | d  q S)   r$   r   r   )r   r   r   r   r   r&   w   s   	 z7DatabaseIntrospection.get_relations.<locals>.<dictcomp>)get_key_columns)r   r"   r*   r   r   r   get_relationsr   s    z#DatabaseIntrospection.get_relationsc             C   s   | j  d | g  | j   S)Na[  
            SELECT a1.attname, c2.relname, a2.attname
            FROM pg_constraint con
            LEFT JOIN pg_class c1 ON con.conrelid = c1.oid
            LEFT JOIN pg_class c2 ON con.confrelid = c2.oid
            LEFT JOIN pg_attribute a1 ON c1.oid = a1.attrelid AND a1.attnum = con.conkey[1]
            LEFT JOIN pg_attribute a2 ON c2.oid = a2.attrelid AND a2.attnum = con.confkey[1]
            WHERE
                c1.relname = %s AND
                con.contype = 'f' AND
                c1.relnamespace = c2.relnamespace AND
                pg_catalog.pg_table_is_visible(c1.oid)
        )r   r!   )r   r"   r*   r   r   r   r0   y   s    z%DatabaseIntrospection.get_key_columnsc             C   s  i  } | j  d | g  x | j   D] \ } } } } } d | d | d k d | d k d | d k r t | j d	 d
   n d d | d k d d d d d | i | | <q& W| j  d | g  x | j   D] \ }	 } }
 } } } } } |	 | k r | d k o|	 j d  o| d k } d | d g k r9| n g  d | d g k rT| n g  d | d |
 d d d d d d d | rt j n | d | d | i
 | |	 <q W| S)z
        Retrieve any constraints or keys (unique, pk, fk, check, index) across
        one or more columns. Also retrieve the definition of expression-based
        indexes.
        aH  
            SELECT
                c.conname,
                array(
                    SELECT attname
                    FROM unnest(c.conkey) WITH ORDINALITY cols(colid, arridx)
                    JOIN pg_attribute AS ca ON cols.colid = ca.attnum
                    WHERE ca.attrelid = c.conrelid
                    ORDER BY cols.arridx
                ),
                c.contype,
                (SELECT fkc.relname || '.' || fka.attname
                FROM pg_attribute AS fka
                JOIN pg_class AS fkc ON fka.attrelid = fkc.oid
                WHERE fka.attrelid = c.confrelid AND fka.attnum = c.confkey[1]),
                cl.reloptions
            FROM pg_constraint AS c
            JOIN pg_class AS cl ON c.conrelid = cl.oid
            WHERE cl.relname = %s AND pg_catalog.pg_table_is_visible(cl.oid)
        columnsZprimary_keypuniqueuZforeign_keyf.r$   NcheckcindexF
definitionoptionsa  
            SELECT
                indexname, array_agg(attname ORDER BY arridx), indisunique, indisprimary,
                array_agg(ordering ORDER BY arridx), amname, exprdef, s2.attoptions
            FROM (
                SELECT
                    c2.relname as indexname, idx.*, attr.attname, am.amname,
                    CASE
                        WHEN idx.indexprs IS NOT NULL THEN
                            pg_get_indexdef(idx.indexrelid)
                    END AS exprdef,
                    CASE am.amname
                        WHEN 'btree' THEN
                            CASE (option & 1)
                                WHEN 1 THEN 'DESC' ELSE 'ASC'
                            END
                    END as ordering,
                    c2.reloptions as attoptions
                FROM (
                    SELECT *
                    FROM pg_index i, unnest(i.indkey, i.indoption) WITH ORDINALITY koi(key, option, arridx)
                ) idx
                LEFT JOIN pg_class c ON idx.indrelid = c.oid
                LEFT JOIN pg_class c2 ON idx.indexrelid = c2.oid
                LEFT JOIN pg_am am ON c2.relam = am.oid
                LEFT JOIN pg_attribute attr ON attr.attrelid = c.oid AND attr.attnum = idx.key
                WHERE c.relname = %s AND pg_catalog.pg_table_is_visible(c.oid)
            ) s2
            GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
        ZbtreeZ_btreeordersTtype)r3   r5   )r   r!   tuplesplitendswithr   suffix)r   r"   r*   constraints
constraintr2   kindZ	used_colsr<   r:   r4   Zprimaryr=   type_r;   Zbasic_indexr   r   r   get_constraints   s8    "*+(z%DatabaseIntrospection.get_constraints)__name__
__module____qualname__Zdata_types_reverser   r   r#   r+   r.   r1   r0   rG   r   r   )r   r   r      s6   &r   N)Z%django.db.backends.base.introspectionr   r   r   Zdjango.db.models.indexesr   r   r   r   r   r   <module>   s   