1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

#include <kooling/json/json.h>

#include <plog/Log.h>

#include <cstddef>
#include <string>
#include <string_view>
#include <utility>

#include <pqxx/pqxx>
#include <pqxx/version>

namespace kooling::database {

struct config
{
    std::string name;
    std::string host;
    int port;
    std::string user;
    std::string pass;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(config, name, host, port, user, pass);

static constexpr int pqxx_version_number{ PQXX_VERSION_MAJOR * 100 + PQXX_VERSION_MINOR };

template<typename... Ts>
struct params : std::tuple<std::decay_t<Ts>...>
{
    params(Ts... ts)
        : std::tuple<std::decay_t<Ts>...>{ std::forward<Ts>(ts)... }
    {}

    pqxx::params to_pqxx_params() const
    {
        pqxx::params ps;
        append<0>(ps);
        return ps;
    };

private:
    template<std::size_t Nth>
    void append(pqxx::params& ps) const
    {
        if constexpr (Nth < sizeof...(Ts))
        {
            ps.append(std::get<Nth>(*this));
            append<Nth + 1>(ps);
        }
    }
};

template<typename... Ts>
params(Ts...) -> params<std::decay_t<Ts>...>;

class database
{
public:
    explicit database(const config&conf);
    explicit database(std::string_view conn_string);

    template<typename F>
    bool query(std::string_view q, F&& f)
    {
        return query(q, params<>{}, std::forward<F>(f));
    }

    template<typename F, typename... Params>
    bool query(std::string_view q, const params<Params...>& ps, F&& f)
    {
        return query(std::make_index_sequence<sizeof...(Params)>{}, q, ps, std::forward<F>(f));
    }

    std::string quote(std::string_view str) const
    {
        return d_connection.quote(str);
    }

    std::string quote_table(std::string_view table_name) const
    {
        return d_connection.quote_table(table_name);
    }

private:
    template<typename F, typename P, std::size_t... Is>
    bool query(std::index_sequence<Is...>, std::string_view q, const P& ps, F&& f)
    {
        try
        {
            pqxx::nontransaction txn{ d_connection };
            if constexpr (pqxx_version_number > 708)
            {
                txn.for_query(pqxx::zview{ q }, std::forward<F>(f), ps.to_pqxx_params());
            }
            else
            {
                txn.exec_params(pqxx::zview{ q }, std::get<Is>(ps)...).for_each(std::forward<F>(f));
            }
        }
        catch (const std::exception& exn)
        {
            PLOG_ERROR << "database error: " << exn.what();
            return false;
        }
        return true;
    } 

private:
    pqxx::connection d_connection;
};

} // namespace kooling::database