File: callbacks.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (178 lines) | stat: -rw-r--r-- 5,858 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

//[example_callbacks

/**
 * This example demonstrates how to use callbacks when using async functions.
 * This can be a good choice when targeting a standard lower than C++20.
 * This example uses the 'boost_mysql_examples' database, which you
 * can get by running db_setup.sql.
 */

#include <boost/mysql/any_connection.hpp>
#include <boost/mysql/connect_params.hpp>
#include <boost/mysql/diagnostics.hpp>
#include <boost/mysql/results.hpp>
#include <boost/mysql/row_view.hpp>
#include <boost/mysql/with_params.hpp>

#include <boost/asio/io_context.hpp>
#include <boost/system/error_code.hpp>

#include <functional>
#include <iostream>
#include <memory>

// When using callbacks, we usually employ error codes instead of exceptions.
using boost::system::error_code;

namespace mysql = boost::mysql;
namespace asio = boost::asio;

// Prints a database employee to stdout
void print_employee(mysql::row_view employee)
{
    std::cout << "Employee '" << employee.at(0) << " "   // first_name (string)
              << employee.at(1) << "' earns "            // last_name  (string)
              << employee.at(2) << " dollars yearly\n";  // salary     (double)
}

// A session object, containing all variables that need to be kept alive for our session.
// We will use a shared_ptr to ensure that all these variables are kept alive
// until the last callback is executed
class session : public std::enable_shared_from_this<session>
{
    mysql::connect_params conn_params;  // MySQL credentials and other connection config
    mysql::any_connection conn;         // Represents the connection to the MySQL server
    mysql::results result;              // A result from a query
    mysql::error_code final_error;      // Will be set in case of error
    mysql::diagnostics diag;            // Will be populated with info about server errors
    const char* company_id;             // The ID of the company whose employees we want to list. Untrusted.
public:
    session(
        asio::io_context& ctx,
        const char* server_hostname,
        const char* username,
        const char* password,
        const char* company_id
    )
        : conn(ctx), company_id(company_id)
    {
        conn_params.server_address.emplace_host_and_port(server_hostname);
        conn_params.username = username;
        conn_params.password = password;
        conn_params.database = "boost_mysql_examples";
    }

    // Accessor for error information, so main can access it
    error_code get_error() const { return final_error; }
    const boost::mysql::diagnostics& get_diagnostics() const { return diag; }

    // Initiates the callback chain
    void start()
    {
        // Will call on_connect when the connect operation completes.
        // The session object is kept alive with the shared_ptr that shared_from_this produces
        conn.async_connect(
            conn_params,
            diag,
            std::bind(&session::on_connect, shared_from_this(), std::placeholders::_1)
        );
    }

    void on_connect(error_code ec)
    {
        // If there was an error, stop the callback chain
        if (ec)
        {
            final_error = ec;
            return;
        }

        // Initiate the query execution. company_id is an untrusted value.
        // with_params will securely compose a SQL query and send it to the server for execution.
        // Returned rows will be read into result.
        // We use the callback chain + shared_ptr technique again
        conn.async_execute(
            mysql::with_params(
                "SELECT first_name, last_name, salary FROM employee WHERE company_id = {}",
                company_id
            ),
            result,
            diag,
            std::bind(&session::on_execute, shared_from_this(), std::placeholders::_1)
        );
    }

    void on_execute(error_code ec)
    {
        // If there was an error, stop the callback chain
        if (ec)
        {
            final_error = ec;
            return;
        }

        // Print the rows returned by the query
        for (boost::mysql::row_view employee : result.rows())
        {
            print_employee(employee);
        }

        // Notify the MySQL server we want to quit and then close the socket
        conn.async_close(diag, std::bind(&session::finish, shared_from_this(), std::placeholders::_1));
    }

    void finish(error_code err) { final_error = err; }
};

void main_impl(int argc, char** argv)
{
    if (argc != 4 && argc != 5)
    {
        std::cerr << "Usage: " << argv[0] << " <username> <password> <server-hostname> [company-id]\n";
        exit(1);
    }

    // The execution context, required to run I/O operations.
    boost::asio::io_context ctx;

    // The company_id whose employees we will be listing. This
    // is user-supplied input, and should be treated as untrusted.
    const char* company_id = argc == 5 ? argv[4] : "HGS";

    // Create the session object and launch it
    auto sess = std::make_shared<session>(ctx, argv[3], argv[1], argv[2], company_id);
    sess->start();

    // Run the callback chain until it completes
    ctx.run();

    // Check for errors
    if (error_code ec = sess->get_error())
    {
        std::cerr << "Error: " << ec << ": " << ec.message() << '\n'
                  << "Server diagnostics: " << sess->get_diagnostics().server_message() << std::endl;
        exit(1);
    }
}

int main(int argc, char** argv)
{
    try
    {
        main_impl(argc, argv);
    }
    catch (const std::exception& err)
    {
        std::cerr << "Error: " << err.what() << std::endl;
        return 1;
    }
}

//]