Skip to content

Conversation

@trueqbit
Copy link
Collaborator

@trueqbit trueqbit commented Oct 12, 2025

SQLite features Hidden columns in virtual tables, which then can be used as Table-valued functions.

This PR brings:

  • Table references for C++17 in order to facilitate table-valued expressions.
  • Support for eponymous virtual tables with hidden columns.
    • Replaced rank(), match<Table>(), is_equal<Table>(), highlight<Table>() with proper column-oriented logic.
    • match can match specific columns as well, not only any column.
  • Support for eponymous virtual tables with hidden columns as table-valued functions.
  • get_all<>() supports an explicit from condition, which is useful when selection from table-valued functions.
  • Support for the "generate_series" extension.

Examples in sqlite_orm speak are:

dbstat

Aggregated database statistics:

storage.get_all<dbstat_table>(from(dbstat_table("main", true)));

generate series

Generate six random numbers:

storage.select(random(), from(generate_series_table(1, 6)))

Where the equivalent by explicitly using hidden columns is:

storage.select(random(), where(generate_series_table->*&generate_series::hidden::start == 1 and
                               generate_series_table->*&generate_series::hidden::stop == 6);

fts5

This is of course available for the fts5 module and its hidden rank column respectively its hidden eponymous column (named after the fts5 table). In C++ it is represented by a placeholder field that I named any:

struct Post {};
constexpr orm_table_reference auto post_table = c<Post>();

storage.select(highlight(post_table->*&fts5::hidden::any, 0, "<b>", "</b>"),
               where(match(post_table->*&fts5::hidden::any, "SQLite")),
               order_by(post_table->*&fts5::hidden::rank)));

A slightly alternative syntax to using column pointers is by explicitly mapping hidden fields into the FTS5 table with an alias template:

struct Post {
    using hidden = fts5::hidden_fields_of<Post>;
};

storage.select(highlight(Post::hidden::any_field, 0, "<b>", "</b>"),
               where(match(Post::hidden::any_field, "SQLite")),
               order_by(Post::hidden::rank_field)));

Note that this approach is simply "syntactic sugar" and up to the programmer to use an alias template. The alias template can have a different name, it can be defined as a nested type name, or it can be defined outside just like a table reference.

For instance (additionally showing that match() accepts a regular column):

struct Post {
    std::string title;
};
using post_hidden = fts5::hidden_fields_of<Post>;

storage.select(highlight(post_hidden::any_field, 0, "<b>", "</b>"),
               where(match(&Post::title, "SQLite")),
               order_by(post_hidden::rank_field)));

It is better to include configuration header files of third-party libraries (including SQLite) in the library's configuration header than in the umbrella header.
C++17 still requires a template argument list, only C++20 has full CTAD support.
…C++17

* Made table references available for C++17 in order to facilitate table-valued expressions.
* Eponymous virtual tables may have hidden columns, which can be used now.
* Eponymous virtual tables with hidden columns can be used like table-valued functions.
* Unit tests for `mapped_type_proxy`.
Clang has the annoying habit of warning about future C++ features that it claims to support through a feature macro.
@trueqbit trueqbit force-pushed the feature/hidden-vtab-columns branch from 709882d to 64c93ec Compare October 20, 2025 20:37
@trueqbit trueqbit marked this pull request as ready for review October 21, 2025 13:16
@trueqbit trueqbit requested a review from fnc12 October 21, 2025 13:18
@trueqbit trueqbit merged commit 92b7959 into dev Oct 23, 2025
3 checks passed
@trueqbit trueqbit deleted the feature/hidden-vtab-columns branch October 23, 2025 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants