[odb-users] How to query entity by map container member?

Boris Kolpackov boris at codesynthesis.com
Thu Jul 15 08:12:37 EDT 2021

Ali Sherief <ali at notatether.com> writes:

> class ExchangeSchema: public Schema {
> [...]
> std::string id_;
> unsigned short type_;
> std::string lastpingtime_;
> std::map<std::string, bool> coin_supported_map_;
> };
> #pragma db object(ExchangeSchema)
> #pragma db member(ExchangeSchema::id_) id
> We need the ability to query the database for Exchanges by whether they
> are supported by a PaymentMethod by checking the UUID string key against
> its boolean value.
> According to an old post [...] , ODB's query language doesn't support
> container members. Is this still the case?

Yes, that's still the case.

> So a query like the below, for example, won't work?
> typedef odb::query<ExchangeSchema> ExchangeQuery;
> ...
> q = ExchangeQuery(ExchangeQuery::coin_supported()[uuid_string] == true);

No, there is no such syntax in the ODB query language.

> I am trying to follow the resolution in the linked post, which is to
> make an "image" object followed by an object loading view.

That's one way to do it. In this case I would personally change the
data model a bit so that I could express such queries directly with
views. Specifically, I would make various supported payment methods
a persistent object and then would replace the map with a vector of
pointers to supported payment methods:

class Coin
  std::string uuid;

class ExchangeSchema: public Schema {

std::vector<std::shared_ptr<Coin>> coin_supported_;

Then you should be able to create a view over these two object that
will allow you to express the above query condition without image

> Is the following image object correct syntax? Mainly, I am concerned that
> I'm using the wrong type for the custom (string) key of this entity.

Check the resulting database schema of the two objects -- if they
match, then you are using compatible types.

More information about the odb-users mailing list