
 X!                 @   s^   d  d l  m Z d  d l m Z e d d d g  Z e d d  Z Gd d	   d	 e  Z d
 S)    )
namedtuple)six	TableInfonametype	FieldInfozAname type_code display_size internal_size precision scale null_okc               @   s   e  Z d  Z d Z i  Z d d   Z d d   Z d d   Z d d	   Z d
 d d d  Z	 d d   Z
 d d d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d
 S)BaseDatabaseIntrospectionzN
    This class encapsulates all backend-specific introspection utilities
    c             C   s   | |  _  d  S)N)
connection)selfr	    r   S/home/ubuntu/projects/ifolica/build/django/django/db/backends/base/introspection.py__init__   s    z"BaseDatabaseIntrospection.__init__c             C   s   |  j  | S)a  Hook for a database backend to use the cursor description to
        match a Django field type to a database column.

        For Oracle, the column data_type on its own is insufficient to
        distinguish between a FloatField and IntegerField, for example.)data_types_reverse)r
   Z	data_typedescriptionr   r   r   get_field_type   s    z(BaseDatabaseIntrospection.get_field_typec             C   s   | S)zApply a conversion to the name for the purposes of comparison.

        The default table name converter is for case sensitive comparison.
        r   )r
   r   r   r   r   table_name_converter   s    z.BaseDatabaseIntrospection.table_name_converterc             C   s   |  j  |  S)z
        Apply a conversion to the column name for the purposes of comparison.

        Uses table_name_converter() by default.
        )r   )r
   r   r   r   r   column_name_converter$   s    z/BaseDatabaseIntrospection.column_name_converterNFc          	      sP      f d d   } | d k rF  j  j    } | |  SWd QXn  | |  S)a  
        Returns a list of names of all tables that exist in the database.
        The returned table list is sorted by Python's default sorting. We
        do NOT use database's ORDER BY here to avoid subtle differences
        in sorting order between databases.
        c                s&   t    f d d    j |   D  S)Nc             3   s-   |  ]# }   s | j  d  k r | j Vq d S)tN)r   r   ).0Zti)include_viewsr   r   	<genexpr>4   s    zKBaseDatabaseIntrospection.table_names.<locals>.get_names.<locals>.<genexpr>)sortedget_table_list)cursor)r   r
   r   r   	get_names3   s    z8BaseDatabaseIntrospection.table_names.<locals>.get_namesN)r	   r   )r
   r   r   r   r   )r   r
   r   table_names,   s
    z%BaseDatabaseIntrospection.table_namesc             C   s   t  d   d S)z
        Returns an unsorted list of TableInfo named tuples of all tables and
        views that exist in the database.
        zMsubclasses of BaseDatabaseIntrospection may require a get_table_list() methodN)NotImplementedError)r
   r   r   r   r   r   ;   s    z(BaseDatabaseIntrospection.get_table_listTc                s   d d l  m } d d l m } t   } x| | j   D]n } xe | j |  j j  D]K } | j	 j
 sm qU n  | j | j	 j  | j d d   | j	 j D  qU Wq6 Wt |  } | r  j d |       f d d   | D } n  | S)	z
        Returns a list of all table names that have associated Django models and
        are in INSTALLED_APPS.

        If only_existing is True, the resulting list will only include the tables
        that actually exist in the database.
        r   )apps)routerc             s   s-   |  ]# } | j  j j j r | j   Vq d  S)N)remote_fieldthrough_metamanagedm2m_db_table)r   fr   r   r   r   S   s    z?BaseDatabaseIntrospection.django_table_names.<locals>.<genexpr>r   c                s+   g  |  ]! }  j  |    k r |  q Sr   )r   )r   r   )existing_tablesr
   r   r   
<listcomp>Z   s   	z@BaseDatabaseIntrospection.django_table_names.<locals>.<listcomp>)django.appsr   	django.dbr   setget_app_configsget_migratable_modelsr	   aliasr!   r"   adddb_tableupdatelocal_many_to_manylistr   )r
   Zonly_existingr   r   r   tables
