a
    xˆc˜  ã                   @   s¸   d Z ddlZ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 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 ddl
mZ e
jdd„ ƒZejG dd„ deƒƒZdS )aã  Provides an abstraction for obtaining database schema information.

Usage Notes:

Here are some general conventions when accessing the low level inspector
methods such as get_table_names, get_columns, etc.

1. Inspector methods return lists of dicts in most cases for the following
   reasons:

   * They're both standard types that can be serialized.
   * Using a dict instead of a tuple allows easy expansion of attributes.
   * Using a list for the outer structure maintains order and is easy to work
     with (e.g. list comprehension [d['name'] for d in cols]).

2. Records that contain a name, such as the column name in a column record
   use the key 'name'. So for most return values, each record will have a
   'name' attribute..
é    Né   )ÚConnectable)Ú
Connection)ÚEngineé   )Úexc)Ú
inspection)Úsql)Úutil)Ú	operators)Úschema)Ú
TypeEngine)Útopologicalc                 O   sŽ   |  dd ¡}|d u r,| ||g|¢R i |¤ŽS | jtdd„ |D ƒƒtdd„ | ¡ D ƒƒf}|  |¡}|d u rŠ| ||g|¢R i |¤Ž}|||< |S )NÚ
info_cachec                 s   s   | ]}t |tjƒr|V  qd S ©N)Ú
isinstancer
   Ústring_types)Ú.0Úa© r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/sqlalchemy/engine/reflection.pyÚ	<genexpr>2   ó    zcache.<locals>.<genexpr>c                 s   s"   | ]\}}|d kr||fV  qdS )r   Nr   )r   ÚkÚvr   r   r   r   3   r   )ÚgetÚ__name__ÚtupleÚitems)ÚfnÚselfÚconÚargsÚkwr   ÚkeyÚretr   r   r   Úcache+   s    ý
r&   c                   @   sî  e Zd ZdZe dd¡dd„ ƒZedd„ ƒZdd	„ Z	d
d„ Z
dd„ Zee dd¡dd„ ƒƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZejdd„ ƒZejdd„ ƒZedd„ ƒZdd„ Zd^d d!„Zd_d"d#„Zd`d$d%„Zdad&d'„Zd(d)„ Z d*d+„ Z!dbd,d-„Z"dcd.d/„Z#ddd0d1„Z$ded2d3„Z%dfd4d5„Z&dgd6d7„Z'dhd8d9„Z(did:d;„Z)djd<d=„Z*dkd>d?„Z+dld@dA„Z,e -dBdC¡dDdE„ ƒZ.dmdHdI„Z/dJdK„ Z0dLdM„ Z1dNdO„ Z2dPdQ„ Z3dRe4j5fdSe4j6fdTe4j7fdUe4j8fgZ9dVdW„ Z:dXdY„ Z;dZd[„ Z<d\d]„ Z=dS )nÚ	Inspectora<  Performs database schema inspection.

    The Inspector acts as a proxy to the reflection methods of the
    :class:`~sqlalchemy.engine.interfaces.Dialect`, providing a
    consistent interface as well as caching support for previously
    fetched metadata.

    A :class:`_reflection.Inspector` object is usually created via the
    :func:`_sa.inspect` function, which may be passed an
    :class:`_engine.Engine`
    or a :class:`_engine.Connection`::

        from sqlalchemy import inspect, create_engine
        engine = create_engine('...')
        insp = inspect(engine)

    Where above, the :class:`~sqlalchemy.engine.interfaces.Dialect` associated
    with the engine may opt to return an :class:`_reflection.Inspector`
    subclass that
    provides additional methods specific to the dialect's target database.

    z1.4a  The __init__() method on :class:`_reflection.Inspector` is deprecated and will be removed in a future release.  Please use the :func:`.sqlalchemy.inspect` function on an :class:`_engine.Engine` or :class:`_engine.Connection` in order to acquire an :class:`_reflection.Inspector`.c                 C   s
   |   |¡S )a‹  Initialize a new :class:`_reflection.Inspector`.

        :param bind: a :class:`~sqlalchemy.engine.Connectable`,
          which is typically an instance of
          :class:`~sqlalchemy.engine.Engine` or
          :class:`~sqlalchemy.engine.Connection`.

        For a dialect-specific instance of :class:`_reflection.Inspector`, see
        :meth:`_reflection.Inspector.from_engine`

        )Ú_init_legacy©r    Úbindr   r   r   Ú__init__U   s    zInspector.__init__c                 C   s,   t |jdƒr|jj} |  | ¡}|||ƒ |S )NÚ	inspector)ÚhasattrÚdialectr,   Ú__new__)ÚclsÚinitr*   r    r   r   r   Ú
