
    ,h3A                       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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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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K!J#r#  \(       a`  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"KJ,r,  SS#K-J.r.  SS$K/J0r0  SS%KJ1r1  SS&KJ2r2  SS'KJ3r3  SS(K4J5r5  SS)K6J7r7  SS*K8J9r9  SS+K:J;r;  S,S-/r<\" S.\S/9r=\>r? " S0 S1\"5      r@ " S2 S3\"5      rA " S4 S-\\=   5      rB " S5 S,\ 5      rC " S6 S7\5      rD    S:S8 jrEg9);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.

.. deepalchemy:: The horizontal sharding extension is an advanced feature,
   involving a complex statement -> database interaction as well as
   use of semi-public APIs for non-trivial cases.   Simpler approaches to
   refering to multiple database "shards", most commonly using a distinct
   :class:`_orm.Session` per "shard", should always be considered first
   before using this more complex and less-production-tested system.



    )annotations)Any)Callable)Dict)Iterable)Optional)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )event)exc)inspect)util)PassiveFlag)OrmExecuteOptionsParameter)	ORMOption)Mapper)Query)_BindArguments)_PKIdentityArgument)Session)Protocol)Self)
Connection)Engine)OptionEngine)IteratorResult)Result)LoaderCallableStatus)_O)BulkUDCompileState)QueryContext)_EntityBindKey)_SessionBind)ORMExecuteState)InstanceState)
Executable)_TP)ClauseElementShardedSessionShardedQuery_T)boundc                  .    \ rS rSr        SS jrSrg)ShardChooserP   c                    g N )selfmapperinstanceclauses       W/var/www/auris/envauris/lib/python3.13/site-packages/sqlalchemy/ext/horizontal_shard.py__call__ShardChooser.__call__Q   s    
     r6   N)r8   Optional[Mapper[_T]]r9   r   r:   Optional[ClauseElement]returnr   __name__
__module____qualname____firstlineno__r<   __static_attributes__r6   r>   r;   r2   r2   P   s-    $  (	
 
r>   r2   c                  :    \ rS rSr              SS jrSrg)IdentityChooserY   c                   g r5   r6   )r7   r8   primary_keylazy_loaded_fromexecution_optionsbind_argumentskws          r;   r<   IdentityChooser.__call__Z   s     r>   r6   Nr8   z
Mapper[_T]rL   r   rM   Optional[InstanceState[Any]]rN   r   rO   r   rP   r   rA   r   rB   r6   r>   r;   rI   rI   Y   sK    		 )	
 7	 6	 '	 	 
	r>   rI   c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )r.   f   a  Query class used with :class:`.ShardedSession`.

.. legacy:: The :class:`.ShardedQuery` is a subclass of the legacy
   :class:`.Query` class.   The :class:`.ShardedSession` now supports
   2.0 style execution via the :meth:`.ShardedSession.execute` method.

c                   > [         TU ]  " U0 UD6  [        U R                  [        5      (       d   eU R                  R
                  U l        U R                  R                  U l        S U l        g r5   )super__init__
isinstancesessionr-   identity_chooserexecute_chooser	_shard_id)r7   argskwargs	__class__s      r;   rX   ShardedQuery.__init__o   sW    $)&)$,,7777 $ = =#||;;r>   c                     U R                  US9$ )ae  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)rN   )r7   shard_ids     r;   	set_shardShardedQuery.set_shardw   s     %%8%<<r>   )r]   r\   r[   )r^   r   r_   r   rA   None)rd   ShardIdentifierrA   r   )	rC   rD   rE   rF   __doc__rX   re   rG   __classcell__r`   s   @r;   r.   r.   f   s    = =r>   c                  t  ^  \ rS rSr% S\S'   S\S'   S\S'   SSS\4SSS	.                 SU 4S
 jjjjrS\R                  S\	R                  S4                 SU 4S jjjr        SS jr   S         SS jjr SSSSS.           SS jjjr      SS jrSrU =r$ )r-      r2   shard_chooserrI   r[   z*Callable[[ORMExecuteState], Iterable[Any]]r\   N)
