Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix auto key bug when use REFLECTION_WITH_NAME #136

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ inline int add_auto_key_field(std::string_view key, std::string_view value) {

template <typename T>
inline auto get_auto_key() {
auto it = g_ormpp_auto_key_map.find(iguana::get_name<T>());
using U = decltype(iguana_reflect_members(std::declval<T>()));
auto it = g_ormpp_auto_key_map.find(U::struct_name());
return it == g_ormpp_auto_key_map.end() ? "" : it->second;
}

template <typename T>
inline auto is_auto_key(std::string_view field_name) {
auto it = g_ormpp_auto_key_map.find(iguana::get_name<T>());
using U = decltype(iguana_reflect_members(std::declval<T>()));
auto it = g_ormpp_auto_key_map.find(U::struct_name());
return it == g_ormpp_auto_key_map.end() ? false : it->second == field_name;
}

Expand All @@ -46,7 +48,8 @@ inline int add_conflict_key_field(std::string_view key,

template <typename T>
inline auto get_conflict_key() {
auto key = iguana::get_name<T>();
using U = decltype(iguana_reflect_members(std::declval<T>()));
auto key = U::struct_name();
auto it = g_ormpp_conflict_key_map.find(key);
if (it == g_ormpp_conflict_key_map.end()) {
auto auto_key = g_ormpp_auto_key_map.find(key);
Expand All @@ -57,7 +60,7 @@ inline auto get_conflict_key() {

#define REGISTER_CONFLICT_KEY(STRUCT_NAME, ...) \
inline auto IGUANA_UNIQUE_VARIABLE(STRUCT_NAME) = \
add_conflict_key_field(#STRUCT_NAME, {MAKE_NAMES(__VA_ARGS__)});
ormpp::add_conflict_key_field(#STRUCT_NAME, {MAKE_NAMES(__VA_ARGS__)});

template <typename T>
struct is_optional_v : std::false_type {};
Expand Down
113 changes: 113 additions & 0 deletions tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,119 @@ TEST_CASE("test enum") {
#endif
}

struct test_enum_with_name_t {
Color color;
Fruit fruit;
int id;
};
REGISTER_AUTO_KEY(test_enum_with_name_t, id)
REFLECTION_WITH_NAME(test_enum_with_name_t, "test_enum", id, color, fruit)

TEST_CASE("test enum with custom name") {
#ifdef ORMPP_ENABLE_MYSQL
dbng<mysql> mysql;
if (mysql.connect(ip, username, password, db)) {
mysql.execute("drop table if exists test_enum");
mysql.create_datatable<test_enum_with_name_t>(ormpp_auto_key{"id"});
mysql.insert<test_enum_with_name_t>({Color::BLUE});
auto vec = mysql.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
mysql.update(vec.front());
vec = mysql.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
mysql.update<test_enum_with_name_t>({Color::BLUE, APPLE, 1}, "id=1");
vec = mysql.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
mysql.replace(vec.front());
vec = mysql.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
mysql.delete_records<test_enum_with_name_t>();
vec = mysql.query<test_enum_with_name_t>();
CHECK(vec.size() == 0);
}
#endif
#ifdef ORMPP_ENABLE_PG
dbng<postgresql> postgres;
if (postgres.connect(ip, username, password, db)) {
postgres.execute("drop table if exists test_enum");
postgres.create_datatable<test_enum_with_name_t>(ormpp_auto_key{"id"});
postgres.insert<test_enum_with_name_t>({Color::BLUE});
auto vec = postgres.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
postgres.update(vec.front());
vec = postgres.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
postgres.update<test_enum_with_name_t>({Color::BLUE, APPLE, 1}, "id=1");
vec = postgres.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
postgres.replace(vec.front());
vec = postgres.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
postgres.delete_records<test_enum_with_name_t>();
vec = postgres.query<test_enum_with_name_t>();
CHECK(vec.size() == 0);
}
#endif
#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
if (sqlite.connect(db)) {
sqlite.execute("drop table if exists test_enum");
sqlite.create_datatable<test_enum_with_name_t>(ormpp_auto_key{"id"});
sqlite.insert<test_enum_with_name_t>({Color::BLUE});
auto vec = sqlite.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
sqlite.update(vec.front());
vec = sqlite.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
sqlite.update<test_enum_with_name_t>({Color::BLUE, APPLE, 1}, "id=1");
vec = sqlite.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::BLUE);
CHECK(vec.front().fruit == APPLE);
vec.front().color = Color::RED;
vec.front().fruit = BANANA;
sqlite.replace(vec.front());
vec = sqlite.query<test_enum_with_name_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().color == Color::RED);
CHECK(vec.front().fruit == BANANA);
sqlite.delete_records<test_enum_with_name_t>();
vec = sqlite.query<test_enum_with_name_t>();
CHECK(vec.size() == 0);
}
#endif
}

struct alias {
std::string name;
int id;
Expand Down
Loading