diff --git a/ormpp/mysql.hpp b/ormpp/mysql.hpp index 68d9f80..e550c81 100644 --- a/ormpp/mysql.hpp +++ b/ormpp/mysql.hpp @@ -209,6 +209,10 @@ class mysql { std::map> &mp, B &is_null) { using U = std::remove_const_t>; if constexpr (is_optional_v::value) { + using value_type = typename U::value_type; + if (!value.has_value()) { + value = value_type{}; + } return set_param_bind(param_bind, *value, i, mp, is_null); } else if constexpr (std::is_enum_v) { diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index d99f66a..4647e5c 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -121,6 +121,17 @@ TEST_CASE("optional") { CHECK(vec2.front().age.value() == 200); CHECK(vec2.front().name.value() == "purecpp"); CHECK(vec2.front().empty_.has_value() == false); + + auto vec3 = mysql.query_s(); + REQUIRE(vec3.size() > 0); + CHECK(vec3.front().age.value() == 200); + CHECK(vec3.front().name.value() == "purecpp"); + CHECK(vec3.front().empty_.has_value() == false); + auto vec4 = mysql.query_s("select * from test_optional;"); + REQUIRE(vec4.size() > 0); + CHECK(vec4.front().age.value() == 200); + CHECK(vec4.front().name.value() == "purecpp"); + CHECK(vec4.front().empty_.has_value() == false); } #endif #ifdef ORMPP_ENABLE_PG @@ -2084,4 +2095,4 @@ TEST_CASE("unsigned type") { CHECK(vec.front().h == 8); } #endif -} \ No newline at end of file +}