ó
H`¾Tc           @   sr   d  Z  d d l m Z d d l m Z d d l m Z d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d	 S(
   s  Horizontal sharding support.

Defines a rudimental 'horizontal sharding' system which allows a Session to
distribute queries and persistence operations across multiple databases.

For a usage example, see the :ref:`examples_sharding` example included in
the source distribution.

i   (   t   util(   t   Session(   t   Queryt   ShardedSessiont   ShardedQueryc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         O   sD   t  t |  ƒ j | | Ž  |  j j |  _ |  j j |  _ d  |  _ d  S(   N(   t   superR   t   __init__t   sessiont
   id_choosert   query_choosert   Nonet	   _shard_id(   t   selft   argst   kwargs(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR      s    c         C   s   |  j  ƒ  } | | _ | S(   s¹   return a new query, limited to a single shard ID.

        all subsequent operations with the returned query will
        be against the single shard regardless of other state.
        (   t   _cloneR   (   R   t   shard_idt   q(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt	   set_shard    s    	c            so   ‡  ‡ f d †  } ˆ  j  d  k	 r. | ˆ  j  ƒ Sg  } x* ˆ  j ˆ  ƒ D] } | j | | ƒ ƒ qD Wt | ƒ Sd  S(   Nc            sM   |  ˆ j  d <ˆ  j d ˆ  j ƒ  d |  ƒ j ˆ j ˆ  j ƒ } ˆ  j | ˆ ƒ S(   NR   t   mapper(   t
   attributest   _connection_from_sessiont   _mapper_zerot   executet	   statementt   _paramst	   instances(   R   t   result(   R   t   context(    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt   iter_for_shard,   s    		(   R   R
   R	   t   extendt   iter(   R   R   R   t   partialR   (    (   R   R   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt   _execute_and_instances+   s    	c         K   s„   |  j  d  k	 r% t t |  ƒ j | ƒ St j | ƒ } xI |  j |  | ƒ D]1 } |  j | ƒ j | |  } | d  k	 rG | SqG Wd  Sd  S(   N(	   R   R
   R   R   t   getR    t   to_listR   R   (   R   t   identR   R   t   o(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR"   @   s    (   t   __name__t
   __module__R   R   R!   R"   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR      s   			c           B   sD   e  Z d e d  „ Z d d d d „ Z d d d d „ Z d „  Z RS(   c         K   s„   t  t |  ƒ j d | |  | |  _ | |  _ | |  _ i  |  _ |  j |  _ | d k	 r€ x% | D] } |  j
 | | | ƒ q_ Wn  d S(   s  Construct a ShardedSession.

        :param shard_chooser: A callable which, passed a Mapper, a mapped
          instance, and possibly a SQL clause, returns a shard ID.  This id
          may be based off of the attributes present within the object, or on
          some round-robin scheme. If the scheme is based on a selection, it
          should set whatever state on the instance to mark it in the future as
          participating in that shard.

        :param id_chooser: A callable, passed a query and a tuple of identity
          values, which should return a list of shard ids where the ID might
          reside.  The databases will be queried in the order of this listing.

        :param query_chooser: For a given Query, returns the list of shard_ids
          where the query should be issued.  Results from all shards returned
          will be combined together into a single listing.

        :param shards: A dictionary of string shard names
          to :class:`~sqlalchemy.engine.Engine` objects.

        t	   query_clsN(   R   R   R   t   shard_chooserR   R	   t   _ShardedSession__bindst
   connectiont   connection_callableR
   t
   bind_shard(   R   R)   R   R	   t   shardsR(   R   t   k(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR   N   s    				c         K   sl   | d  k r! |  j | | ƒ } n  |  j d  k	 rF |  j j | d | ƒS|  j | d | d | ƒj |   Sd  S(   NR   t   instance(   R
   R)   t   transactionR+   t   get_bindt   contextual_connect(   R   R   R0   R   R   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR+   o   s    	c         K   s2   | d  k r' |  j | | d | ƒ} n  |  j | S(   Nt   clause(   R
   R)   R*   (   R   R   R   R0   R4   t   kw(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR2   |   s    c         C   s   | |  j  | <d  S(   N(   R*   (   R   R   t   bind(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR-   ‚   s    N(   R&   R'   R
   R   R   R+   R2   R-   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR   M   s    N(
   t   __doc__t    R    t   orm.sessionR   t	   orm.queryR   t   __all__R   R   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt   <module>   s   4