Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions include/sqlgen/parsing/Parser_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <string>
#include <type_traits>

#include "../Result.hpp"

Check warning on line 9 in include/sqlgen/parsing/Parser_default.hpp

View workflow job for this annotation

GitHub Actions / (windows-mysql)

relative include path contains '..'

Check warning on line 9 in include/sqlgen/parsing/Parser_default.hpp

View workflow job for this annotation

GitHub Actions / (windows-mysql)

relative include path contains '..'
#include "../dynamic/Type.hpp"

Check warning on line 10 in include/sqlgen/parsing/Parser_default.hpp

View workflow job for this annotation

GitHub Actions / (windows-mysql)

relative include path contains '..'

Check warning on line 10 in include/sqlgen/parsing/Parser_default.hpp

View workflow job for this annotation

GitHub Actions / (windows-mysql)

relative include path contains '..'
#include "../dynamic/types.hpp"
#include "../transpilation/has_reflection_method.hpp"
#include "Parser_base.hpp"
Expand All @@ -32,16 +32,28 @@
try {
if constexpr (std::is_floating_point_v<Type>) {
return static_cast<Type>(std::stod(*_str));
} else if constexpr (std::is_integral_v<Type>) {
return static_cast<Type>(std::stoll(*_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_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");
}
Expand Down
61 changes: 61 additions & 0 deletions tests/mysql/test_boolean.cpp
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions tests/postgres/test_boolean.cpp
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions tests/postgres/test_read_to_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/sqlite/test_boolean.cpp
Original file line number Diff line number Diff line change
@@ -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

Loading