ó
®â0_c           @  s‚   d  d l  m Z d  d l Z d  d l m Z m Z m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d e f d „  ƒ  YZ d S(	   iÿÿÿÿ(   t   unicode_literalsN(   t   BaseDatabaseIntrospectiont	   FieldInfot	   TableInfo(   t   Index(   t   RemovedInDjango21Warning(   t
   force_textt   DatabaseIntrospectionc           B  s×   e  Z i d  d 6d d 6d d 6d d 6d d	 6d
 d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z g  Z d  Z d! „  Z d" „  Z d# „  Z d$ „  Z d% „  Z	 d& „  Z
 d' „  Z RS((   u   BooleanFieldi   u   BinaryFieldi   u   BigIntegerFieldi   u   SmallIntegerFieldi   u   IntegerFieldi   u	   TextFieldi   u
   FloatFieldi¼  i½  u   GenericIPAddressFieldie  u	   CharFieldi  i  u	   DateFieldi:  u	   TimeFieldi;  u   DateTimeFieldiZ  i   iò  u   DecimalFieldi¤  u	   UUIDFieldi†  u…  
        SELECT 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
        WHERE c.oid = idx.indrelid
            AND idx.indexrelid = c2.oid
            AND attr.attrelid = c.oid
            AND attr.attnum = idx.indkey[0]
            AND c.relname = %sc         C  sZ   t  t |  ƒ j | | ƒ } | j rV d | j k rV | d k rC d S| d k rV d Sn  | S(   Nu   nextvalu   IntegerFieldu	   AutoFieldu   BigIntegerFieldu   BigAutoField(   t   superR   t   get_field_typet   default(   t   selft	   data_typet   descriptiont
   field_type(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyR	   0   s    c         C  se   | j  d ƒ g  | j ƒ  D]G } | d |  j k r t | d i d d 6d d 6j | d ƒ ƒ ^ q S(   uQ   
        Returns a list of table and view names in the current database.
        u=  
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid)i    u   tu   ru   vi   (   t   executet   fetchallt   ignored_tablesR   t   get(   R   t   cursort   row(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_table_list9   s
    c      	   C  sµ   | j  d | g ƒ d „  | j ƒ  Dƒ } | j  d |  j j j | ƒ ƒ g  | j D]^ } t t | d ƒ f | d d !| t | d ƒ d d k | t | d ƒ d f Œ  ^ qS S(   uQ   Returns a description of the table, with the DB-API cursor.description interface.uŠ   
            SELECT column_name, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_name = %sc         S  s!   i  |  ] } | d  | d “ q S(   i   i    (    (   t   .0t   line(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pys
   <dictcomp>P   s   	 u   SELECT * FROM %s LIMIT 1i    i   i   u   YES(   R   R   t
   connectiont   opst
   quote_nameR   R   R   (   R   R   t
   table_namet	   field_mapR   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_table_descriptionH   s     c         C  sP   | j  d | g ƒ i  } x0 | j ƒ  D]" } | d | d f | | d <q& W| S(   u˜   
        Returns a dictionary of {field_name: (field_name_other_table, other_table)}
        representing all relationships to the given table.
        uÑ  
            SELECT c2.relname, a1.attname, 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'i   i    i   (   R   R   (   R   R   R   t	   relationsR   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_relationsZ   s     c         C  s0   g  } | j  d | g ƒ | j | j ƒ  ƒ | S(   Nu	  
            SELECT kcu.column_name, ccu.table_name AS referenced_table, ccu.column_name AS referenced_column
            FROM information_schema.constraint_column_usage ccu
            LEFT JOIN information_schema.key_column_usage kcu
                ON ccu.constraint_catalog = kcu.constraint_catalog
                    AND ccu.constraint_schema = kcu.constraint_schema
                    AND ccu.constraint_name = kcu.constraint_name
            LEFT JOIN information_schema.table_constraints tc
                ON ccu.constraint_catalog = tc.constraint_catalog
                    AND ccu.constraint_schema = tc.constraint_schema
                    AND ccu.constraint_name = tc.constraint_name
            WHERE kcu.table_name = %s AND tc.constraint_type = 'FOREIGN KEY'(   R   t   extendR   (   R   R   R   t   key_columns(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_key_columnsm   s
    c         C  sÐ   t  j d t d d ƒ| j |  j | g ƒ i  } x— | j ƒ  D]‰ } d | d k r[ q? n  | d | k rŠ i t d 6t d 6| | d <n  | d	 r© t | | d d <n  | d r? t | | d d <q? q? W| S(
   Nu:   get_indexes() is deprecated in favor of get_constraints().t
   stackleveli   u    i   i    u   primary_keyu   uniquei   (   t   warningst   warnR   R   t   _get_indexes_queryR   t   Falset   True(   R   R   R   t   indexesR   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_indexes~   s    

c      	   C  sž  i  } | j  d d | g ƒ x› | j ƒ  D] \ } } } } } i | d 6| d k d 6| d k d 6| d k r† t | j d	 d
 ƒ ƒ n d d 6| d k d 6t d 6d d 6| d 6| | <q) W| j  d | g ƒ xÊ | j ƒ  D]¼ \ }	 } }
 } } } } } |	 | k rÚ i
 | d g k r| n g  d 6| d g k r8| n g  d 6| d 6|
 d 6d d 6t d 6t d 6| d k rwt j n | d 6| d 6| d 6| |	 <qÚ qÚ W| S(   u·   
        Retrieve any constraints or keys (unique, pk, fk, check, index) across
        one or more columns. Also retrieve the definition of expression-based
        indexes.
        uí  
            SELECT
                c.conname,
                array(
                    SELECT attname
                    FROM (
                        SELECT unnest(c.conkey) AS colid,
                               generate_series(1, array_length(c.conkey, 1)) AS arridx
                    ) AS cols
                    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
            JOIN pg_namespace AS ns ON cl.relnamespace = ns.oid
            WHERE ns.nspname = %s AND cl.relname = %s
        u   publicu   columnsu   pu   primary_keyu   uu   uniqueu   fu   .i   u   foreign_keyu   cu   checku   indexu
   definitionu   optionsu²  
            SELECT
                indexname, array_agg(attname ORDER BY rnum), indisunique, indisprimary,
                array_agg(ordering ORDER BY rnum), amname, exprdef, s2.attoptions
            FROM (
                SELECT
                    row_number() OVER () as rnum, 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
                        *, unnest(i.indkey) as key, unnest(i.indoption) as option
                    FROM pg_index i
                ) 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
            ) s2
            GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
        u   ordersu   btreeu   type(   u   pu   uN(	   R   R   t   tuplet   splitt   NoneR'   R(   R   t   suffix(   R   R   R   t   constraintst
   constraintt   columnst   kindt	   used_colst   optionst   indext   uniquet   primaryt   orderst   type_t
   definition(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   get_constraints—   s:    "++(   t   __name__t
   __module__t   data_types_reverseR   R&   R	   R   R   R   R"   R*   R;   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyR      s8   

							(   t
   __future__R    R$   t%   django.db.backends.base.introspectionR   R   R   t   django.db.models.indexesR   t   django.utils.deprecationR   t   django.utils.encodingR   R   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/backends/postgresql/introspection.pyt   <module>   s   