hydra/src/libhydra/db.hh

44 lines
1 KiB
C++
Raw Normal View History

2015-07-07 08:17:21 +00:00
#pragma once
#include <pqxx/pqxx>
#include "util.hh"
struct Connection : pqxx::connection
{
Connection() : pqxx::connection(getFlags()) { };
2015-07-07 08:29:43 +00:00
std::string getFlags()
2015-07-07 08:17:21 +00:00
{
2015-07-07 08:29:43 +00:00
using namespace nix;
auto s = getEnv("HYDRA_DBI", "dbi:Pg:dbname=hydra;");
std::string prefix = "dbi:Pg:";
if (std::string(s, 0, prefix.size()) != prefix)
2015-07-07 08:17:21 +00:00
throw Error("$HYDRA_DBI does not denote a PostgreSQL database");
return concatStringsSep(" ", tokenizeString<Strings>(string(s, prefix.size()), ";"));
}
};
class receiver : public pqxx::notification_receiver
2015-07-07 08:17:21 +00:00
{
std::experimental::optional<std::string> status;
public:
2015-07-07 08:17:21 +00:00
receiver(pqxx::connection_base & c, const std::string & channel)
: pqxx::notification_receiver(c, channel) { }
2015-07-07 08:29:43 +00:00
void operator() (const std::string & payload, int pid) override
2015-07-07 08:17:21 +00:00
{
status = payload;
2015-07-07 08:17:21 +00:00
};
std::experimental::optional<std::string> get() {
auto s = status;
status = std::experimental::nullopt;
return s;
2015-07-07 08:17:21 +00:00
}
};