Skip to content

Commit

Permalink
Merge pull request #140 from Jacyking/master
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 4, 2024
2 parents c3ae7b4 + 15bf474 commit 74d1777
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
18 changes: 14 additions & 4 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,13 @@ class mysql {
param.buffer = const_cast<void *>(static_cast<const void *>(&value));
}
else if constexpr (std::is_arithmetic_v<U>) {
param.buffer_type =
(enum_field_types)ormpp_mysql::type_to_id(identity<U>{});
if constexpr (std::is_same_v<bool, U>) {
param.buffer_type = MYSQL_TYPE_TINY;
}
else {
param.buffer_type =
(enum_field_types)ormpp_mysql::type_to_id(identity<U>{});
}
param.buffer = const_cast<void *>(static_cast<const void *>(&value));
}
else if constexpr (std::is_same_v<std::string, U>) {
Expand Down Expand Up @@ -214,8 +219,13 @@ class mysql {
param_bind.buffer = const_cast<void *>(static_cast<const void *>(&value));
}
else if constexpr (std::is_arithmetic_v<U>) {
param_bind.buffer_type =
(enum_field_types)ormpp_mysql::type_to_id(identity<U>{});
if constexpr (std::is_same_v<bool, U>) {
param_bind.buffer_type = MYSQL_TYPE_TINY;
}
else {
param_bind.buffer_type =
(enum_field_types)ormpp_mysql::type_to_id(identity<U>{});
}
param_bind.buffer = const_cast<void *>(static_cast<const void *>(&value));
}
else if constexpr (std::is_same_v<std::string, U>) {
Expand Down
12 changes: 9 additions & 3 deletions include/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <type_traits>

#include "iguana/detail/charconv.h"
#include "utility.hpp"

using namespace std::string_literals;
Expand Down Expand Up @@ -526,17 +527,22 @@ class postgresql {
}
else if constexpr (std::is_enum_v<U> && !iguana::is_int64_v<U>) {
std::vector<char> temp(20, 0);
itoa_fwd(static_cast<int>(value), temp.data());
iguana::detail::to_chars(temp.data(), static_cast<int>(value));
param_values.push_back(std::move(temp));
}
else if constexpr (std::is_integral_v<U> && !iguana::is_int64_v<U>) {
std::vector<char> temp(20, 0);
itoa_fwd(value, temp.data());
if constexpr (iguana::is_char_type<U>::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<U>) {
std::vector<char> temp(65, 0);
xtoa(value, temp.data(), 10, std::is_signed_v<U>);
iguana::detail::to_chars(temp.data(), value);
param_values.push_back(std::move(temp));
}
else if constexpr (std::is_floating_point_v<U>) {
Expand Down
3 changes: 3 additions & 0 deletions include/type_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ inline std::string id_to_type(
return res;
}

inline constexpr auto type_to_name(identity<bool>) noexcept {
return "BOOLEAN"sv;
}
inline constexpr auto type_to_name(identity<char>) noexcept {
return "TINYINT"sv;
}
Expand Down
58 changes: 58 additions & 0 deletions tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> mysql;
if (mysql.connect(ip, username, password, db)) {
mysql.execute("drop table if exists test_bool_t");
mysql.create_datatable<test_bool_t>(ormpp_auto_key{"id"});
mysql.insert(test_bool_t{true});
auto vec = mysql.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == true);
mysql.delete_records<test_bool_t>();
mysql.insert(test_bool_t{false});
vec = mysql.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == false);
}
#endif
#ifdef ORMPP_ENABLE_PG
dbng<postgresql> postgres;
if (postgres.connect(ip, username, password, db)) {
postgres.execute("drop table if exists test_bool_t");
postgres.create_datatable<test_bool_t>(ormpp_auto_key{"id"});
postgres.insert(test_bool_t{true});
auto vec = postgres.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == true);
postgres.delete_records<test_bool_t>();
postgres.insert(test_bool_t{false});
vec = postgres.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == false);
}
#endif
#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
if (sqlite.connect(db)) {
sqlite.execute("drop table if exists test_bool_t");
sqlite.create_datatable<test_bool_t>(ormpp_auto_key{"id"});
sqlite.insert(test_bool_t{true});
auto vec = sqlite.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == true);
sqlite.delete_records<test_bool_t>();
sqlite.insert(test_bool_t{false});
vec = sqlite.query<test_bool_t>();
CHECK(vec.size() == 1);
CHECK(vec.front().ok == false);
}
#endif
}

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

0 comments on commit 74d1777

Please sign in to comment.