[odb-users] aggregate or dis-aggregate a data member

Boris Kolpackov boris at codesynthesis.com
Thu Sep 24 11:36:34 EDT 2015


Hi,

MM <finjulhich at gmail.com> writes:

> class D
> { public:
> const std::string& get_type() const;
> const std::uint64_t& get_id() const;
> }; class C {
> public:
> const D* d() const;
> void d(const D&);
> private:
> const D* d_;
> };
> const D* get_D( const std::string& type, std::uint64_t id );
> 
> so, I need the d member to map to 2 columns in the table, and the 2 columns
> in the table to map back to a single d object, obtained through the get_D()
> free function.
> 
> Currently I have:
> 
> #pragma db object(C) abstract definition
> #pragma db member(C::d) transient
> #pragma db member(C::dtype) virtual(std::string)\
>   get(this.d()? this.d()->get_type(): std::string())\
>   set()
> #pragma db member(C::did) virtual(std::uint64_t)\
>   get(this.d()? this.d()->get_id(): std::uint64_t())\
>   set()
> 
> For the setting part, I would need both columns to then do the 
> get_D() call.

Right, and that's the reason why your approach will not work: ODB
will supply you the columns separately but you need them both at
the same time.

The way to handle this is with a composite value. Something along
these lines:

#pragma db value
struct D_Image
{
  #pragma db column("dtype")
  std::string type;

  #pragma db column("did")
  std::uint64_t id;
};

#pragma db member(C::d) virtual(D_Image) column("")          \
  get(D_Image{this.d ()->get_type (), this.d ()->get_id ()}) \
  set(this.d (get_D ((?).type, (?).id))

Boris



More information about the odb-users mailing list