Make sure the first test compiles

This commit is contained in:
Dr. Patrick Urbanke
2025-04-21 18:05:12 +02:00
parent 39d78d7cb5
commit b167d37dca
6 changed files with 84 additions and 61 deletions

View File

@@ -32,22 +32,32 @@ class Connection : public sqlgen::Connection {
Result<Nothing> commit() final { return execute("COMMIT;"); }
Result<Nothing> execute(const std::string& _sql) noexcept final;
Result<Nothing> execute(const std::string& _sql) noexcept final {
return error("TODO");
}
Result<Ref<IteratorBase>> read(const dynamic::SelectFrom& _query) final;
Result<Ref<IteratorBase>> read(const dynamic::SelectFrom& _query) final {
return error("TODO");
}
std::string to_sql(const dynamic::Statement& _stmt) noexcept final;
Result<Nothing> start_write(const dynamic::Insert& _stmt) final;
Result<Nothing> start_write(const dynamic::Insert& _stmt) final {
return error("TODO");
}
Result<Nothing> end_write() final;
Result<Nothing> end_write() final { return error("TODO"); }
Result<Nothing> write(
const std::vector<std::vector<std::optional<std::string>>>& _data) final;
const std::vector<std::vector<std::optional<std::string>>>& _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_;

View File

@@ -16,7 +16,7 @@ namespace sqlgen::postgres {
class Iterator : public sqlgen::IteratorBase {
public:
Iterator(const StmtPtr& _stmt, const ConnPtr& _conn);
Iterator();
~Iterator();

View File

@@ -33,4 +33,43 @@ typename Connection::ConnPtr Connection::make_conn(
return ConnPtr::make(std::shared_ptr<PGconn>(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<decltype(_s)>;
/*if constexpr (std::is_same_v<S, dynamic::CreateTable>) {
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 {
return "TODO";
// static_assert(rfl::always_false_v<S>, "Unsupported type.");
}
});
}
} // namespace sqlgen::postgres

View File

@@ -1,4 +1,4 @@
#include "sqlgen/sqlite/Iterator.hpp"
#include "sqlgen/postgres/Iterator.hpp"
#include <ranges>
#include <rfl.hpp>
@@ -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<std::vector<std::vector<std::optional<std::string>>>> Iterator::next(
const size_t _batch_size) {
@@ -29,31 +22,7 @@ Result<std::vector<std::vector<std::optional<std::string>>>> Iterator::next(
return error("End is reached.");
}
std::vector<std::vector<std::optional<std::string>>> batch;
for (size_t i = 0; i < _batch_size; ++i) {
std::vector<std::optional<std::string>> 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<const char*>(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

View File

@@ -135,21 +135,6 @@ Result<Ref<IteratorBase>> 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<decltype(_s)>;
if constexpr (std::is_same_v<S, dynamic::CreateTable>) {
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;
@@ -199,6 +184,21 @@ Result<Nothing> 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<decltype(_s)>;
if constexpr (std::is_same_v<S, dynamic::CreateTable>) {
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.");
}
});
}
Result<Nothing> Connection::write(
const std::vector<std::vector<std::optional<std::string>>>& _data) {
if (!stmt_) {

View File

@@ -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<TestTable>();
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