[odb-users] reuse inheritance with templated base class

Boris Kolpackov boris at codesynthesis.com
Fri Aug 29 04:47:22 EDT 2014


Hi Marcel,

Marcel Nehring <mne at qosmotec.com> writes:

> Let's say I have two classes with two members each. One of these members is
> "the same" in both classes i.e. I want them to be mapped to the same
> column. The other member should be mapped to different columns. So that if I
> persist one of the classes it will use two of the table's columns and set
> the other one to null. In my specific case we are talking about the same
> template class with different template parameters/members.  However, ODB
> does only generate the DDL for one of the classes. So in my example I would
> have a table with two instead of three columns. So when you said it works,
> did you mean it works if I generate the table by hand?

If ODB sees two or more classes mapped to the same table and you asked
it to generate the schema, it simply generates the schema based on the
first class and ignores all the others. This suggests that one way to
make this work would be to provide the first (perhaps dummy) class with
all the data members that you want in the database. For example:

// Dummy "schema-producing" class for a and b below.
//
#pragma db object table("ab")
struct ab_schema
{
  int common;

  #pragma db null
  int only_in_a;

  #pragma db null
  int only_in_b;
};

#pragma db object table("ab")
class a
{
  int common;
  int only_in_a;
};

#pragma db object table("ab")
class b
{
  int common;
  int only_in_b;
};


You could even place it into a separate header and then discard its
generated code. You just need to make sure that it is included before
the other two during ODB compilation when you generate the schema.


> Unfortunately there is another thing I could not solve myself. ODB
> supports C++11 enum classes, but when I try to compile code that uses
> them I get a compile error.  That is because in traits.hxx in 
> big_int_value_traits::set_value ODB tries to assign the value 0 to 
> an instance of my enum class. But implicit conversions are illegal 
> for  enum classes.

Yes, this is a bug. These statements:

  v = 0;

Should be replaced with:

  v = T ();

I've applied the fix in the repository. Let me know if you want me to
send you trait.hxx or libodb-oracle. It is probably easiest for you
to just change the affected lines directly.

Boris



More information about the odb-users mailing list