From 7fc16e2a2d678b44f3129fb4610c7d9e06d44b6d Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Wed, 9 Apr 2025 09:07:34 +0200 Subject: [PATCH] Added to_select_from --- include/sqlgen/parsing/to_select_from.hpp | 36 +++++++++++++++++++++++ tests/test_to_select_from.cpp | 26 ++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 include/sqlgen/parsing/to_select_from.hpp create mode 100644 tests/test_to_select_from.cpp diff --git a/include/sqlgen/parsing/to_select_from.hpp b/include/sqlgen/parsing/to_select_from.hpp new file mode 100644 index 0000000..0f4fbff --- /dev/null +++ b/include/sqlgen/parsing/to_select_from.hpp @@ -0,0 +1,36 @@ +#ifndef SQLGEN_PARSING_TO_SELECT_FROM_HPP_ +#define SQLGEN_PARSING_TO_SELECT_FROM_HPP_ + +#include +#include +#include +#include +#include +#include + +#include "../dynamic/SelectFrom.hpp" +#include "../dynamic/Table.hpp" +#include "get_schema.hpp" +#include "get_tablename.hpp" +#include "make_columns.hpp" + +namespace sqlgen::parsing { + +template + requires std::is_class_v> && + std::is_aggregate_v> +dynamic::SelectFrom to_select_from() { + using NamedTupleType = rfl::named_tuple_t>; + using Fields = typename NamedTupleType::Fields; + + const auto columns = make_columns( + std::make_integer_sequence>()); + + return dynamic::SelectFrom{.table = dynamic::Table{.name = get_tablename(), + .schema = get_schema()}, + .columns = columns}; +} + +} // namespace sqlgen::parsing + +#endif diff --git a/tests/test_to_select_from.cpp b/tests/test_to_select_from.cpp new file mode 100644 index 0000000..12213f3 --- /dev/null +++ b/tests/test_to_select_from.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include +#include + +namespace test_to_select_from { + +struct TestTable { + std::string field1; + int32_t field2; + sqlgen::PrimaryKey id; + std::optional nullable; +}; + +TEST(general, test_to_select_from) { + const auto select_from_stmt = sqlgen::parsing::to_select_from(); + + const std::string expected = + R"({"table":{"name":"TestTable"},"columns":[{"name":"field1","type":{"type":"Text","properties":{"primary":false,"nullable":false}}},{"name":"field2","type":{"type":"Int32","properties":{"primary":false,"nullable":false}}},{"name":"id","type":{"type":"UInt32","properties":{"primary":true,"nullable":false}}},{"name":"nullable","type":{"type":"Text","properties":{"primary":false,"nullable":true}}}]})"; + + const auto json_str = rfl::json::write(select_from_stmt); + + EXPECT_EQ(json_str, expected); +} +} // namespace test_to_select_from