Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (network) (push) Successful in 21m3s
Foundation / checks (client) (push) Successful in 22m14s
Foundation / checks (tsan) (push) Successful in 24m52s
Foundation / checks (postgres) (push) Failing after 36m19s
Foundation / checks (checks) (push) Successful in 1h10m25s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
56 lines
2.5 KiB
C++
56 lines
2.5 KiB
C++
#pragma once
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#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>>>;
|
|
// Reads one section of a libpq connection service file (M8.3): host, hostaddr,
|
|
// port, dbname, user and sslrootcert (as ca_file). sslmode may only be verify-full
|
|
// (or disable for a local socket), and connect_timeout is accepted and ignored (the
|
|
// adapter has its own deadlines). Anything else (password, passfile, options, a
|
|
// missing or repeated section, a repeated key, a file over 4 KiB) is refused with
|
|
// config. password_file stays empty: the caller supplies it.
|
|
[[nodiscard]] std::expected<PgOptions, PgError> read_pg_service(const std::string& file,
|
|
std::string_view section);
|
|
// 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);
|
|
// True only for a connected, idle session outside an explicit transaction.
|
|
// Read-only status inspection; no query, lock or transaction is started.
|
|
[[nodiscard]] bool transaction_idle() const noexcept;
|
|
// Drop the owned connection and any server-side transaction. Idempotent;
|
|
// future queries return closed. No SQL, allocation or automatic reconnect.
|
|
void disconnect() noexcept;
|
|
[[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
|