From e868539c9c3313d6fd678ba94a3f3fadf05ef931 Mon Sep 17 00:00:00 2001 From: klaus triendl Date: Tue, 24 Oct 2023 20:08:39 +0200 Subject: [PATCH] Update check now also works with auto-incrementable primary keys --- dev/constraints.h | 24 +++++++---------- dev/statement_serializer.h | 8 +++--- include/sqlite_orm/sqlite_orm.h | 32 ++++++++++------------- tests/static_tests/table_static_tests.cpp | 9 +++++++ 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/dev/constraints.h b/dev/constraints.h index e59bb42a9..211ac2751 100644 --- a/dev/constraints.h +++ b/dev/constraints.h @@ -44,12 +44,15 @@ namespace sqlite_orm { }; template - struct primary_key_with_autoincrement { + struct primary_key_with_autoincrement : T { using primary_key_type = T; - primary_key_type primary_key; - - primary_key_with_autoincrement(primary_key_type primary_key_) : primary_key(primary_key_) {} + const primary_key_type& as_base() const { + return *this; + } +#ifndef SQLITE_ORM_AGGREGATE_BASES_SUPPORTED + primary_key_with_autoincrement(T primary_key) : T{primary_key} {} +#endif }; /** @@ -65,7 +68,7 @@ namespace sqlite_orm { columns_tuple columns; - primary_key_t(decltype(columns) columns) : columns(std::move(columns)) {} + primary_key_t(columns_tuple columns) : columns(std::move(columns)) {} self asc() const { auto res = *this; @@ -433,16 +436,10 @@ namespace sqlite_orm { using is_foreign_key = polyfill::bool_constant>; template - struct is_primary_key : std::false_type {}; - - template - struct is_primary_key> : std::true_type {}; - - template - struct is_primary_key> : std::true_type {}; + SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = std::is_base_of::value; template - SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = is_primary_key::value; + using is_primary_key = polyfill::bool_constant>; template SQLITE_ORM_INLINE_VAR constexpr bool is_generated_always_v = @@ -477,7 +474,6 @@ namespace sqlite_orm { check_if_is_template, check_if_is_template, check_if_is_template, - check_if_is_template, check_if_is_type, check_if>, T>; diff --git a/dev/statement_serializer.h b/dev/statement_serializer.h index 3dad4c474..ddaad6feb 100644 --- a/dev/statement_serializer.h +++ b/dev/statement_serializer.h @@ -907,13 +907,13 @@ namespace sqlite_orm { } }; - template - struct statement_serializer, void> { - using statement_type = primary_key_with_autoincrement; + template + struct statement_serializer, void> { + using statement_type = primary_key_with_autoincrement; template std::string operator()(const statement_type& statement, const Ctx& context) const { - return serialize(statement.primary_key, context) + " AUTOINCREMENT"; + return serialize(statement.as_base(), context) + " AUTOINCREMENT"; } }; diff --git a/include/sqlite_orm/sqlite_orm.h b/include/sqlite_orm/sqlite_orm.h index 691d67cab..dc2ed46d0 100644 --- a/include/sqlite_orm/sqlite_orm.h +++ b/include/sqlite_orm/sqlite_orm.h @@ -1351,12 +1351,15 @@ namespace sqlite_orm { }; template - struct primary_key_with_autoincrement { + struct primary_key_with_autoincrement : T { using primary_key_type = T; - primary_key_type primary_key; - - primary_key_with_autoincrement(primary_key_type primary_key_) : primary_key(primary_key_) {} + const primary_key_type& as_base() const { + return *this; + } +#ifndef SQLITE_ORM_AGGREGATE_BASES_SUPPORTED + primary_key_with_autoincrement(T primary_key) : T{primary_key} {} +#endif }; /** @@ -1372,7 +1375,7 @@ namespace sqlite_orm { columns_tuple columns; - primary_key_t(decltype(columns) columns) : columns(std::move(columns)) {} + primary_key_t(columns_tuple columns) : columns(std::move(columns)) {} self asc() const { auto res = *this; @@ -1740,16 +1743,10 @@ namespace sqlite_orm { using is_foreign_key = polyfill::bool_constant>; template - struct is_primary_key : std::false_type {}; - - template - struct is_primary_key> : std::true_type {}; + SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = std::is_base_of::value; template - struct is_primary_key> : std::true_type {}; - - template - SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = is_primary_key::value; + using is_primary_key = polyfill::bool_constant>; template SQLITE_ORM_INLINE_VAR constexpr bool is_generated_always_v = @@ -1784,7 +1781,6 @@ namespace sqlite_orm { check_if_is_template, check_if_is_template, check_if_is_template, - check_if_is_template, check_if_is_type, check_if>, T>; @@ -16692,13 +16688,13 @@ namespace sqlite_orm { } }; - template - struct statement_serializer, void> { - using statement_type = primary_key_with_autoincrement; + template + struct statement_serializer, void> { + using statement_type = primary_key_with_autoincrement; template std::string operator()(const statement_type& statement, const Ctx& context) const { - return serialize(statement.primary_key, context) + " AUTOINCREMENT"; + return serialize(statement.as_base(), context) + " AUTOINCREMENT"; } }; diff --git a/tests/static_tests/table_static_tests.cpp b/tests/static_tests/table_static_tests.cpp index e96895de0..ff782f178 100644 --- a/tests/static_tests/table_static_tests.cpp +++ b/tests/static_tests/table_static_tests.cpp @@ -52,6 +52,15 @@ TEST_CASE("table static count_of()") { STATIC_REQUIRE(table.count_of_columns_with() == 0); #if defined(SQLITE_ORM_FOLD_EXPRESSIONS_SUPPORTED) STATIC_REQUIRE(dedicated_pk_columns_count_t::value == 1); +#endif + } + { // 1 column with 1 dedicated pk autoincrement + auto table = make_table("users", make_column("id", &User::id), primary_key(&User::id).autoincrement()); + STATIC_REQUIRE(table.count_of() == 1); + STATIC_REQUIRE(table.count_of() == 1); + STATIC_REQUIRE(table.count_of_columns_with() == 0); +#if defined(SQLITE_ORM_FOLD_EXPRESSIONS_SUPPORTED) + STATIC_REQUIRE(dedicated_pk_columns_count_t::value == 1); #endif } { // 2 columns no pk