Files
Home-AssistantConfig/deps/sqlalchemy/engine/__pycache__/reflection.cpython-34.pyc

388 lines
24 KiB
Plaintext
Raw Normal View History

2016-10-11 16:42:06 +00:00
<EFBFBD>
}<7D><>W<EFBFBD>o<00>@s<>dZddlmZmZddlmZddlmZddlmZddlm Z ddlm
Z
ddlm Z d d
l m Z ejd d <00><00>ZGd d<00>de<00>ZdS)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..
<EFBFBD>)<02>exc<78>sql)<01>schema)<01>util)<01>
TypeEngine)<01>
deprecated)<01> topological)<01>
inspection<EFBFBD>)<01> ConnectablecOs<>|jdd<00>}|dkr1|||||<00>S|jtdd<00>|D<><00>tdd<00>|j<00>D<><00>f}|j|<00>}|dkr<>|||||<00>}|||<n|S)N<>
info_cachecss'|]}t|tj<00>r|VqdS)N)<03>
isinstancer<00> string_types)<02>.0<EFBFBD>a<>r<00>B/tmp/pip-build-zkr322cu/sqlalchemy/sqlalchemy/engine/reflection.py<70> <genexpr>-szcache.<locals>.<genexpr>cssA|]7\}}t|tjtjtf<17>r||fVqdS)N)r rrZ int_types<65>float)r<00>k<>vrrrr.s )<04>get<65>__name__<5F>tuple<6C>items)<08>fn<66>self<6C>con<6F>args<67>kwr <00>key<65>retrrr<00>cache&s "  r"c@s<>eZdZdZdd<00>Zedd<00><00>Zeje <00>dd<00><00>Z
e dd <00><00>Z d
d <00>Z d d d d<00>Zd dd<00>Zdd<00>Zdd<00>Zd dd<00>Zd dd<00>Zd dd<00>Zd dd<00>Zedd<00>d dd <00><00>Zd d!d"<00>Zd d#d$<00>Zd d%d&<00>Zd d'd(<00>Zfd)d*<00>Zd+d,<00>Zd-d.<00>Zd/d0<00>Zd1d2<00>Z d3d4<00>Z!d5d6<00>Z"d S)7<> InspectoramPerforms 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:`.Inspector` object is usually created via the
:func:`.inspect` function::
from sqlalchemy import inspect, create_engine
engine = create_engine('...')
insp = inspect(engine)
The inspection method above is equivalent to using the
:meth:`.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:`.Inspector` subclass that provides additional
methods specific to the dialect's target database.
cCsn||_t|d<00>r'|j|_n ||_|j|krR|j<00>j<00>n|jj|_i|_dS)ajInitialize a new :class:`.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:`.Inspector`, see
:meth:`.Inspector.from_engine`
<20>engineN)<07>bind<6E>hasattrr$<00>connect<63>close<73>dialectr )rr%rrr<00>__init__Vs  zInspector.__init__cCs,t|jd<00>r"|jj|<00>St|<00>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:`.Inspector` in that the
:class:`~sqlalchemy.engine.interfaces.Dialect` is given a chance to
provide a dialect-specific :class:`.Inspector` instance, which may
provide additional methods.
See the example at :class:`.Inspector`.
<20> inspector)r&r)r+r#)<02>clsr%rrr<00> from_enginerszInspector.from_enginecCs tj|<00>S)N)r#r-)r%rrr<00>_insp<73>szInspector._inspcCs
|jjS)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)<00>default_schema_name)rrrrr/<00>s zInspector.default_schema_namecCs2t|jd<00>r.|jj|jd|j<00>SgS)z!Return all schema names.
<20>get_schema_namesr )r&r)r0r%r )rrrrr0<00>s
zInspector.get_schema_namesNcCs<>t|jd<00>r6|jj|j|d|j<00>}n|jj|<00>}|dkr<>g}xU|D]M}xD|j||<00>D]0}||dkrz|j|d|f<00>qzqzWqaWt t
j ||<00><00>}n|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:`.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`.
:param order_by: Optional, may be the string "foreign_key" to sort
the result on foreign key dependencies. Does not automatically
resolve cycles, and will raise :class:`.CircularDependencyError`
if cycles exist.
.. deprecated:: 1.0.0 - see
:meth:`.Inspector.get_sorted_table_and_fkc_names` for a version
of this which resolves foreign key cycles between tables
automatically.
.. versionchanged:: 0.8 the "foreign_key" sorting sorts tables
in order of dependee to dependent; that is, in creation
order, rather than in drop order. This is to maintain
consistency with similar features such as
:attr:`.MetaData.sorted_tables` and :func:`.util.sort_tables`.
.. seealso::
:meth:`.Inspector.get_sorted_table_and_fkc_names`
:attr:`.MetaData.sorted_tables`
<20>get_table_namesr Z foreign_key<65>referred_table) r&r)r1r%r r$<00> table_names<65>get_foreign_keys<79>append<6E>listr<00>sort)rrZorder_by<62>tnames<65>tuples<65>tname<6D>fkeyrrrr1<00>s&   "zInspector.get_table_namesc s<>t|jd<00>r6|jj|j|d|j<00>}n|jj|<00>}t<00>}t<00><00>i<00>xx|D]p}|j||<00>}tdd<00>|D<><00><00>|<x8|D]0}||dkr<>|j |d|f<00>q<>q<>WqgWyt
t j ||<00><00>}Wn<57>t jk
r<>}zfxF|jD];<00>|j<00><00><01>j<00>fdd<00><00><00>dD<><00>qWt
t j ||<00><00>}WYd d }~XnX<><00>fd
d<00>|D<>d t
<00><00>fgS) axReturn 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:`.Inspector.get_table_names`
:func:`.sort_tables_and_constraints` - similar method which works
with an already-given :class:`.MetaData`.
r1r cSsg|]}|d<19>qS)<01>namer)rZfkrrr<00>
<listcomp><3E>s z<Inspector.get_sorted_table_and_fkc_names.<locals>.<listcomp>r2c3s|]}<00>d|fVqdS)r
Nr)rZfkc)<01>edgerrrsz;Inspector.get_sorted_table_and_fkc_names.<locals>.<genexpr>r
Ncs)g|]}|<00>|j<00><00>f<00>qSr)<01>
difference)rr:)<02>fknames_for_table<6C>remaining_fkcsrrr= s )r&r)r1r%r r$r3<00>setr4<00>addr6rr7rZCircularDependencyError<6F>edges<65>remove<76>update) rrr8r9r:<00>fkeysr;Zcandidate_sort<72>errr)r>r@rAr<00>get_sorted_table_and_fkc_names<65>s4     " +z(Inspector.get_sorted_table_and_fkc_namescCs|jj|jd|j<00>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
r )r)<00>get_temp_table_namesr%r )rrrrrJs zInspector.get_temp_table_namescCs|jj|jd|j<00>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
r )r)<00>get_temp_view_namesr%r )rrrrrKs zInspector.get_temp_view_namescKs;t|jd<00>r7|jj|j||d|j|<00>SiS)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`.
<20>get_table_optionsr )r&r)rLr%r )r<00>
table_namerrrrrrL's
  zInspector.get_table_optionscCs|jj|j|d|j<00>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`.
