Skip to content

Commit

Permalink
fix null
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Aug 23, 2023
1 parent 53ad6ef commit 9328562
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
27 changes: 18 additions & 9 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ class mysql {

template <typename T>
void set_param_bind(MYSQL_BIND &param_bind, T &&value, int i,
std::map<size_t, std::vector<char>> &mp) {
std::map<size_t, std::vector<char>> &mp, bool &is_null) {
using U = std::remove_const_t<std::remove_reference_t<T>>;
if constexpr (is_optional_v<U>::value) {
return set_param_bind(param_bind, *value, i, mp);
return set_param_bind(param_bind, *value, i, mp, is_null);
}
else if constexpr (std::is_arithmetic_v<U>) {
param_bind.buffer_type =
Expand Down Expand Up @@ -377,6 +377,7 @@ class mysql {
param_bind.buffer = &(mp.rbegin()->second[0]);
param_bind.buffer_length = 65536;
}
param_bind.is_null = &is_null;
}

template <typename T>
Expand Down Expand Up @@ -436,12 +437,13 @@ class mysql {

std::array<MYSQL_BIND, SIZE> param_binds = {};
std::map<size_t, std::vector<char>> mp;
std::array<bool, SIZE> nulls = {};

std::vector<T> v;
T t{};
int index = 0;
iguana::for_each(t, [&](auto item, auto i) {
set_param_bind(param_binds[index], t.*item, index, mp);
iguana::for_each(t, [&](auto item, auto /*i*/) {
set_param_bind(param_binds[index], t.*item, index, mp, nulls[index]);
index++;
});

Expand Down Expand Up @@ -471,13 +473,20 @@ class mysql {
p.second.assign(p.second.size(), 0);
}

v.push_back(std::move(t));
iguana::for_each(t, [&mp, &t](auto item, auto /*i*/) {
using U = std::remove_reference_t<decltype(std::declval<T>().*item)>;
if constexpr (std::is_arithmetic_v<U>) {
memset(&(t.*item), 0, sizeof(U));
iguana::for_each(t, [&nulls, &t](auto item, auto i) {
constexpr auto Idx = decltype(i)::value;
if (nulls.at(Idx)) {
using U = std::remove_reference_t<decltype(std::declval<T>().*item)>;
if constexpr (is_optional_v<U>::value) {
t.*item = {};
}
else if constexpr (std::is_arithmetic_v<U>) {
t.*item = {};
}
}
});

v.push_back(std::move(t));
}

return v;
Expand Down
7 changes: 6 additions & 1 deletion tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ struct test_optional {
int id;
std::optional<std::string> name;
std::optional<int> age;
std::optional<int> empty;
};
REFLECTION(test_optional, id, name, age);

TEST_CASE("test_optional") {
TEST_CASE("test optional") {
#ifdef ORMPP_ENABLE_MYSQL
dbng<mysql> mysql;
if (mysql.connect(ip, "root", password, db)) {
Expand All @@ -104,9 +105,11 @@ TEST_CASE("test_optional") {
REQUIRE(v1.size() > 0);
CHECK(v1.front().age.value() == 200);
CHECK(v1.front().name.value() == "purecpp");
CHECK(v1.front().empty.has_value() == false);
REQUIRE(v2.size() > 0);
CHECK(v2.front().age.value() == 200);
CHECK(v2.front().name.value() == "purecpp");
CHECK(v2.front().empty.has_value() == false);
}
#endif
#ifdef ORMPP_ENABLE_SQLITE3
Expand All @@ -120,9 +123,11 @@ TEST_CASE("test_optional") {
REQUIRE(v1.size() > 0);
CHECK(v1.front().age.value() == 200);
CHECK(v1.front().name.value() == "purecpp");
CHECK(v1.front().empty.has_value() == false);
REQUIRE(v2.size() > 0);
CHECK(v2.front().age.value() == 200);
CHECK(v2.front().name.value() == "purecpp");
CHECK(v2.front().empty.has_value() == false);
}
#endif
}
Expand Down

0 comments on commit 9328562

Please sign in to comment.