-
Notifications
You must be signed in to change notification settings - Fork 33
/
async_single_query.cpp
85 lines (67 loc) · 2.39 KB
/
async_single_query.cpp
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
#include "utils.hpp"
#include <amy/connector.hpp>
#include <amy/placeholders.hpp>
#include <iostream>
global_options opts;
void handle_store_result(AMY_SYSTEM_NS::error_code const& ec,
amy::result_set rs,
amy::connector& connector)
{
check_error(ec);
std::cout
<< "Affected rows: " << rs.affected_rows()
<< ", field count: " << rs.field_count()
<< ", result set size: " << rs.size()
<< std::endl;
const auto& fields_info = rs.fields_info();
for (const auto& row : rs) {
std::cout
<< fields_info[0].name() << ": " << row[0].as<std::string>() << ", "
<< fields_info[1].name() << ": " << row[1].as<amy::sql_bigint>()
<< std::endl;
}
}
void handle_query(AMY_SYSTEM_NS::error_code const& ec,
amy::connector& connector)
{
check_error(ec);
connector.async_store_result(
std::bind(handle_store_result,
amy::placeholders::error,
amy::placeholders::result_set,
std::ref(connector)));
}
void handle_connect(AMY_SYSTEM_NS::error_code const& ec,
amy::connector& connector)
{
check_error(ec);
auto statement =
"SELECT character_set_name, maxlen\n"
"FROM information_schema.character_sets\n"
"WHERE character_set_name LIKE 'latin%'";
connector.async_query(statement,
std::bind(handle_query,
amy::placeholders::error,
std::ref(connector)));
}
int main(int argc, char* argv[]) {
parse_command_line_options(argc, argv);
AMY_ASIO_NS::io_service io_service;
amy::connector connector(io_service);
connector.async_connect(opts.tcp_endpoint(),
opts.auth_info(),
opts.schema,
amy::default_flags,
std::bind(handle_connect,
amy::placeholders::error,
std::ref(connector)));
try {
io_service.run();
} catch (AMY_SYSTEM_NS::system_error const& e) {
report_system_error(e);
} catch (std::exception const& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
// vim:ft=cpp sw=4 ts=4 tw=80 et