ó
    øÞ j5A  ã                  ó¢  • 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
   referring 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)ÚShardChooseréQ   c                ó   • g ©N© )ÚselfÚmapperÚinstanceÚclauses       ÚY/var/www/html/gaurav/venv/lib/python3.13/site-packages/sqlalchemy/ext/horizontal_shard.pyÚ__call__ÚShardChooser.__call__R   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   Q   s-   † ðà$ðð ðð (ð	ð
 
÷r>   r2   c                  ó:   • \ rS rSr              SS jrSrg)ÚIdentityChooseréZ   c               ó   • g r5   r6   )r7   r8   Úprimary_keyÚlazy_loaded_fromÚexecution_optionsÚbind_argumentsÚkws          r;   r<   ÚIdentityChooser.__call__[   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   Z   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.   ég   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__p   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_shardx   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.   g   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                  X´U   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_chooserÚ9ShardedSession.__init__.<locals>._legacy_identity_chooserÇ   s2   ø€ ð —J‘J˜vÓ&�Þ#Ø×,Ñ,Ð-=Ó>�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_chooserÚ9ShardedSession.__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 	‰ÒÑ7 9Ð7°Ò7ä�ŠØÐ"Ô$9À$ò	
ð +ˆÔæØ$ˆKÜ× Ò ð3àôð3Ø"ð3à0ð3ð #?ð	3ð
 $>ð3ð !/ð3ð ð3ð ÷3ð 3ð %=ˆDÕ!ÞØ$4ˆDÕ!ä×#Ò#Ø<óð ö Ø*ˆ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Ø"3Þ7Eœt NÔ3È2ð 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Ø�y�yØŸ	™	 !™�ØÑ(Ð(Ð(Ø�Ø×%×%Ø×+Ñ+Ð+ä˜&¤&×)Ñ)Ð)Ð)Ø×%Ò% 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_callableB  s¨   € ð ÑØ×4Ñ4°VÓFˆHà×Ñ× Ñ Ø×(Ñ(Ó*ˆ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_shardp  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Ñ6Ø9=Ø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_idiv  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°   v  s/   † ñð8 5€Ið 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Ñ5‰HØ˜{×<Ñ<Ó<Ø"×4Ñ4°^ÑD‰HØ˜;×5Ñ5Ó5Ø"×1Ñ1°*Ñ=‰HàˆHàÑÙ˜hÓ'Ð'àˆØ×/Ñ/°Ö<ˆHÙ$ XÓ.ˆGØ�N‰N˜7Ö#ñ =ð �q‰z×Ò ¨¨ Ð-Ð-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'ô^/9�9ô /9ðd7.Ø ð7.à+õ7.r>   