Skip to content

Commit

Permalink
Update check now also works with auto-incrementable primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
trueqbit committed Oct 24, 2023
1 parent 55b7be6 commit e868539
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
24 changes: 10 additions & 14 deletions dev/constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ namespace sqlite_orm {
};

template<class T>
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
};

/**
Expand All @@ -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;
Expand Down Expand Up @@ -433,16 +436,10 @@ namespace sqlite_orm {
using is_foreign_key = polyfill::bool_constant<is_foreign_key_v<T>>;

template<class T>
struct is_primary_key : std::false_type {};

template<class... Cs>
struct is_primary_key<primary_key_t<Cs...>> : std::true_type {};

template<class T>
struct is_primary_key<primary_key_with_autoincrement<T>> : std::true_type {};
SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = std::is_base_of<primary_key_base, T>::value;

template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = is_primary_key<T>::value;
using is_primary_key = polyfill::bool_constant<is_primary_key_v<T>>;

template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_generated_always_v =
Expand Down Expand Up @@ -477,7 +474,6 @@ namespace sqlite_orm {
check_if_is_template<unique_t>,
check_if_is_template<default_t>,
check_if_is_template<check_t>,
check_if_is_template<primary_key_with_autoincrement>,
check_if_is_type<collate_constraint_t>,
check_if<is_generated_always>>,
T>;
Expand Down
8 changes: 4 additions & 4 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,13 @@ namespace sqlite_orm {
}
};

template<class T>
struct statement_serializer<primary_key_with_autoincrement<T>, void> {
using statement_type = primary_key_with_autoincrement<T>;
template<class PK>
struct statement_serializer<primary_key_with_autoincrement<PK>, void> {
using statement_type = primary_key_with_autoincrement<PK>;

template<class Ctx>
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";
}
};

Expand Down
32 changes: 14 additions & 18 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1351,12 +1351,15 @@ namespace sqlite_orm {
};

template<class T>
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
};

/**
Expand All @@ -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;
Expand Down Expand Up @@ -1740,16 +1743,10 @@ namespace sqlite_orm {
using is_foreign_key = polyfill::bool_constant<is_foreign_key_v<T>>;

template<class T>
struct is_primary_key : std::false_type {};

template<class... Cs>
struct is_primary_key<primary_key_t<Cs...>> : std::true_type {};
SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = std::is_base_of<primary_key_base, T>::value;

template<class T>
struct is_primary_key<primary_key_with_autoincrement<T>> : std::true_type {};

template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_primary_key_v = is_primary_key<T>::value;
using is_primary_key = polyfill::bool_constant<is_primary_key_v<T>>;

template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_generated_always_v =
Expand Down Expand Up @@ -1784,7 +1781,6 @@ namespace sqlite_orm {
check_if_is_template<unique_t>,
check_if_is_template<default_t>,
check_if_is_template<check_t>,
check_if_is_template<primary_key_with_autoincrement>,
check_if_is_type<collate_constraint_t>,
check_if<is_generated_always>>,
T>;
Expand Down Expand Up @@ -16692,13 +16688,13 @@ namespace sqlite_orm {
}
};

template<class T>
struct statement_serializer<primary_key_with_autoincrement<T>, void> {
using statement_type = primary_key_with_autoincrement<T>;
template<class PK>
struct statement_serializer<primary_key_with_autoincrement<PK>, void> {
using statement_type = primary_key_with_autoincrement<PK>;

template<class Ctx>
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";
}
};

Expand Down
9 changes: 9 additions & 0 deletions tests/static_tests/table_static_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ TEST_CASE("table static count_of<is_column>()") {
STATIC_REQUIRE(table.count_of_columns_with<is_primary_key>() == 0);
#if defined(SQLITE_ORM_FOLD_EXPRESSIONS_SUPPORTED)
STATIC_REQUIRE(dedicated_pk_columns_count_t<decltype(table.elements)>::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<is_column>() == 1);
STATIC_REQUIRE(table.count_of<is_primary_key>() == 1);
STATIC_REQUIRE(table.count_of_columns_with<is_primary_key>() == 0);
#if defined(SQLITE_ORM_FOLD_EXPRESSIONS_SUPPORTED)
STATIC_REQUIRE(dedicated_pk_columns_count_t<decltype(table.elements)>::value == 1);
#endif
}
{ // 2 columns no pk
Expand Down

0 comments on commit e868539

Please sign in to comment.