r )r)<00>get_view_namesr%r )rrrrrrN;szInspector.get_view_namescCs"|jj|j||d|j<00>S)z<>Return definition for `view_name`.
:param schema: Optional, retrieve names from a non-default schema.
For special quoting, use :class:`.quoted_name`.
r )r)<00>get_view_definitionr%r )rZ view_namerrrrrOFs zInspector.get_view_definitioncKse|jj|j||d|j|<00>}x7|D]/}|d}t|t<00>s.|<00>|d<q.q.W|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
:class:`~sqlalchemy.types.TypeEngine`
nullable
boolean
default
the column's default value
attrs
dict containing optional column attributes
: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 <00>type)r)<00> get_columnsr%r r r)rrMrrZcol_defsZcol_def<65>coltyperrrrQQs  
zInspector.get_columnsz0.7zKCall to deprecated method get_primary_keys. Use get_pk_constraint instead.cKs)|jj|j||d|j|<00>dS)z<>Return 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.
r <00>constrained_columns)r)<00>get_pk_constraintr%r )rrMrrrrr<00>get_primary_keysys zInspector.get_primary_keyscKs%|jj|j||d|j|<00>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 )r)rTr%r )rrMrrrrrrT<00>szInspector.get_pk_constraintcKs%|jj|j||d|j|<00>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 )r)r4r%r )rrMrrrrrr4<00>szInspector.get_foreign_keyscKs%|jj|j||d|j|<00>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
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 )r)<00> get_indexesr%r )rrMrrrrrrV<00>szInspector.get_indexescKs%|jj|j||d|j|<00>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`.
.. versionadded:: 0.8.4
r )r)<00>get_unique_constraintsr%r )rrMrrrrrrW<00>s z Inspector.get_unique_constraintsc s<>|jj}<00>j}<00>j}t<00>fdd<00>|jD<><00>}|j||<00>j<00>}|rn<00>j|<00>nt j
r<>t |t <00>r<>|j |j<00>}nt |t <00>r<>|j |j<00>}q<>nd} i}
x?|j||<00>j<00>D]%} d} |j<00>| |||
<00>q<>W| s+tj<00>j<00><00>n|j||<00>|
|<00>|j||<00>|
||<00>|j||<00>|
|||<00>|j||<00>|
|||<00>dS)aGiven a 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 import reflection
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.
c3s6|],}|<00>jkr|<00>jj|<00>fVqdS)N)<02>dialect_kwargsr)rr)<01>tablerrrsz)Inspector.reflecttable.<locals>.<genexpr>FTN)r%r)rr<<00>dict<63>reflection_optionsrLrXZ_validate_dialect_kwargsrZpy2kr <00>str<74>decode<64>encodingrQ<00>_reflect_columnrZNoSuchTableError<6F> _reflect_pk<70> _reflect_fk<66>_reflect_indexes<65>_reflect_unique_constraints) rrY<00>include_columns<6E>exclude_columnsr)rrMr[Ztbl_optsZ found_table<6C>cols_by_orig_name<6D>col_dr)rYr<00> reflecttable<6C>sH       
   zInspector.reflecttablec s?<00>d}|jj||<00><00><01>d}|r<||ksN|rR||krRdS<>d}t<00>fdd<00>ddddd gD<><00>} g}
<00>jd
<00>dk r<>|
jtjtj<00>d
<19>d d <00><01>nd <00>kr<>|j <00>|
<00>ntj
|||
| <00>||<} | j |j kr.d | _ n|j | <00>dS)Nr<rPc3s+|]!}|<00>kr|<00>|fVqdS)Nr)rr)rgrrr^sz,Inspector._reflect_column.<locals>.<genexpr>ZnullableZ autoincrement<6E>quote<74>infor <00>defaultZ
_reflectedT<EFBFBD>sequence)<0E>dispatchZcolumn_reflectrZrr5<00> sa_schemaZ DefaultClauser<00>text<78>_reflect_col_sequenceZColumnr <00> primary_keyZ append_column) rrYrgrdrerfZ orig_namer<rRZcol_kw<6B>colargs<67>colr)rgrr_Ls*


    zInspector._reflect_columncCs{d|krw|d}tj|ddd<00>}d|krK|d|_nd|krg|d|_n|j|<00>ndS)Nrlr<r
<00>start<72> increment)rn<00>Sequencertrur5)rrgrr<00>seqrlrrrrpxs 
  zInspector._reflect_col_sequencecsj|j|||j<00>}|rf<00><00>fdd<00>|dD<>}|jd<00>|j_|jj|<00>ndS)Ncs2g|](}|<00>kr|<00>kr<00>|<19>qSrr)r<00>pk)rfrerrr=<00>s z)Inspector._reflect_pk.<locals>.<listcomp>rSr<)rTrXrrqr<Z_reload)rrMrrYrfreZpk_consZpk_colsr)rfrerr`<00>szInspector._reflect_pkc s<>|j|||j<00>}x<>|D]{}|d} <00>fdd<00>|dD<>}