id_chooserquery_chooserc               h  >^ ^^ [         TT ]  " SSU0UD6  [        R                  " T S[        SS9  UT l        U(       a8  Um[        R                  " SS5                      SUU 4S jjn	U	T l        O%U(       a  UT l        O[        R                  " S5      eU(       aG  Um[        R                  " S	S
5        U(       a  [        R                  " S5      e    SU4S jjn
Uc  U
nUc  [        R                  " S5      eUT l        0 T l        Ub  U H  nT R                  XU   5        M     gg)aN  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 identity_chooser: A callable, passed a Mapper and primary key
 argument, which should return a list of shard ids where this
 primary key might reside.

  .. versionchanged:: 2.0  The ``identity_chooser`` parameter
     supersedes the ``id_chooser`` parameter.

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

	query_clsdo_orm_executeT)retvalzLThe ``id_chooser`` parameter is deprecated; please use ``identity_chooser``.z2.0c               f   > TR                  U 5      nU(       a  UR                  U5      nT" Xa5      $ r5   )query_set_lazyload_from)	r8   rL   rM   rN   rO   rP   q_id_chooserr7   s	          r;   _legacy_identity_chooser9ShardedSession.__init__.<locals>._legacy_identity_chooser   s2     JJv&#,,-=>A"122r>   z*identity_chooser or id_chooser is requiredzNThe ``query_chooser`` 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      $ r5   )	statement)orm_context_query_choosers    r;   _default_execute_chooser9ShardedSession.__init__.<locals>._default_execute_chooser   s     &k&;&;<<r>   Nz,execute_chooser or query_chooser is requiredr6   rR   )r~   r(   rA   zIterable[Any])rW   rX   r   listenexecute_and_instancesrn   r   warn_deprecatedr[   r   ArgumentErrorr\   _ShardedSession__shards
bind_shard)r7   rn   r[   r\   shardsrr   ro   rp   r_   rz   r   kry   r   r`   s   `           @@r;   rX   ShardedSession.__init__   s   X 	7977"$9$	
 +$K  33"303 #?	3
 $>3 !/3 3 3 3 %=D!$4D!##<  *N  2
 ''( 
=,==
 &":"##>   /=?!9-  r>   c           	        > Ub  [         TU ]  " UU4SU0UD6n	U	$ U R                  UUUUU(       a  [        U5      O0 S9 H  n
[         T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)rM   rN   rO   )r   rM   )rW   _identity_lookupr[   dict)r7   r8   primary_key_identityr   passiverM   rN   rO   rP   objrd   obj2r`   s               r;   r   ShardedSession._identity_lookup   s    ( %'*$  . 	C J 11$!1"37EtN32 2  w/( $,%5	
  #K" r>   c                   UbO  [        U5      nUR                  (       a  UR                  S   nUc   eU$ UR                  (       a  UR                  $ [        U[        5      (       d   eU R
                  " X40 UD6nUb  UWl        U$ )Nr   )r   keyr   rY   r   rn   )r7   r8   r9   rP   statetokenrd   s          r;   _choose_shard_and_assign'ShardedSession._choose_shard_and_assign,  s     H%Eyy		!(((%%+++&&))))%%f="=#+E r>   c                <   Uc  U R                  X5      nU R                  5       (       a$  U R                  5       nUc   eUR                  XS9$ U R	                  XUS9n[        U[        5      (       a  UR                  " S0 UD6$ [        U[        5      (       d   eU$ )zQProvide a :class:`_engine.Connection` to use in the unit of work
flush process.

)rd   )r8   rd   r9   r6   )	r   in_transactionget_transaction
connectionget_bindrY   r   connectr   )r7   r8   r9   rd   rP   transbinds          r;   connection_callable"ShardedSession.connection_callableA  s     44VFH  ((*E$$$##F#>>==8 ! D $''||)b))!$
3333r>   )rd   r9   r:   c               P    Uc  U R                  XUS9nUc   eU R                  U   $ )N)r9   r:   )r   r   )r7   r8   rd   r9   r:   rP   s         r;   r   ShardedSession.get_bind_  sA     44& 5 H '''}}X&&r>   c                     X R                   U'   g r5   )r   )r7   rd   r   s      r;   r   ShardedSession.bind_shardo  s     #'hr>   )__shardsr\   r[   rn   )rn   r2   r[   zOptional[IdentityChooser]r\   z4Optional[Callable[[ORMExecuteState], Iterable[Any]]]r   zOptional[Dict[str, Any]]rr   zType[Query[_T]]ro   z<Optional[Callable[[Query[_T], Iterable[_T]], Iterable[Any]]]rp   z/Optional[Callable[[Executable], Iterable[Any]]]r_   r   rA   rg   )r8   z
Mapper[_O]r   zUnion[Any, Tuple[Any, ...]]r   Optional[Any]r   r   rM   rS   rN   r   rO   zOptional[_BindArguments]rP   r   rA   z)Union[Optional[_O], LoaderCallableStatus])r8   Optional[_EntityBindKey[_O]]r9   r   rP   r   rA   r   )NNN)
r8   r?   r9   r   rd   Optional[ShardIdentifier]rP   r   rA   r   r5   )r8   r   rd   r   r9   r   r:   r@   rP   r   rA   r'   )rd   rh   r   zUnion[Engine, OptionEngine]rA   rg   )rC   rD   rE   rF   __annotations__r.   rX   r   PASSIVE_OFFr   
EMPTY_DICTr   r   r   r   r   rG   rj   rk   s   @r;   r-   r-      s   %%??
 7; +/%1n. IMn.#n. 4n.
	n. )n. #n.
n. Gn. n. 
n. n.h )-*669=8<37// :/ &	/
 / 7/ 6/ 1/ / 
3/ /b,  	
 
. (,"&.2	$   ,	
  
@ 04' /3"&*.',' ,	'
  ' (' ' 
' '''/J'	' 'r>   c                  2    \ rS rSrSrSr S   SS jjrSrg)	set_shard_idiu  a  a loader option for statements to apply a specific shard id to the
primary query as well as for additional relationship and column
loaders.

The :class:`_horizontal.set_shard_id` option may be applied using
the :meth:`_sql.Executable.options` method of any executable statement::

    stmt = (
        select(MyObject)
        .where(MyObject.name == "some name")
        .options(set_shard_id("shard1"))
    )

Above, the statement when invoked will limit to the "shard1" shard
identifier for the primary query as well as for all relationship and
column loading strategies, including eager loaders such as
:func:`_orm.selectinload`, deferred column loaders like :func:`_orm.defer`,
and the lazy relationship loader :func:`_orm.lazyload`.

In this way, the :class:`_horizontal.set_shard_id` option has much wider
scope than using the "shard_id" argument within the
:paramref:`_orm.Session.execute.bind_arguments` dictionary.


.. versionadded:: 2.0.0

rd   propagate_to_loadersc                    Xl         X l        g)a   Construct a :class:`_horizontal.set_shard_id` option.

:param shard_id: shard identifier
:param propagate_to_loaders: if left at its default of ``True``, the
 shard option will take place for lazy loaders such as
 :func:`_orm.lazyload` and :func:`_orm.defer`; if False, the option
 will not be propagated to loaded objects. Note that :func:`_orm.defer`
 always limits to the shard_id of the parent row in any case, so the
 parameter only has a net effect on the behavior of the
 :func:`_orm.lazyload` strategy.

Nr   )r7   rd   r   s      r;   rX   set_shard_id.__init__  s     !$8!r>   )r   rd   N)T)rd   rh   r   bool)rC   rD   rE   rF   ri   	__slots__rX   rG   r6   r>   r;   r   r   u  s/    8 5I GK9'9?C9 9r>   r   c                  ^  T R                   (       a  T R                  nO1T R                  (       d  T R                  (       a  T R                  nOS nT R
                  n[        U[        5      (       d   e    SU 4S jjnT R                   H&  n[        U[        5      (       d  M  UR                  n  Oe   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" U5      $ / nUR                  T 5       H  nU" U5      nUR                  U5        M     US   R                   " USS  6 $ )Nrd   c                r   > [        TR                  5      nXS'   TR                  U S9  TR                  US9$ )Nrd   )r   )rO   )r   rO   update_execution_optionsinvoke_statement)rd   rO   r~   s     r;   iter_for_shard-execute_and_instances.<locals>.iter_for_shard  sB     k889%-z",,H,E++>+JJr>   rc   r      )rd   rh   rA   &Union[Result[_T], IteratorResult[_TP]])	is_selectload_options	is_update	is_deleteupdate_delete_optionsrZ   rY   r-   _non_compile_orm_optionsr   rd   _identity_tokenrN   rO   r\   appendmerge)r~   active_optionsrZ   r   orm_optrd   partialresult_s   `       r;   r   r     sY    $11			+"7"7$::!!Gg~....K!K	/K 77 g|,,''H 8 n<<H%55H{<<<"44^DH;555"11*=HHh''//<H$X.GNN7# = qz--r>   N)r~   r(   rA   r   )Fri   
__future__r   typingr   r   r   r   r   r	   r
   r   r   r    r   r   r   r   ormr   orm._typingr   orm.interfacesr   
orm.mapperr   	orm.queryr   orm.sessionr   r   r   util.typingr   r   engine.baser   r   r   engine.resultr    r!   r"   r#   orm.bulk_persistencer$   orm.contextr%   r&   r'   r(   	orm.stater)   sqlr*   sql._typingr+   sql.elementsr,   __all__r/   strrh   r2   rI   r.   r-   r   r   r6   r>   r;   <module>r      s  $ #                 4 &   ( - ! " ($*.&* 9*,*-) !,^
,T 8 
h 
=59 =@l'W l'^/99 /9d7. 7.+7.r>   