U aL@sdZddlZddlmZddlmZddlmZddlmZdd lmZdd lm Z dd lm Z dd l m Z dd l m Z ddlmZddl mZe jddZejGdddeZdS)aProvides 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) topologicalcOs|dd}|dkr&|||f||S|jtdd|Dtdd|Df}||}|dkr~|||f||}|||<|S)N info_cachecss|]}t|tjr|VqdSN) isinstancer string_types).0ar]C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\engine\reflection.py 2s zcache..css"|]\}}|dkr||fVqdS)rNr)rkvrrrr3s)get__name__tupleitems)fnselfconargskwrkeyretrrrcache+s  r%c@seZdZdZeddddZeddZdd Z d d Z d d Z eeddddZ e eddZe eddZe eddZejddZejddZeddZddZd^d d!Zd_d"d#Zd`d$d%Zdad&d'Zd(d)Z d*d+Z!dbd,d-Z"dcd.d/Z#ddd0d1Z$ded2d3Z%dfd4d5Z&dgd6d7Z'dhd8d9Z(did:d;Z)djdd?Z+dld@dAZ,e-dBdCdDdEZ.dmdHdIZ/dJdKZ0dLdMZ1dNdOZ2dPdQZ3dRe4j5fdSe4j6fdTe4j7fdUe4j8fgZ9dVdWZ:dXdYZ;dZd[Zt|jdr:| }|jj||jdW5QRSQRXgS)zReturn all schema names.get_schema_namesr)r,r-r@rDrr>rrrrDs  zInspector.get_schema_namesNc Cs4|"}|jj|||jdW5QRSQRXdS)aReturn 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` rEN)r@r-get_table_namesrrr r?rrrrFs  zInspector.get_table_namesc Cs0|}|j|||W5QRSQRXdS)aMReturn 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)r@r- has_table)r table_namer r?rrrrHs zInspector.has_tablec Cs0|}|j|||W5QRSQRXdS)zReturn 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)r@r- has_sequence)rZ sequence_namer r?rrrrJs zInspector.has_sequencec s0|}|jj|||jd}W5QRXt}ti|D]N}|||}tdd|D|<|D]"}||dkrf||d|fqfqNsz.referred_tablec3s|]}d|fVqdS)rNr)rZfkc)edgerrrWsz;Inspector.get_sorted_table_and_fkc_names..rNcsg|]}||fqSr) difference)rtname)fknames_for_tableremaining_fkcsrrrL\s)r@r-rFrsetget_foreign_keysaddlistrsortrZCircularDependencyErroredgesremoveupdate) rr r?ZtnamesZtuplesrPfkeysZfkeyZcandidate_sorterrr)rNrQrRrget_sorted_table_and_fkc_names*s:      "  z(Inspector.get_sorted_table_and_fkc_namesc Cs2| }|jj||jdW5QRSQRXdS)zReturn 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 rEN)r@r-get_temp_table_namesrr>rrrr^as zInspector.get_temp_table_namesc Cs2| }|jj||jdW5QRSQRXdS)zReturn 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 rEN)r@r-get_temp_view_namesrr>rrrr_ps zInspector.get_temp_view_namesc KsJt|jdrF|,}|jj|||fd|ji|W5QRSQRXiS)aReturn 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)r,r-r@r`rrrIr r"r?rrrr`~s  zInspector.get_table_optionsc Cs4|"}|jj|||jdW5QRSQRXdS)zReturn all view names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. rEN)r@r-get_view_namesrrGrrrrbs  zInspector.get_view_namesc Cs4|"}|jj|||jdW5QRSQRXdS)zReturn all sequence names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. rEN)r@r-get_sequence_namesrrGrrrrcs  zInspector.get_sequence_namesc Cs6|$}|jj||||jdW5QRSQRXdS)zReturn definition for `view_name`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. rEN)r@r-get_view_definitionr)rZ view_namer r?rrrrds zInspector.get_view_definitionc Ks\|$}|jj|||fd|ji|}W5QRX|D] }|d}t|ts6||d<q6|S)aReturn 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. rtype)r@r- get_columnsrrr )rrIr r"r?Zcol_defsZcol_defcoltyperrrrfs6   zInspector.get_columnsc Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn 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`. rN)r@r-get_pk_constraintrrarrrrhs zInspector.get_pk_constraintc Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn 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`. rN)r@r-rTrrarrrrTs zInspector.get_foreign_keysc Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn 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`. rN)r@r- get_indexesrrarrrri8s$ zInspector.get_indexesc Ks>|,}|jj|||fd|ji|W5QRSQRXdS)aReturn 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`. rN)r@r-get_unique_constraintsrrarrrrjas z Inspector.get_unique_constraintsc 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 dictionary with these keys: * ``text`` - text of the comment. Raises ``NotImplementedError`` for a dialect that does not support comments. .. versionadded:: 1.2 rN)r@r-get_table_commentrrarrrrk{s zInspector.get_table_commentc Ks>|,}|jj|||fd|ji|W5QRSQRXdS)acReturn 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 rN)r@r-get_check_constraintsrrarrrrls zInspector.get_check_constraintsz*:meth:`_reflection.Inspector.reflecttable`zThe :meth:`_reflection.Inspector.reflecttable` method was renamed to :meth:`_reflection.Inspector.reflect_table`. This deprecated alias will be removed in a future release.cOs |j||S)z1See reflect_table. This method name is deprecated) reflect_table)rr!kwargsrrr reflecttables zInspector.reflecttablerTc sz|dk r|krdS||jj}|}|}W5QRXj} tfdd|jD} |j| |fj } | r | t j rt |tr||j}t | tr| |j} d} i} |j| |fj D]}d} ||||| q| stj|| || ||| || |||| || || ||| || || ||| || || ||| || || 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. Nc3s(|] }|jkr|j|fVqdSr)dialect_kwargsrrrtablerrrs z*Inspector.reflect_table..FT)rUr)r-r@Zschema_for_objectrKdictreflection_optionsr`rpZ_validate_dialect_kwargsr Zpy2krstrdecodeencodingrf_reflect_columnrZNoSuchTableError _reflect_pk _reflect_fk_reflect_indexes_reflect_unique_constraints_reflect_check_constraints_reflect_table_comment)rrsinclude_columnsexclude_columns resolve_fks _extend_onr-r?r rIruZtbl_optsZ found_tablecols_by_orig_namecol_drrrrrms              zInspector.reflect_tablecsd}|jj|||j||d}|r>||ksJ|rN||krNdSd}tfdddD} dkr| dg} ddk rd} t| tjj rt j | dd } n$t| t j st j t ddd } | | d kr t jfd } | | d kr0t jfd } | | d krF|| t j||f| | ||<} | j|jkrvd| _|j| dd dS)NrKrec3s"|]}|kr||fVqdSrrrqrrrrZsz,Inspector._reflect_column..)ZnullableZ autoincrementquoteinfor#commentdialect_optionsdefaultT)Z _reflectedcomputedidentitysequence)Zreplace_existing)metadatadispatchZcolumn_reflectrtrZrrr elementsZ TextClause sa_schemaZ DefaultClauseZ FetchedValuetextappendZComputedZIdentity_reflect_col_sequenceZColumnr# primary_keyZ append_column)rrsrrrr orig_namerKrgZcol_kwcolargsrrcolrrrryGs\             zInspector._reflect_columncCsTd|krP|d}t|ddd}d|kr4|d|_d|krF|d|_||dS)NrrKrstart increment)rSequencerrr)rrrseqrrrrrs  zInspector._reflect_col_sequencecsL|j||f|j}|rHfdd|dD}|d|j_|j|dS)Ncs$g|]}|kr|kr|qSrr)rpkrrrrrLsz)Inspector._reflect_pk..constrained_columnsrK)rhrprrrKZ_reload)rrIr rsrrZpk_consZpk_colsrrrrzs zInspector._reflect_pkc  s:|j||f|j} | D]} | d} fdd| dD} |rNt| |rNq| d} | d}| d}g}| dk r|rtj||jf| |j|d||D]}|d | ||gqnD|rtj||jf|jtj |d ||D]}|d ||gqd | kr| d }ni}| tj | || fd d i|qdS)NrKcs"g|]}|kr|jn|qSr)r#)rcrrrrLsz)Inspector._reflect_fk..rreferred_schemarMreferred_columns)r autoload_withr.)rr roptionsZ link_to_nameT) rTrprS intersectionrZTablerr)rjoinZ BLANK_SCHEMAappend_constraintZForeignKeyConstraint)rrIr rsrrrrrur[Zfkey_dconnamerrrMrZrefspeccolumnrrrrr{s|      zInspector._reflect_fkZascdescZ nulls_firstZ nulls_lastc CsT|||}|D]<} | d} | d} | di} | d} | dd}| di}| d}|rt| |std |d | fq|rqg}| D]}z||kr||n|j|}Wn,tk rtd |||fYqYnX| |d }|j D]\}}||kr||}q| |qt j | f|d |it t|d| fgqdS)NrK column_namescolumn_sortinguniquereindexrZduplicates_constraintz5Omitting %s key for (%s), key covers omitted columns., z5%s key '%s' was not located in columns for table '%s'rZ_table)rirrSissubsetr warnrrKeyError_index_sort_exprsrrZIndexrtrVr)rrIr rsrrrruZindexesZindex_drKcolumnsrrZflavorr duplicatesZidx_colsrZidx_colZ c_sortingroprrrr|s\            zInspector._reflect_indexesc Csz|||}Wntk r&YdSX|D]} | d} | d} | d} |rrt| |srtdd| q,| rxq,g} | D]T}z||kr||n|j|}Wn&t k rtd||fYqX| |q| t j | d| iq,dS)NrKrZduplicates_indexzDOmitting unique constraint key for (%s), key covers omitted columns.rzDunique constraint key '%s' was not located in columns for table '%s')rjNotImplementedErrorrrSrr rrrrrrrZUniqueConstraint)rrIr rsrrrru constraintsconst_drrrZconstrained_colsrZconstrained_colrrrr}/sD     z%Inspector._reflect_unique_constraintsc CsHz|||}Wntk r&YdSX|D]} |tjf| q,dSr)rlrrrZCheckConstraint) rrIr rsrrrrurrrrrr~bs z$Inspector._reflect_check_constraintscCs:z|||}Wntk r&YdSX|dd|_dS)Nr)rkrrr)rrIr rsruZ comment_dictrrrrus z Inspector._reflect_table_comment)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)rTN)>r __module__ __qualname____doc__r deprecatedr* classmethodr1r'r3r2r9rZ _inspectsrr;rr<rr= contextlibcontextmanagerr@rBpropertyrCrDrFrHrJr]r^r_r`rbrcrdrfrhrTrirjrkrlZ deprecated_20rormryrrzr{r Zasc_opZdesc_opZnulls_first_opZ nulls_last_oprr|r}r~rrrrrr&<s            7  A  $ )   "   B G:3r&)rrbaserrrrrr r r r rZ sql.type_apir r decoratorr%Z_self_inspectsobjectr&rrrrs