SOCI / soci

Official repository of the SOCI - The C++ Database Access Library
http://soci.sourceforge.net/
Boost Software License 1.0
1.42k stars 478 forks source link

Using boost::tuple with ORM #983

Open VadVergasov opened 2 years ago

VadVergasov commented 2 years ago

I'm trying to use tuple with my type and some other. Here is the example of the code:

std::string query = "SELECT stock.*, laptop.name, provider.name FROM stock JOIN laptop ON laptop.id=stock.laptop JOIN provider ON provider.id=stock.source LIMIT 10";
soci::rowset<boost::tuple<stock, std::string, std::string>> rs = (sql.prepare << query);
for (auto it = rs.begin(); it != rs.end(); it++) {
    result.push_back({it->get<0>(), it->get<1>()});
}

And I'm getting such exception: Cannot convert data while fetching data from "SELECT stock.*, laptop.name, provider.name FROM stock JOIN laptop ON laptop.id=stock.laptop JOIN provider ON provider.id=stock.source LIMIT 10". In struct stock there are all required conversions (Here is the code). Is SOCI isn't supporting using tuples with ORM types, only based on base types?

zann1x commented 2 years ago

I'm not familiar with the boost integration of SOCI, but my guess is that you're either missing the soci/boost-tuple.h include or SOCI doesn't provide a type_conversion for boost::tuple anymore, so you have to provide your own for that (see below).

https://github.com/SOCI/soci/blob/98e0b8b7866ff6f9f0184ddea08d0c3b723cb77b/src/core/boost-tuple.h#L63-L85

VadVergasov commented 2 years ago

I've included boost-tuple.h and it works with base types like string, int and etc. but not with ORM types.

VadVergasov commented 2 years ago

Also I'm getting exception:

Only one Row element allowed in a single statement.

when trying to execute this code:

std::string query = "SELECT stock.*, laptop.*, provider.* FROM stock JOIN laptop ON laptop.id = stock.laptop JOIN provider ON provider.id = stock.source LIMIT 10";
soci::rowset<boost::tuple<stock, laptop, provider>> rs = (sql.prepare << query);

Also all types are defined and added all required conversions. Can't find any additional info about this exception