
    -h"                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SS	/r
 " S
 S	\5      r " S S\	5      rS rg)a  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.

   )event)exc)inspect)util)Query)SessionShardedSessionShardedQueryc                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )r
      c                    > [         [        U ]
  " U0 UD6  U R                  R                  U l        U R                  R
                  U l        U R                  R                  U l        S U l        g N)superr
   __init__session
id_chooserquery_chooserexecute_chooser	_shard_id)selfargskwargs	__class__s      tC:\Users\ROHAN GUPTA\OneDrive\Desktop\mathbuddy-assessment\venv\Lib\site-packages\sqlalchemy/ext/horizontal_shard.pyr   ShardedQuery.__init__   sS    lD*D;F;,,11!\\77#||;;    c                      U R                  US9$ )a{  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.

The shard_id can be passed for a 2.0 style execution to the
bind_arguments dictionary of :meth:`.Session.execute`::

    results = session.execute(
        stmt,
        bind_arguments={"shard_id": "my_shard"}
    )

)_sa_shard_id)execution_options)r   shard_ids     r   	set_shardShardedQuery.set_shard$   s     %%8%<<r   )r   r   r   r   )__name__
__module____qualname____firstlineno__r   r!   __static_attributes____classcell__r   s   @r   r
   r
      s    = =r   c                   j   ^  \ rS rSrSS\4U 4S jjr  S
U 4S jjrS r SS jr SS jr	S r
S	rU =r$ )r	   6   Nc                   >^ UR                  SS5      m[        [        U ]  " SSU0UD6  [        R
                  " U S[        SS9  Xl        X l        T(       aA  [        R                  " SS5        U(       a  [        R                  " S	5      eU4S
 jnX0l        OX0l        TU l        0 U l        Ub  U H  nU R!                  XtU   5        M     gg)a  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 execute_chooser: For a given :class:`.ORMExecuteState`,
  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.

  .. versionchanged:: 1.4  The ``execute_chooser`` parameter
     supersedes the ``query_chooser`` parameter.

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

r   N	query_clsdo_orm_executeT)retvalzMThe ``query_choser`` parameter is deprecated; please use ``execute_chooser``.z1.4z>Can't pass query_chooser and execute_chooser at the same time.c                 (   > T" U R                   5      $ r   )	statement)orm_contextr   s    r   r   0ShardedSession.__init__.<locals>.execute_choosern   s    $[%:%:;;r    )popr   r	   r   r   listenexecute_and_instancesshard_chooserr   r   warn_deprecatedr   ArgumentErrorr   r   _ShardedSession__binds
bind_shard)
r   r8   r   r   shardsr-   r   kr   r   s
           @r   r   ShardedSession.__init__7   s    D 

?D9nd,KyKFK"$9$	
 +$  2
 ''( 
< $3 #2 *!9-  r   c                    > Ub  [         [        U ]
  " UU4SU0UD6$ U R                  U5      nU(       a  UR	                  U5      nU R                  Xb5       H#  n[         [        U ]
  " UU4UUS.UD6nUc  M!  Us  $    g)a7  override the default :meth:`.Session._identity_lookup` method so
that we search for a given non-token primary key identity across all
possible identity tokens (e.g. shard ids).

.. versionchanged:: 1.4  Moved :meth:`.Session._identity_lookup` from
   the :class:`_query.Query` object to the :class:`.Session`.

Nidentity_token)rA   lazy_loaded_from)r   r	   _identity_lookupquery_set_lazyload_fromr   )
r   mapperprimary_key_identityrA   rB   kwqr    objr   s
            r   rC   ShardedSession._identity_lookupz   s    " %?$  . 	  

6"A(()9: OOADNDB( $,%5	
  ?J E r   c                     UbO  [        U5      nUR                  (       a  UR                  S   nUc   eU$ UR                  (       a  UR                  $ U R                  " X40 UD6nUb  UWl        U$ )Nr   )r   keyrA   r8   )r   rF   instancerH   statetokenr    s          r   _choose_shard_and_assign'ShardedSession._choose_shard_and_assign   st    H%Eyy		!(((%%+++%%f="=#+E r   c                     Uc  U R                  X5      nU R                  5       (       a  U R                  5       R                  XS9$ U R	                  XUS9R
                  " S0 UD6$ )zQProvide a :class:`_engine.Connection` to use in the unit of work
flush process.

)r    )r    rN   r4   )rQ   in_transactionget_transaction
connectionget_bindconnect)r   rF   rN   r    r   s        r   connection_callable"ShardedSession.connection_callable   sx     44VFH  '')44V4OO==H ! g     r   c                 F    Uc  U R                  XUS9nU R                  U   $ )N)clause)rQ   r;   )r   rF   r    rN   r\   rH   s         r   rW   ShardedSession.get_bind   s5     44 5 H ||H%%r   c                      X R                   U'   g r   )r;   )r   r    binds      r   r<   ShardedSession.bind_shard   s    !%Xr   )__bindsr   r   r   r8   )NN)NNN)NNNN)r#   r$   r%   r&   r
   r   rC   rQ   rY   rW   r<   r'   r(   r)   s   @r   r	   r	   6   sH    
 A.N 'R  48 & AE&& &r   c                 H  ^  T R                   (       a  T R                  =pS nO7T R                  (       d  T R                  (       a  S nT R                  =p2OS =n=p2T R
                  nU 4S jnU(       a  UR                  b  UR                  nOBST R                  ;   a  T R                  S   nO"ST R                  ;   a  T R                  S   nOS nUb	  U" XaU5      $ / nUR                  T 5       H  nU" XaU5      nUR                  U5        M     US   R                  " USS  6 $ )Nc                   > [        TR                  5      n[        TR                  5      nXS'   TR                  (       a  USU 0-  nXS'   O-TR                  (       d  TR
                  (       a  USU 0-  nX#S'   TR                  XCS9$ )Nr    _refresh_identity_token_sa_orm_load_options_sa_orm_update_options)bind_argumentsr   )dictlocal_execution_optionsrg   	is_select	is_update	is_deleteinvoke_statement)r    load_optionsupdate_optionsr   rg   r2   s        r   iter_for_shard-execute_and_instances.<locals>.iter_for_shard   s     !D!DEk889%-z"  6AAL8D45""k&;&;8(CCN:H67++) , 
 	
r   r   r           )rj   rn   rk   rl   update_delete_optionsr   rd   r   rg   r   appendmerge)	r2   rn   active_optionsro   r   rp   r    partialresult_s	   `        r   r7   r7      s    (3(@(@@			+"7"7*5*K*KK9===~!!G
" .@@L!99	;88	800@	{11	1--j9hnEE//<H$X^LGNN7# = qz--r   N)__doc__ r   r   r   r   	orm.queryr   orm.sessionr   __all__r
   r	   r7   r4   r   r   <module>r      sH         !^
,=5 =4X&W X&v/.r   