mirror of
https://github.com/getml/sqlgen.git
synced 2025-12-31 22:49:35 -06:00
committed by
GitHub
parent
15108fba46
commit
91e993ffdd
@@ -4,7 +4,6 @@
|
||||
#include <rfl.hpp>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "Column.hpp"
|
||||
#include "ColumnOrValue.hpp"
|
||||
#include "Operation.hpp"
|
||||
|
||||
@@ -16,6 +15,10 @@ struct Condition {
|
||||
Ref<Condition> cond2;
|
||||
};
|
||||
|
||||
struct BooleanColumnOrValue {
|
||||
ColumnOrValue col_or_val;
|
||||
};
|
||||
|
||||
struct Equal {
|
||||
Operation op1;
|
||||
Operation op2;
|
||||
@@ -84,9 +87,9 @@ struct Condition {
|
||||
};
|
||||
|
||||
using ReflectionType =
|
||||
rfl::TaggedUnion<"what", And, Equal, GreaterEqual, GreaterThan, In,
|
||||
IsNull, IsNotNull, LesserEqual, LesserThan, Like, Not,
|
||||
NotEqual, NotIn, NotLike, Or>;
|
||||
rfl::TaggedUnion<"what", And, BooleanColumnOrValue, Equal, GreaterEqual,
|
||||
GreaterThan, In, IsNull, IsNotNull, LesserEqual,
|
||||
LesserThan, Like, Not, NotEqual, NotIn, NotLike, Or>;
|
||||
|
||||
const ReflectionType& reflection() const { return val; }
|
||||
|
||||
|
||||
@@ -13,6 +13,10 @@ struct Duration {
|
||||
int64_t val;
|
||||
};
|
||||
|
||||
struct Boolean {
|
||||
bool val;
|
||||
};
|
||||
|
||||
struct Float {
|
||||
double val;
|
||||
};
|
||||
@@ -30,8 +34,8 @@ struct Timestamp {
|
||||
};
|
||||
|
||||
struct Value {
|
||||
using ReflectionType =
|
||||
rfl::TaggedUnion<"type", Duration, Float, Integer, String, Timestamp>;
|
||||
using ReflectionType = rfl::TaggedUnion<"type", Duration, Boolean, Float,
|
||||
Integer, String, Timestamp>;
|
||||
const auto& reflection() const { return val; }
|
||||
ReflectionType val;
|
||||
};
|
||||
|
||||
@@ -20,6 +20,9 @@ struct ToValue {
|
||||
if constexpr (std::is_floating_point_v<Type>) {
|
||||
return dynamic::Value{dynamic::Float{.val = static_cast<double>(_t)}};
|
||||
|
||||
} else if constexpr (std::is_same_v<Type, bool>) {
|
||||
return dynamic::Value{dynamic::Boolean{.val = _t}};
|
||||
|
||||
} else if constexpr (std::is_integral_v<Type>) {
|
||||
return dynamic::Value{dynamic::Integer{.val = static_cast<int64_t>(_t)}};
|
||||
|
||||
|
||||
@@ -219,6 +219,10 @@ std::string condition_to_sql_impl(const ConditionType& _condition) noexcept {
|
||||
stream << "(" << condition_to_sql(*_condition.cond1) << ") AND ("
|
||||
<< condition_to_sql(*_condition.cond2) << ")";
|
||||
|
||||
} else if constexpr (std::is_same_v<
|
||||
C, dynamic::Condition::BooleanColumnOrValue>) {
|
||||
stream << column_or_value_to_sql(_condition.col_or_val);
|
||||
|
||||
} else if constexpr (std::is_same_v<C, dynamic::Condition::Equal>) {
|
||||
stream << operation_to_sql(_condition.op1) << " = "
|
||||
<< operation_to_sql(_condition.op2);
|
||||
|
||||
@@ -135,6 +135,9 @@ std::string column_or_value_to_sql(
|
||||
} else if constexpr (std::is_same_v<Type, dynamic::Timestamp>) {
|
||||
return "to_timestamp(" + std::to_string(_v.seconds_since_unix) + ")";
|
||||
|
||||
} else if constexpr (std::is_same_v<Type, dynamic::Boolean>) {
|
||||
return _v.val ? "TRUE" : "FALSE";
|
||||
|
||||
} else {
|
||||
return std::to_string(_v.val);
|
||||
}
|
||||
@@ -171,6 +174,10 @@ std::string condition_to_sql_impl(const ConditionType& _condition) noexcept {
|
||||
stream << "(" << condition_to_sql(*_condition.cond1) << ") AND ("
|
||||
<< condition_to_sql(*_condition.cond2) << ")";
|
||||
|
||||
} else if constexpr (std::is_same_v<
|
||||
C, dynamic::Condition::BooleanColumnOrValue>) {
|
||||
stream << column_or_value_to_sql(_condition.col_or_val);
|
||||
|
||||
} else if constexpr (std::is_same_v<C, dynamic::Condition::Equal>) {
|
||||
stream << operation_to_sql(_condition.op1) << " = "
|
||||
<< operation_to_sql(_condition.op2);
|
||||
|
||||
@@ -179,6 +179,10 @@ std::string condition_to_sql_impl(const ConditionType& _condition) noexcept {
|
||||
stream << "(" << condition_to_sql(*_condition.cond1) << ") AND ("
|
||||
<< condition_to_sql(*_condition.cond2) << ")";
|
||||
|
||||
} else if constexpr (std::is_same_v<
|
||||
C, dynamic::Condition::BooleanColumnOrValue>) {
|
||||
stream << column_or_value_to_sql(_condition.col_or_val);
|
||||
|
||||
} else if constexpr (std::is_same_v<C, dynamic::Condition::Equal>) {
|
||||
stream << operation_to_sql(_condition.op1) << " = "
|
||||
<< operation_to_sql(_condition.op2);
|
||||
|
||||
63
tests/mysql/test_boolean_conditions.cpp
Normal file
63
tests/mysql/test_boolean_conditions.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#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_conditions {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(mysql, test_boolean_conditions) {
|
||||
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 homer =
|
||||
sqlgen::write(conn, people1)
|
||||
.and_then(sqlgen::read<Person> | where("has_children"_c == true) |
|
||||
order_by("id"_c))
|
||||
.value();
|
||||
|
||||
const auto json1 = rfl::json::write(people1.at(0));
|
||||
const auto json2 = rfl::json::write(homer);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_conditions
|
||||
|
||||
#endif
|
||||
67
tests/mysql/test_boolean_update.cpp
Normal file
67
tests/mysql/test_boolean_update.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#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_update {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(mysql, test_boolean_update) {
|
||||
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(update<Person>("has_children"_c.set(false)) |
|
||||
where("has_children"_c == true))
|
||||
.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("has_children"_c == false) | order_by("id"_c))
|
||||
.value();
|
||||
|
||||
people1.at(0).has_children = false;
|
||||
|
||||
const auto json1 = rfl::json::write(people1);
|
||||
const auto json2 = rfl::json::write(people2);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_update
|
||||
|
||||
#endif
|
||||
63
tests/postgres/test_boolean_conditions.cpp
Normal file
63
tests/postgres/test_boolean_conditions.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#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_conditions {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(postgres, test_boolean_conditions) {
|
||||
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 homer =
|
||||
sqlgen::write(conn, people1)
|
||||
.and_then(sqlgen::read<Person> | where("has_children"_c == true) |
|
||||
order_by("id"_c))
|
||||
.value();
|
||||
|
||||
const auto json1 = rfl::json::write(people1.at(0));
|
||||
const auto json2 = rfl::json::write(homer);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_conditions
|
||||
|
||||
#endif
|
||||
67
tests/postgres/test_boolean_update.cpp
Normal file
67
tests/postgres/test_boolean_update.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#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_update {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(postgres, test_boolean_update) {
|
||||
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(update<Person>("has_children"_c.set(false)) |
|
||||
where("has_children"_c == true))
|
||||
.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("has_children"_c == false) | order_by("id"_c))
|
||||
.value();
|
||||
|
||||
people1.at(0).has_children = false;
|
||||
|
||||
const auto json1 = rfl::json::write(people1);
|
||||
const auto json2 = rfl::json::write(people2);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_update
|
||||
|
||||
#endif
|
||||
54
tests/sqlite/test_boolean_conditions.cpp
Normal file
54
tests/sqlite/test_boolean_conditions.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_boolean_conditions {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_boolean_conditions) {
|
||||
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 homer =
|
||||
sqlite::connect()
|
||||
.and_then(sqlgen::write(people1))
|
||||
.and_then(sqlgen::read<Person> | where("has_children"_c == true) |
|
||||
order_by("id"_c))
|
||||
.value();
|
||||
|
||||
const auto json1 = rfl::json::write(people1.at(0));
|
||||
const auto json2 = rfl::json::write(homer);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_conditions
|
||||
|
||||
59
tests/sqlite/test_boolean_update.cpp
Normal file
59
tests/sqlite/test_boolean_update.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_boolean_update {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
bool has_children;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_boolean_update) {
|
||||
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(update<Person>("has_children"_c.set(false)) |
|
||||
where("has_children"_c == true))
|
||||
.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("has_children"_c == false) | order_by("id"_c))
|
||||
.value();
|
||||
|
||||
people1.at(0).has_children = false;
|
||||
|
||||
const auto json1 = rfl::json::write(people1);
|
||||
const auto json2 = rfl::json::write(people2);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_boolean_update
|
||||
|
||||
Reference in New Issue
Block a user