Files
aevolution/include/evo/persistence/postgres.hpp
T

41 lines
1.4 KiB
C++

#pragma once
#include <array>
#include <chrono>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <vector>
namespace evo::persistence {
enum class PgError : std::uint8_t { config, connect, version, sql, unknown, bounds, closed };
struct PgFailure {
PgError kind{};
std::array<char, 6> sqlstate{}; // Sanitized SQLSTATE only, never server text.
};
struct PgOptions {
std::string host, hostaddr, port, database, user, password_file, ca_file;
};
using PgRows = std::vector<std::vector<std::optional<std::string>>>;
// Linux I/O-owner adapter. Query text is trusted application SQL, values are parameters.
// No automatic retry. unknown means the command may have executed; connection is closed.
class Postgres {
public:
~Postgres();
Postgres(const Postgres&) = delete;
Postgres& operator=(const Postgres&) = delete;
[[nodiscard]] static std::expected<std::unique_ptr<Postgres>, PgFailure>
connect(const PgOptions& options, std::chrono::milliseconds timeout);
[[nodiscard]] std::expected<PgRows, PgFailure> query(const std::string& sql,
std::span<const std::string> parameters,
std::chrono::milliseconds timeout);
private:
struct Impl;
explicit Postgres(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::persistence