-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.cpp
202 lines (135 loc) · 5.65 KB
/
test.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
// uncomment the following line if needed:
#define NO_STD_OPTIONAL
#include "mysql+++/mysql+++.h"
using namespace std;
using namespace daotk::mysql;
int main()
{
// connection
connection my{ "localhost", "tester", "tester", "test_test_test" };
if (!my) {
cout << "Connection failed" << endl;
return -1;
}
/* This testing program uses a table defined as follows:
create table person(
id int unsigned auto_increment primary key,
name varchar(50),
weight double,
birthday date,
avatar int
);
insert into person(name, weight, birthday, avatar) values
("Nguyen Van X", 62.54, "1981-07-18", 4),
("Tran Thi Y", 56.78, "1999-05-12", 3),
("Bui Xuan Z", NULL, NULL, 5);
+----+--------------+--------+------------+--------+
| id | name | weight | birthday | avatar |
+----+--------------+--------+------------+--------+
| 1 | Nguyen Van X | 62.54 | 1981-07-18 | 4 |
| 2 | Tran Thi Y | 56.78 | 1999-05-12 | 3 |
| 3 | Bui Xuan Z | NULL | NULL | 5 |
+----+--------------+--------+------------+--------+
*/
int sample_count = 0;
try {
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Simple query and simple way to get back a single value:
auto row_count = my.query("select count(*) from person")
.get_value<int>();
cout << "Count: " << row_count << endl;
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Simple query without getting any value back (note that query() also
// works here but is less performant, as the returned `result' object
// when not stored is automatically destroyed):
my.exec("update person set avatar = floor(rand()*10)");
cout << "Successful" << endl;
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// 1) queries can be passed using convenient printf-style
// 2) `optional` type can be used to know whether the returned value is available
auto avg_weight = my.query("select avg(weight) from person where avatar >= %d or weight <= %f", 2, 70.5)
.get_value<optional<double>>();
if (avg_weight)
cout << "Mean weight: " << *avg_weight << endl;
else cout << "No max weight value" << endl;
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Example showing how to getting back multiple typed values in a row:
int id;
string name;
optional<double> weight;
auto res = my.query("select id, name, weight from person where id = %d", 3);
if (!res.is_empty()) {
res.fetch(id, name, weight);
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
cout << endl;
}
else cout << "Query failed" << endl;
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// A multi-row data query using lambda function with row fields in parameter:
// (`optional` type can also be used, and `datetime` is a supoorting type is used for handing date and time)
my.query("select id, name, weight, birthday from person")
.each([](int id, string name, optional<double> weight, optional<datetime> birthday) {
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
if (birthday) cout << ", birthday: " << birthday->to_sql();
cout << endl;
// return true to continue, false to stop
return true;
});
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Another way to iterate through rows using a container-like object:
// (Note that unsuccessful/no-result queries will return an empty container)
res = my.query("select id, name, weight from person");
for (auto& row : res.as_container<int, string, optional<double>>()) {
weight.reset();
tie(id, name, weight) = row;
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
cout << endl;
}
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Of course, the-old-good-time-style loop is also possible:
res = my.query("select id, name, weight from person");
while (!res.eof()) {
weight.reset();
res.fetch(id, name, weight);
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
cout << endl;
res.next();
}
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// Use `mquery` to handle multiple queries, or queries that returns more than one dataset in an execution;
// make sure to enable `CLIENT_MULTI_STATEMENTS' first using `connect_options::client_flags` when connecting, or
// using `set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON)` after connection
my.set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON);
auto datasets = my.mquery("select count(*) from person; select id, name, weight from person");
cout << datasets[0].get_value<int>(0) << endl;
datasets[1].each([](int id, string name, optional<double> weight) {
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
cout << endl;
return true;
});
cout << "** QUERY EXAMPLE " << ++sample_count << endl;
// We also have support for prepared statements and binding:
prepared_stmt stmt(my, "select id, name, weight from person where weight > ? or weight is null");
double pweight = 60;
stmt.bind_param(pweight);
stmt.bind_result(id, name, weight);
stmt.execute();
while (stmt.fetch()) {
cout << "ID: " << id << ", name: " << name;
if (weight) cout << ", weight: " << *weight;
cout << endl;
}
} catch (mysql_exception exp) {
cout << "Query #" << sample_count << " failed with error: " << exp.error_number() << " - " << exp.what() << endl;
}
catch (mysqlpp_exception exp) {
cout << "Query #" << sample_count << " failed with error: " << exp.what() << endl;
}
return 0;
}