U a#@sdZddlmZddlmZddlmZddlmZddlmZddlm Z dd gZ Gd d d eZ Gd dde Z d d Z dS)aHorizontal 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)SessionShardedSession ShardedQuerycs$eZdZfddZddZZS)r cs:tt|j|||jj|_|jj|_|jj|_d|_dSN)superr __init__session id_chooser query_chooserexecute_chooserZ _shard_id)selfargskwargs __class__`C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\ext\horizontal_shard.pyr s    zShardedQuery.__init__cCs |j|dS)aReturn 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)rshard_idrrr set_shard$szShardedQuery.set_shard)__name__ __module__ __qualname__r r __classcell__rrrrr s csVeZdZddeffdd Zdfdd ZddZddd Zdd d Zd d Z Z S)rNc s|ddtt|jfd|i|tj|dtdd||_||_rvt dd|rbt d fd d }||_ n||_ |_i|_|dk r|D]}||||qdS) aConstruct 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. rN query_clsZdo_orm_executeT)retvalzMThe ``query_choser`` parameter is deprecated; please use ``execute_chooser``.z1.4z>Can't pass query_chooser and execute_chooser at the same time.cs |jSr )Z statement orm_contextrrrrnsz0ShardedSession.__init__..execute_chooser)popr rr rlistenexecute_and_instances shard_chooserrrZwarn_deprecatedr ArgumentErrorrr_ShardedSession__binds bind_shard)rr(rrZshardsr rkrr$rr 7s6"  zShardedSession.__init__c  s|dk r&tt|j||fd|i|S||}|r>||}|||D]4}tt|j||f||d|}|dk rJ|SqJdSdS)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`. Nidentity_token)r-lazy_loaded_from)r r_identity_lookupqueryZ_set_lazyload_fromr) rmapperZprimary_key_identityr-r.kwqrobjrrrr/zs2     zShardedSession._identity_lookupcKs^|dk rr<r+rrrrrr6s$G)  c sjrj}}d}nd}j}}j}fdd}|jdk rH|j}n0djkr^jd}ndjkrtjd}nd}|dk r||||Sg}|D]}||||}||q|dj |ddSdS)NcsZtj}tj}||d<jr8|d|i7}||d<n|d|i7}||d<j||dS)Nr_refresh_identity_tokenZ_sa_orm_load_optionsZ_sa_orm_update_options)bind_argumentsr)dictZlocal_execution_optionsrB is_selectZinvoke_statement)r load_optionsupdate_optionsrrBr"rriter_for_shards     z-execute_and_instances..iter_for_shardrr) rDrEZupdate_delete_optionsr rArrBrappendmerge) r#rEZactive_optionsrFr rGrpartialZresult_rr"rr's*           r'N)__doc__rrrrZ orm.queryrZ orm.sessionr__all__r rr'rrrrs