[odb-users] Query Language and Polymorphic Types

Brian E. Coggins becoggins at hotmail.com
Fri Mar 10 11:00:01 EST 2023


Hi Boris and Friends,

Imagine one has a polymorphic hierarchy like the following (all of these as database classes):

struct base
{
	…
};

struct derived : public base
{
	...
	int data;
};

And this is connected by relationship to another class (also a database class):

struct owner
{
	...
	std::shared_ptr< base > owned;
};

Now imagine I’m querying for owner instances where I already know that owned is of type derived and I’d like to include derived.data as a query field.  Does ODB’s query language offer any kind of trick for including the derived member in the query?  I’ve been messing around with odb::query<owner>::owned but can’t figure out any obvious way to poke into the derived type from within the query language.

I’d love to avoid having to load every owned, dynamic_cast each of them to derived one-at-a-time, and manually inspect each data member.

Of course I could also just hand-code a SELECT…JOIN query and make a custom view class, and maybe that’s the best answer.  But it would be nifty if the query language had a solution.

Thanks,
Brian





More information about the odb-users mailing list