app_configmodelr   )r%   r
   r   django_table_namesB   s     	"z,BaseDatabaseIntrospection.django_table_namesc                s   d d l  m } d d l m } g  } x3 | j   D]% } | j | j |   j j   q3 Wt	 t
   j        f d d   | D S)zLReturns a set of all models represented by the provided list of table names.r   )r   )r   c                s1   h  |  ]' }   j  | j j   k r |  q Sr   )r   r!   r.   )r   m)r
   r2   r   r   	<setcomp>i   s   	 z=BaseDatabaseIntrospection.installed_models.<locals>.<setcomp>)r'   r   r(   r   r*   extendr+   r	   r,   r1   mapr   )r
   r2   r   r   Z
all_modelsr3   r   )r
   r2   r   installed_models`   s    #z*BaseDatabaseIntrospection.installed_modelsc             C   s,  d d l  m } d d l m } m } g  } x | j   D] } x | j | |  j j  D] } | j	 j
 sp qX n  | j	 j r qX n  xN | j	 j D]@ } t | | j  r | j i | j	 j d 6| j d 6 Pq q WxJ | j	 j D]< } | j j d k r | j i | j   d 6d d 6 q q WqX Wq9 W| S)zPReturns a list of information about all DB sequences for all models in all apps.r   )r   )modelsr   tablecolumnN)r'   r   r(   r;   r   r*   r+   r	   r,   r!   r"   ZswappedZlocal_fields
isinstanceZ	AutoFieldappendr.   r=   r0   r   r    r#   )r
   r   r;   r   sequence_listr3   r4   r$   r   r   r   r@   m   s"    $0z'BaseDatabaseIntrospection.sequence_listc             C   s   t  d   d S)z
        Backends can override this to return a list of (column_name, referenced_table_name,
        referenced_column_name) for all key columns in given table.
        zNsubclasses of BaseDatabaseIntrospection may require a get_key_columns() methodN)r   )r
   r   
table_namer   r   r   get_key_columns   s    z)BaseDatabaseIntrospection.get_key_columnsc             C   s@   x9 t  j |  j | |   D] } | d d r | d Sq Wd S)zQ
        Returns the name of the primary key column for the given table.
           Zprimary_keyr   N)r   	iteritemsget_indexes)r
   r   rA   r=   r   r   r   get_primary_key_column   s    "z0BaseDatabaseIntrospection.get_primary_key_columnc             C   s   t  d   d S)aX  
        Returns a dictionary of indexed fieldname -> infodict for the given
        table, where each infodict is in the format:
            {'primary_key': boolean representing whether it's the primary key,
             'unique': boolean representing whether it's a unique index}

        Only single-column indexes are introspected.
        zJsubclasses of BaseDatabaseIntrospection may require a get_indexes() methodN)r   )r
   r   rA   r   r   r   rE      s    	z%BaseDatabaseIntrospection.get_indexesc             C   s   t  d   d S)a  
        Retrieves any constraints or keys (unique, pk, fk, check, index)
        across one or more columns.

        Returns a dict mapping constraint names to their attributes,
        where attributes is a dict with keys:
         * columns: List of columns this covers
         * primary_key: True if primary key, False otherwise
         * unique: True if this is a unique constraint, False otherwise
         * foreign_key: (table, column) of target, or None
         * check: True if check constraint, False otherwise
         * index: True if index, False otherwise.

        Some backends may return special constraint names that don't exist
        if they don't name constraints of a certain type (e.g. SQLite)
        zNsubclasses of BaseDatabaseIntrospection may require a get_constraints() methodN)r   )r
   r   rA   r   r   r   get_constraints   s    z)BaseDatabaseIntrospection.get_constraints)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r5   r:   r@   rB   rF   rE   rG   r   r   r   r   r      s   	r   N)collectionsr   Zdjango.utilsr   r   r   objectr   r   r   r   r   <module>   s   