o
    uvXj)  ã                   @   s4   d dl mZmZmZ d dlmZ G dd„ deƒZdS )é    )ÚBaseDatabaseIntrospectionÚ	FieldInfoÚ	TableInfo)ÚIndexc                       sÆ   e Zd Zi 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!œ¥Zg Z‡ fd"d#„Zd$d%„ Zd&d'„ Zd1d)d*„Zd+d,„ Z	d-d.„ Z
d/d0„ Z‡  ZS )2ÚDatabaseIntrospectioné   ÚBooleanFieldé   ÚBinaryFieldé   ÚBigIntegerFieldé   ÚSmallIntegerFieldé   ÚIntegerFieldé   Ú	TextFieldi¼  Ú
FloatFieldi½  ie  ÚGenericIPAddressFieldi  Ú	CharFieldi  i:  Ú	DateFieldi;  Ú	TimeFieldiZ  ÚDateTimeFieldi   i¢  ÚDurationFieldiò  ÚDecimalFieldÚ	UUIDField)i¤  i†  c                    s:   t ƒ  ||¡}|jrd|jv r|dkrdS |dkrdS |S )NÚnextvalr   Ú	AutoFieldr   ÚBigAutoField)ÚsuperÚget_field_typeÚdefault)ÚselfÚ	data_typeÚdescriptionÚ
field_type©Ú	__class__© úh/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/backends/postgresql/introspection.pyr    !   s   z$DatabaseIntrospection.get_field_typec                    s4   |  d ˆ jjjrdnd¡¡ ‡ fdd„| ¡ 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 vrt|Ž ‘qS )r   )Úignored_tablesr   ©Ú.0Úrow©r"   r(   r)   Ú
<listcomp>5   ó   " z8DatabaseIntrospection.get_table_list.<locals>.<listcomp>)ÚexecuteÚformatÚ
connectionÚfeaturesÚsupports_table_partitionsÚfetchall)r"   Úcursorr(   r/   r)   Úget_table_list*   s   ø	z$DatabaseIntrospection.get_table_listc                    sL   |  d|g¡ dd„ | ¡ D ƒ‰ |  d| 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 )r   é   Nr(   ©r-   Úliner(   r(   r)   Ú
<dictcomp>N   s    z?DatabaseIntrospection.get_table_description.<locals>.<dictcomp>zSELECT * FROM %s LIMIT 1c              	      s8   g | ]}t |j|j|j|j|j|jgˆ |j ¢R Ž ‘qS r(   )r   ÚnameÚ	type_codeÚdisplay_sizeÚinternal_sizeÚ	precisionÚscaler;   ©Ú	field_mapr(   r)   r0   P   s    
÷úùÿz?DatabaseIntrospection.get_table_description.<locals>.<listcomp>)r2   r7   r4   ÚopsÚ
quote_namer$   ©r"   r8   Ú
table_namer(   rD   r)   Úget_table_description7   s   ò

öz+DatabaseIntrospection.get_table_descriptionr(   c                    s$   |  dˆ g¡ ‡ fdd„| ¡ 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
                JOIN pg_namespace n ON n.oid = tbl.relnamespace
            WHERE s.relkind = 'S'
              AND d.deptype in ('a', 'n')
              AND n.nspname = 'public'
              AND tbl.relname = %s
        c                    s    g | ]}|d  ˆ |d dœ‘qS )r   r:   )r>   ÚtableÚcolumnr(   r,   ©rI   r(   r)   r0   l   s    ÿÿz7DatabaseIntrospection.get_sequences.<locals>.<listcomp>©r2   r7   )r"   r8   rI   Útable_fieldsr(   rM   r)   Úget_sequences]   s   ó
þz#DatabaseIntrospection.get_sequencesc                 C   s    |  d|g¡ dd„ | ¡ D ƒS )z—
        Return a dictionary of {field_name: (field_name_other_table, other_table)}
        representing all relationships to the given table.
        aÊ  
            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'
        c                 S   s"   i | ]}|d  |d |d f“qS )r:   é   r   r(   r,   r(   r(   r)   r=      r1   z7DatabaseIntrospection.get_relations.<locals>.<dictcomp>rN   rH   r(   r(   r)   Úget_relationsq   s   ø	z#DatabaseIntrospection.get_relationsc                 C   s   |  d|g¡ | ¡ S )Na  
            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'
        rN   rH   r(   r(   r)   Úget_key_columns�   s   ôz%DatabaseIntrospection.get_key_columnsc                 C   s  i }|  dd|g¡ | ¡ D ]&\}}}}}||dk|dv |dkr(t| dd¡ƒnd|d	kd
d|dœ||< q|  d|g¡ | ¡ D ]>\}	}}
}}}}}|	|vr~|dko[|	 d¡ o[|du }|dgkrc|ng |dgkrk|ng ||
dd
d|rvtjn|||dœ
||	< q@|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.
        aq  
            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
            JOIN pg_namespace AS ns ON cl.relnamespace = ns.oid
            WHERE ns.nspname = %s AND cl.relname = %s
        ÚpublicÚp)rU   ÚuÚfÚ.r:   NÚcF)ÚcolumnsÚprimary_keyÚuniqueÚforeign_keyÚcheckÚindexÚ
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
            ) s2
            GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
        ÚbtreeÚ_btreeT)
rZ   Úordersr[   r\   r]   r^   r_   Útyper`   ra   )r2   r7   ÚtupleÚsplitÚendswithr   Úsuffix)r"   r8   rI   ÚconstraintsÚ
constraintrZ   ÚkindÚ	used_colsra   r_   r\   Úprimaryrd   Útype_r`   Úbasic_indexr(   r(   r)   Úget_constraints‘   sB   ìøã
ö€z%DatabaseIntrospection.get_constraints)r(   )Ú__name__Ú
__module__Ú__qualname__Údata_types_reverser+   r    r9   rJ   rP   rR   rS   rq   Ú__classcell__r(   r(   r&   r)   r      s^    ÿþýüûúùø	÷
öõôóòñðïí	
&r   N)Ú%django.db.backends.base.introspectionr   r   r   Údjango.db.models.indexesr   r   r(   r(   r(   r)   Ú<module>   s    