From b167d37dcac6a08da5f33bb964c74e82d993a00c Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 21 Apr 2025 18:05:12 +0200 Subject: [PATCH] Make sure the first test compiles --- include/sqlgen/postgres/Connection.hpp | 20 +++++++++--- include/sqlgen/postgres/Iterator.hpp | 2 +- src/sqlgen/postgres/Connection.cpp | 39 +++++++++++++++++++++++ src/sqlgen/postgres/Iterator.cpp | 43 ++++---------------------- src/sqlgen/sqlite/Connection.cpp | 30 +++++++++--------- tests/postgres/test_to_select_from.cpp | 11 +++++-- 6 files changed, 84 insertions(+), 61 deletions(-) diff --git a/include/sqlgen/postgres/Connection.hpp b/include/sqlgen/postgres/Connection.hpp index 75e6101..89e919b 100644 --- a/include/sqlgen/postgres/Connection.hpp +++ b/include/sqlgen/postgres/Connection.hpp @@ -32,22 +32,32 @@ class Connection : public sqlgen::Connection { Result commit() final { return execute("COMMIT;"); } - Result execute(const std::string& _sql) noexcept final; + Result execute(const std::string& _sql) noexcept final { + return error("TODO"); + } - Result> read(const dynamic::SelectFrom& _query) final; + Result> read(const dynamic::SelectFrom& _query) final { + return error("TODO"); + } std::string to_sql(const dynamic::Statement& _stmt) noexcept final; - Result start_write(const dynamic::Insert& _stmt) final; + Result start_write(const dynamic::Insert& _stmt) final { + return error("TODO"); + } - Result end_write() final; + Result end_write() final { return error("TODO"); } Result write( - const std::vector>>& _data) final; + const std::vector>>& _data) final { + return error("TODO"); + } private: static ConnPtr make_conn(const std::string& _conn_str); + std::string select_from_to_sql(const dynamic::SelectFrom& _stmt) noexcept; + private: ConnPtr conn_; diff --git a/include/sqlgen/postgres/Iterator.hpp b/include/sqlgen/postgres/Iterator.hpp index 29debd9..be34268 100644 --- a/include/sqlgen/postgres/Iterator.hpp +++ b/include/sqlgen/postgres/Iterator.hpp @@ -16,7 +16,7 @@ namespace sqlgen::postgres { class Iterator : public sqlgen::IteratorBase { public: - Iterator(const StmtPtr& _stmt, const ConnPtr& _conn); + Iterator(); ~Iterator(); diff --git a/src/sqlgen/postgres/Connection.cpp b/src/sqlgen/postgres/Connection.cpp index acdbf53..700cae9 100644 --- a/src/sqlgen/postgres/Connection.cpp +++ b/src/sqlgen/postgres/Connection.cpp @@ -33,4 +33,43 @@ typename Connection::ConnPtr Connection::make_conn( return ConnPtr::make(std::shared_ptr(raw_ptr, &PQfinish)).value(); } +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(); +} + +std::string Connection::to_sql(const dynamic::Statement& _stmt) noexcept { + return _stmt.visit([&](const auto& _s) -> std::string { + using S = std::remove_cvref_t; + /*if constexpr (std::is_same_v) { + return create_table_to_sql(_s); + } else if constexpr (std::is_same_v) { + return insert_to_sql(_s); + } else*/ + if constexpr (std::is_same_v) { + return select_from_to_sql(_s); + } else { + return "TODO"; + // static_assert(rfl::always_false_v, "Unsupported type."); + } + }); +} + } // namespace sqlgen::postgres diff --git a/src/sqlgen/postgres/Iterator.cpp b/src/sqlgen/postgres/Iterator.cpp index c0c5475..f1c8fa1 100644 --- a/src/sqlgen/postgres/Iterator.cpp +++ b/src/sqlgen/postgres/Iterator.cpp @@ -1,4 +1,4 @@ -#include "sqlgen/sqlite/Iterator.hpp" +#include "sqlgen/postgres/Iterator.hpp" #include #include @@ -8,20 +8,13 @@ #include "sqlgen/internal/strings/strings.hpp" #include "sqlgen/sqlite/Iterator.hpp" -namespace sqlgen::sqlite { +namespace sqlgen::postgres { -Iterator::Iterator(const StmtPtr& _stmt, const ConnPtr& _conn) - : end_(false), - rownum_(0), - num_cols_(sqlite3_column_count(_stmt.get())), - stmt_(_stmt), - conn_(_conn) { - step(); -} +Iterator::Iterator() {} Iterator::~Iterator() = default; -bool Iterator::end() const { return end_; } +bool Iterator::end() const { return true; } Result>>> Iterator::next( const size_t _batch_size) { @@ -29,31 +22,7 @@ Result>>> Iterator::next( return error("End is reached."); } - std::vector>> batch; - - for (size_t i = 0; i < _batch_size; ++i) { - std::vector> new_row; - - for (int j = 0; j < num_cols_; ++j) { - auto ptr = sqlite3_column_text(stmt_.get(), j); - if (ptr) { - new_row.emplace_back( - std::string(std::launder(reinterpret_cast(ptr)))); - } else { - new_row.emplace_back(std::nullopt); - } - } - - batch.emplace_back(std::move(new_row)); - - step(); - - if (end()) { - return batch; - } - } - - return batch; + return error("TODO"); } -} // namespace sqlgen::sqlite +} // namespace sqlgen::postgres diff --git a/src/sqlgen/sqlite/Connection.cpp b/src/sqlgen/sqlite/Connection.cpp index 3081810..63da1ff 100644 --- a/src/sqlgen/sqlite/Connection.cpp +++ b/src/sqlgen/sqlite/Connection.cpp @@ -135,21 +135,6 @@ Result> Connection::read(const dynamic::SelectFrom& _query) { }); } -std::string Connection::to_sql(const dynamic::Statement& _stmt) noexcept { - return _stmt.visit([&](const auto& _s) -> std::string { - using S = std::remove_cvref_t; - if constexpr (std::is_same_v) { - return create_table_to_sql(_s); - } else if constexpr (std::is_same_v) { - return insert_to_sql(_s); - } else if constexpr (std::is_same_v) { - return select_from_to_sql(_s); - } else { - static_assert(rfl::always_false_v, "Unsupported type."); - } - }); -} - std::string Connection::select_from_to_sql( const dynamic::SelectFrom& _stmt) noexcept { using namespace std::ranges::views; @@ -199,6 +184,21 @@ Result Connection::start_write(const dynamic::Insert& _stmt) { return Nothing{}; } +std::string Connection::to_sql(const dynamic::Statement& _stmt) noexcept { + return _stmt.visit([&](const auto& _s) -> std::string { + using S = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return create_table_to_sql(_s); + } else if constexpr (std::is_same_v) { + return insert_to_sql(_s); + } else if constexpr (std::is_same_v) { + return select_from_to_sql(_s); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + }); +} + Result Connection::write( const std::vector>>& _data) { if (!stmt_) { diff --git a/tests/postgres/test_to_select_from.cpp b/tests/postgres/test_to_select_from.cpp index b8d45db..1aadbe1 100644 --- a/tests/postgres/test_to_select_from.cpp +++ b/tests/postgres/test_to_select_from.cpp @@ -14,12 +14,17 @@ struct TestTable { }; TEST(postgres, test_to_select_from) { - /*const auto select_from_stmt = + const auto select_from_stmt = sqlgen::transpilation::to_select_from(); - const auto conn = sqlgen::postgres::connect().value(); + const auto credentials = sqlgen::postgres::Credentials{.user = "postgres", + .password = "postgres", + .host = "localhost", + .dbname = "postgres", + .port = 5432}; + const auto conn = sqlgen::postgres::connect(credentials).value(); const auto expected = R"(SELECT "field1", "field2", "id", "nullable" FROM "TestTable";)"; - EXPECT_EQ(conn->to_sql(select_from_stmt), expected);*/ + EXPECT_EQ(conn->to_sql(select_from_stmt), expected); } } // namespace test_to_select_from