|rmt|
<00>j|<00>rmqn|d} |d} |d} g}| dk r<>tj| |jdd d
| d |j|<00>x<>| D]%}|jd j | | |g<00><00>q<>Wn[tj| |jdd d |jd
tj
|<00>x*| D]"}|jd j | |g<00><00>q0Wd |kro|d }ni}|j tj |
|| dd |<00><01>qWdS)Nr<cs/g|]%}|<00>kr%<00>|jn|<00>qSr)r )r<00>c)rfrrr=<00>s z)Inspector._reflect_fk.<locals>.<listcomp>rS<00>referred_schemar2<00>referred_columnsZautoloadTrZ autoload_with<74>.<2E>optionsZ link_to_name) r4rXrB<00> intersectionrnZTable<6C>metadatar%r5<00>joinZ BLANK_SCHEMA<4D>append_constraintZForeignKeyConstraint)rrMrrYrfrer[rGZfkey_d<5F>connamerSrzr2r{Zrefspec<65>columnr}r)rfrra<00>sD 
 


           zInspector._reflect_fkc Csr|j||<00>}xY|D]Q} | d}
| d} | d} | jdd<00>} | jdi<00>}| jd<00>}|r<>t| <00>j|<00> r<>tjd| d j| <00>f<16>qn|r<>qng}xq| D]i}y'||kr<>||n
|j|}Wn,tk
r&tjd
| ||f<16>Yq<59>X|j |<00>q<>Wt
j |
|t t |j<00><00>d| fg<17><00>qWdS) Nr<<00> column_names<65>uniquerP<00>index<65>dialect_optionsZduplicates_constraintz5Omitting %s key for (%s), key covers omitted columns.z, z5%s key '%s' was not located in columns for table '%s')rVrrB<00>issubsetr<00>warnr<6E>ry<00>KeyErrorr5rn<00>IndexrZr6r)rrMrrYrfrdrer[<00>indexesZindex_dr<<00>columnsr<73>Zflavorr<72><00>
duplicatesZidx_colsryZidx_colrrrrb<00>s8 


 ' zInspector._reflect_indexescCsBy|j||<00>}Wntk
r.dSYnXx |D]} | d}
| d} | jd<00>} |r<>t| <00>j|<00> r<>tjddj| <00><16>q6n| r<>q6ng} xn| D]f}y'||kr<>||n
|j|}Wn)t k
r tjd||f<16>Yq<59>X| j
|<00>q<>W|j t j d|
| <00><01>q6WdS)Nr<r<>Zduplicates_indexzDOmitting unique constraint key for (%s), key covers omitted columns.z, zDunique constraint key '%s' was not located in columns for table '%s')rW<00>NotImplementedErrorrrBr<>rr<>r<>ryr<>r5r<>rnZUniqueConstraint)rrMrrYrfrdrer[<00> constraintsZconst_dr<64>r<>r<>Zconstrained_colsryZconstrained_colrrrrc<00>s6   

 ' z%Inspector._reflect_unique_constraints)#r<00>
__module__<EFBFBD> __qualname__<5F>__doc__r*<00> classmethodr-r Z _inspectsr r.<00>propertyr/r0r1rIrJrKrLrNrOrQrrUrTr4rVrWrhr_rpr`rarbrcrrrrr#;s6   4:   (  #"O ,  / *r#N)r<><00>rrrrnrZ sql.type_apirrrr <00>baser Z decoratorr"<00>objectr#rrrr<00><module>s