_constructn   s
    

zInspector._constructc                 C   s$   t |dƒr|  |¡ n
|  |¡ d S )NZexec_driver_sql)r-   Ú_init_connectionÚ_init_enginer)   r   r   r   r(   x   s    
zInspector._init_legacyc                 C   s2   | | _ | _| ¡  ¡  d| _| jj| _i | _d S )NT)r*   ÚengineÚconnectÚcloseÚ_op_context_requires_connectr.   r   )r    r5   r   r   r   r4   ~   s
    
zInspector._init_enginec                 C   s(   || _ |j| _d| _| jj| _i | _d S )NF)r*   r5   r8   r.   r   )r    Ú
connectionr   r   r   r3   …   s
    
zInspector._init_connectiona  The from_engine() method on :class:`_reflection.Inspector` is deprecated and will be removed in a future release.  Please use the :func:`.sqlalchemy.inspect` function on an :class:`_engine.Engine` or :class:`_engine.Connection` in order to acquire an :class:`_reflection.Inspector`.c                 C   s   |   | j|¡S )aº  Construct a new dialect-specific Inspector object from the given
        engine or connection.

        :param bind: a :class:`~sqlalchemy.engine.Connectable`,
          which is typically an instance of
          :class:`~sqlalchemy.engine.Engine` or
          :class:`~sqlalchemy.engine.Connection`.

        This method differs from direct a direct constructor call of
        :class:`_reflection.Inspector` in that the
        :class:`~sqlalchemy.engine.interfaces.Dialect` is given a chance to
        provide a dialect-specific :class:`_reflection.Inspector` instance,
        which may
        provide additional methods.

        See the example at :class:`_reflection.Inspector`.

        )r2   r(   )r0   r*   r   r   r   Úfrom_engineŒ   s    zInspector.from_enginec                 C   s   t  t j| ¡S r   )r'   r2   r(   ©r*   r   r   r   Ú_connectable_insp­   s    zInspector._connectable_inspc                 C   s   t  t j| ¡S r   )r'   r2   r4   r;   r   r   r   Ú_engine_insp´   s    zInspector._engine_inspc                 C   s   t  t j| ¡S r   )r'   r2   r3   r;   r   r   r   Ú_connection_insp¸   s    zInspector._connection_inspc                 c   sF   | j r| j ¡ }n| j}z|V  W | j rB| ¡  n| j r@| ¡  0 dS )a  Return a context that optimizes for multiple operations on a single
        transaction.

        This essentially allows connect()/close() to be called if we detected
        that we're against an :class:`_engine.Engine` and not a
        :class:`_engine.Connection`.

        N)r8   r*   r6   r7   ©r    Úconnr   r   r   Ú_operation_context¼   s    

ÿzInspector._operation_contextc                 c   sJ   |   ¡ �.}|  | jj|¡}| j|_|V  W d  ƒ n1 s<0    Y  dS )z…Return an :class:`_reflection.Inspector`
        from this one that will run all
        operations on a single connection.

        N)rA   r2   Ú	__class__r3   r   )r    r@   Zsub_inspr   r   r   Ú_inspection_contextÐ   s    
