def get_query(name, user_id=None): """Get the named pre-built query, sharding on user_id if given. This is a helper function to return an appropriate pre-built SQL query while taking sharding of the WBO table into account. Call it with the name of the query and optionally the user_id on which to shard. """ if user_id is None: table = wbo else: table = get_wbo_table(user_id) queries['ITEM_ID_COL_USER'] = and_( table.c.collection == bindparam('collection_id'), table.c.username == bindparam('user_id'), table.c.id == bindparam('item_id'), table.c.ttl > bindparam('ttl')) query = queries.get(name) if query is None: raise ValueError(name) if isinstance(query, str): if '%(wbo)s' in query: query = query % {'wbo': table.name} query = text(query) return query
def _get_wbo_table(self, user_id): if self.shard: return get_wbo_table(user_id, self.shardsize) return _wbo