mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-01 23:19:58 -06:00
Added to_select_from
This commit is contained in:
@@ -5,10 +5,11 @@
|
||||
|
||||
#include "CreateTable.hpp"
|
||||
#include "Insert.hpp"
|
||||
#include "SelectFrom.hpp"
|
||||
|
||||
namespace sqlgen::dynamic {
|
||||
|
||||
using Statement = rfl::TaggedUnion<"stmt", CreateTable, Insert>;
|
||||
using Statement = rfl::TaggedUnion<"stmt", CreateTable, Insert, SelectFrom>;
|
||||
|
||||
} // namespace sqlgen::dynamic
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ class Connection : public sqlgen::Connection {
|
||||
/// Expresses the properies as SQL.
|
||||
std::string properties_to_sql(const dynamic::types::Properties& _p) noexcept;
|
||||
|
||||
/// Transforms an Insert Statement to an SQL string.
|
||||
std::string select_from_to_sql(const dynamic::SelectFrom& _stmt) noexcept;
|
||||
|
||||
/// Expresses the type as SQL.
|
||||
std::string type_to_sql(const dynamic::Type& _type) noexcept;
|
||||
|
||||
|
||||
@@ -119,12 +119,36 @@ std::string Connection::to_sql(const dynamic::Statement& _stmt) noexcept {
|
||||
return create_table_to_sql(_s);
|
||||
} else if constexpr (std::is_same_v<S, dynamic::Insert>) {
|
||||
return insert_to_sql(_s);
|
||||
} else if constexpr (std::is_same_v<S, dynamic::SelectFrom>) {
|
||||
return select_from_to_sql(_s);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<S>, "Unsupported type.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::string Connection::select_from_to_sql(
|
||||
const dynamic::SelectFrom& _stmt) noexcept {
|
||||
using namespace std::ranges::views;
|
||||
|
||||
const auto to_str = [](const auto& _col) -> std::string {
|
||||
return "\"" + _col.name + "\"";
|
||||
};
|
||||
|
||||
std::stringstream stream;
|
||||
stream << "SELECT ";
|
||||
stream << internal::strings::join(
|
||||
", ", internal::collect::vector(_stmt.columns | transform(to_str)));
|
||||
|
||||
stream << " FROM ";
|
||||
if (_stmt.table.schema) {
|
||||
stream << "\"" << *_stmt.table.schema << "\".";
|
||||
}
|
||||
stream << "\"" << _stmt.table.name << "\";";
|
||||
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
Result<Nothing> Connection::start_write(const dynamic::Insert& _stmt) {
|
||||
if (stmt_) {
|
||||
return error(
|
||||
|
||||
24
tests/sqlite/test_to_select_from.cpp
Normal file
24
tests/sqlite/test_to_select_from.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/parsing/to_select_from.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
|
||||
namespace test_to_select_from {
|
||||
|
||||
struct TestTable {
|
||||
std::string field1;
|
||||
int32_t field2;
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_to_select_from) {
|
||||
const auto select_from_stmt = sqlgen::parsing::to_select_from<TestTable>();
|
||||
const auto conn = sqlgen::sqlite::connect().value();
|
||||
const auto expected =
|
||||
R"(SELECT "field1", "field2", "id", "nullable" FROM "TestTable";)";
|
||||
|
||||
EXPECT_EQ(conn->to_sql(select_from_stmt), expected);
|
||||
}
|
||||
} // namespace test_to_select_from
|
||||
Reference in New Issue
Block a user