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

Ali Sherief ali at notatether.com
Wed Jul 14 21:05:28 EDT 2021


At my company I'm in charge of writing a payment processor in C++. One of the things I have to do is use ODB to store a list of Exchange (financial store of value) entities in the database. We have Exchange entity defined as an "id" std::string, a "type" int, a "lastpingtime" string, and we have a map container that maps the string UUID of a PaymentMethod (aka. "Coin") to a boolean to indicate whether the Exchange supports it, like so:

class ExchangeSchema: public Schema {

const std::string& get_id() const;
void set_id(const std::string&);

const unsigned short get_type() const;
void set_type(const unsigned short);

const std::string& get_lastpingtime() const;
void set_lastpingtime(const std::string&);


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. However, I am not quite sure how to accomplish this.

According to an old post https://odb-users.codesynthesis.narkive.com/CPkFAOWx/querying-entity-s-container , ODB's query language doesn't support container members. Is this 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);

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

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.

#pragma db object table("exchange_coinsupport") no_id abstract
struct exchange_coinsupport
std::string id;
unsigned short type;
std::string lastpingtime;

Also, the following object loading view corresponds to it?

#pragma db view object(ExchangeSchema) object(exchange_coinsupport = coin_support) \
struct exchange_view // I would use this in odb::query
std::chared_ptr<ExchangeSchema> p;

Thanks for your time.
- Ali

More information about the odb-users mailing list