o
    [hT-                     @   sp   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 edej
d Zedej
d	 ZG d
d deZdS )    )
namedtuple)BaseDatabaseIntrospection)	FieldInfo)	TableInfo)Indexr   )is_autofieldcommentr   )r   c                       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!d"Zd#Zg Z fd$d%Zd&d' Zd(d) Zd1d+d,Z	d-d. Z
d/d0 Z  ZS )2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  ZDecimalFieldZ	UUIDFieldZ	JSONField)i  i  i  Zbtreec                    sL   t  ||}|js|jr$d|jv r$|dkrdS |dkrdS |dkr$dS |S )NZnextvalr   Z	AutoFieldr   ZBigAutoFieldr   ZSmallAutoField)superget_field_typer   default)selfZ	data_typedescriptionZ
field_type	__class__ /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/db/backends/postgresql/introspection.pyr   )   s   
z$DatabaseIntrospection.get_field_typec                    s    | d  fdd| D S )z>Return a list of table and view names in the current database.aB  
            SELECT
                c.relname,
                CASE
                    WHEN c.relispartition THEN 'p'
                    WHEN c.relkind IN ('m', 'v') THEN 'v'
                    ELSE 't'
                END,
                obj_description(c.oid, 'pg_class')
            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)
        c                    s"   g | ]}|d   j vrt| qS )r   )ignored_tablesr   .0rowr   r   r   
<listcomp>K   s
    z8DatabaseIntrospection.get_table_list.<locals>.<listcomp>executefetchall)r   cursorr   r    r   get_table_list8   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,
                CASE WHEN collname = 'default' THEN NULL ELSE collname END AS collation,
                a.attidentity != '' AS is_autofield,
                col_description(a.attrelid, a.attnum) AS column_comment
            FROM pg_attribute a
            LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
            LEFT JOIN pg_collation co ON a.attcollation = co.oid
            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>o   s    z?DatabaseIntrospection.get_table_description.<locals>.<dictcomp>zSELECT * FROM %s LIMIT 1c              	      sH   g | ] }t |j|j|jd u r|jn|j|j|j|jg |j R  qS )N)r   nameZ	type_codeZdisplay_sizeZinternal_size	precisionscaler(   Z	field_mapr   r   r!   s   s    z?DatabaseIntrospection.get_table_description.<locals>.<listcomp>)r#   r$   
connectionopsZ
quote_namer   r   r%   
table_namer   r.   r   get_table_descriptionQ   s   
z+DatabaseIntrospection.get_table_descriptionr   c                    s$   | d g  fdd| D S )Na  
            SELECT
                s.relname AS sequence_name,
                a.attname AS colname
            FROM
                pg_class s
                JOIN pg_depend d ON d.objid = s.oid
                    AND d.classid = 'pg_class'::regclass
                    AND d.refclassid = 'pg_class'::regclass
                JOIN pg_attribute a ON d.refobjid = a.attrelid
                    AND d.refobjsubid = a.attnum
                JOIN pg_class tbl ON tbl.oid = d.refobjid
                    AND tbl.relname = %s
                    AND pg_catalog.pg_table_is_visible(tbl.oid)
            WHERE
                s.relkind = 'S';
        c                    s    g | ]}|d   |d dqS )r   r'   )r+   tablecolumnr   r   r2   r   r   r!      s    z7DatabaseIntrospection.get_sequences.<locals>.<listcomp>r"   )r   r%   r2   Ztable_fieldsr   r6   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 foreign keys in the given table.
        a{  
            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)
        c                 S   s"   i | ]}|d  |d |d fqS )r      r'   r   r   r   r   r   r*      s   " z7DatabaseIntrospection.get_relations.<locals>.<dictcomp>r"   r1   r   r   r   get_relations   s
   z#DatabaseIntrospection.get_relationsc                 C   s  i }| d|g | D ]&\}}}}}||dk|dv |dkr't|ddnd|dkd	d|d
||< q| d| j|g | D ]?\}	}}
}}}}}|	|vr|| jko]|	d o]|du }|dgkre|ng |dgkrm|ng ||
dd	d|rxtjn|||d
||	< qA|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)
        p)r:   uf.r'   NcF)columnsprimary_keyuniqueforeign_keycheckindex
definitionoptionsaa  
            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 %s 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_btreeT)
r?   ordersr@   rA   rB   rC   rD   typerE   rF   )r#   r$   tuplesplitindex_default_access_methodendswithr   suffix)r   r%   r2   constraints
constraintr?   kindZ	used_colsrF   rD   rA   primaryrG   type_rE   Zbasic_indexr   r   r   get_constraints   s`   (4

	
z%DatabaseIntrospection.get_constraints)r   )__name__
__module____qualname__Zdata_types_reverserK   r   r   r&   r3   r7   r9   rS   __classcell__r   r   r   r   r	      s`    	

0r	   N)collectionsr   Z%django.db.backends.base.introspectionr   r   ZBaseFieldInfor   ZBaseTableInfoZdjango.db.modelsr   _fieldsr	   r   r   r   r   <module>   s    