σ
H`ΎTc           @   sΈ   d  Z  d d l m Z m Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l m
 Z
 d d l m Z d	 d
 l m Z e j d    Z d e f d     YZ d S(   sγ  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..
i   (   t   exct   sql(   t   schema(   t   util(   t
   TypeEngine(   t
   deprecated(   t   topological(   t
   inspectioni   (   t   Connectablec         O   sͺ   | j  d d   } | d  k r1 |  | | | |  S|  j t d   | D  t d   | j   D  f } | j  |  } | d  k r¦ |  | | | |  } | | | <n  | S(   Nt
   info_cachec         s   s'   |  ] } t  | t j  r | Vq d  S(   N(   t
   isinstanceR   t   string_types(   t   .0t   a(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pys	   <genexpr>-   s    c         s   sA   |  ]7 \ } } t  | t j t j t f  r | | f Vq d  S(   N(   R
   R   R   t	   int_typest   float(   R   t   kt   v(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pys	   <genexpr>.   s    	(   t   gett   Nonet   __name__t   tuplet   items(   t   fnt   selft   cont   argst   kwR	   t   keyt   ret(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   cache&   s    t	   Inspectorc           B   sμ   e  Z d  Z d   Z e d    Z e j e  d    Z	 e
 d    Z d   Z d d d  Z d d  Z d d  Z d d	  Z d d
  Z e d d  d d   Z d d  Z d d  Z d d  Z d d  Z d d  Z RS(   sm  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:`.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.

    c         C   sn   | |  _  t | d  r' | j |  _ n	 | |  _ |  j | k rR | j   j   n  |  j j |  _ i  |  _ d S(   sj  Initialize 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`

        t   engineN(   t   bindt   hasattrR    t   connectt   closet   dialectR	   (   R   R!   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   __init__V   s    		c         C   s,   t  | j d  r" | j j |  St |  S(   s  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`.

        t	   inspector(   R"   R%   R'   R   (   t   clsR!   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   from_enginer   s    c         C   s   t  j |   S(   N(   R   R)   (   R!   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   _insp   s    c         C   s
   |  j  j S(   sΟ   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%   t   default_schema_name(   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR+      s    	c         C   s2   t  |  j d  r. |  j j |  j d |  j Sg  S(   s!   Return all schema names.
        t   get_schema_namesR	   (   R"   R%   R,   R!   R	   (   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR,      s    
c         C   sΡ   t  |  j d  r6 |  j j |  j | d |  j } n |  j j |  } | d k rΝ g  } xU | D]M } xD |  j | |  D]0 } | | d k rz | j | d | f  qz qz Wqa Wt	 t
 j | |   } n  | S(   sV  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.

         .. 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::

            :attr:`.MetaData.sorted_tables`

        t   get_table_namesR	   t   foreign_keyt   referred_table(   R"   R%   R-   R!   R	   R    t   table_namest   get_foreign_keyst   appendt   listR   t   sort(   R   R   t   order_byt   tnamest   tuplest   tnamet   fkey(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR-   ‘   s    	"c         K   s;   t  |  j d  r7 |  j j |  j | | d |  j | Si  S(   sΡ  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`.

        t   get_table_optionsR	   (   R"   R%   R:   R!   R	   (   R   t
   table_nameR   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR:   Μ   s
    	c         C   s   |  j  j |  j | d |  j S(   s±   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%   t   get_view_namesR!   R	   (   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR<   ΰ   s    c         C   s"   |  j  j |  j | | d |  j S(   s±   Return definition for `view_name`.

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

        R	   (   R%   t   get_view_definitionR!   R	   (   R   t	   view_nameR   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR=   λ   s    	c         K   se   |  j  j |  j | | d |  j | } x7 | D]/ } | d } t | t  s. |   | d <q. q. W| S(   sμ  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	   t   type(   R%   t   get_columnsR!   R	   R
   R   (   R   R;   R   R   t   col_defst   col_deft   coltype(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR@   φ   s    	
s   0.7sK   Call to deprecated method get_primary_keys.  Use get_pk_constraint instead.c         K   s)   |  j  j |  j | | d |  j | d S(   sΘ   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	   t   constrained_columns(   R%   t   get_pk_constraintR!   R	   (   R   R;   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   get_primary_keys  s    	c         K   s%   |  j  j |  j | | d |  j | S(   s  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%   RE   R!   R	   (   R   R;   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyRE   +  s    c         K   s%   |  j  j |  j | | d |  j | S(   s  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%   R1   R!   R	   (   R   R;   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR1   C  s    c         K   s%   |  j  j |  j | | d |  j | S(   sn  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

        :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%   t   get_indexesR!   R	   (   R   R;   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyRG   f  s    c         K   s%   |  j  j |  j | | d |  j | S(   s  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%   t   get_unique_constraintsR!   R	   (   R   R;   R   R   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyRH     s    	c   (         s  |  j  j }   j }   j } t   f d   | j D  } |  j | |   j  } | rk   j |  n  t	 j
 rΏ t | t  r | j | j  } n  t | t  rΏ | j | j  } qΏ n  t }	 i  }
 xΎ|  j | |   j  D]€ t }	  d }   j j |       d } | r2| | k r2qδ n  | rJ| | k rJqδ n   d } t  f d   d d d d d	 g D  } g  }  j d
  d  k	 rΖ| j t j t j  d
  d t  n  d  k r= d } t j | d d d  } d | k r| d | _ n  d | k r-| d | _ n  | j |  n  t j | | | |  |
 | <} | j   j  k r{t | _  n    j! |  qδ W|	 s§t" j#   j   n  |  j$ | |   j  } | r&g  | d D]( } | |
 k rΠ| | k rΠ|
 | ^ qΠ} | j d    j  _   j  j% |  n  |  j& | |   j  } x| D]} | d } g  | d D]% } | |
 k r|
 | j n | ^ q`} | r¬t' |  j( |  r¬qEn  | d } | d } | d } g  } | d  k	 r:t j) |   j* d t d | d |  j  | x | D]% } | j d j+ | | | g   qWnR t j) |   j* d t d |  j  | x* | D]" } | j d j+ | | g   qfWd | k r₯| d }  n i  }    j, t j- | | | d t |   qEW|  j. | |  }! x!|! D]}" |" d } |" d }# |" d }$ |" j d d  }% | rkt' |#  j/ |  rkt	 j0 d |% pQd d j+ |#  f  qνn  g  }& xv |# D]n } y' | |
 k r|
 | n
   j1 | }' Wn1 t2 k
 rΨt	 j0 d |% pΗd | | f  qxX|& j |'  qxWt j3 | |& t d |$   qνWd  S(!   s  Given 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.

        c         3   s6   |  ], } |   j  k r |   j  j |  f Vq d  S(   N(   t   dialect_kwargsR   (   R   R   (   t   table(    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pys	   <genexpr>»  s   t   nameR?   c         3   s+   |  ]! } |   k r |   | f Vq d  S(   N(    (   R   R   (   t   col_d(    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pys	   <genexpr>α  s   t   nullablet   autoincrementt   quotet   infoR   t   defaultt
   _reflectedt   sequencei   t   startt	   incrementRD   t   referred_schemaR/   t   referred_columnst   autoloadR   t   autoload_witht   .t   optionst   link_to_namet   column_namest   uniquet   indexs5   Omitting %s key for (%s), key covers omitted columns.s   , s5   %s key '%s' was not located in columns for table '%s'N(4   R!   R%   R   RK   t   dictt   reflection_optionsR:   RI   t   _validate_dialect_kwargsR   t   py2kR
   t   strt   decodet   encodingt   FalseR@   t   Truet   dispatcht   column_reflectR   R   R2   t	   sa_schemat   DefaultClauseR   t   textt   SequenceRT   RU   t   ColumnR   t   primary_keyt   append_columnR    t   NoSuchTableErrorRE   t   _reloadR1   t   sett   intersectiont   Tablet   metadatat   joint   append_constraintt   ForeignKeyConstraintRG   t   issubsett   warnt   ct   KeyErrort   Index((   R   RJ   t   include_columnst   exclude_columnsR%   R   R;   Ra   t   tbl_optst   found_tablet   cols_by_orig_namet	   orig_nameRK   RC   t   col_kwt   colargst   seqRS   t   colt   pk_const   pkt   pk_colst   fkeyst   fkey_dt   connameR}   RD   RV   R/   RW   t   refspect   columnR[   t   indexest   index_dt   columnsR^   t   flavort   idx_colst   idx_col(    (   RJ   RL   sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   reflecttable  sβ    				


 
 (
3	


 	


'N(    (   R   t
   __module__t   __doc__R&   t   classmethodR)   R   t	   _inspectsR   R*   t   propertyR+   R,   R   R-   R:   R<   R=   R@   R   RF   RE   R1   RG   RH   R   (    (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyR   ;   s$   			+(#N(   R   t    R    R   R   Rk   R   t   sql.type_apiR   R   R   R   t   baseR   t	   decoratorR   t   objectR   (    (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/engine/reflection.pyt   <module>   s   