From c199e756dc550c3e267661060bb2ebc445c56abe Mon Sep 17 00:00:00 2001 From: vaijns Date: Tue, 17 Mar 2026 22:03:20 +0100 Subject: [PATCH] Add user-defined literal to create a nametag provider from a string literal, usable as normal parameter to .as() --- docs/names.md | 6 ++++++ include/sqlpp23/core/name/create_reflection_name_tag.h | 8 ++++++++ tests/core/meta/alias.cpp | 6 ++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/names.md b/docs/names.md index 1322d1b2..5e6084e9 100644 --- a/docs/names.md +++ b/docs/names.md @@ -98,6 +98,12 @@ When used with C++26 and reflection support, the library offers another overload // rename entities. foo.as<"left">(); // tab_foo AS left max(left.id).as<"max_id">(); // MAX(left.id) AS max_id + +// alternatively you can use the user-defined literal "_alias" instead of template arguments. +using namespace sqlpp::literals; // using namespace required in scope + +foo.as("left"_alias); // tab_foo AS left +max(left.id).as("max_id"_alias); // MAX(left.id) AS max_id ``` **Experimental** diff --git a/include/sqlpp23/core/name/create_reflection_name_tag.h b/include/sqlpp23/core/name/create_reflection_name_tag.h index 80bcd157..d0d37101 100644 --- a/include/sqlpp23/core/name/create_reflection_name_tag.h +++ b/include/sqlpp23/core/name/create_reflection_name_tag.h @@ -93,4 +93,12 @@ consteval auto make_alias() -> ::sqlpp::meta::reflection_alias { } } // namespace sqlpp::meta + +namespace sqlpp::literals { +template <::sqlpp::detail::fixed_string Alias> +consteval auto operator""_alias() -> ::sqlpp::meta::reflection_alias { + return {}; +} + +} // namespace sqlpp::literals #endif diff --git a/tests/core/meta/alias.cpp b/tests/core/meta/alias.cpp index 8e76602c..e74657c8 100644 --- a/tests/core/meta/alias.cpp +++ b/tests/core/meta/alias.cpp @@ -28,6 +28,8 @@ int main(int, char*[]) { #if SQLPP_INCLUDE_REFLECTION == 1 + using namespace sqlpp::literals; + const auto foo = test::TabFoo{}; const auto bar = test::TabBar{}; @@ -35,10 +37,10 @@ int main(int, char*[]) { "AVG(tab_bar.id + 7) AS my_average"); const auto table_a = foo.as<"table_a">(); - const auto table_b = bar.as<"table_b">(); + const auto table_b = bar.as("table_b"_alias); SQLPP_COMPARE(sqlpp::select(table_a.id.as<"id_a">(), table_a.intN, - table_b.id.as<"id_b">(), table_b.textN) + table_b.id.as("id_b"_alias), table_b.textN) .from(table_a.join(table_b).on(table_a.id == table_b.id)), "SELECT table_a.id AS id_a, table_a.int_n, table_b.id AS id_b, " "table_b.text_n "