Skip to content

Instantly share code, notes, and snippets.

@egorpugin
Created February 19, 2020 21:24
Show Gist options
  • Save egorpugin/598176b1b31f95df0b22847cf10c7336 to your computer and use it in GitHub Desktop.
Save egorpugin/598176b1b31f95df0b22847cf10c7336 to your computer and use it in GitHub Desktop.
/*
c++: 17
deps:
- org.sw.demo.jtv.pqxx
- org.sw.demo.apolukhin.magic_get
*/
#include <boost/pfr/precise/core.hpp>
#include <pqxx/pqxx>
int main()
{
pqxx::connection c("dbname=sw user=webapp password=webapp");
pqxx::work tx(c);
/*tx.exec0("CREATE TEMP TABLE str (s text)");
tx.exec0("INSERT INTO str (s) VALUES ('foo')");
int i{0};
std::string out;
for (std::tuple<std::string> t : reader.iter<std::string>())
{
i++;
out = std::get<0>(t);
}*/
c.prepare("x", "select * from package_version");
auto r = tx.exec_prepared("x");
for (const auto &row : r)
{
// manual way
struct my_struct { int id, pkgver; };
my_struct data;
boost::pfr::for_each_field(data, [&row](auto &field, size_t idx)
{
field = row[idx].as<std::decay_t<decltype(field)>>();
});
// with macro
#define ASSIGN_PQXX_RESULT(t) \
t &operator=(const pqxx::row &row) \
{ \
boost::pfr::for_each_field( \
*this, [&row](auto &field, size_t idx) { field = row[idx].as<std::decay_t<decltype(field)>>(); }); \
return *this; \
}
struct my_struct2
{
int id, pkgver;
ASSIGN_PQXX_RESULT(my_struct2)
};
my_struct2 v;
v = row;
//
std::string x = row[0].c_str();
auto i = row[0].as<int>();
/*std::string x = r[0];
for (std::tuple<std::string> t : r)
{
}*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment