
    +h"                         d 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	gZ
 G d
 d	e      Z G d de	      Zd Zy)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                   $     e Zd Z fdZd Z xZS )r
   c                     t        t        | 
  |i | | j                  j                  | _        | j                  j
                  | _        | j                  j                  | _        d | _        y N)superr
   __init__session
id_chooserquery_chooserexecute_chooser	_shard_id)selfargskwargs	__class__s      R/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/ext/horizontal_shard.pyr   zShardedQuery.__init__   sS    lD*D;F;,,11!\\77#||;;    c                 &    | j                  |      S )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_shardzShardedQuery.set_shard$   s     %%8%<<r   )__name__
__module____qualname__r   r   __classcell__r   s   @r   r
   r
      s    =r   c                   V     e Zd Zddef fd	Z	 	 d fd	Zd Z	 d	dZ	 d
dZd Z	 xZ
S )r	   Nc                    |j                  dd      t        t        |   dd|i| t	        j
                  | dt        d       || _        || _        r:t        j                  dd       |rt        j                  d	      fd
}|| _        n|| _        | _        i | _        ||D ]  }| j!                  |||           yy)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                 (     | j                         S r   )	statement)orm_contextr   s    r   r   z0ShardedSession.__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   r1   r   r   shardsr'   r   kr   r   s
           @r   r   zShardedSession.__init__7   s    D 

?D9nd,KyKFK"$9$	
 +$  2
 ''( 
< $3D #2D * .6!9-. r   c                     |t        t        | 
  ||fd|i|S | j                  |      }|r|j	                  |      }| j                  ||      D ]   }t        t        | 
  ||f||d|}||c S  y)a_  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)r9   lazy_loaded_from)r   r	   _identity_lookupquery_set_lazyload_fromr   )
r   mapperprimary_key_identityr9   r:   kwqr   objr   s
            r   r;   zShardedSession._identity_lookupz   s    " %?$  . 	  

6"A(()9: OOA/CD 	NDB( $,%5	
  ?J	 r   c                     |Dt        |      }|j                  r|j                  d   }|J |S |j                  r|j                  S  | j                  ||fi |}||_        |S )Nr   )r   keyr9   r1   )r   r>   instancer@   statetokenr   s          r   _choose_shard_and_assignz'ShardedSession._choose_shard_and_assign   sx    H%Eyy		!(((%%+++%4%%fh="=#+E r   c                     || j                  ||      }| j                         r!| j                         j                  ||      S  | j	                  |||      j
                  di |S )zaProvide a :class:`_engine.Connection` to use in the unit of work
        flush process.

        )r   )r   rE   r-   )rH   in_transactionget_transaction
connectionget_bindconnect)r   r>   rE   r   r   s        r   connection_callablez"ShardedSession.connection_callable   sz     44VXFH '')44Vh4OO4==H ! g    r   c                 L    || j                  |||      }| j                  |   S )N)clause)rH   r4   )r   r>   r   rE   rQ   r@   s         r   rM   zShardedSession.get_bind   s7     44 5 H ||H%%r   c                 "    || j                   |<   y r   )r4   )r   r   binds      r   r5   zShardedSession.bind_shard   s    !%Xr   )NN)NNN)NNNN)r    r!   r"   r
   r   r;   rH   rO   rM   r5   r#   r$   s   @r   r	   r	   6   sC    
 A.N 'R  48 & AE&&r   c                      j                   r j                  x}}d }n/ j                  s j                  rd } j                  x}}nd x}x}} j
                  } fd}|r|j                  |j                  }n>d j                  v r j                  d   }n d j                  v r j                  d   }nd }|
 ||||      S g }|j                         D ]  } ||||      }|j                  |         |d   j                  |dd   S )Nc                    t        j                        }t        j                        }| |d<   j                  r|d| iz  }||d<   n$j                  sj
                  r|d| iz  }||d<   j                  ||      S )Nr   _refresh_identity_token_sa_orm_load_options_sa_orm_update_options)bind_argumentsr   )dictlocal_execution_optionsrY   	is_select	is_update	is_deleteinvoke_statement)r   load_optionsupdate_optionsr   rY   r,   s        r   iter_for_shardz-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++)=N , 
 	
r   r   r          )r\   r`   r]   r^   update_delete_optionsr   rV   r   rY   r   appendmerge)	r,   r`   active_optionsra   r   rb   r   partialresult_s	   `        r   r0   r0      s/   (3(@(@@~			+"7"7*5*K*KK9===~!!G
" .@@L!99	;88	800@	{11	1--j9hnEE//< 	$H$X|^LGNN7#	$  wqz--r   N)__doc__ r   r   r   r   	orm.queryr   orm.sessionr   __all__r
   r	   r0   r-   r   r   <module>rp      sH         !^
,=5 =4X&W X&v/.r   