6M]HJdZddlmZddlmZddlmZddlmZddlmZddlmZdd lm Z dd l m Z dd lm Z dd lmZejd ZGddeZdS)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.. ) Connectable)exc) inspection)sql)util) operators)schema) TypeEngine) deprecated) topologicalc6|dd}| |||g|Ri|S|jtd|Dtd|Df}||}||||g|Ri|}|||<|S)N info_cachec3NK|] }t|tj|V!dSN) isinstancer string_types).0as /srv/buildsys-work-dir/castor/build_node/builder-2/WGSG1/unpkd_srcs/cloudlinux-venv-1.0.6/venv/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py zcache../s4 B BAAt/@!A!A Ba B B B B B Bc30K|]\}}|dk ||fVdS)rN)rkvs rrzcache..0s4 A AAqL/@/@q!f/@/@/@/@ A Ar)get__name__tupleitems)fnselfconargskwrkeyrets rcacher((s d++Jr$)d)))b)))  B B B B BBB A A A A AAA C ..  C {bs(T(((R(( 3 JrceZdZdZdZedZeje dZ e dZ dZ ejdd*d Zd+d Zd Zd Zd+dZd+dZd+dZd+dZeddd+dZd+dZd+dZd+dZd+dZd+dZd+dZ d,dZ dZ!dZ"d Z#d!Z$d"e%j&fd#e%j'fd$e%j(fd%e%j)fgZ*d&Z+d'Z,d(Z-d)Z.d S)- InspectoraPerforms 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:: from sqlalchemy import inspect, create_engine engine = create_engine('...') insp = inspect(engine) The inspection method above is equivalent to using the :meth:`_reflection.Inspector.from_engine` method, i.e.:: engine = create_engine('...') insp = Inspector.from_engine(engine) Where above, the :class:`~sqlalchemy.engine.interfaces.Dialect` may opt to return an :class:`_reflection.Inspector` subclass that provides additional methods specific to the dialect's target database. c||_t|dr |j|_n||_|j|ur&||jj|_i|_dS)aInitialize 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` engineN)bindhasattrr,connectclosedialectr)r"r-s r__init__zInspector.__init__Uso  4 " " +DKKDK ;$   LLNN " " "{* rc~t|jdr|j|St|S)aConstruct 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`. inspector)r.r1r4r*)clsr-s r from_enginezInspector.from_engineqs:( 4< - - 0<))$// /rc6t|Sr)r*r6)r-s r_inspzInspector._insps$$T***rc|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. )r1default_schema_namer"s rr:zInspector.default_schema_names|//rc|t|jdr&|j|j|jSgS)zReturn all schema names.get_schema_namesr)r.r1r=r-rr;s rr=zInspector.get_schema_namessH 4 foreign_keyreferred_table) r.r1rAr-rr, table_namesget_foreign_keysappendlistr sort)r"r r?tnamestuplestnamefkeys rrAzInspector.get_table_namessN 4cg|] }|d S)namer)rfks r z.s+G+G+G2BvJ+G+G+GrrCc3,K|]}d|fVdS)rNr)rfkcedges rrz;Inspector.get_sorted_table_and_fkc_names..s<&&'*T!WcN&&&&&&rrNcJg|]}||f Sr) difference)rrKfknames_for_tableremaining_fkcss rrQz. sB   %e,77GG H   r)r.r1rAr-rr,rDsetrEaddrGr rHrCircularDependencyErroredgesremoveupdate) r"r rIrJrKfkeysrLcandidate_sorterrrTrWrXs @@@rget_sorted_table_and_fkc_namesz(Inspector.get_sorted_table_and_fkc_namess)0 4??? @ D!+"266"B"BCCNN* D D D    d###%%&&&&.?Q.H&&&"+"266"B"BCCNNNNNN D     '   D(( ) *+ +s"DF A.FF cN|j|j|jS)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 r>)r1get_temp_table_namesr-rr;s rrdzInspector.get_temp_table_namess,|00 I$/1   rcN|j|j|jS)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 r>)r1get_temp_view_namesr-rr;s rrfzInspector.get_temp_view_namess,|// I$/0   rc rt|jdr!|jj|j||fd|ji|SiS)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.r1rhr-rr" table_namer r%s rrhzInspector.get_table_options)sY 4/MO  rcP|j|j||jS)zReturn all view names in `schema`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. r>)r1get_view_namesr-r)r"r s rrlzInspector.get_view_names=s.|** Iv$/+   rcR|j|j|||jS)zReturn definition for `view_name`. :param schema: Optional, retrieve names from a non-default schema. For special quoting, use :class:`.quoted_name`. r>)r1get_view_definitionr-r)r" view_namer s rrnzInspector.get_view_definitionIs0|// Iy&T_0   rc |jj|j||fd|ji|}|D],}|d}t |t s ||d<-|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. * ``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)r1 get_columnsr-rrr )r"rjr r%col_defscol_defcoltypes rrrzInspector.get_columnsUs|b,4<+ Iz6  6:o IK    , ,GfoGgz22 ,")'))rz0.7zThe :meth:`_reflection.Inspector.get_primary_keys` method is deprecated and will be removed in a future release. Please refer to the :meth:`_reflection.Inspector.get_pk_constraint` method.c P|jj|j||fd|ji|dS)zReturn information about primary keys in `table_name`. Given a string `table_name`, and an optional string `schema`, return primary key information as a list of column names. rconstrained_columnsr1get_pk_constraintr-rris rget_primary_keyszInspector.get_primary_keyssE.t|- Iz6  6:o IK  ! !rc D|jj|j||fd|ji|S)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`. rrxris rryzInspector.get_pk_constraints=(.t|- Iz6  6:o IK   rc D|jj|j||fd|ji|S)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`. r)r1rEr-rris rrEzInspector.get_foreign_keyss=>-t|, Iz6  6:o IK   rc D|jj|j||fd|ji|S)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``, ``nullsfirst``, ``nullslast``. .. 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)r1 get_indexesr-rris rr~zInspector.get_indexess>H(t|' Iz6  6:o IK   rc D|jj|j||fd|ji|S)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`. r)r1get_unique_constraintsr-rris rrz Inspector.get_unique_constraintss=*3t|2 Iz6  6:o IK   rc D|jj|j||fd|ji|S)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 r)r1get_table_commentr-rris rrzInspector.get_table_comments= .t|- Iz6  6:o IK   rc D|jj|j||fd|ji|S)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 r)r1get_check_constraintsr-rris rrzInspector.get_check_constraints2s=:2t|1 Iz6  6:o IK   rrTc ||vrdS||jj}|j}j}t fd|jD} |j||fij} | r | tj r^t|tr||j}t|tr||j}d} i} |j||fijD]} d} || ||| | st%jj|||| ||||| |||| |||| ||| |||| ||| |||| ||| |||| dS)aGiven 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.engine.reflection import Inspector engine = create_engine('...') meta = MetaData() user_table = Table('user', meta) insp = Inspector.from_engine(engine) insp.reflecttable(user_table, None) :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. Nc3bK|])}|jv |j|fV*dSr)dialect_kwargsr)rrtables rrz)Inspector.reflecttable..sS" " E((($((++ ,((((" " rFT)rZr-r1schema_for_objectrOdictreflection_optionsrhr_validate_dialect_kwargsrpy2krstrdecodeencodingrr_reflect_columnrNoSuchTableError _reflect_pk _reflect_fk_reflect_indexes_reflect_unique_constraints_reflect_check_constraints_reflect_table_comment)r"rinclude_columnsexclude_columns resolve_fks _extend_onr1r rjrtbl_opts found_tablecols_by_orig_namecol_ds ` r reflecttablezInspector.reflecttableSs:  ! ""u%%%)#,,U33Z "" " " " /" " "   *4)   "'"6    5  * *8 4 4 4 9 A&#&& 9w'788*c** A'..w/?@@  %T%   "'"6   EK  !      3&uz22 2  '8/                         ((           ''           ## '9     rcd}|j||d}|r||vs|r||vrdSd}tfddD} dvr| dg} dd} t | t jjrtj | d} nHt | tj s.tj t j dd} | | d vr,tjd id } | | d vr|| tj||g| Ri| x||<} | j|jvrd| _|| dS) NrOrqc34K|]}|v||fVdSrr)rrrs rrz,Inspector._reflect_column..sA   EzzaMzzz   r)nullable autoincrementquoteinfor&commentdialect_optionsdefaultT) _reflectedcomputedsequencer)dispatchcolumn_reflectrr^rrrelements TextClause sa_schema DefaultClause FetchedValuetextrFComputed_reflect_col_sequenceColumnr& primary_key append_column)r"rrrrr orig_namerOrucol_kwcolargsrrcols ` rrzInspector._reflect_columns/&M  %%dE5999V}  O ; ; !< $ 7 7 F-             % % MM% 12 3 3 3 99Y   +I&G'3<#:;; #1'dKKK)?@@ #1HU9-..4 NN7 # # #    )>>E*,=>>H NN8 $ $ $     & &ug 6 6 6-6-= '. #. . . '-. .  )$s 7e' ' '"CO C     rcd|vr]|d}tj|ddd}d|vr |d|_d|vr |d|_||dSdS)NrrOrstart increment)rSequencerrrF)r"rrseqrs rrzInspector._reflect_col_sequences{    #C )#f+q!<.(s>***r/H/H""%/H/H/HrrwrO)ryrrrrO_reload)r"rjr rrrpk_conspk_colss `` rrzInspector._reflect_pk!s)$(   "'"6    /!"78G&-[[%8%8E  "   % %g . . . . . / /rc |j||fi|j} | D]J} | d} fd| dD} |r#t| |rD| d} | d}| d}g}| U|r"t j||jfd| |j|d||D]-}|d | ||g.n]|r,t j||jfd|jtj |d ||D],}|d ||g-d | vr | d }ni}| t j | || fd di|LdS) NrOc6g|]}|vr |jn|Sr)r&)rcrs rrQz)Inspector._reflect_fk..GsB###-.1B,B,B!!$((###rrwreferred_schemarCreferred_columnsT)autoloadr autoload_withr.)rrr roptions link_to_name) rErrY intersectionrTablemetadatar-rFjoin BLANK_SCHEMAappend_constraintForeignKeyConstraint)r"rjr rrrrrrr_fkey_dconnamerwrrCrrefspeccolumnrs ` rrzInspector._reflect_fk5sa&%   "'"6  8 8 FVnG#### 56###  3':#;#;#H#H$$ $%67O#$45N%&89 G* O&"&.&*i#- -/FNN/>6!JKK  O&"&&*i(5#- -/GGFNN388^V,D#E#EFFFFF"" +  # #.'"&       a8 8 rascdesc nullsfirst nullslastc |||}|D]} | d} | d} | di} | d} | dd}| di}| d}|rQt| |s/t jd |d d | d |rg}| D]} ||vr||n |j|}n-#t$r t j|d |d|dYFwxYw| |d}|j D]\}}||vr ||}| |tj | g|Rd|itt|d| fgzdS)NrO column_namescolumn_sortinguniquerqindexrduplicates_constraintz Omitting z key for (, z), key covers omitted columns.z key '(' was not located in columns for table ''r_table)r~rrYissubsetrwarnrrKeyError_index_sort_exprsrFrIndexrrGr )r"rjr rrrrrindexesindex_drOcolumnsrrflavorr duplicatesidx_colsridx_col c_sortingrops rrzInspector._reflect_indexessa"":v66, , G6?Dn-G$[[)92>>NX&F[[11F%kk*;R@@O %<==J s7||'<'<_'M'M  vvtyy11113 H ) )  111*!,,"WQZG  I4:FFAAAzzzKH  +..q"55 !3..EArI~~"$"W++(((( O     tO11334468J7KKLL     O, , s(D'D,+D,c Z |||}n#t$rYdSwxYw|D]} | d} | d} | d} |rMt| |s+t jdd| zv| ryg} | D]\} ||vr||n |j|}| |2#t$rt jd|d|dYYwxYw| tj | d| idS) NrOrduplicates_indexzDOmitting unique constraint key for (%s), key covers omitted columns.rzunique constraint key 'rr)rNotImplementedErrorrrYrrrrrrFrrrUniqueConstraint)r"rjr rrrrr constraintsconst_drrrconstrained_colsrconstrained_cols rrz%Inspector._reflect_unique_constraintss 55j&IIKK"    FF #  GfoGn-G %788J s7||'<'<_'M'M  248IIg4F4FG  "  = = = 111*!,,"WQZ$%++O<<<<  II45AAzzzC  # #*,<K7KK    ;  s '',C%DDc |||}n#t$rYdSwxYw|D]&} |tjdi| 'dS)Nr)rrrrCheckConstraint) r"rjr rrrrrrrs rrz$Inspector._reflect_check_constraintss 44ZHHKK"    FF # J JG  # #I$=$H$H$H$H I I I I J Js  ''c |||}|dd|_dS#t$rYdSwxYw)Nr)rrrr)r"rjr rr comment_dicts rrz Inspector._reflect_table_comments] ;11*fEEL),,VT::EMMM#    FF s5 AA)NNr)rTN)/r __module__ __qualname____doc__r2 classmethodr6r _inspectsrr8propertyr:r=rdeprecated_paramsrArbrdrfrhrlrnrrr rzryrEr~rrrrrrrrr asc_opdesc_op nullsfirst_op nullslast_oprrrrrrrrr*r*9s68[.Z+&&++'&+00X0T    )))  )V6+6+6+6+p        (          9999vZ  B ! ! ! !    0! ! ! ! F& & & & P    2    (    J D D D D L;!;!;!z % % %///(FFFR   ! "# y./ i,- 888t111fJJJ&;;;;;rr*N)r baserrrrrr r r sql.type_apir r r decoratorr(objectr*rrrrs (%%%%%%%%%%%%    S;S;S;S;S;S;S;S;S;S;r