U kfw%@sdZddlmZddlZddlmZddlmZddlmZddlm Z ddl m Z dd l m Z dd l m Z dd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z#dd l$m%Z%dd!l&m'Z'dd"l(m)Z)dd#l*m+Z+dd$l m,Z,dd%l-m.Z.er dd&l/m0Z0dd'l/m1Z1dd(l/m2Z2dd)l/m3Z3dd*l/m4Z4dd+l/m5Z5dd,l/m6Z6dd-l/m7Z7dd.l/m8Z8ed/Z9e j:d0d1d2d3d3d/d4d5d6Z;d7d8d9d:d;Zd?d?eZ>ej?Gd@dAdAej@dAZAe.GdBdCdCZBeGdDdEdEZCdS)FaProvides 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.. ) annotationsN) dataclass)auto)Flag)unique)Any)Callable) Collection)Dict) Generator)Iterable)List)Optional)Sequence)Set)Tuple) TYPE_CHECKING)TypeVar)Union) Connection)Engine)exc) inspection)sql)util) operators)schema)_ad_hoc_cache_key_from_args) TextClause) TypeEngine)InternalTraversal) topological)final)Dialect)ReflectedCheckConstraint)ReflectedColumn)ReflectedForeignKeyConstraint)ReflectedIndex)ReflectedPrimaryKeyConstraint)ReflectedTableComment)ReflectedUniqueConstraint)TableKey_RCallable[..., _R]r%rrfnselfconargskwreturncs|dd}|dkr&|||f||Sddh|jtdd|Dtfdd|Df}||}|dkr|||f||}|||<|S)N info_cache unreflectablecss|]}t|tr|VqdSN) isinstancestr).0ar>L/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/engine/reflection.py \s zcache..c3s"|]\}}|kr||fVqdSr9r>)r<kvexcluder>r?r@]s)get__name__tupleitemsr1r2r3r4r5r7keyretr>rCr?cacheNs  rLzTuple[str, InternalTraversal]z0Callable[[Callable[..., _R]], Callable[..., _R]]) traverse_argsr6cs&tjdddddddfdd }|S) Nr/r%rrr.r0csf|dd}|dkr&|||f||St|jf|}||}|dkrb|||f||}|||<|S)Nr7)rErrFrIrMr>r?gois  zflexi_cache..go)r decorator)rMrOr>rNr? flexi_cachefsrQc@s6eZdZdZeZeZeZeeBZeeBeBZ dS) ObjectKindaEnumerator that indicates which kind of object to return when calling the ``get_multi`` methods. This is a Flag enum, so custom combinations can be passed. For example, to reflect tables and plain views ``ObjectKind.TABLE | ObjectKind.VIEW`` may be used. .. note:: Not all dialect may support all kind of object. If a dialect does not support a particular object an empty dict is returned. In case a dialect supports an object, but the requested method is not applicable for the specified kind the default value will be returned for each reflected object. For example reflecting check constraints of view return a dict with all the views with empty lists as values. N) rF __module__ __qualname____doc__rTABLEZVIEWZMATERIALIZED_VIEWZANY_VIEWANYr>r>r>r?rR~s rRc@s$eZdZdZeZeZeeBZdS) ObjectScopez]Enumerator that indicates which scope to use when calling the ``get_multi`` methods. N)rFrSrTrUrDEFAULT TEMPORARYrWr>r>r>r?rXs rXc @s@eZdZUdZded<ded<ded<ded <d ed <ed d ddddZeddddddZ dddddZ dddddZ dddddZ dd d!d"Z eed d#dddd$d%Zeedddd&d'Zeedddd(d)Zejd*d d+d,Zejd-d d.d/Zed0d d1d2Zd3d4d5d6d7Zdd0d3d4d9d:d;Zddd?ZdddddddddddZ?dddddddddZ@dddddddddVdd ddZAeBjCeBjDeBjEeBjFdZGdddddddVddddZHdddddddVddddZIdddddddVddddZJddddVddddZKdd0dddd3ddddZLd8S) 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. zUnion[Engine, Connection]bindrenginebool_op_context_requires_connectr%dialectzDict[Any, Any]r7z1.4aThe __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`.r\cCs||dS)aInitialize a new :class:`_reflection.Inspector`. :param bind: a :class:`~sqlalchemy.engine.Connection`, 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` N) _init_legacyr2r\r>r>r?__init__szInspector.__init__zCallable[..., Any])initr\r6cCs,t|jdr|jj}||}||||S)N inspector)hasattrr`rf__new__)clsrer\r2r>r>r? _constructs    zInspector._constructNone)r\r6cCs$t|dr||n ||dS)NZexec_driver_sql)rg_init_connection _init_enginercr>r>r?rbs  zInspector._init_legacy)r]r6cCs2||_|_|d|_|jj|_i|_dS)NT)r\r]connectcloser_r`r7)r2r]r>r>r?rms    zInspector._init_enginer) connectionr6cCs(||_|j|_d|_|jj|_i|_dS)NF)r\r]r_r`r7)r2rpr>r>r?rls  zInspector._init_connectionr6cCs|jdS)zreset the cache for this :class:`.Inspector`. Inspection methods that have data cached will emit SQL queries when next called to get new data. .. versionadded:: 2.0 N)r7clearr2r>r>r? clear_caches zInspector.clear_cacheaThe 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`.cCs||j|S)aZConstruct a new dialect-specific Inspector object from the given engine or connection. :param bind: a :class:`~sqlalchemy.engine.Connection` or :class:`~sqlalchemy.engine.Engine`. 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`. )rjrb)rir\r>r>r? from_engineszInspector.from_enginecCsttj|Sr9)r[rjrmrar>r>r? _engine_insp-szInspector._engine_inspcCsttj|Sr9)r[rjrlrar>r>r?_connection_insp1szInspector._connection_inspz!Generator[Connection, None, None]ccs8|jr|j}n|j}z |VW5|jr2|XdS)aReturn 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)r_r\rnro)r2connr>r>r?_operation_context5s   zInspector._operation_contextz Generator[Inspector, None, None]c cs6|$}||jj|}|j|_|VW5QRXdS)zReturn an :class:`_reflection.Inspector` from this one that will run all operations on a single connection. N)ryrj __class__rlr7)r2rxZsub_inspr>r>r?_inspection_contextJs zInspector._inspection_contextz Optional[str]cCs|jjS)zReturn 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_namersr>r>r?r|Ws zInspector.default_schema_namerz List[str])r5r6c Ks:|(}|jj|fd|ji|W5QRSQRXdS)zReturn all schema names. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. r7N)ryr`get_schema_namesr7r2r5rxr>r>r?r}bs zInspector.get_schema_namesN)rr5r6c Ks<|*}|jj||fd|ji|W5QRSQRXdS)aReturn all table names 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` and/or :meth:`_reflection.Inspector.get_materialized_view_names` methods. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. seealso:: :meth:`_reflection.Inspector.get_sorted_table_and_fkc_names` :attr:`_schema.MetaData.sorted_tables` r7N)ryr`get_table_namesr7r2rr5rxr>r>r?ros zInspector.get_table_namesr;) table_namerr5r6c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn True if the backend has a table, view, or temporary 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. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionadded:: 1.4 - the :meth:`.Inspector.has_table` method replaces the :meth:`_engine.Engine.has_table` method. .. versionchanged:: 2.0:: :meth:`.Inspector.has_table` now formally supports checking for additional table-like objects: * any type of views (plain or materialized) * temporary tables of any kind Previously, these two checks were not formally specified and different dialects would vary in their behavior. The dialect testing suite now includes tests for all of these object types and should be supported by all SQLAlchemy-included dialects. Support among third party dialects may be lagging, however. r7N)ryr` has_tabler7r2rrr5rxr>r>r?rs zInspector.has_table) sequence_namerr5r6c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn True if the backend has a sequence with the given name. :param sequence_name: name of the sequence to check :param schema: schema name to query, if not the default schema. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionadded:: 1.4 r7N)ryr` has_sequencer7)r2rrr5rxr>r>r?rs zInspector.has_sequence)r index_namerr5r6c Ks@|.}|jj||||fd|ji|W5QRSQRXdS)aCheck the existence of a particular index name in the database. :param table_name: the name of the table the index belongs to :param index_name: the name of the index to check :param schema: schema name to query, if not the default schema. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionadded:: 2.0 r7N)ryr` has_indexr7)r2rrrr5rxr>r>r?rs zInspector.has_index) schema_namer5r6c Ks<|*}|jj||fd|ji|W5QRSQRXdS)aTReturn True if the backend has a schema with the given name. :param schema_name: name of the schema to check :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionadded:: 2.0 r7N)ryr` has_schemar7)r2rr5rxr>r>r?rs zInspector.has_schemaz;List[Tuple[Optional[str], List[Tuple[str, Optional[str]]]]]cKsdd|j|fdDS)agReturn 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. :param schema: schema name to query, if not the default schema. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. seealso:: :meth:`_reflection.Inspector.get_table_names` :func:`.sort_tables_and_constraints` - similar method which works with an already-given :class:`_schema.MetaData`. cSs.g|]&\}}|r|dnddd|DfqS)rNcSsg|]\\}}}||fqSr>r>)r<_tnameZfksr>r>r? s zGInspector.get_sorted_table_and_fkc_names...r>)r< table_keyZ fk_collectionr>r>r?rs  z.)consider_schemas)%sort_tables_on_foreign_key_dependency)r2rr5r>r>r?get_sorted_table_and_fkc_namess z(Inspector.get_sorted_table_and_fkc_namesr9zCollection[Optional[str]]zgList[Tuple[Optional[Tuple[Optional[str], str]], List[Tuple[Tuple[Optional[str], str], Optional[str]]]]])rr5r6c sZttttf}t}tig}|D]}|j|f|}|||D]b\\}} } dd| D|| f<| D]:} | | dks|| dkrr|| d| df|| ffqrqLq(ztt ||} Wnlt j k r2} zJ| j D].|fdddDqtt ||} W5d} ~ XYnXfd d | D}|dtfgS) aReturn dependency-sorted table and foreign key constraint names referred to within multiple schemas. This method may be compared to :meth:`.Inspector.get_sorted_table_and_fkc_names`, which works on one schema at a time; here, the method is a generalization that will consider multiple schemas at once including that it will resolve for cross-schema foreign keys. .. versionadded:: 2.0 cSsh|] }|dqS)namer>r<Zfkr>r>r? ?szBInspector.sort_tables_on_foreign_key_dependency..referred_tablereferred_schemac3s|]}d|fVqdS)rNr>)r<Zfkc)edger>r?r@VszBInspector.sort_tables_on_foreign_key_dependency..rNcsDg|]<\ffddfddDDfqS)csg|]}f|fqSr>r>rschnamerr>r?raszNInspector.sort_tables_on_foreign_key_dependency...css|]\}}|VqdSr9r>)r<rrr>r>r?r@cszMInspector.sort_tables_on_foreign_key_dependency...) difference)r<)fknames_for_tableremaining_fkcsrr?r^s  zCInspector.sort_tables_on_foreign_key_dependency..)rrr;setget_multi_foreign_keysextendrHaddlistr#sortrZCircularDependencyErroredgesremoveupdate)r2rr5Z SchemaTabZtuplesZtnamesrZ schema_fkeysrrfkeysZfkeyZcandidate_sorterrrKr>)rrrr?rsJ       "  z/Inspector.sort_tables_on_foreign_key_dependencyc Ks:|(}|jj|fd|ji|W5QRSQRXdS)awReturn a list of temporary table names for the current bind. This method is unsupported by most dialects; currently only Oracle, PostgreSQL and SQLite implements it. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. r7N)ryr`get_temp_table_namesr7r~r>r>r?rls zInspector.get_temp_table_namesc Ks:|(}|jj|fd|ji|W5QRSQRXdS)anReturn a list of temporary view names for the current bind. This method is unsupported by most dialects; currently only PostgreSQL and SQLite implements it. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. r7N)ryr`get_temp_view_namesr7r~r>r>r?r}s zInspector.get_temp_view_namesDict[str, Any]c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aqReturn a dictionary of options specified when the table of the given name was created. This currently includes some options that apply to MySQL and Oracle 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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dict with the table options. The returned keys depend on the dialect in use. Each one is prefixed with the dialect name. .. seealso:: :meth:`Inspector.get_multi_table_options` r7N)ryr`get_table_optionsr7rr>r>r?rs zInspector.get_table_optionszOptional[Sequence[str]]rRrXDict[TableKey, Dict[str, Any]])r filter_nameskindscoper5r6c KsJ|8}|jj|f|||||jd|}t|W5QRSQRXdS)aReturn a dictionary of options specified when the tables in the given schema were created. The tables can be filtered by passing the names to use to ``filter_names``. This currently includes some options that apply to MySQL and Oracle tables. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if options of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are dictionaries with the table options. The returned keys in each dict depend on the dialect in use. Each one is prefixed with the dialect name. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_table_options` rrrrr7N)ryr`get_multi_table_optionsr7dict)r2rrrrr5rxresr>r>r?rs-  z!Inspector.get_multi_table_optionsc Ks<|*}|jj||fd|ji|W5QRSQRXdS)aReturn all non-materialized view names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionchanged:: 2.0 For those dialects that previously included the names of materialized views in this list (currently PostgreSQL), this method no longer returns the names of materialized views. the :meth:`.Inspector.get_materialized_view_names` method should be used instead. .. seealso:: :meth:`.Inspector.get_materialized_view_names` r7N)ryr`get_view_namesr7rr>r>r?rs zInspector.get_view_namesc Ks<|*}|jj||fd|ji|W5QRSQRXdS)aReturn all materialized view names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. .. versionadded:: 2.0 .. seealso:: :meth:`.Inspector.get_view_names` r7N)ryr`get_materialized_view_namesr7rr>r>r?rs z%Inspector.get_materialized_view_namesc Ks<|*}|jj||fd|ji|W5QRSQRXdS)akReturn all sequence names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. r7N)ryr`get_sequence_namesr7rr>r>r?rs zInspector.get_sequence_names) view_namerr5r6c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn definition for the plain or materialized view called ``view_name``. :param view_name: Name of the view. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. r7N)ryr`get_view_definitionr7)r2rrr5rxr>r>r?r,s zInspector.get_view_definitionList[ReflectedColumn]c KsF|$}|jj|||fd|ji|}W5QRX|rB||g|S)aJReturn information about columns in ``table_name``. Given a string ``table_name`` and an optional string ``schema``, return column information as a list of :class:`.ReflectedColumn`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: list of dictionaries, each representing the definition of a database column. .. seealso:: :meth:`Inspector.get_multi_columns`. r7)ryr` get_columnsr7_instantiate_types)r2rrr5rxcol_defsr>r>r?r@s  zInspector.get_columnszIterable[List[ReflectedColumn]])datar6cCs4|D]*}|D] }|d}t|ts ||d<q qdS)Ntype)r:r!)r2rrZcol_defcoltyper>r>r?rbs  zInspector._instantiate_types%Dict[TableKey, List[ReflectedColumn]]c KsL|,}t|jj|f|||||jd|}W5QRX|||S)aWReturn information about columns in all objects in the given schema. The objects can be filtered by passing the names to use to ``filter_names``. For each table the value is a list of :class:`.ReflectedColumn`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if columns of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are list of dictionaries, each representing the definition of a database column. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_columns` r)ryrr`get_multi_columnsr7rvalues)r2rrrrr5rxZtable_col_defsr>r>r?rls ,  zInspector.get_multi_columnsr*c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)a`Return information about primary key constraint in ``table_name``. Given a string ``table_name``, and an optional string `schema`, return primary key information as a :class:`.ReflectedPrimaryKeyConstraint`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary representing the definition of a primary key constraint. .. seealso:: :meth:`Inspector.get_multi_pk_constraint` r7N)ryr`get_pk_constraintr7rr>r>r?rs zInspector.get_pk_constraintz-Dict[TableKey, ReflectedPrimaryKeyConstraint]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)auReturn information about primary key constraints in all tables in the given schema. The tables can be filtered by passing the names to use to ``filter_names``. For each table the value is a :class:`.ReflectedPrimaryKeyConstraint`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if primary keys of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are dictionaries, each representing the definition of a primary key constraint. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_pk_constraint` rN)ryrr`get_multi_pk_constraintr7r2rrrrr5rxr>r>r?rs+ z!Inspector.get_multi_pk_constraint#List[ReflectedForeignKeyConstraint]c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)agReturn 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 :class:`.ReflectedForeignKeyConstraint`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a list of dictionaries, each representing the a foreign key definition. .. seealso:: :meth:`Inspector.get_multi_foreign_keys` r7N)ryr`get_foreign_keysr7rr>r>r?rs zInspector.get_foreign_keys3Dict[TableKey, List[ReflectedForeignKeyConstraint]]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)aoReturn information about foreign_keys in all tables in the given schema. The tables can be filtered by passing the names to use to ``filter_names``. For each table the value is a list of :class:`.ReflectedForeignKeyConstraint`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if foreign keys of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are list of dictionaries, each representing a foreign key definition. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_foreign_keys` rN)ryrr`rr7rr>r>r?rs- z Inspector.get_multi_foreign_keysList[ReflectedIndex]c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)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 :class:`.ReflectedIndex`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a list of dictionaries, each representing the definition of an index. .. seealso:: :meth:`Inspector.get_multi_indexes` r7N)ryr` get_indexesr7rr>r>r?rUs zInspector.get_indexes$Dict[TableKey, List[ReflectedIndex]]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)aPReturn information about indexes in in all objects in the given schema. The objects can be filtered by passing the names to use to ``filter_names``. For each table the value is a list of :class:`.ReflectedIndex`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if indexes of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are list of dictionaries, each representing the definition of an index. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_indexes` rN)ryrr`get_multi_indexesr7rr>r>r?rss, zInspector.get_multi_indexesList[ReflectedUniqueConstraint]c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)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 :class:`.ReflectedUniqueConstraint`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a list of dictionaries, each representing the definition of an unique constraint. .. seealso:: :meth:`Inspector.get_multi_unique_constraints` r7N)ryr`get_unique_constraintsr7rr>r>r?rs z Inspector.get_unique_constraints/Dict[TableKey, List[ReflectedUniqueConstraint]]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)aReturn information about unique constraints in all tables in the given schema. The tables can be filtered by passing the names to use to ``filter_names``. For each table the value is a list of :class:`.ReflectedUniqueConstraint`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if constraints of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are list of dictionaries, each representing the definition of an unique constraint. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_unique_constraints` rN)ryrr`get_multi_unique_constraintsr7rr>r>r?rs- z&Inspector.get_multi_unique_constraintsr+c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn information about the table comment for ``table_name``. Given a string ``table_name`` and an optional string ``schema``, return table comment information as a :class:`.ReflectedTableComment`. Raises ``NotImplementedError`` for a dialect that does not support comments. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary, with the table comment. .. versionadded:: 1.2 .. seealso:: :meth:`Inspector.get_multi_table_comment` r7N)ryr`get_table_commentr7rr>r>r?rs zInspector.get_table_commentz%Dict[TableKey, ReflectedTableComment]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)aReturn information about the table comment in all objects in the given schema. The objects can be filtered by passing the names to use to ``filter_names``. For each table the value is a :class:`.ReflectedTableComment`. Raises ``NotImplementedError`` for a dialect that does not support comments. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if comments of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are dictionaries, representing the table comments. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_table_comment` rN)ryrr`get_multi_table_commentr7rr>r>r?r's/ z!Inspector.get_multi_table_commentList[ReflectedCheckConstraint]c Ks>|,}|jj|||fd|ji|W5QRSQRXdS)ayReturn 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 :class:`.ReflectedCheckConstraint`. :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`. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a list of dictionaries, each representing the definition of a check constraints. .. seealso:: :meth:`Inspector.get_multi_check_constraints` r7N)ryr`get_check_constraintsr7rr>r>r?rcs zInspector.get_check_constraints.Dict[TableKey, List[ReflectedCheckConstraint]]c KsF|4}t|jj|f|||||jd|W5QRSQRXdS)aReturn information about check constraints in all tables in the given schema. The tables can be filtered by passing the names to use to ``filter_names``. For each table the value is a list of :class:`.ReflectedCheckConstraint`. :param schema: string schema name; if omitted, uses the default schema of the database connection. For special quoting, use :class:`.quoted_name`. :param filter_names: optionally return information only for the objects listed here. :param kind: a :class:`.ObjectKind` that specifies the type of objects to reflect. Defaults to ``ObjectKind.TABLE``. :param scope: a :class:`.ObjectScope` that specifies if constraints of default, temporary or any tables should be reflected. Defaults to ``ObjectScope.DEFAULT``. :param \**kw: Additional keyword argument to pass to the dialect specific implementation. See the documentation of the dialect in use for more information. :return: a dictionary where the keys are two-tuple schema,table-name and the values are list of dictionaries, each representing the definition of a check constraints. The schema is ``None`` if no schema is provided. .. versionadded:: 2.0 .. seealso:: :meth:`Inspector.get_check_constraints` rN)ryrr`get_multi_check_constraintsr7rr>r>r?rs- z%Inspector.get_multi_check_constraintsr>Tzsa_schema.TablezOptional[Collection[str]]zCollection[str]zOptional[Set[sa_schema.Table]]zOptional[_ReflectionInfo])tableinclude_columnsexclude_columns resolve_fks _extend_on _reflect_infor6c s|dk r|krdS||jj}|}|} W5QRXj} fdd|jD} | | f} |dksx| |jkr|j| f| gt j t j |dj }| |j kr|j | | |jkrt| |jr|j| } | r| d}i}|j| D]}d}|||||q|s0|| | s0t| ||| ||||| |||||| ||| |||| ||| |||| ||| |||| ||| | dS)aZGiven 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. Ncs$i|]}|jkr|j|qSr>)dialect_kwargsrEr<rArr>r? s z+Inspector.reflect_table..)rrrrFT)rr\r`ryZschema_for_objectrreflection_optionscolumns_get_reflection_inforRrWrXrr8rZNoSuchTableError table_optionsrEZ_validate_dialect_kwargs_reflect_columnr _reflect_pk _reflect_fk_reflect_indexes_reflect_unique_constraints_reflect_check_constraints_reflect_table_comment)r2rrrrrrr`rxrrrrZtbl_optsZ found_tablecols_by_orig_namecol_dr>rr? reflect_tables!               zInspector.reflect_tabler'z Dict[str, sa_schema.Column[Any]])rrrrrr6csxd}|jj|||j||d}|r>||ksJ|rN||krNdSd}fdddD} dkr~| dg} ddk rd} | dk stt| trtj | dd } n&t| tj stj t | dd } n| } | | d krtjfd } | | d kr6tjfd }| |tj||f| | ||<}|j|jkrfd|_|j|dd dS) Nrrcsi|]}|kr||qSr>r>rrr>r?rjsz-Inspector._reflect_column..)ZnullableZ autoincrementquoteinforJcommentdialect_optionsdefaultT)Z _reflectedcomputedidentity)Zreplace_existing)metadatadispatchZcolumn_reflectrrEAssertionErrorr:r sa_schemaZ DefaultClauseZ FetchedValuertextappendZComputedZIdentityColumnrJ primary_key append_column)r2rrrrr orig_namerrZcol_kwZcolargsZ default_textrrrcolr>rr?rSsb            zInspector._reflect_column_ReflectionInfor-)rrrrrr6csV|j|}|rRfdd|dD}|d|j_|dd|j_|j|dS)Ncs$g|]}|kr|kr|qSr>r>)r<pkrrr>r?rsz)Inspector._reflect_pk..constrained_columnsrr) pk_constraintrErrrZ_reload)r2rrrrrZpk_consZpk_colsr>rr?rs  zInspector._reflect_pk) rrrrrrrrrr6c s|j|g} | D]~} | d} fdd| dD} |rHt| |s|r\t| |r\q| d}| d}| d}g}|dk r|rtj||jf||j||d| |D]}| d |||gqnF|rtj||jf|jtj ||d | |D]}| d ||gqd | kr"| d }ni}z,| tj | || fd | d d|Wqtjk rtd|jd| dd | dYqXqdS)Nrcs"g|]}|kr|jn|qSr>)rJ)r<crr>r?rsz)Inspector._reflect_fk..rrrreferred_columns)r autoload_withrr.)r rrroptionsTr)Z link_to_namerzOn reflected table z0, skipping reflection of foreign key constraint z-; one or more subject columns within name(s) z, z are not present in the table) foreign_keysrEr intersectionrrTablerr\rjoinZ BLANK_SCHEMAappend_constraintZForeignKeyConstraintrZConstraintColumnNotFoundErrorrwarnr)r2rrrrrrrrrrZfkey_dconnamerrrrZrefspeccolumnr r>rr?rs          zInspector._reflect_fk)ascdescZ nulls_firstZ nulls_last)rrrrrrrr6c Cs|j|g}|D]v} | d} | d} | d} | di} | d}| dd}| di}| d }|r|t| |s|q|rqg}t| D]\}}|dkr| std |d | d |d dqt| |}nz ||kr||}n |j |}Wn6t k r.t|d|d|j YqYnX| |dD]$}||j kr<|j |}||}q<| |qtj| f|||d|qdS)Nr column_names expressionscolumn_sortingrrindexrZduplicates_constraintz Skipping  z because key rz5 reflected as None but no 'expressions' were returnedz key z& was not located in columns for table r>)_tabler)indexesrErissubset enumeraterrrrrKeyErrorr_index_sort_exprsrrIndex)r2rrrrrrrrZindex_drrrrrZflavorr duplicatesZ idx_elementsrrZ idx_elementoptionopr>r>r?rs^             zInspector._reflect_indexesc Cs|j|g}|D]} | d} | d} | d} | d} | di}|rZt| |sZq| r`qg}| D]V}z||kr~||n|j|}Wn(tk rtd||jfYqhX| |qh| t j || | d|qdS)NrrrZduplicates_indexrzDunique constraint key '%s' was not located in columns for table '%s')rr) unique_constraintsrErrrrrrrrrrZUniqueConstraint)r2rrrrrrr constraintsconst_drrrr"rZconstrained_colsrZconstrained_colr>r>r?rUsD       z%Inspector._reflect_unique_constraintsc Cs.|j|g}|D]} |tjf| qdSr9)check_constraintsrErrZCheckConstraint) r2rrrrrrrr&r'r>r>r?rs z$Inspector._reflect_check_constraints)rrrrr6cCs|j|}|r|d|_dSNr) table_commentrEr)r2rrrrZ comment_dictr>r>r?rs z Inspector._reflect_table_comment)rr availablerr5r6c s|d<r.|r.tdkr.tt|ndid<}ddddddddd fd d }t|jdd |j|j|j|jdd |jdd |jdd |j dd |d }|r| ||S|SdS)Nrdr8TF)optionalcheck_filter_names_from_methrr^)methr-r.r6csxdksdksj|js$}nd}z,rN|fd|i}|rR|sRdni}Wntk rr|sji}YnX|S)Ng?rF)r`Z_overrides_defaultrFNotImplementedError)r/r-r._fnrrZfractionZ has_resultr5r2r>r?runs&   z+Inspector._get_reflection_info..run)r.)r-) rrr rr%r*r(rr8) lenrrrrrrrrrr) r2rrr+rr5r8r3rr>r2r?rsB $#   zInspector._get_reflection_info)N)N)N)N)N)r9)N)N)N)N)N)N)N)N)N)N)N)N)r>TNN)NNNN)MrFrSrTrU__annotations__r deprecatedrd classmethodrjrbrmrlrtrurZ _inspectsrrvrrw contextlibcontextmanagerryr{propertyr|r}rrrrrrrrrrrRrVrXrYrrrrrrrrrrrrrrrrrrrrrrrrrZasc_opZdesc_opZnulls_first_opZ nulls_last_opr rrrrrr>r>r>r?r[s       ""/N!:" <9!; :!;$=!>H ZB0 r[c@seZdZdZeddddZeddddZed dd d Zed dd dZeddddZ eddddZ eddddZ eddddZ dS)ReflectionDefaultsz5provides blank default values for reflection methods.rrqcCsgSr9r>rir>r>r?rszReflectionDefaults.columnsr*cCs dgdS)N)rrr>r<r>r>r?rsz ReflectionDefaults.pk_constraintrcCsgSr9r>r<r>r>r?r szReflectionDefaults.foreign_keysrcCsgSr9r>r<r>r>r?rszReflectionDefaults.indexesrcCsgSr9r>r<r>r>r?r%sz%ReflectionDefaults.unique_constraintsrcCsgSr9r>r<r>r>r?r(sz$ReflectionDefaults.check_constraintsrcCsiSr9r>r<r>r>r?r sz ReflectionDefaults.table_optionsr+cCsddiSr)r>r<r>r>r?r*sz ReflectionDefaults.table_commentN) rFrSrTrUr7rrr rr%r(rr*r>r>r>r?r;s"r;c@sfeZdZUded<ded<ded<ded<d ed <d ed <d ed<ded<ded<dddddZdS)rrrz7Dict[TableKey, Optional[ReflectedPrimaryKeyConstraint]]rrr rrrr%z/Dict[TableKey, Optional[ReflectedTableComment]]r*rr(rrz+Dict[TableKey, exc.UnreflectableTableError]r8rk)otherr6cCsJ|jD]:\}}t||}|dk r |dkr:t|||q ||q dSr9)__dict__rHgetattrsetattrr)r2r=rArBovr>r>r?r"s  z_ReflectionInfo.updateN)rFrSrTr5rr>r>r>r?rs r)DrU __future__rr8 dataclassesrenumrrrtypingrrr r r r r rrrrrrrbaserrrrrrrrrZ sql.cache_keyrZ sql.elementsr Z sql.type_apir!Z sql.visitorsr"r#Z util.typingr$Z interfacesr%r&r'r(r)r*r+r,r-r.rPrLrQrRrXZ_self_inspectsZ Inspectabler[r;rr>r>r>r?s                                           N'