Skip to content

Commit d705375

Browse files
committed
Added string literal operator template for the query view name annotation
1 parent 3e662e0 commit d705375

10 files changed

Lines changed: 73 additions & 34 deletions

File tree

dev/schema/dbo_name.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,27 @@ namespace sqlite_orm::internal {
7070
}
7171

7272
SQLITE_ORM_EXPORT namespace sqlite_orm {
73+
inline namespace literals {
74+
/**
75+
* Database object name annotation factory.
76+
* Use as a class-scope annotation:
77+
* `struct [[="users"_dbo_name]] User { ... };`
78+
* `struct [[= sqlite_orm::operator""_dbo_name<"users">()]] User { ... };`
79+
* `make_view<T>()` consumes this annotation.
80+
*/
81+
template<internal::dbo_name_literal dboName>
82+
[[nodiscard]] consteval auto operator""_dbo_name() {
83+
return dboName;
84+
}
85+
}
86+
7387
/**
7488
* Database object name annotation factory.
7589
* Use as a class-scope annotation: `struct [[=dbo_name("users")]] User { ... };`.
76-
* Both `make_table<T>()` and `make_view<T>()` consume this annotation; when absent
77-
* the name falls back to `T`'s reflected identifier.
90+
* `make_view<T>()` consumes this annotation.
7891
*/
7992
template<size_t N>
80-
constexpr internal::dbo_name_literal<N> dbo_name(const char (&dboName)[N]) {
93+
consteval internal::dbo_name_literal<N> dbo_name(const char (&dboName)[N]) {
8194
return {dboName};
8295
}
8396
}

dev/schema/view.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ namespace sqlite_orm::internal {
3333
};
3434

3535
template<class T>
36-
inline constexpr bool is_view_v = polyfill::is_specialization_of_v<T, query_view>;
36+
constexpr bool is_view_v = polyfill::is_specialization_of_v<T, query_view>;
3737
#else
3838
template<class T>
39-
inline constexpr bool is_view_v = false;
39+
constexpr bool is_view_v = false;
4040
#endif
4141

4242
template<class T>
@@ -71,7 +71,7 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
7171
* Factory function for a view definition.
7272
*
7373
* The mapped object type is explicitly specified, columns and their names are deferred from the object type.
74-
* The object type must be an aggregate. The optional `[[=dbo_name("…")]]` class-scope annotation overrides
74+
* The object type must be an aggregate. The optional `[[="…"_dbo_name]]` class-scope annotation overrides
7575
* the view name (otherwise the type's reflected identifier is used).
7676
*/
7777
template<class O, class Select>
@@ -90,7 +90,7 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
9090
* Factory function for a view definition.
9191
*
9292
* The mapped object type is explicitly specified, columns and their names are deferred from the object type.
93-
* The object type must be an aggregate. The optional `[[=dbo_name("…")]]` class-scope annotation overrides
93+
* The object type must be an aggregate. The optional `[[="…"_dbo_name]]` class-scope annotation overrides
9494
* the view name (otherwise the type's reflected identifier is used).
9595
*/
9696
template<orm_table_reference auto table, class Select>

examples/view.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ struct Department {
4545
// The fields are automatically mapped through C++ reflection
4646

4747
// View 1: High earners (employees earning more than 60000)
48-
struct[[= dbo_name("high_earners")]] HighEarner {
48+
struct[[= "high_earners"_dbo_name]] HighEarner {
4949
int64 id;
5050
std::string name;
5151
double salary;
5252
};
5353

5454
// View 2: Department summary with employee count and average salary
55-
struct[[= dbo_name("department_summary")]] DepartmentSummary {
55+
struct[[= "department_summary"_dbo_name]] DepartmentSummary {
5656
std::string department_name;
5757
int employee_count;
5858
double avg_salary;
5959
};
6060

6161
// View 3: Complete employee information with department details (join result)
62-
struct[[= dbo_name("employee_details")]] EmployeeDetail {
62+
struct[[= "employee_details"_dbo_name]] EmployeeDetail {
6363
int64 id;
6464
std::string employee_name;
6565
double salary;
@@ -84,7 +84,7 @@ inline auto initStorage(const std::string& path) {
8484
// Define views - notice how we only specify the SELECT statement.
8585
// The column mappings and view name are derived from the view object type
8686
// (column names from non-static data members; view name from the optional
87-
// `[[=dbo_name("…")]]` annotation, falling back to the type's identifier).
87+
// `[[="…"_dbo_name]]` annotation, falling back to the type's identifier).
8888

8989
// View 1: Filter high earners
9090
make_view<HighEarner>(

include/sqlite_orm/sqlite_orm.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13953,14 +13953,27 @@ namespace sqlite_orm::internal {
1395313953
}
1395413954

1395513955
SQLITE_ORM_EXPORT namespace sqlite_orm {
13956+
inline namespace literals {
13957+
/**
13958+
* Database object name annotation factory.
13959+
* Use as a class-scope annotation:
13960+
* `struct [[="users"_dbo_name]] User { ... };`
13961+
* `struct [[= sqlite_orm::operator""_dbo_name<"users">()]] User { ... };`
13962+
* `make_view<T>()` consumes this annotation.
13963+
*/
13964+
template<internal::dbo_name_literal dboName>
13965+
[[nodiscard]] consteval auto operator""_dbo_name() {
13966+
return dboName;
13967+
}
13968+
}
13969+
1395613970
/**
1395713971
* Database object name annotation factory.
1395813972
* Use as a class-scope annotation: `struct [[=dbo_name("users")]] User { ... };`.
13959-
* Both `make_table<T>()` and `make_view<T>()` consume this annotation; when absent
13960-
* the name falls back to `T`'s reflected identifier.
13973+
* `make_view<T>()` consumes this annotation.
1396113974
*/
1396213975
template<size_t N>
13963-
constexpr internal::dbo_name_literal<N> dbo_name(const char (&dboName)[N]) {
13976+
consteval internal::dbo_name_literal<N> dbo_name(const char (&dboName)[N]) {
1396413977
return {dboName};
1396513978
}
1396613979
}
@@ -13982,10 +13995,10 @@ namespace sqlite_orm::internal {
1398213995
};
1398313996

1398413997
template<class T>
13985-
inline constexpr bool is_view_v = polyfill::is_specialization_of_v<T, query_view>;
13998+
constexpr bool is_view_v = polyfill::is_specialization_of_v<T, query_view>;
1398613999
#else
1398714000
template<class T>
13988-
inline constexpr bool is_view_v = false;
14001+
constexpr bool is_view_v = false;
1398914002
#endif
1399014003

1399114004
template<class T>
@@ -14020,7 +14033,7 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
1402014033
* Factory function for a view definition.
1402114034
*
1402214035
* The mapped object type is explicitly specified, columns and their names are deferred from the object type.
14023-
* The object type must be an aggregate. The optional `[[=dbo_name("…")]]` class-scope annotation overrides
14036+
* The object type must be an aggregate. The optional `[[="…"_dbo_name]]` class-scope annotation overrides
1402414037
* the view name (otherwise the type's reflected identifier is used).
1402514038
*/
1402614039
template<class O, class Select>
@@ -14039,7 +14052,7 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
1403914052
* Factory function for a view definition.
1404014053
*
1404114054
* The mapped object type is explicitly specified, columns and their names are deferred from the object type.
14042-
* The object type must be an aggregate. The optional `[[=dbo_name("…")]]` class-scope annotation overrides
14055+
* The object type must be an aggregate. The optional `[[="…"_dbo_name]]` class-scope annotation overrides
1404314056
* the view name (otherwise the type's reflected identifier is used).
1404414057
*/
1404514058
template<orm_table_reference auto table, class Select>

tests/logger_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace {
2626

2727
#ifdef SQLITE_ORM_WITH_VIEW
2828
#ifdef SQLITE_ORM_REFLECTION_SUPPORTED
29-
struct[[= dbo_name("users_view")]] UserViewLoggerTests {
29+
struct[[= "users_view"_dbo_name]] UserViewLoggerTests {
3030
int id = 0;
3131
std::string name;
3232
};

tests/schema/view_tests.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
using namespace sqlite_orm;
77

88
namespace {
9-
struct[[= dbo_name("user_view")]] UserViewSchemaTests {
9+
struct[[= "user_view"_dbo_name]] UserViewSchemaTests {
10+
int64 id = 0;
11+
std::string name;
12+
};
13+
14+
struct[[= dbo_name("user_view")]] UserViewSchemaTests2 {
1015
int64 id = 0;
1116
std::string name;
1217
};
@@ -23,11 +28,16 @@ TEST_CASE("view make_view name resolution") {
2328
std::string name;
2429
};
2530

26-
SECTION("[[=dbo_name(...)]] annotation supplies the view name") {
31+
SECTION("annotation supplies the view name") {
2732
auto view = make_view<UserViewSchemaTests>(select(asterisk<User>()));
2833
REQUIRE(view.name == "user_view");
2934
}
3035

36+
SECTION("annotation supplies the view name 2") {
37+
auto view = make_view<UserViewSchemaTests2>(select(asterisk<User>()));
38+
REQUIRE(view.name == "user_view");
39+
}
40+
3141
SECTION("fallback to type identifier") {
3242
auto view = make_view<UserViewSchemaTestsDefaultName>(select(asterisk<User>()));
3343
REQUIRE(view.name == "UserViewSchemaTestsDefaultName");

tests/statement_serializer_tests/schema/view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using namespace sqlite_orm;
77
using internal::serialize;
88

99
namespace {
10-
struct[[= dbo_name("user_view")]] UserViewSerializerTests {
10+
struct[[= "user_view"_dbo_name]] UserViewSerializerTests {
1111
int id = 0;
1212
std::string name;
1313
};

tests/static_tests/view_static_tests.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ using internal::col_index_sequence_of, internal::col_index_sequence_with_field_t
88
using internal::is_column;
99

1010
namespace {
11-
struct[[= dbo_name("user_view")]] UserViewStaticTests {
12-
int id = 0;
11+
struct[[= "user_view"_dbo_name]] UserViewStaticTests {
12+
int64 id = 0;
1313
std::string name;
14+
15+
private:
16+
std::string _privateDummy;
1417
};
1518
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
1619
constexpr orm_table_reference auto user_view = c<UserViewStaticTests>();
@@ -19,7 +22,7 @@ namespace {
1922

2023
TEST_CASE("view static count_of<is_column>()") {
2124
struct User {
22-
int id = 0;
25+
int64 id = 0;
2326
std::string name;
2427
};
2528

@@ -28,7 +31,7 @@ TEST_CASE("view static count_of<is_column>()") {
2831
using elements_type = decltype(view.elements);
2932
STATIC_REQUIRE(view.count_of<is_column>() == 2);
3033
STATIC_REQUIRE(col_index_sequence_of<elements_type>::size() == 2);
31-
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int>::size() == 1);
34+
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int64>::size() == 1);
3235
}
3336
SECTION("derived") {
3437
struct DerivedUserView : UserViewStaticTests {};
@@ -37,15 +40,15 @@ TEST_CASE("view static count_of<is_column>()") {
3740
using elements_type = decltype(view.elements);
3841
STATIC_REQUIRE(view.count_of<is_column>() == 2);
3942
STATIC_REQUIRE(col_index_sequence_of<elements_type>::size() == 2);
40-
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int>::size() == 1);
43+
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int64>::size() == 1);
4144
}
4245
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
4346
SECTION("table reference") {
4447
auto view = make_view<user_view>(select(asterisk<User>()));
4548
using elements_type = decltype(view.elements);
4649
STATIC_REQUIRE(view.count_of<is_column>() == 2);
4750
STATIC_REQUIRE(col_index_sequence_of<elements_type>::size() == 2);
48-
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int>::size() == 1);
51+
STATIC_REQUIRE(col_index_sequence_with_field_type<elements_type, int64>::size() == 1);
4952
}
5053
#endif
5154
}

tests/storage_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ namespace {
271271
constexpr char usersViewName[] = "users_view";
272272

273273
struct[[= dbo_name(usersViewName)]] UserViewDropViewTests {
274-
int id = 0;
274+
int64 id = 0;
275275
std::string name;
276276
};
277277
}

tests/view_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using namespace sqlite_orm;
88

99
namespace {
10-
struct[[= dbo_name("user_view")]] UserViewTests {
11-
int id = 0;
10+
struct[[= "user_view"_dbo_name]] UserViewTests {
11+
int64 id = 0;
1212
std::string name;
1313

1414
#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
@@ -20,7 +20,7 @@ namespace {
2020
#endif
2121
};
2222

23-
struct[[= dbo_name("user_view")]] UserView2Tests {
23+
struct[[= "user_view"_dbo_name]] UserView2Tests {
2424
std::string name;
2525

2626
#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
@@ -37,7 +37,7 @@ TEST_CASE("view") {
3737
using Catch::Matchers::UnorderedEquals;
3838

3939
struct User {
40-
int id = 0;
40+
int64 id = 0;
4141
std::string name;
4242
};
4343

@@ -94,7 +94,7 @@ TEST_CASE("view") {
9494

9595
TEST_CASE("view sync") {
9696
struct User {
97-
int id = 0;
97+
int64 id = 0;
9898
std::string name;
9999
};
100100

0 commit comments

Comments
 (0)