pg_async
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Processing query results

Result set (tip::db::pg::resultset) is passed to client via supplied callback when a query receives results from the server. A tip::db::pg::resultset is an object that provides an interface to reading and converting underlying data buffers. It is lightweight and is designed to be copied around by value. It can be stored in memory for later use.

A resultset object mimics standard container interface for the data rows. It provides random access to data rows via indexing operator and random access iterators.

A resultset row mimics standard container interface for the fields. It provides index operators for accessing fields by their index or by their name. It also provides random access iterators for iterating the fields.

Result set provides interface for reading field definitions.

Checking the resultset

resultset res;
if (res.size() > 0) {
// Process the result set
}
if (!result.empty()) {
// Process the result set
}
if (res) {
// Process the result set
}

Accessing rows and fields

resultset res;
for (auto row : res) {
for (auto field : row) {
// Do something with the field value
}
}
auto first_row = res.front();
auto last_row = res.back();
auto mid_row = res[ res.size() / 2 ]; // Index access
auto first_field = first_row[0]; // Access field by index
auto id_field = last_row["id"]; // Access field by name

Field descriptions

resultset res;
// Get a reference to field descriptions
row_description_type const& rd = res.row_description();
See Also
tip::db::pg::field_description for reference

Accessing field data

Field values are extracted via the system of buffer parsers. Default format for PosgreSQL protocol is text, so most if a data type is extractable from a stream, it can be extracted from a text data buffer. If a special parsing routine is required, a text data parser must be provided. All datatypes can be extracted as strings.

For binary data format a data parser must be provided.

An exception tip::db::pg::value_is_null will be thrown if the field is null. A boost::optional can be used as a nullable datatype, the value will be cleared, and no exception will be thrown. A coalesce function can be used to get a default value if the field is null.

field f = row[0];
f.to(s); // template parameter deduced from the paramter
int i = f.as<int>();
f.to(i);
if (f.is_null()) {
boost::optional<int> nullable = f.as< boost::optional<int> >();
int val = f.coalesce(100500);
}
See Also
Values input/output

Using tuples to extract row data

A std::tuple can be used to extract data from row fields

struct data {
int id;
};
query(alias, "select id, name from some_table")
([](transaction_ptr t, resultset res, bool complete) {
for (auto row : res) {
data d;
row.to(std::tie(d.id, d.name));
}
},
[](error::db_error const&) {
});
See Also
Values input/output
Todo:

concept of an interface for a datatype that can be stored/retrieved from the database. Template functions enabled for such an interface.

non-select command results

See Also
Running queries
tip::db::pg::resultset
tip::db::pg::query
tip::db::pg::field_definition