zInspector._inspection_contextc                 C   s   | j jS )zÏReturn the default schema name presented by the dialect
        for the current engine's database user.

        E.g. this is typically ``public`` for PostgreSQL and ``dbo``
        for SQL Server.

        )r.   Údefault_schema_name)r    r   r   r   rD   Ý   s    	zInspector.default_schema_namec                 C   sJ   t | jdƒrF|  ¡ �"}| jj|| jd�W  d  ƒ S 1 s<0    Y  g S )zReturn all schema names.Úget_schema_names©r   N)r-   r.   rA   rE   r   r?   r   r   r   rE   è   s    
ÿ$zInspector.get_schema_namesNc                 C   s@   |   ¡ �$}| jj||| jd�W  d  ƒ S 1 s20    Y  dS )aì  Return all table names in referred to within a particular schema.

        The names are expected to be real tables only, not views.
        Views are instead returned using the
        :meth:`_reflection.Inspector.get_view_names`
        method.


        :param schema: Schema name. If ``schema`` is left at ``None``, the
         database's default schema is
         used, else the named schema is searched.  If the database does not
         support named schemas, behavior is undefined if ``schema`` is not
         passed as ``None``.  For special quoting, use :class:`.quoted_name`.

        .. seealso::

            :meth:`_reflection.Inspector.get_sorted_table_and_fkc_names`

            :attr:`_schema.MetaData.sorted_tables`

        rF   N)rA   r.   Úget_table_namesr   ©r    r   r@   r   r   r   rG   ò   s    
ÿzInspector.get_table_namesc                 C   s<   |   ¡ � }| j |||¡W  d  ƒ S 1 s.0    Y  dS )aM  Return True if the backend has a table of the given name.


        :param table_name: name of the table to check
        :param schema: schema name to query, if not the default schema.

        .. versionadded:: 1.4 - the :meth:`.Inspector.has_table` method
           replaces the :meth:`_engine.Engine.has_table` method.

        N)rA   r.   Ú	has_table)r    Ú
table_namer   r@   r   r   r   rI     s    
zInspector.has_tablec                 C   s<   |   ¡ � }| j |||¡W  d  ƒ S 1 s.0    Y  dS )zäReturn True if the backend has a table of the given name.

        :param sequence_name: name of the table to check
        :param schema: schema name to query, if not the default schema.

        .. versionadded:: 1.4

        N)rA   r.   Úhas_sequence)r    Zsequence_namer   r@   r   r   r   rK     s    

zInspector.has_sequencec           
   
      sF  |   ¡ �$}| jj||| jd�}W d  ƒ n1 s20    Y  tƒ }tƒ ‰i ‰|D ]N}|  ||¡}tdd„ |D ƒƒˆ|< |D ]"}||d krz| |d |f¡ qzqPztt 	||¡ƒ}W nn t
j�y" }	 zR|	jD ].‰ | ˆ ¡ ˆ ‡ fdd„ˆˆ d  D ƒ¡ qÎtt 	||¡ƒ}W Y d}	~	n
d}	~	0 0 ‡‡fd	d„|D ƒdtˆƒfg S )
a‰  Return dependency-sorted table and foreign key constraint names in
        referred to within a particular schema.

        This will yield 2-tuples of
        ``(tablename, [(tname, fkname), (tname, fkname), ...])``
        consisting of table names in CREATE order grouped with the foreign key
        constraint names that are not detected as belonging to a cycle.
        The final element
        will be ``(None, [(tname, fkname), (tname, fkname), ..])``
        which will consist of remaining
        foreign key constraint names that would require a separate CREATE
        step after-the-fact, based on dependencies between tables.

        .. versionadded:: 1.0.-

        .. seealso::

            :meth:`_reflection.Inspector.get_table_names`

            :func:`.sort_tables_and_constraints` - similar method which works
            with an already-given :class:`_schema.MetaData`.

        rF   Nc                 S   s   g | ]}|d  ‘qS )Únamer   )r   Zfkr   r   r   Ú
