diff --git a/include/mysql.hpp b/include/mysql.hpp index e39a3d08..80f47f33 100644 --- a/include/mysql.hpp +++ b/include/mysql.hpp @@ -177,8 +177,13 @@ class mysql { param.buffer = const_cast(static_cast(&value)); } else if constexpr (std::is_arithmetic_v) { - param.buffer_type = - (enum_field_types)ormpp_mysql::type_to_id(identity{}); + if constexpr (std::is_same_v) { + param.buffer_type = MYSQL_TYPE_TINY; + } + else { + param.buffer_type = + (enum_field_types)ormpp_mysql::type_to_id(identity{}); + } param.buffer = const_cast(static_cast(&value)); } else if constexpr (std::is_same_v) { @@ -214,8 +219,13 @@ class mysql { param_bind.buffer = const_cast(static_cast(&value)); } else if constexpr (std::is_arithmetic_v) { - param_bind.buffer_type = - (enum_field_types)ormpp_mysql::type_to_id(identity{}); + if constexpr (std::is_same_v) { + param_bind.buffer_type = MYSQL_TYPE_TINY; + } + else { + param_bind.buffer_type = + (enum_field_types)ormpp_mysql::type_to_id(identity{}); + } param_bind.buffer = const_cast(static_cast(&value)); } else if constexpr (std::is_same_v) { diff --git a/include/postgresql.hpp b/include/postgresql.hpp index 832f01d1..e5514d8b 100644 --- a/include/postgresql.hpp +++ b/include/postgresql.hpp @@ -11,6 +11,7 @@ #include #include +#include "iguana/detail/charconv.h" #include "utility.hpp" using namespace std::string_literals; @@ -526,17 +527,22 @@ class postgresql { } else if constexpr (std::is_enum_v && !iguana::is_int64_v) { std::vector temp(20, 0); - itoa_fwd(static_cast(value), temp.data()); + iguana::detail::to_chars(temp.data(), static_cast(value)); param_values.push_back(std::move(temp)); } else if constexpr (std::is_integral_v && !iguana::is_int64_v) { std::vector temp(20, 0); - itoa_fwd(value, temp.data()); + if constexpr (iguana::is_char_type::value) { + itoa_fwd(value, temp.data()); + } + else { + iguana::detail::to_chars(temp.data(), value); + } param_values.push_back(std::move(temp)); } else if constexpr (iguana::is_int64_v) { std::vector temp(65, 0); - xtoa(value, temp.data(), 10, std::is_signed_v); + iguana::detail::to_chars(temp.data(), value); param_values.push_back(std::move(temp)); } else if constexpr (std::is_floating_point_v) { diff --git a/include/type_mapping.hpp b/include/type_mapping.hpp index fe6f717d..76995718 100644 --- a/include/type_mapping.hpp +++ b/include/type_mapping.hpp @@ -50,6 +50,9 @@ inline std::string id_to_type( return res; } +inline constexpr auto type_to_name(identity) noexcept { + return "BOOLEAN"sv; +} inline constexpr auto type_to_name(identity) noexcept { return "TINYINT"sv; } diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index 7626b70e..a365b008 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -1568,6 +1568,64 @@ TEST_CASE("test enum with custom name") { #endif } +struct test_bool_t { + bool ok; + int id; +}; +REGISTER_AUTO_KEY(test_bool_t, id) +REFLECTION(test_bool_t, id, ok) + +TEST_CASE("test bool") { +#ifdef ORMPP_ENABLE_MYSQL + dbng mysql; + if (mysql.connect(ip, username, password, db)) { + mysql.execute("drop table if exists test_bool_t"); + mysql.create_datatable(ormpp_auto_key{"id"}); + mysql.insert(test_bool_t{true}); + auto vec = mysql.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == true); + mysql.delete_records(); + mysql.insert(test_bool_t{false}); + vec = mysql.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == false); + } +#endif +#ifdef ORMPP_ENABLE_PG + dbng postgres; + if (postgres.connect(ip, username, password, db)) { + postgres.execute("drop table if exists test_bool_t"); + postgres.create_datatable(ormpp_auto_key{"id"}); + postgres.insert(test_bool_t{true}); + auto vec = postgres.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == true); + postgres.delete_records(); + postgres.insert(test_bool_t{false}); + vec = postgres.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == false); + } +#endif +#ifdef ORMPP_ENABLE_SQLITE3 + dbng sqlite; + if (sqlite.connect(db)) { + sqlite.execute("drop table if exists test_bool_t"); + sqlite.create_datatable(ormpp_auto_key{"id"}); + sqlite.insert(test_bool_t{true}); + auto vec = sqlite.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == true); + sqlite.delete_records(); + sqlite.insert(test_bool_t{false}); + vec = sqlite.query(); + CHECK(vec.size() == 1); + CHECK(vec.front().ok == false); + } +#endif +} + struct alias { std::string name; int id;