Added write functions

This commit is contained in:
Dr. Patrick Urbanke
2025-04-05 10:42:14 +02:00
parent 412205d2a8
commit 7f9393ee46
7 changed files with 177 additions and 10 deletions

View File

@@ -7,5 +7,6 @@
#include "sqlgen/PrimaryKey.hpp"
#include "sqlgen/Ref.hpp"
#include "sqlgen/Result.hpp"
#include "sqlgen/write.hpp"
#endif

View File

@@ -0,0 +1,36 @@
#ifndef SQLGEN_INTERNAL_TO_STR_HPP_
#define SQLGEN_INTERNAL_TO_STR_HPP_
#include <optional>
#include <string>
#include <type_traits>
#include "../parsing/has_reflection_method.hpp"
#include "../parsing/is_nullable.hpp"
namespace sqlgen::internal {
template <class T>
std::optional<std::string> to_str(const T& _val) {
using Type = std::remove_cvref_t<T>;
if constexpr (parsing::is_nullable_v<Type>) {
if (!_val) {
return std::nullopt;
} else {
return to_str(*_val);
}
} else if constexpr (parsing::has_reflection_method<Type>) {
return to_str(_val.reflection());
} else if constexpr (std::is_same_v<Type, std::string>) {
return _val;
} else {
return std::to_string(_val);
}
}
} // namespace sqlgen::internal
#endif

View File

@@ -0,0 +1,25 @@
#ifndef SQLGEN_INTERNAL_TO_STR_VEC_HPP_
#define SQLGEN_INTERNAL_TO_STR_VEC_HPP_
#include <optional>
#include <rfl.hpp>
#include <string>
#include <type_traits>
#include <vector>
#include "to_str.hpp"
namespace sqlgen::internal {
template <class T>
std::vector<std::optional<std::string>> to_str_vec(const T& _t) {
return rfl::apply(
[](auto... _ptrs) {
return std::vector<std::optional<std::string>>({to_str(*_ptrs)...});
},
rfl::to_view(_t).values());
}
} // namespace sqlgen::internal
#endif

65
include/sqlgen/write.hpp Normal file
View File

@@ -0,0 +1,65 @@
#ifndef SQLGEN_WRITE_HPP_
#define SQLGEN_WRITE_HPP_
#include <iterator>
#include <optional>
#include <rfl.hpp>
#include <string>
#include <type_traits>
#include <vector>
#include "Connection.hpp"
#include "Ref.hpp"
#include "Result.hpp"
#include "internal/to_str_vec.hpp"
#include "parsing/to_create_table.hpp"
#include "parsing/to_insert.hpp"
namespace sqlgen {
template <class ItBegin, class ItEnd>
Result<Nothing> write(const Ref<Connection>& _conn, ItBegin _begin,
ItEnd _end) {
using T =
std::remove_cvref_t<typename std::iterator_traits<ItBegin>::value_type>;
const auto start_write = [&](const auto&) -> Result<Nothing> {
const auto insert_stmt = parsing::to_insert<T>();
return _conn->start_write(insert_stmt);
};
const auto write = [&](const auto&) -> Result<Nothing> {
try {
std::vector<std::vector<std::optional<std::string>>> data;
for (auto it = _begin; it != _end; ++it) {
data.emplace_back(internal::to_str_vec(*it));
if (data.size() == 50000) {
_conn->write(data).value();
data.clear();
}
}
if (data.size() != 0) {
_conn->write(data).value();
}
return Nothing{};
} catch (std::exception& e) {
_conn->end_write();
return error(e.what());
}
};
const auto end_write = [&](const auto&) -> Result<Nothing> {
return _conn->end_write();
};
const auto create_table_stmt = parsing::to_create_table<T>();
return _conn->execute(_conn->to_sql(create_table_stmt))
.and_then(start_write)
.and_then(write)
.and_then(end_write);
}
} // namespace sqlgen
#endif