#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <pgsql/libpq-fe.h>
#include <memory>
#include <unordered_map>
#include <pqxx/pqxx>

class Database {
public:
    Database(const std::string &conninfo);
    
    typedef std::unordered_map<std::string, std::string> FieldMap;
    typedef std::vector<FieldMap> FieldList;

    std::vector<std::map<std::string, std::string>> query(const std::string &sql);
    void prepare(const std::string &stmtName, const std::string &sql);
    FieldList execute(
           const std::string &stmtName, 
           const std::vector<std::string> &params = {}
    );
    void remove(const std::string &stmtName);
    bool isOpen() const { return connection_ && connection_->is_open(); }
    bool isValid() const;

private:
    std::unique_ptr<pqxx::connection> connection_;
};
