sqlpp11 icon indicating copy to clipboard operation
sqlpp11 copied to clipboard

enum usage with sqlpp11

Open zamazan4ik opened this issue 5 years ago • 7 comments

Hi!

Is there any possibility to work with enum data type with sqlpp11? I didn't find any example in the wiki or tests.

If it matters - I am insterested in enum from PostgreSQL.

Thank you!

zamazan4ik avatar Apr 05 '20 23:04 zamazan4ik

Hi,

There is no library support for enum data as of now. I would want SQL-enum values to be mapped to C++ enum values, which seems difficult without reflection. Or you could generate C++ enums from the DDL, which is sub-optimal, IMHO.

Personally, I use integer values in the database plus mapping functions between C++ enum and said integer values.

I am open to suggestions, of course.

Best,

Roland

rbock avatar Apr 06 '20 07:04 rbock

Beside the mapping from SQL-enum to c++ enum values I have a generator that creates a data type to be used together with sqlpp11. Maybe there is some value in adding that to sqlpp11. I am using that for 'strong' types (see: https://github.com/foonathan/type_safe).

When using the above, you are able to do something like:

struct special_id : type_safe::strong_typedef<special_id, uint32_t>,
                    type_safe::strong_typedef_op::equality_comparison<special_id>
{
  using strong_typedef::strong_typedef;
};

const auto& res = db(select(tbl.special_id).from(...));

// special_id x = 5U; // Not allowed, will not compile
// uint32_t x = res.front().special_id; // Not allowed, will not compile
special_id x = res.front().special_id;

I do this because I was bitten once by the fact that I compared to integer values which had different meanings...

matthijs avatar Apr 06 '20 08:04 matthijs

I would want SQL-enum values to be mapped to C++ enum values, which seems difficult without reflection.

@rbock I'm unsure what black magic this library uses, and I think it's C++17 only, but it appears to be able to do some static reflection of enums: https://github.com/Neargye/magic_enum

tommitytom avatar May 09 '20 05:05 tommitytom

Thanks for the link. That looks pretty cool. But it is indeed black magic (very clever, though).

I would not want to integrate that into the sqlpp11 because it depends on implementation details of the various compilers and it would probably be super slow if your enum values are large (by default, it covers enums values [-128, +128]).

That being said, it might make sense to use the library in application code to replace hand-written enum<->int mappers.

Best,

Roland

rbock avatar May 09 '20 06:05 rbock

The only performant way I could think of would be to generate the c++ enum and parsing and string functions with ddl2cpp. Still probably a little slower than using the int values in the database if your connector is not all text conversion but no reflection.

On Sat, May 9, 2020, 02:15 Roland Bock [email protected] wrote:

Thanks for the link. That looks pretty cool. But it is indeed black magic (very clever, though).

I would not want to integrate that into the sqlpp11 because it depends on implementation details of the various compilers and it would probably be super slow if your enum values are large (by default, it covers enums values [-128, +128]).

That being said, it might make sense to use the library in application code to replace hand-written enum<->int mappers.

Best,

Roland

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rbock/sqlpp11/issues/325#issuecomment-626113631, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHZB24KAHURPZZMF25BQLLRQTYJJANCNFSM4MBS5IXQ .

Erroneous1 avatar May 09 '20 16:05 Erroneous1

Is there some complete example I can refer to for Enum hack? I am not able to connect the dots on using this library for Enums with workaround. Please advise, as we already have an approved database design with few Enum columns in it (like Direction - BUY/SELL, Trade status -> TRADE_COMPLETED, TRADE_REJECTED, PARTIALLY_EXECUTED). For all such columns, Enum makes sense to have strong integrity and avoid corrupted data in trade table. Thanks!

ashishmodi avatar Jan 16 '23 12:01 ashishmodi

I am afraid that the library has no direct support for enums. There are some ideas mentioned earlier in this thread. I have not seen any of them in a full example, though.

Using https://github.com/Neargye/magic_enum might be the most straight forward, integrating into the code generator might be best long term, but that certainly needs some upfront investment.

rbock avatar Jan 17 '23 05:01 rbock