...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
To use the static interface, we must first define a data structure that describes the shape of our rows. We have several options:
struct
with BOOST_DESCRIBE_STRUCT
to enable reflection on it.
pfr_by_name
or pfr_by_position
to use PFR automatic reflection capabilities.
std::tuple
.
We will start with Boost.Describe and explain the other options later. For a comparison, please refer to this table.
For example, given the following table definition:
constexpr const char* table_definition = R"%( CREATE TEMPORARY TABLE posts ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR (256) NOT NULL, body TEXT NOT NULL ) )%";
We can define our row type like this:
// We can use a plain struct with ints and strings to describe our rows. // This must be placed at the namespace level struct post { int id; std::string title; std::string body; }; // We use BOOST_DESCRIBE_STRUCT to add reflection capabilities to post. // We must list all the fields that should be populated by Boost.MySQL BOOST_DESCRIBE_STRUCT(post, (), (id, title, body))
And write the following to query our table:
static_results<post> result; conn.execute("SELECT id, title, body FROM posts", result); for (const post& p : result.rows()) { // Process the post as required std::cout << "Title: " << p.title << "\n" << p.body << "\n"; }
Note that static_results::rows
returns a boost::span
object, which is a C++11 backport of
std::span
. The span points into memory owned by
the static_results
object.
Care must be taken not to use this view object after the static_results
goes out of scope.
Columns in the query are matched to fields in the struct by name. If a struct field cannot be matched to any query column, an error is issued. Extra columns in the query are ignored.
If your query contains columns with names that don't qualify as C++ identifiers, you can use SQL aliases. For example, given this struct:
struct statistics { std::string company; double average; double max_value; }; BOOST_DESCRIBE_STRUCT(statistics, (), (company, average, max_value))
You can write your query as:
// Summing 0e0 is MySQL way to cast a DECIMAL field to DOUBLE constexpr const char* sql = R"%( SELECT IFNULL(AVG(salary), 0.0) + 0e0 AS average, IFNULL(MAX(salary), 0.0) + 0e0 AS max_value, company_id AS company FROM employee GROUP BY company_id )%"; static_results<statistics> result; conn.execute(sql, result);
The static interface will try to validate as soon as possible that the provided row type is compatible with the schema returned by the server. This process is known as metadata checking, and is performed before reading any data. The following checks are performed:
std::int32_t
is compatible with TINYINT
(1 byte integer), but not with
BIGINT
(8 byte integer).
For a full list of allowable field types, refer
to this table.
NULL
, your type
must account for it. You can use std::optional<T>
or boost::optional<T>
for these columns.
For example, if your table is defined like this:
constexpr const char* table_definition = R"%( CREATE TEMPORARY TABLE posts_v2 ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR (256) NOT NULL, body TEXT ) )%";
Using the post
type we defined
above will cause an error, because the body
field may be NULL
, but our
type doesn't account for it. In this case, the correct definition would be:
struct post_v2 { int id; std::string title; std::optional<std::string> body; // body may be NULL }; BOOST_DESCRIBE_STRUCT(post_v2, (), (id, title, body))
If you're using C++20 or above, you can use Boost.PFR to reflect types without
the BOOST_DESCRIBE_STRUCT
macro:
// post_v3 doesn't contain any metadata - we're not using BOOST_DESCRIBE_STRUCT here struct post_v3 { int id; std::string title; std::string body; };
PFR reflection can be enabled in Boost.MySQL by using pfr_by_name
:
// pfr_by_name is a marker type. It tells static_results to use // Boost.PFR for reflection, instead of Boost.Describe. static_results<pfr_by_name<post_v3>> result; // As with Boost.Describe, query fields are matched to struct // members by name. This means that the fields in the query // may appear in any order. conn.execute("SELECT body, id, title FROM posts", result); // Note that result.rows() is a span of post_v3 objects, // rather than pfr_by_name<post_v3> objects. post_v3 // is the underlying row type for pfr_by_name<post_v3> for (const post_v3& p : result.rows()) { // Process the post as required std::cout << "Title: " << p.title << "\n" << p.body << "\n"; }
Note that pfr_by_name
is what we call a marker type - an empty type that tells
classes like static_results
how to reflect a type. If no marker type is used, Boost.Describe is used to
retrieve reflection data for struct types.
pfr_by_position
is similar to pfr_by_name
,
but will match columns in the query to struct fields by position, rather than
name. It only requires C++14 to work. For instance:
// pfr_by_position is another marker type. // Fields in post_v3 must appear in the same order as in the query, // as matching will be done by position. static_results<pfr_by_position<post_v3>> result; conn.execute("SELECT id, title, body FROM posts", result); // The underlying row type is post_v3 for (const post_v3& p : result.rows()) { // Process the post as required std::cout << "Title: " << p.title << "\n" << p.body << "\n"; }
Please refer to this table for a comparison with Boost.Describe.
You can also use std::tuple
s as row types. This can be handy for
simple queries:
static_results<std::tuple<std::int64_t>> result; conn.execute("SELECT COUNT(*) FROM employee", result); std::cout << "Number of employees: " << std::get<0>(result.rows()[0]) << "\n";
Fields in tuples are matched to query columns by order. The query must return as many columns as fields the tuple has, at least. Any extra trailing columns in the query are ignored.
You can use both with the dynamic interface. Please refer to the sections on multi-resultset operations and multi-function operations for more information.
Should I use Boost.Describe, Boost.PFR or tuples? Each one has its advantages and drawbacks. This table may help you decide:
Technique |
Sample code |
Minimum C++ standard |
Comments |
Feature test macro |
---|---|---|---|---|
Boost.Describe |
// Definition should be at namespace scope struct post { int id; std::string title; std::string body; }; BOOST_DESCRIBE_STRUCT(post, (), (id, title, body))
// Usage static_results<post> result; conn.execute("SELECT title, body, id FROM posts", result); |
C++14 |
|
|
Boost.PFR using names |
// Definition should be at namespace scope struct post { int id; std::string title; std::string body; };
// Usage static_results<pfr_by_name<post>> result; conn.execute("SELECT title, body, id FROM posts", result); |
C++20 |
|
|
Boost.PFR using field position |
// Definition should be at namespace scope struct post { int id; std::string title; std::string body; };
// Usage static_results<pfr_by_position<post>> result; conn.execute("SELECT id, title, body FROM posts", result); |
C++17 |
|
|
Standard tuples |
using tuple_t = std::tuple<int, std::string, std::string>; static_results<tuple_t> result; conn.execute("SELECT id, title, body FROM posts", result); |
C++14 |
|
|
Note that using the static interface always requires C++14, at least. The
BOOST_MYSQL_CXX14
test macro
is defined only if the static interface is supported. Including the static
interface headers on an unsupported compiler doesn't cause any error, but classes
like static_results
and static_execution_state
are not defined. The test macro is brought on scope by any of the static interface
headers.
All the types used within your Describe structs or tuples must be within the
following table. A Describe struct or tuple composed of valid field types models
the StaticRow
concept.
The following table is a reference of the C++ types that can be used in a
StaticRow
and their compatibility
with MySQL database types:
C++ type |
Compatible with... |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Any type compatible with |
|
Any type compatible with |