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

Soroush Rabiei soroush.rabiei at gmail.com
Fri Nov 25 12:00:05 EST 2016


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