<listcomp>N  r   z<Inspector.get_sorted_table_and_fkc_names.<locals>.<listcomp>Úreferred_tablec                 3   s   | ]}ˆ d  |fV  qdS )r   Nr   )r   Zfkc)Úedger   r   r   W  s   z;Inspector.get_sorted_table_and_fkc_names.<locals>.<genexpr>r   c                    s   g | ]}|ˆ |   ˆ¡f‘qS r   )Ú
difference)r   Útname)Úfknames_for_tableÚremaining_fkcsr   r   rM   \  s   ÿ)rA   r.   rG   r   ÚsetÚget_foreign_keysÚaddÚlistr   Úsortr   ZCircularDependencyErrorÚedgesÚremoveÚupdate)
r    r   r@   ZtnamesZtuplesrQ   ÚfkeysZfkeyZcandidate_sortÚerrr   )rO   rR   rS   r   Úget_sorted_table_and_fkc_names*  s6    
ÿ$


ÿ
&þýz(Inspector.get_sorted_table_and_fkc_namesc                 C   s>   |   ¡ �"}| jj|| jd�W  d  ƒ S 1 s00    Y  dS )zÊReturn a list of temporary table names for the current bind.

        This method is unsupported by most dialects; currently
        only SQLite implements it.

        .. versionadded:: 1.0.0

        rF   N)rA   r.   Úget_temp_table_namesr   r?   r   r   r   r_   a  s    

