[odb-users] Store vector index within data using custom containers

Andrew Cunningham odb at a-cunningham.com
Fri Nov 25 20:16:43 EST 2016


Soroush,
Maybe I am missing  something here, but it seems that you can use your
"records" class  as is - that's the beauty of ODB.
Put a #pragma db object in front of the class definition to define the
"records" class as a database object. And everything follows automagically.


Andrew

On Fri, Nov 25, 2016 at 9:00 AM, Soroush Rabiei <soroush.rabiei at gmail.com>
wrote:

> Hi
>
> I wonder if there is a way to store both index and value of a std:vector
> using ODB. Let's say I have two tables; `records' and `weights'. Every
> record has up to eight `weight' entries. Following classes and table
> entries are examples of what I want to achieve.
>
> Class: records
>
> > class records {
> >     int id;
> >     std::string name;
> >     int weights; // = weight_values.size()  just for clarifying
> >     std::vector<int> weight_values;
> > }
>
>
> Table: records
>
> > +----+------+---------+
> > | id | name | weights |
> > +----+------+---------+
> > |  1 | foo  |       3 |
> > |  2 | bar  |       4 |
> > |  3 | zoo  |       1 |
> > +----+------+---------+
>
>
> Table: weights
>
> > +-----------+-------+--------+
> > | record_id | index | weight |
> > +-----------+-------+--------+
> > |         1 |     1 |    100 |
> > |         1 |     2 |    110 |
> > |         1 |     3 |     98 |
> > |         2 |     1 |     41 |
> > |         2 |     2 |     45 |
> > |         2 |     3 |     46 |
> > |         2 |     4 |     38 |
> > |         3 |     1 |    400 |
> > +-----------+-------+--------+
>
>
>
> I've managed to store the vector in another table following description on
> section '5. Containers' of the documentation. However can not find the
> proper way to add `index' column to the tables.
>
> Section `5.5 Using Custom Containers' says it's possible to implement
> custom container support. Though I don't understand the details. Should I
> specialize `container_traits' template class for `std::vector<int>' and add
> a second column for `index'?
>
> Thanks
>


More information about the odb-users mailing list