Fixed issue with boolean data; resolves #69 (#72)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-10-22 23:15:47 +02:00
committed by GitHub
parent 6c0af08321
commit 816ca6adec
5 changed files with 194 additions and 4 deletions

View File

@@ -32,16 +32,28 @@ struct Parser {
try {
if constexpr (std::is_floating_point_v<Type>) {
return static_cast<Type>(std::stod(*_str));
} else if constexpr (std::is_same_v<Type, bool>) {
if (*_str == "t" || *_str == "T" || *_str == "true" ||
*_str == "TRUE") {
return true;
}
if (*_str == "f" || *_str == "F" || *_str == "false" ||
*_str == "FALSE") {
return false;
}
return std::stoi(*_str) != 0;
} else if constexpr (std::is_integral_v<Type>) {
return static_cast<Type>(std::stoll(*_str));
} else if constexpr (std::is_same_v<Type, bool>) {
return std::stoi(*_str) != 0;
} else if constexpr (std::is_enum_v<Type>) {
if (auto res = rfl::string_to_enum<Type>(*_str)) {
return Type{*res};
} else {
return error(res.error());
}
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type");
}

View File

@@ -0,0 +1,61 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/mysql.hpp>
#include <vector>
namespace test_boolean {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
bool has_children;
};
TEST(mysql, test_boolean) {
const auto people1 = std::vector<Person>({Person{.id = 0,
.first_name = "Homer",
.last_name = "Simpson",
.has_children = true},
Person{.id = 1,
.first_name = "Bart",
.last_name = "Simpson",
.has_children = false},
Person{.id = 2,
.first_name = "Lisa",
.last_name = "Simpson",
.has_children = false},
Person{.id = 3,
.first_name = "Maggie",
.last_name = "Simpson",
.has_children = false}});
const auto credentials = sqlgen::mysql::Credentials{.host = "localhost",
.user = "sqlgen",
.password = "password",
.dbname = "mysql"};
using namespace sqlgen;
using namespace sqlgen::literals;
const auto conn =
mysql::connect(credentials).and_then(drop<Person> | if_exists);
const auto people2 = sqlgen::write(conn, people1)
.and_then(sqlgen::read<std::vector<Person>>)
.value();
const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);
EXPECT_EQ(json1, json2);
}
} // namespace test_boolean
#endif

View File

@@ -0,0 +1,61 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_boolean {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
bool has_children;
};
TEST(postgres, test_boolean) {
const auto people1 = std::vector<Person>({Person{.id = 0,
.first_name = "Homer",
.last_name = "Simpson",
.has_children = true},
Person{.id = 1,
.first_name = "Bart",
.last_name = "Simpson",
.has_children = false},
Person{.id = 2,
.first_name = "Lisa",
.last_name = "Simpson",
.has_children = false},
Person{.id = 3,
.first_name = "Maggie",
.last_name = "Simpson",
.has_children = false}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
using namespace sqlgen::literals;
const auto conn =
postgres::connect(credentials).and_then(drop<Person> | if_exists);
const auto people2 = sqlgen::write(conn, people1)
.and_then(sqlgen::read<std::vector<Person>>)
.value();
const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);
EXPECT_EQ(json1, json2);
}
} // namespace test_boolean
#endif

View File

@@ -23,8 +23,12 @@ TEST(postgres, test_write_and_read_dry) {
const auto query2 =
postgres::to_sql(sqlgen::read<Person> | where("id"_c == 1));
std::cout << query1 << std::endl;
std::cout << query2 << std::endl;
EXPECT_EQ(
query1,
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "id" = 1)");
EXPECT_EQ(
query2,
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "id" = 1)");
}
} // namespace test_write_and_read_dry

View File

@@ -0,0 +1,52 @@
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/sqlite.hpp>
#include <vector>
namespace test_boolean {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
bool has_children;
};
TEST(sqlite, test_boolean) {
const auto people1 = std::vector<Person>({Person{.id = 0,
.first_name = "Homer",
.last_name = "Simpson",
.has_children = true},
Person{.id = 1,
.first_name = "Bart",
.last_name = "Simpson",
.has_children = false},
Person{.id = 2,
.first_name = "Lisa",
.last_name = "Simpson",
.has_children = false},
Person{.id = 3,
.first_name = "Maggie",
.last_name = "Simpson",
.has_children = false}});
using namespace sqlgen;
using namespace sqlgen::literals;
const auto conn = sqlite::connect();
const auto people2 = sqlgen::write(conn, people1)
.and_then(sqlgen::read<std::vector<Person>>)
.value();
const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);
EXPECT_EQ(json1, json2);
}
} // namespace test_boolean