sqlpp11: [Feedback Requested] Results returning optional, string_view, and span
Hi,
I would love to get your feedback on this:
Triggered by #568 I started back porting some aspects of sqlpp17, in particular for SELECT result rows.
- nullable result values are represented by
(std|sqlpp)::optional. - text result values are represented by
(std|sqlpp)::sting_view. - blob result values are represented by
(std|sqlpp)::span<uint8_t>.
Assuming positive feedback, I would expand the use of these types similar to how they are used in sqlpp17. The most drastic change would be to express dynamic queries with optional query parts, e.g.:
auto s = dynamic_select(db)
.dynamic_columns(all_of(t))
.dynamic_from(t)
.unconditionally();
if (use_omega)
{
s.selected_columns.add(without_table_check(f.omega));
s.from.add(dynamic_cross_join(f));
}
for (const auto& row : db(s))
{
print(row.at("omega")); // will crash if !use_omega
}
would become something like
const auto s = select(all_of(t), f.omega.if_(use_omega))
.from(t.cross_join(f.if_(use_omega))
.unconditionally();
for (const auto& row : db(s))
{
print(row.omega); // row.omega will be NULL if !use_omega.
}
with expression.if_(bool condition) being equivalent to condition ? sqlpp::make_optional(expression) : sqlpp::nullopt.
What do you all think?
Thanks, Roland
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (23 by maintainers)
Thanks for the feedback and the question: That would be a breaking change
@CJCombrink If I remember correctly the SQL standard requires any PRIMARY KEY column to be NOT NULL. However I don’t have the standard handy, so I cannot quote the relevant part.
From my observations pretty much any SQL database enforces this requirement, except for sqlite which as usual lives in a world of its own.
OK, tried the
optionalbranch of sqlpp and my first impressions from the changes are good!Building sqlpp + tests (core, postgresql, mysql, sqlite) with
g++ (GCC) 14.1.1 20240522 (Red Hat 14.1.1-4)went without any errors or warnings. The core, postgresql and sqlite tests also ran without any errors. The mysql tests failed because I did not have a mysql server running, so that is fine too, I guess.Then I tried building one of my pet projects with the new sqlpp. The project builds in c++23 mode with all warnings (
-Werror -Wall -Wextra) enabled. I did not notice any warnings or errors caused by c++23 compatibility issues in the sqlpp includes, which is good.Now about the problems that I noticed.
sqlpp11-ddl2cpp seems to generate incorrect definitions for postgresql primary fields by marking them as nullable, even though they are not. For example, I have the following database definition:
The generated header looks like this:
So the field
gap_idhas propertysqlpp::tag::can_be_null, which in turn means that the result field is represented bystd::optional<int64_t>and trying to assign the result field to an int64_t variable in my program fails.I guss it is more of a bug in sqlpp11-ddl2cpp and the switch to
std::optionaljust uncovered the bug in sqlpp11-ddl2cpp which is good.Also I noticed that the compat versions of
optional,span, andstring_viewgo directly into thesqlppnamespace instead ofsqlpp::compat, unlikemake_uniquewhich goes tosqlpp::compat. Maybe it makes sense to put the new compatibility classes intosqlpp::compatin order to prevent pollution of thesqlppnamespace? Most users use c++17 or newer and they will work with thestd::optional,std::string_viewandstd::span, anyway, so for them it won’t add any extra typing on the keyboard.Forgot to mention: If you want to get your hands dirty and play with code, try out
https://github.com/rbock/sqlpp11/tree/optional