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)

Most upvoted comments

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 optional branch 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:

CREATE TABLE tg_gap_defs (
	gap_id serial4 PRIMARY KEY,
	channel_id int8 NOT NULL REFERENCES tg_channels (channel_id),
	-- Lower end of the message gap. This message does not belong to the gap. A NULL value means that the lower end of the gap is open.
	min_message_id int8,
	-- Last fetched message ID. Fetched messages are ordered by message ID in descending order, so this field
	-- gradually decreases until it reaches min_message_id. A NULL value means that there are no fetched messages
	-- for this gap yet so the gap is open at its upper end.
	last_fetched_id int8
);
CREATE INDEX ON tg_gap_defs (channel_id);
CREATE UNIQUE INDEX ON tg_gap_defs (channel_id, min_message_id);

The generated header looks like this:

  namespace tg_gap_defs_
  {
    struct gap_id
    {
      struct _alias_t
      {
        static constexpr const char _literal[] =  "gap_id";
        using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
        template<typename T>
        struct _member_t
          {
            T gap_id;
            T& operator()() { return gap_id; }
            const T& operator()() const { return gap_id; }
          };
      };
      using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::must_not_insert, sqlpp::tag::must_not_update, sqlpp::tag::can_be_null>;
    };
    struct channel_id
    {
      struct _alias_t
      {
        static constexpr const char _literal[] =  "channel_id";
        using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
        template<typename T>
        struct _member_t
          {
            T channel_id;
            T& operator()() { return channel_id; }
            const T& operator()() const { return channel_id; }
          };
      };
      using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::require_insert>;
    };
    struct min_message_id
    {
      struct _alias_t
      {
        static constexpr const char _literal[] =  "min_message_id";
        using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
        template<typename T>
        struct _member_t
          {
            T min_message_id;
            T& operator()() { return min_message_id; }
            const T& operator()() const { return min_message_id; }
          };
      };
      using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::can_be_null>;
    };
    struct last_fetched_id
    {
      struct _alias_t
      {
        static constexpr const char _literal[] =  "last_fetched_id";
        using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
        template<typename T>
        struct _member_t
          {
            T last_fetched_id;
            T& operator()() { return last_fetched_id; }
            const T& operator()() const { return last_fetched_id; }
          };
      };
      using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::can_be_null>;
    };
  } // namespace tg_gap_defs_

So the field gap_id has property sqlpp::tag::can_be_null, which in turn means that the result field is represented by std::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::optional just uncovered the bug in sqlpp11-ddl2cpp which is good.

Also I noticed that the compat versions of optional, span, and string_view go directly into the sqlpp namespace instead of sqlpp::compat, unlike make_unique which goes to sqlpp::compat. Maybe it makes sense to put the new compatibility classes into sqlpp::compat in order to prevent pollution of the sqlpp namespace? Most users use c++17 or newer and they will work with the std::optional, std::string_view and std::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