ÿzInspector.get_temp_table_namesc                 C   s>   |   ¡ �"}| jj|| jd�W  d  ƒ S 1 s00    Y  dS )zÉReturn a list of temporary view names for the current bind.

        This method is unsupported by most dialects; currently
        only SQLite implements it.

        .. versionadded:: 1.0.0

        rF   N)rA   r.   Úget_temp_view_namesr   r?   r   r   r   r`   p  s    	
ÿzInspector.get_temp_view_namesc                 K   sV   t | jdƒrR|  ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 sH0    Y  i S )aÑ  Return a dictionary of options specified when the table of the
        given name was created.

        This currently includes some options that apply to MySQL tables.

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        Úget_table_optionsr   N)r-   r.   rA   ra   r   ©r    rJ   r   r#   r@   r   r   r   ra   ~  s    
ÿÿÿ$zInspector.get_table_optionsc                 C   s@   |   ¡ �$}| jj||| jd�W  d  ƒ S 1 s20    Y  dS )z±Return all view names in `schema`.

        :param schema: Optional, retrieve names from a non-default schema.
         For special quoting, use :class:`.quoted_name`.

        rF   N)rA   r.   Úget_view_namesr   rH   r   r   r   rc   “  s    
ÿzInspector.get_view_namesc                 C   s@   |   ¡ �$}| jj||| jd�W  d  ƒ S 1 s20    Y  dS )zµReturn all sequence names in `schema`.

        :param schema: Optional, retrieve names from a non-default schema.
         For special quoting, use :class:`.quoted_name`.

        rF   N)rA   r.   Úget_sequence_namesr   rH   r   r   r   rd      s    
ÿzInspector.get_sequence_namesc                 C   sB   |   ¡ �&}| jj|||| jd�W  d  ƒ S 1 s40    Y  dS )z±Return definition for `view_name`.

        :param schema: Optional, retrieve names from a non-default schema.
         For special quoting, use :class:`.quoted_name`.

        rF   N)rA   r.   Úget_view_definitionr   )r    Z	view_namer   r@   r   r   r   re   ­  s    

ÿzInspector.get_view_definitionc                 K   sp   |   ¡ �.}| jj|||fd| ji|¤Ž}W d  ƒ n1 s<0    Y  |D ] }|d }t|tƒsJ|ƒ |d< qJ|S )a  Return information about columns in `table_name`.

        Given a string `table_name` and an optional string `schema`, return
        column information as a list of dicts with these keys:

        * ``name`` - the column's name

        * ``type`` - the type of this column; an instance of
          :class:`~sqlalchemy.types.TypeEngine`

        * ``nullable`` - boolean flag if the column is NULL or NOT NULL

        * ``default`` - the column's server default value - this is returned
          as a string SQL expression.

        * ``autoincrement`` - indicates that the column is auto incremented -
          this is returned as a boolean or 'auto'

        * ``comment`` - (optional) the comment on the column. Only some
          dialects return this key

        * ``computed`` - (optional) when present it indicates that this column
          is computed by the database. Only some dialects return this key.
          Returned as a dict with the keys:

          * ``sqltext`` - the expression used to generate this column returned
            as a string SQL expression

          * ``persisted`` - (optional) boolean that indicates if the column is
            stored in the table

          .. versionadded:: 1.3.16 - added support for computed reflection.

        * ``identity`` - (optional) when present it indicates that this column
          is a generated always column. Only some dialects return this key.
          For a list of keywords on this dict see :class:`_schema.Identity`.

          .. versionadded:: 1.4 - added support for identity column reflection.

        * ``dialect_options`` - (optional) a dict with dialect specific options

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        :return: list of dictionaries, each representing the definition of
         a database column.

        r   NÚtype)rA   r.   Úget_columnsr   r   r   )r    rJ   r   r#   r@   Zcol_defsZcol_defÚcoltyper   r   r   rg   º  s    6
ÿÿÿ$
zInspector.get_columnsc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )a¨  Return information about primary key constraint on `table_name`.

        Given a string `table_name`, and an optional string `schema`, return
        primary key information as a dictionary with these keys:

        * ``constrained_columns`` -
          a list of column names that make up the primary key

        * ``name`` -
          optional name of the primary key constraint.

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        r   N)rA   r.   Úget_pk_constraintr   rb   r   r   r   ri   û  s    
ÿÿÿzInspector.get_pk_constraintc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )a¿  Return information about foreign_keys in `table_name`.

        Given a string `table_name`, and an optional string `schema`, return
        foreign key information as a list of dicts with these keys:

        * ``constrained_columns`` -
          a list of column names that make up the foreign key

        * ``referred_schema`` -
          the name of the referred schema

        * ``referred_table`` -
          the name of the referred table

        * ``referred_columns`` -
          a list of column names in the referred table that correspond to
          constrained_columns

        * ``name`` -
          optional name of the foreign key constraint.

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        r   N)rA   r.   rU   r   rb   r   r   r   rU     s    
ÿÿÿzInspector.get_foreign_keysc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )a  Return information about indexes in `table_name`.

        Given a string `table_name` and an optional string `schema`, return
        index information as a list of dicts with these keys:

        * ``name`` -
          the index's name

        * ``column_names`` -
          list of column names in order

        * ``unique`` -
          boolean

        * ``column_sorting`` -
          optional dict mapping column names to tuple of sort keywords,
          which may include ``asc``, ``desc``, ``nulls_first``, ``nulls_last``.

          .. versionadded:: 1.3.5

        * ``dialect_options`` -
          dict of dialect-specific index options.  May not be present
          for all dialects.

          .. versionadded:: 1.0.0

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        r   N)rA   r.   Úget_indexesr   rb   r   r   r   rj   8  s    $
ÿÿÿzInspector.get_indexesc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )a  Return information about unique constraints in `table_name`.

        Given a string `table_name` and an optional string `schema`, return
        unique constraint information as a list of dicts with these keys:

        * ``name`` -
          the unique constraint's name

        * ``column_names`` -
          list of column names in order

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        r   N)rA   r.   Úget_unique_constraintsr   rb   r   r   r   rk   a  s    
ÿÿÿz Inspector.get_unique_constraintsc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )a�  Return information about the table comment for ``table_name``.

        Given a string ``table_name`` and an optional string ``schema``,
        return table comment information as a dictionary with these keys:

        * ``text`` -
            text of the comment.

        Raises ``NotImplementedError`` for a dialect that does not support
        comments.

        .. versionadded:: 1.2

        r   N)rA   r.   Úget_table_commentr   rb   r   r   r   rl   {  s    
ÿÿÿzInspector.get_table_commentc                 K   sJ   |   ¡ �.}| jj|||fd| ji|¤ŽW  d  ƒ S 1 s<0    Y  dS )ac  Return information about check constraints in `table_name`.

        Given a string `table_name` and an optional string `schema`, return
        check constraint information as a list of dicts with these keys:

        * ``name`` -
          the check constraint's name

        * ``sqltext`` -
          the check constraint's SQL expression

        * ``dialect_options`` -
          may or may not be present; a dictionary with additional
          dialect-specific options for this CHECK constraint

          .. versionadded:: 1.3.8

        :param table_name: string name of the table.  For special quoting,
         use :class:`.quoted_name`.

        :param schema: string schema name; if omitted, uses the default schema
         of the database connection.  For special quoting,
         use :class:`.quoted_name`.

        .. versionadded:: 1.1.0

        r   N)rA   r.   Úget_check_constraintsr   rb   r   r   r   rm   �  s    
ÿÿÿzInspector.get_check_constraintsz*:meth:`_reflection.Inspector.reflecttable`z¬The :meth:`_reflection.Inspector.reflecttable` method was renamed to :meth:`_reflection.Inspector.reflect_table`. This deprecated alias will be removed in a future release.c                 O   s   | j |i |¤ŽS )z1See reflect_table. This method name is deprecated)Úreflect_table)r    r"   Úkwargsr   r   r   Úreflecttable²  s    	zInspector.reflecttabler   Tc                    s¦  |durˆ |v rdS |  ˆ ¡ | jj}|  ¡ �}| ˆ ¡}W d  ƒ n1 sN0    Y  ˆ j}	t‡ fdd„|jD ƒƒ}
| j|	|fi ˆ j	¤Ž}|ršˆ  
|¡ tjrÌt|tƒr¶| |j¡}t|	tƒrÌ|	 |j¡}	d}i }| j|	|fi ˆ j	¤ŽD ]}d}|  ˆ ||||¡ qê|�s$|  |	|¡�s$t |	¡‚|  |	|ˆ ||¡ |  |	|ˆ ||||||
¡	 |  |	|ˆ ||||
¡ |  |	|ˆ ||||
¡ |  |	|ˆ ||||
¡ |  |	|ˆ |
¡ dS )aZ  Given a :class:`_schema.Table` object, load its internal
        constructs based on introspection.

        This is the underlying method used by most dialects to produce
        table reflection.  Direct usage is like::

            from sqlalchemy import create_engine, MetaData, Table
            from sqlalchemy import inspect

            engine = create_engine('...')
            meta = MetaData()
            user_table = Table('user', meta)
            insp = inspect(engine)
            insp.reflect_table(user_table, None)

        .. versionchanged:: 1.4 Renamed from ``reflecttable`` to
           ``reflect_table``

        :param table: a :class:`~sqlalchemy.schema.Table` instance.
        :param include_columns: a list of string column names to include
          in the reflection process.  If ``None``, all columns are reflected.

        Nc                 3   s(   | ] }|ˆ j v r|ˆ j  |¡fV  qd S r   )Údialect_kwargsr   ©r   r   ©Útabler   r   r   î  s   
þz*Inspector.reflect_table.<locals>.<genexpr>FT)rV   r*   r.   rA   Zschema_for_objectrL   ÚdictÚreflection_optionsra   rq   Z_validate_dialect_kwargsr
   Zpy2kr   ÚstrÚdecodeÚencodingrg   Ú_reflect_columnrI   r   ZNoSuchTableErrorÚ_reflect_pkÚ_reflect_fkÚ_reflect_indexesÚ_reflect_unique_constraintsÚ_reflect_check_constraintsÚ_reflect_table_comment)r    rt   Úinclude_columnsÚexclude_columnsÚresolve_fksÚ
_extend_onr.   r@   r   rJ   rv   Ztbl_optsZfound_tableÚcols_by_orig_nameÚcol_dr   rs   r   rn   ½  s¨     

(þÿÿ


ÿÿ
û	

ÿ÷ù
ù
ù
ÿzInspector.reflect_tablec                    s–  ˆ d }|j j | |ˆ ¡ |j | |ˆ ¡ ˆ d }|r>||vsJ|rN||v rNd S ˆ d }t‡ fdd„dD ƒƒ}	dˆ v r‚|	 ˆ d ¡ g }
ˆ  d¡d urèˆ d }t|tjj	ƒrºt
j|dd	�}n$t|t
jƒsÞt
jt ˆ d ¡dd	�}|
 |¡ d
ˆ v �rt
jf i ˆ d
 ¤Ž}|
 |¡ dˆ v �r8t
jf i ˆ d ¤Ž}|
 |¡ dˆ v �rN|  ˆ |
¡ t
j||g|
¢R i |	¤Ž ||< }|j|jv �r„d|_|j|dd� d S )NrL   rf   c                 3   s"   | ]}|ˆ v r|ˆ | fV  qd S r   r   rr   ©r†   r   r   r   \  s   ÷z,Inspector._reflect_column.<locals>.<genexpr>)ZnullableZautoincrementÚquoteÚinfor$   ÚcommentÚdialect_optionsÚdefaultT)Z
_reflectedÚcomputedÚidentityÚsequence)Zreplace_existing)ÚmetadataÚdispatchZcolumn_reflectru   r[   r   r   r	   ÚelementsZ
TextClauseÚ	sa_schemaZDefaultClauseZFetchedValueÚtextÚappendZComputedZIdentityÚ_reflect_col_sequenceZColumnr$   Úprimary_keyZappend_column)r    rt   r†   r�   r‚   r…   Z	orig_namerL   rh   Zcol_kwÚcolargsrŒ   r�   Úcolr   r‡   r   rz   I  sX    ÿÿþÿ





ÿÿÿzInspector._reflect_columnc                 C   sT   d|v rP|d }t  |d dd¡}d|v r4|d |_d|v rF|d |_| |¡ d S )Nr�   rL   r   ÚstartÚ	increment)r“   ÚSequencerš   r›   r•   )r    r†   r˜   Úseqr�   r   r   r   r–   ‹  s    

zInspector._reflect_col_sequencec                    sP   | j ||fi |j¤Ž}|rL‡ ‡fdd„|d D ƒ}| d¡|j_|j |¡ d S )Nc                    s$   g | ]}|ˆ v r|ˆvrˆ | ‘qS r   r   )r   Úpk©r…   r‚   r   r   rM   �  s   þz)Inspector._reflect_pk.<locals>.<listcomp>Úconstrained_columnsrL   )ri   rq   r   r—   rL   Z_reload)r    rJ   r   rt   r…   r‚   Zpk_consZpk_colsr   rŸ   r   r{   –  s    ÿÿþzInspector._reflect_pkc
              	      sP  | j ||fi |j¤Ž}
|
D �].}|d }‡ fdd„|d D ƒ}|rPt|ƒ |¡s|rdt|ƒ |¡rdq|d }|d }|d }g }|d urÎ|r¬tj||jf|| j|dœ|	¤Ž |D ]}| 	d	 
|||g¡¡ q°nD|rôtj||jf| jtj|d
œ|	¤Ž |D ]}| 	d	 
||g¡¡ qød|v �r&|d }ni }| tj|||fddi|¤Ž¡ qd S )NrL   c                    s"   g | ]}|ˆ v rˆ | j n|‘qS r   )r$   )r   Úc©r…   r   r   rM   ½  s   ÿz)Inspector._reflect_fk.<locals>.<listcomp>r    Úreferred_schemarN   Úreferred_columns)r   Úautoload_withr„   Ú.)r¥   r   r„   ÚoptionsZlink_to_nameT)rU   rq   rT   ÚintersectionrP   r“   ZTabler�   r*   r•   ÚjoinZBLANK_SCHEMAÚappend_constraintZForeignKeyConstraint)r    rJ   r   rt   r…   r�   r‚   rƒ   r„   rv   r\   Zfkey_dÚconnamer    r£   rN   r¤   ZrefspecÚcolumnr§   r   r¢   r   r|   ª  s„    ÿÿ

þÿþüûþûúÿþûú

ýüûÿzInspector._reflect_fkZascÚdescZnulls_firstZ
nulls_lastc                 C   sT  |   ||¡}|D �]<}	|	d }
|	d }|	 di ¡}|	d }|	 dd¡}|	 di ¡}|	 d¡}|rˆt|ƒ |¡sˆt d	|d
 |¡f ¡ q|rŽqg }|D ]„}z||v r¬|| n|j| }W n* tyâ   t d|||f ¡ Y q–Y n0 | |d¡}| j	D ]\}}||v rö||ƒ}qö| 
|¡ q–tj|
g|¢R d|itt| ¡ ƒd|fg ƒ¤Ž qd S )NrL   Úcolumn_namesÚcolumn_sortingÚuniquerf   Úindexr‹   Zduplicates_constraintz5Omitting %s key for (%s), key covers omitted columns.ú, z5%s key '%s' was not located in columns for table '%s'r   Z_table)rj   r   rT   Úissubsetr
   Úwarnr©   r¡   ÚKeyErrorÚ_index_sort_exprsr•   r“   ÚIndexru   rW   r   )r    rJ   r   rt   r…   r�   r‚   rv   ZindexesZindex_drL   Úcolumnsr¯   r°   Zflavorr‹   Ú
duplicatesZidx_colsr¡   Zidx_colZ	c_sortingr   Úopr   r   r   r}      s\    

ÿÿÿ
ýÿÿ

ÿþýüzInspector._reflect_indexesc              
   C   sî   z|   ||¡}W n ty$   Y d S 0 |D ]¾}	|	d }
|	d }|	 d¡}|rpt|ƒ |¡spt dd |¡ ¡ q*|rvq*g }|D ]R}z||v r”|| n|j| }W n$ t	yÄ   t d||f ¡ Y q~0 | 
|¡ q~| tj|d|
iŽ¡ q*d S )NrL   r®   Zduplicates_indexzDOmitting unique constraint key for (%s), key covers omitted columns.r²   zDunique constraint key '%s' was not located in columns for table '%s')rk   ÚNotImplementedErrorr   rT   r³   r
   r´   r©   r¡   rµ   r•   rª   r“   ZUniqueConstraint)r    rJ   r   rt   r…   r�   r‚   rv   ÚconstraintsÚconst_dr«   r¸   r¹   Zconstrained_colsr¡   Zconstrained_colr   r   r   r~   :  sD    
ÿÿÿ
ýÿÿ
ÿz%Inspector._reflect_unique_constraintsc           
      C   sJ   z|   ||¡}W n ty$   Y d S 0 |D ]}	| tjf i |	¤Ž¡ q*d S r   )rm   r»   rª   r“   ZCheckConstraint)
r    rJ   r   rt   r…   r�   r‚   rv   r¼   r½   r   r   r   r   m  s    
z$Inspector._reflect_check_constraintsc                 C   s8   z|   ||¡}W n ty$   Y d S 0 | dd ¡|_d S )Nr”   )rl   r»   r   rŠ   )r    rJ   r   rt   rv   Zcomment_dictr   r   r   r€   €  s
    z Inspector._reflect_table_comment)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)r   TN)>r   Ú
__module__Ú__qualname__Ú__doc__r
   Ú
deprecatedr+   Úclassmethodr2   r(   r4   r3   r:   r   Z	_inspectsr   r<   r   r=   r   r>   Ú
contextlibÚcontextmanagerrA   rC   ÚpropertyrD   rE   rG   rI   rK   r^   r_   r`   ra   rc   rd   re   rg   ri   rU   rj   rk   rl   rm   Zdeprecated_20rp   rn   rz   r–   r{   r|   r   Zasc_opZdesc_opZnulls_first_opZnulls_last_opr¶   r}   r~   r   r€   r   r   r   r   r'   <   sŒ   þ

	þ











7




A

$
)


"þ
   ú
 BPü:3r'   )rÀ   rÃ   Úbaser   r   r   Ú r   r   r	   r
   r   r   r“   Zsql.type_apir   r   Ú	decoratorr&   Z_self_inspectsÚobjectr'   r   r   r   r   Ú<module>   s    
