pg_async
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pg_async Documentation

pg_async is an unofficial PostreSQL database asynchronous client library written in modern C++ (std=c++11) on top of boost::asio library used for asynchronous network input-output.

Features

Short usage overview

All classes are located in namespace tip::db::pg, if not specified other way.

Registering a connection

#include <tip/db/pg.hpp>
using namespace tip::db::pg;
db_service::add_connection("main=tcp://user:pass@the-database-server:5432[databasename]");
db_service::add_connection("logs=tcp://user:pass@the-database-server:5432[logs_db]");
See Also
Connection string and database alias
tip::db::pg::db_service reference

Transactions

db_service::begin(
"main"_db, // Database alias
// Asynchronous callback, will be called as soon as a connection becomes
// idle, and a new transaction will be started in it.
[] (transaction_ptr tran)
{
// Run the queries here
// A transaction MUST be explicitly commited or it will be rolled back
tran->commit();
},
// Transaction error handler.
[](db_error const& error)
{
}
);
See Also
Transactions
Connection string and database alias
Threads and thread safety
tip::db::pg::transaction for reference

Querying

#include <tip/db/pg.hpp>
query("main"_db, "select * from pg_catalog.pg_types").run_async(
[&](transaction_ptr t, resultset res, bool complete) {
// Process the query results
},
[&](db_error const& err) {
// Handle query error
});
See Also
Running queries for more information
Errors and exceptions
Callback types
Values input/output
tip::db::pg::query for reference

Processing query results

resultset res; // Passed to a callback from query
if (res) {
for (auto row : res) { // Row iteration
for (auto field : row) {
std::string value = field.coalesce("default value");
}
}
}
See Also
Processing query results for more information
Values input/output
tip::db::pg::resultset for reference
tip::db::pg::resultset::row for reference
tip::db::pg::resultset::field for reference