Skip to content

SQL Query Views#1481

Open
trueqbit wants to merge 55 commits intodevfrom
experimental/sql-view
Open

SQL Query Views#1481
trueqbit wants to merge 55 commits intodevfrom
experimental/sql-view

Conversation

@trueqbit
Copy link
Copy Markdown
Collaborator

@trueqbit trueqbit commented Apr 26, 2026

Views in SQL are virtual tables based on the result-set of a SELECT statement, also known as query views.

With sqlite_orm, query views are defined using make_view() which leverages C++ reflection to automatically map columns from the SELECT statement to struct fields and to specify the query view's name with a class-scope annotation.

SQL views created with make_view() don't require explicit column definitions - the columns are automatically determined from the view's object type through reflection.

[[=dbo_name("high_earners")]]
struct HighEarner {
    int64 id;
    std::string name;
    double salary;
};

make_storage("",
             make_table("employee", /*...*/),
             make_view<HighEarner>(select(columns(&Employee::id, &Employee::name, &Employee::salary), where(c(&Employee::salary) > 0.0))));

Resolves the following discussions and tickets:

Note: There are no tests executed yet as no github action platform (Linux Ubuntu) provides gcc-16 binaries. However I tested the "view" example and unit tests in a local Windows Subsystem for Linux (WSL) environment.

trueqbit added 30 commits June 7, 2024 19:51
`c_str()` expresses the intent better than `data()`
Note: C++ reflection-based approach isn't implemented yet.
* Removed `serializer_context_builder`, which is now entirely replaced by `obtain_db_objects()`.
* Renamed serializer context flag variables.
* Factored out common functionality into common base classes and mixins.
* Renamed structures.
* Split CTE table class into its own structure, effectively decoupling it from a table DBO.
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.
clang/gcc use an intrinsic compiler builtin [`__builtin_offsetof`], which doesn't work the way we need.
@trueqbit trueqbit linked an issue Apr 28, 2026 that may be closed by this pull request
@trueqbit trueqbit marked this pull request as ready for review April 28, 2026 19:38
@trueqbit trueqbit requested a review from fnc12 April 28, 2026 19:38
@trueqbit trueqbit marked this pull request as draft April 30, 2026 20:12
@trueqbit trueqbit marked this pull request as ready for review May 1, 2026 18:01
@trueqbit trueqbit force-pushed the experimental/sql-view branch 3 times, most recently from 2fa0827 to 1285ce1 Compare May 2, 2026 16:10
Comment thread dev/conditions.h
}

/**
* [Deprecation notice] This expression factory function is deprecated and will be removed in v1.11.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to deprecate it cause it is also used in window funcs and it is not going to be deprecated there. So I'd remove [Deprecation notice]

Copy link
Copy Markdown
Collaborator Author

@trueqbit trueqbit May 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order_by(rank()) can only be used with FTS not with Window functions, that's why I added it here (actually moved from the rank() factory).

Comment thread dev/storage_base.h Outdated
Comment thread dev/storage_base.h Outdated
Comment thread dev/storage_base.h Outdated
Comment thread dev/storage_base.h
data_t objectNames;
std::stringstream ss;
ss << "SELECT name FROM sqlite_master WHERE type=" << quote_string_literal(std::string(type));
ss << "SELECT name FROM sqlite_master WHERE type=" << quote_string_literal(std::string(type)) << std::flush;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can make it more thiner in memory consumption: declare std::stringstream inside sub-scope/lambda which returns string and pass string while std::stringstream is already destroyed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but then the function needs an additional std::string variable, whose size is an additional 40 bytes (with msvc).

Comment thread dev/storage_base.h
Comment thread examples/view.cpp
Comment thread examples/view.cpp
Comment thread examples/view.cpp
cout << endl;

cout << "Example completed successfully!" << endl;
} catch (const std::system_error& e) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feature is definitely GOAT

Comment thread tests/schema/view_tests.cpp Outdated
@fnc12 fnc12 added the goat label May 2, 2026
@trueqbit trueqbit force-pushed the experimental/sql-view branch from 1285ce1 to 3580f15 Compare May 2, 2026 16:55
@trueqbit trueqbit force-pushed the experimental/sql-view branch from 3580f15 to 1ced217 Compare May 2, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using views

2 participants