mirror of
https://github.com/getml/sqlgen.git
synced 2026-05-04 14:11:20 -05:00
Added aggregations and GROUP BY (#17)
This commit is contained in:
committed by
GitHub
parent
21564cacf6
commit
775f15babf
@@ -0,0 +1,63 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_aggregations {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_aggregations) {
|
||||
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 num_children;
|
||||
int num_last_names;
|
||||
double avg_age;
|
||||
double max_age;
|
||||
double min_age;
|
||||
double sum_age;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
avg("age"_c).as<"avg_age">(), count().as<"num_children">(),
|
||||
max("age"_c).as<"max_age">(), min("age"_c).as<"min_age">(),
|
||||
sum("age"_c).as<"sum_age">(),
|
||||
count_distinct("last_name"_c).as<"num_last_names">()) |
|
||||
where("age"_c < 18) | to<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.num_children, 3);
|
||||
EXPECT_EQ(children.num_last_names, 1);
|
||||
EXPECT_EQ(children.avg_age, 6.0);
|
||||
EXPECT_EQ(children.max_age, 10.0);
|
||||
EXPECT_EQ(children.min_age, 0.0);
|
||||
EXPECT_EQ(children.sum_age, 18.0);
|
||||
}
|
||||
|
||||
} // namespace test_aggregations
|
||||
|
||||
@@ -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_aggregations_with_nullable {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::optional<double> age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_aggregations_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 = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{.id = 3, .first_name = "Maggie", .last_name = "Simpson"}});
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
struct Children {
|
||||
int num_children;
|
||||
int num_last_names;
|
||||
std::optional<double> avg_age;
|
||||
std::optional<double> max_age;
|
||||
std::optional<double> min_age;
|
||||
std::optional<double> sum_age;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
avg("age"_c).as<"avg_age">(), count().as<"num_children">(),
|
||||
max("age"_c).as<"max_age">(), min("age"_c).as<"min_age">(),
|
||||
sum("age"_c).as<"sum_age">(),
|
||||
count_distinct("last_name"_c).as<"num_last_names">()) |
|
||||
where("age"_c < 18 or "age"_c.is_null()) | to<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.num_children, 3);
|
||||
EXPECT_EQ(children.num_last_names, 1);
|
||||
EXPECT_EQ(children.avg_age, 9.0);
|
||||
EXPECT_EQ(children.max_age, 10.0);
|
||||
EXPECT_EQ(children.min_age, 8.0);
|
||||
EXPECT_EQ(children.sum_age, 18.0);
|
||||
}
|
||||
|
||||
} // namespace test_aggregations_with_nullable
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
|
||||
#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 {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_group_by) {
|
||||
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;
|
||||
int num_children;
|
||||
int num_last_names;
|
||||
double avg_age;
|
||||
double max_age;
|
||||
double min_age;
|
||||
double sum_age;
|
||||
};
|
||||
|
||||
const auto get_children =
|
||||
select_from<Person>(
|
||||
"last_name"_c, avg("age"_c).as<"avg_age">(),
|
||||
count().as<"num_children">(), max("age"_c).as<"max_age">(),
|
||||
min("age"_c).as<"min_age">(), sum("age"_c).as<"sum_age">(),
|
||||
count_distinct("last_name"_c).as<"num_last_names">()) |
|
||||
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).num_children, 3);
|
||||
EXPECT_EQ(children.at(0).num_last_names, 1);
|
||||
EXPECT_EQ(children.at(0).avg_age, 6.0);
|
||||
EXPECT_EQ(children.at(0).max_age, 10.0);
|
||||
EXPECT_EQ(children.at(0).min_age, 0.0);
|
||||
EXPECT_EQ(children.at(0).sum_age, 18.0);
|
||||
}
|
||||
|
||||
} // namespace test_group_by
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_range_select_from {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_range_select_from) {
|
||||
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;
|
||||
|
||||
const auto people2 =
|
||||
sqlgen::sqlite::connect()
|
||||
.and_then(sqlgen::write(std::ref(people1)))
|
||||
.and_then(select_from<Person>("first_name"_c) | order_by("id"_c))
|
||||
.value();
|
||||
|
||||
using namespace std::ranges::views;
|
||||
|
||||
const auto first_names =
|
||||
internal::collect::vector(people2 | transform([](const auto& _r) {
|
||||
return rfl::get<"first_name">(_r.value());
|
||||
}));
|
||||
|
||||
EXPECT_EQ(first_names.at(0), "Homer");
|
||||
EXPECT_EQ(first_names.at(1), "Bart");
|
||||
EXPECT_EQ(first_names.at(2), "Lisa");
|
||||
EXPECT_EQ(first_names.at(3), "Maggie");
|
||||
}
|
||||
|
||||
} // namespace test_range_select_from
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_range_select_from_with_to {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_range_select_from_with_to) {
|
||||
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 FirstName {
|
||||
std::string first_name;
|
||||
};
|
||||
|
||||
const auto people2 =
|
||||
sqlite::connect()
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(write(std::ref(people1)))
|
||||
.and_then(select_from<Person>("first_name"_c) | order_by("id"_c) |
|
||||
to<std::vector<FirstName>>)
|
||||
.value();
|
||||
|
||||
EXPECT_EQ(people2.at(0).first_name, "Homer");
|
||||
EXPECT_EQ(people2.at(1).first_name, "Bart");
|
||||
EXPECT_EQ(people2.at(2).first_name, "Lisa");
|
||||
EXPECT_EQ(people2.at(3).first_name, "Maggie");
|
||||
}
|
||||
|
||||
} // namespace test_range_select_from_with_to
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <sqlgen/transpilation/to_select_from.hpp>
|
||||
|
||||
namespace test_to_select_from {
|
||||
|
||||
@@ -15,7 +14,7 @@ struct TestTable {
|
||||
|
||||
TEST(sqlite, test_to_select_from) {
|
||||
const auto select_from_stmt =
|
||||
sqlgen::transpilation::to_select_from<TestTable>();
|
||||
sqlgen::transpilation::read_to_select_from<TestTable>();
|
||||
const auto conn = sqlgen::sqlite::connect().value();
|
||||
const auto expected =
|
||||
R"(SELECT "field1", "field2", "id", "nullable" FROM "TestTable";)";
|
||||
|
||||
Reference in New Issue
Block a user