mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-08 02:19:32 -06:00
Added numerous operations like concat(...), coalesce(...), abs(...), etc (#20)
This commit is contained in:
committed by
GitHub
parent
a6bad187fe
commit
30ba548f1e
62
tests/sqlite/test_group_by_with_operations.cpp
Normal file
62
tests/sqlite/test_group_by_with_operations.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_group_by_with_operations {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_group_by_with_operations) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
struct Children {
|
||||
std::string last_name;
|
||||
std::string last_name_trimmed;
|
||||
double avg_age;
|
||||
double max_age_plus_one;
|
||||
double min_age_plus_one;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
"last_name"_c, trim("last_name"_c).as<"last_name_trimmed">(),
|
||||
max(cast<double>("age"_c) + 1.0).as<"max_age_plus_one">(),
|
||||
(min(cast<double>("age"_c)) + 1.0).as<"min_age_plus_one">(),
|
||||
round(avg(cast<double>("age"_c))).as<"avg_age">()) |
|
||||
where("age"_c < 18) | group_by("last_name"_c) | to<std::vector<Children>>;
|
||||
|
||||
const auto children = sqlite::connect()
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(write(std::ref(people1)))
|
||||
.and_then(get_children)
|
||||
.value();
|
||||
|
||||
EXPECT_EQ(children.size(), 1);
|
||||
EXPECT_EQ(children.at(0).last_name, "Simpson");
|
||||
EXPECT_EQ(children.at(0).last_name_trimmed, "Simpson");
|
||||
EXPECT_EQ(children.at(0).avg_age, 6.0);
|
||||
EXPECT_EQ(children.at(0).max_age_plus_one, 11.0);
|
||||
EXPECT_EQ(children.at(0).min_age_plus_one, 1.0);
|
||||
}
|
||||
|
||||
} // namespace test_group_by_with_operations
|
||||
|
||||
75
tests/sqlite/test_operations.cpp
Normal file
75
tests/sqlite/test_operations.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_operations {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_operations) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
struct Children {
|
||||
int id_plus_age;
|
||||
int age_times_2;
|
||||
int id_plus_2_minus_age;
|
||||
int age_mod_3;
|
||||
int abs_age;
|
||||
double exp_age;
|
||||
double sqrt_age;
|
||||
size_t length_first_name;
|
||||
std::string full_name;
|
||||
std::string first_name_lower;
|
||||
std::string first_name_upper;
|
||||
std::string first_name_replaced;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
("id"_c + "age"_c) | as<"id_plus_age">,
|
||||
("age"_c * 2) | as<"age_times_2">, ("age"_c % 3) | as<"age_mod_3">,
|
||||
abs("age"_c * (-1)) | as<"abs_age">,
|
||||
round(exp(cast<double>("age"_c)), 2) | as<"exp_age">,
|
||||
round(sqrt(cast<double>("age"_c)), 2) | as<"sqrt_age">,
|
||||
("id"_c + 2 - "age"_c) | as<"id_plus_2_minus_age">,
|
||||
length(trim("first_name"_c)) | as<"length_first_name">,
|
||||
concat(ltrim("first_name"_c), " ", rtrim("last_name"_c)) |
|
||||
as<"full_name">,
|
||||
upper(rtrim(concat("first_name"_c, " "))) | as<"first_name_upper">,
|
||||
lower(ltrim(concat(" ", "first_name"_c))) | as<"first_name_lower">,
|
||||
replace("first_name"_c, "Bart", "Hugo") | as<"first_name_replaced">) |
|
||||
where("age"_c < 18) | order_by("age"_c.desc()) |
|
||||
to<std::vector<Children>>;
|
||||
|
||||
const auto children = sqlite::connect()
|
||||
.and_then(write(std::ref(people1)))
|
||||
.and_then(get_children)
|
||||
.value();
|
||||
|
||||
const std::string expected =
|
||||
R"([{"id_plus_age":11,"age_times_2":20,"id_plus_2_minus_age":-7,"age_mod_3":1,"abs_age":10,"exp_age":22026.47,"sqrt_age":3.16,"length_first_name":4,"full_name":"Bart Simpson","first_name_lower":"bart","first_name_upper":"BART","first_name_replaced":"Hugo"},{"id_plus_age":10,"age_times_2":16,"id_plus_2_minus_age":-4,"age_mod_3":2,"abs_age":8,"exp_age":2980.96,"sqrt_age":2.83,"length_first_name":4,"full_name":"Lisa Simpson","first_name_lower":"lisa","first_name_upper":"LISA","first_name_replaced":"Lisa"},{"id_plus_age":3,"age_times_2":0,"id_plus_2_minus_age":5,"age_mod_3":0,"abs_age":0,"exp_age":1.0,"sqrt_age":0.0,"length_first_name":6,"full_name":"Maggie Simpson","first_name_lower":"maggie","first_name_upper":"MAGGIE","first_name_replaced":"Maggie"}])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(children), expected);
|
||||
}
|
||||
|
||||
} // namespace test_operations
|
||||
|
||||
62
tests/sqlite/test_operations_with_nullable.cpp
Normal file
62
tests/sqlite/test_operations_with_nullable.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_operations_with_nullable {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::optional<std::string> last_name;
|
||||
std::optional<int> age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_operations_with_nullable) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Hugo", .age = 10},
|
||||
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
struct Children {
|
||||
std::optional<int> id_plus_age;
|
||||
std::optional<int> age_times_2;
|
||||
std::optional<int> id_plus_2_minus_age;
|
||||
std::optional<std::string> full_name;
|
||||
std::string last_name_or_none;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
("id"_c + "age"_c) | as<"id_plus_age">,
|
||||
("age"_c * 2) | as<"age_times_2">,
|
||||
("id"_c + 2 - "age"_c) | as<"id_plus_2_minus_age">,
|
||||
concat(upper("last_name"_c), ", ", "first_name"_c) | as<"full_name">,
|
||||
coalesce(upper("last_name"_c), "none") | as<"last_name_or_none">) |
|
||||
where("age"_c < 18) | to<std::vector<Children>>;
|
||||
|
||||
const auto children = sqlite::connect()
|
||||
.and_then(write(std::ref(people1)))
|
||||
.and_then(get_children)
|
||||
.value();
|
||||
|
||||
const std::string expected =
|
||||
R"([{"id_plus_age":11,"age_times_2":20,"id_plus_2_minus_age":-7,"full_name":"SIMPSON, Bart","last_name_or_none":"SIMPSON"},{"id_plus_age":12,"age_times_2":20,"id_plus_2_minus_age":-6,"last_name_or_none":"none"},{"id_plus_age":11,"age_times_2":16,"id_plus_2_minus_age":-3,"full_name":"SIMPSON, Lisa","last_name_or_none":"SIMPSON"},{"id_plus_age":4,"age_times_2":0,"id_plus_2_minus_age":6,"full_name":"SIMPSON, Maggie","last_name_or_none":"SIMPSON"}])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(children), expected);
|
||||
}
|
||||
|
||||
} // namespace test_operations_with_nullable
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST(sqlite, test_where) {
|
||||
using namespace sqlgen;
|
||||
|
||||
const auto query = sqlgen::read<std::vector<Person>> |
|
||||
where("age"_c < 18 and "first_name"_c != "Hugo") |
|
||||
where("age"_c < 18 and not("first_name"_c == "Hugo")) |
|
||||
order_by("age"_c);
|
||||
|
||||
const auto people2 = query(conn).value();
|
||||
|
||||
48
tests/sqlite/test_where_with_nullable_operations.cpp
Normal file
48
tests/sqlite/test_where_with_nullable_operations.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <optional>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_where_with_nullable_operations {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::optional<int> age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_where_with_nullable_operations) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0},
|
||||
Person{
|
||||
.id = 4, .first_name = "Hugo", .last_name = "Simpson", .age = 10}});
|
||||
|
||||
const auto conn = sqlgen::sqlite::connect();
|
||||
|
||||
sqlgen::write(conn, people1);
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
const auto query = sqlgen::read<std::vector<Person>> |
|
||||
where("age"_c * 2 + 4 < 40 and "first_name"_c != "Hugo") |
|
||||
order_by("age"_c);
|
||||
|
||||
const auto people2 = query(conn).value();
|
||||
|
||||
const std::string expected =
|
||||
R"([{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10}])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(people2), expected);
|
||||
}
|
||||
|
||||
} // namespace test_where_with_nullable_operations
|
||||
47
tests/sqlite/test_where_with_operations.cpp
Normal file
47
tests/sqlite/test_where_with_operations.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_where_with_operations {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_where_with_operations) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0},
|
||||
Person{
|
||||
.id = 4, .first_name = "Hugo", .last_name = "Simpson", .age = 10}});
|
||||
|
||||
const auto conn = sqlgen::sqlite::connect();
|
||||
|
||||
sqlgen::write(conn, people1);
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
const auto query = sqlgen::read<std::vector<Person>> |
|
||||
where("age"_c * 2 + 4 < 40 and "first_name"_c != "Hugo") |
|
||||
order_by("age"_c);
|
||||
|
||||
const auto people2 = query(conn).value();
|
||||
|
||||
const std::string expected =
|
||||
R"([{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10}])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(people2), expected);
|
||||
}
|
||||
|
||||
} // namespace test_where_with_operations
|
||||
Reference in New Issue
Block a user