From: Mike Bayer Date: Fri, 10 Aug 2007 21:18:01 +0000 (+0000) Subject: #725 add query arg to id_chooser() X-Git-Tag: rel_0_4beta1~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b01c63280e16cd549601fb91393acbff3695821a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git #725 add query arg to id_chooser() --- diff --git a/examples/sharding/attribute_shard.py b/examples/sharding/attribute_shard.py index 6e47329896..df3f7467f7 100644 --- a/examples/sharding/attribute_shard.py +++ b/examples/sharding/attribute_shard.py @@ -118,7 +118,7 @@ def shard_chooser(mapper, instance): # pk so we just return all shard ids. often, youd want to do some # kind of round-robin strategy here so that requests are evenly # distributed among DBs -def id_chooser(ident): +def id_chooser(query, ident): return ['north_america', 'asia', 'europe', 'south_america'] # query_chooser. this also returns a list of shard ids, which can diff --git a/lib/sqlalchemy/orm/shard.py b/lib/sqlalchemy/orm/shard.py index 69d6921673..7f4f3bd210 100644 --- a/lib/sqlalchemy/orm/shard.py +++ b/lib/sqlalchemy/orm/shard.py @@ -15,9 +15,10 @@ class ShardedSession(Session): the future as participating in that shard. id_chooser - a callable, passed 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. + 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. query_chooser for a given Query, returns the list of shard_ids where the query @@ -98,7 +99,7 @@ class ShardedQuery(Query): if self._shard_id is not None: return super(ShardedQuery, self).get(ident) else: - for shard_id in self.id_chooser(ident): + for shard_id in self.id_chooser(self, ident): o = self.set_shard(shard_id).get(ident, **kwargs) if o is not None: return o @@ -109,7 +110,7 @@ class ShardedQuery(Query): if self._shard_id is not None: return super(ShardedQuery, self).load(ident) else: - for shard_id in self.id_chooser(ident): + for shard_id in self.id_chooser(self, ident): o = self.set_shard(shard_id).load(ident, raiseerr=False, **kwargs) if o is not None: return o diff --git a/test/orm/sharding/shard.py b/test/orm/sharding/shard.py index c1dd63d657..69839c8c7a 100644 --- a/test/orm/sharding/shard.py +++ b/test/orm/sharding/shard.py @@ -70,7 +70,7 @@ class ShardTest(PersistTest): else: return shard_chooser(mapper, instance.location) - def id_chooser(ident): + def id_chooser(query, ident): return ['north_america', 'asia', 'europe', 'south_america'] def query_chooser(query):