SNode.C
Loading...
Searching...
No Matches
testmariadb.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 * 2021, 2022 Daniel Flockert
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "core/SNodeC.h"
22#include "core/timer/Timer.h"
23#include "database/mariadb/MariaDBClient.h"
24#include "database/mariadb/MariaDBCommandSequence.h"
25#include "utils/Config.h"
26
27#ifndef DOXYGEN_SHOULD_SKIP_THIS
28
29#include "log/Logger.h"
30
31#include <cstdlib>
32#include <functional>
33#include <mysql.h>
34#include <string>
35
36#endif /* DOXYGEN_SHOULD_SKIP_THIS */
37
38int main(int argc, char* argv[]) {
39 utils::Config::addStringOption("--db-host", "Hostname of IP-Address of Server", "[hostname|IP-address]", "localhost", true);
40
41 core::SNodeC::init(argc, argv);
42
43 const database::mariadb::MariaDBConnectionDetails details = {
44 .hostname = utils::Config::getStringOptionValue("--db-host"),
45 .username = "snodec",
46 .password = "pentium5",
47 .database = "snodec",
48 .port = 3306,
49 .socket = "/run/mysqld/mysqld.sock",
50 .flags = 0,
51 };
52
53 // CREATE DATABASE snodec;
54 // CREATE TABLE snodec (username text NOT NULL, password text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
55 // CREATE USER 'snodec'@localhost IDENTIFIED BY 'pentium5';
56 // GRANT ALL PRIVILEGES ON *.* TO 'snodec'@localhost;
57 // GRANT ALL PRIVILEGES ON snodec.snodec TO 'snodec'@localhost;
58 // FLUSH PRIVILEGES;
59
60 database::mariadb::MariaDBClient db1(details);
61
62 int r = 0;
63
64 db1.exec(
65 "DELETE FROM `snodec`",
66 [&db1](void) -> void {
67 VLOG(0) << "********** OnQuery 0;";
68 db1.affectedRows(
69 [](my_ulonglong affectedRows) -> void {
70 VLOG(0) << "********** AffectedRows 1: " << affectedRows;
71 },
72 [](const std::string& errorString, unsigned int errorNumber) -> void {
73 VLOG(0) << "Error 1: " << errorString << " : " << errorNumber;
74 });
75 },
76 [](const std::string& errorString, unsigned int errorNumber) -> void {
77 VLOG(0) << "********** Error 0: " << errorString << " : " << errorNumber;
78 })
79
80 .exec(
81 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
82 [&db1](void) -> void {
83 VLOG(0) << "********** OnQuery 1: ";
84 db1.affectedRows(
85 [](my_ulonglong affectedRows) -> void {
86 VLOG(0) << "********** AffectedRows 2: " << affectedRows;
87 },
88 [](const std::string& errorString, unsigned int errorNumber) -> void {
89 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
90 });
91 },
92 [](const std::string& errorString, unsigned int errorNumber) -> void {
93 VLOG(0) << "********** Error 1: " << errorString << " : " << errorNumber;
94 })
95 .query(
96 "SELECT * FROM snodec",
97 [&r](const MYSQL_ROW row) -> void {
98 if (row != nullptr) {
99 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
100 r++;
101 } else {
102 VLOG(0) << "********** Row Result 2: " << r;
103 }
104 },
105 [](const std::string& errorString, unsigned int errorNumber) -> void {
106 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
107 })
108 .query(
109 "SELECT * FROM snodec",
110 [&r](const MYSQL_ROW row) -> void {
111 if (row != nullptr) {
112 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
113 r++;
114 } else {
115 VLOG(0) << "********** Row Result 2: " << r;
116 }
117 },
118 [](const std::string& errorString, unsigned int errorNumber) -> void {
119 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
120 });
121
122 database::mariadb::MariaDBClient db2(details);
123 {
124 db2.query(
125 "SELECT * FROM snodec",
126 [](const MYSQL_ROW row) -> void {
127 if (row != nullptr) {
128 VLOG(0) << "Row Result 3: " << row[0] << " : " << row[1];
129 } else {
130 VLOG(0) << "Row Result 3:";
131 }
132 },
133 [](const std::string& errorString, unsigned int errorNumber) -> void {
134 VLOG(0) << "Error 3: " << errorString << " : " << errorNumber;
135 });
136
137 int r1 = 0;
138 int r2 = 0;
139
140 db2.query(
141 "SELECT * FROM snodec",
142 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
143 if (row != nullptr) {
144 VLOG(0) << "Row Result 4: " << row[0] << " : " << row[1];
145 } else {
146 VLOG(0) << "Row Result 4:";
147
148 db2.query(
149 "SELECT * FROM snodec",
150 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
151 if (row != nullptr) {
152 VLOG(0) << "Row Result 5: " << row[0] << " : " << row[1];
153 } else { // After all results have been fetched
154 VLOG(0) << "Row Result 5:";
155
156 core::timer::Timer dbTimer1 = core::timer::Timer::intervalTimer(
157 [&db2, &r1](const std::function<void()>& stop) -> void {
158 static int i = 0;
159 VLOG(0) << "Tick 2: " << i++;
160
161 r1 = 0;
162 db2.query(
163 "SELECT * FROM snodec",
164 [&r1](const MYSQL_ROW row) -> void {
165 if (row != nullptr) {
166 VLOG(0) << "Row Result 6: " << row[0] << " : " << row[1];
167 r1++;
168 } else {
169 VLOG(0) << "Row Result 6: " << r1;
170 }
171 },
172 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
173 VLOG(0) << "Error 6: " << errorString << " : " << errorNumber;
174 stop();
175 });
176 },
177 2);
178
179 core::timer::Timer dbTimer2 = core::timer::Timer::intervalTimer(
180 [&db2, &r2](const std::function<void()>& stop) -> void {
181 static int i = 0;
182 VLOG(0) << "Tick 0.7: " << i++;
183
184 r2 = 0;
185 db2.query(
186 "SELECT * FROM snodec",
187 [&db2, &r2](const MYSQL_ROW row) -> void {
188 if (row != nullptr) {
189 VLOG(0) << "Row Result 7: " << row[0] << " : " << row[1];
190 r2++;
191 } else {
192 VLOG(0) << "Row Result 7: " << r2;
193 db2.fieldCount(
194 [](unsigned int fieldCount) -> void {
195 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
196 },
197 [](const std::string& errorString, unsigned int errorNumber) -> void {
198 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
199 });
200 }
201 },
202 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
203 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
204 stop();
205 })
206 .fieldCount(
207 [](unsigned int fieldCount) -> void {
208 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
209 },
210 [](const std::string& errorString, unsigned int errorNumber) -> void {
211 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
212 });
213 },
214 0.7);
215 }
216 },
217 [](const std::string& errorString, unsigned int errorNumber) -> void {
218 VLOG(0) << "Error 5: " << errorString << " : " << errorNumber;
219 });
220 }
221 },
222 [](const std::string& errorString, unsigned int errorNumber) -> void {
223 VLOG(0) << "Error 4: " << errorString << " : " << errorNumber;
224 });
225
226 core::timer::Timer dbTimer = core::timer::Timer::intervalTimer(
227 [&db2](const std::function<void()>& stop) -> void {
228 static int i = 0;
229 VLOG(0) << "Tick 0.1: " << i++;
230
231 if (i >= 60000) {
232 VLOG(0) << "Stop Stop";
233 stop();
234 }
235
236 int j = i;
237 db2.startTransactions(
238 [](void) -> void {
239 VLOG(0) << "Transactions activated 10:";
240 },
241 [](const std::string& errorString, unsigned int errorNumber) -> void {
242 VLOG(0) << "Error 8: " << errorString << " : " << errorNumber;
243 })
244 .exec(
245 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
246 [&db2, j](void) -> void {
247 VLOG(0) << "Inserted 10: " << j;
248 db2.affectedRows(
249 [](my_ulonglong affectedRows) -> void {
250 VLOG(0) << "AffectedRows 11: " << affectedRows;
251 },
252 [](const std::string& errorString, unsigned int errorNumber) -> void {
253 VLOG(0) << "Error 11: " << errorString << " : " << errorNumber;
254 });
255 },
256 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
257 VLOG(0) << "Error 10: " << errorString << " : " << errorNumber;
258 stop();
259 })
260 .rollback(
261 [](void) -> void {
262 VLOG(0) << "Rollback success 11";
263 },
264 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
265 VLOG(0) << "Error 12: " << errorString << " : " << errorNumber;
266 stop();
267 })
268 .exec(
269 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
270 [&db2, j](void) -> void {
271 VLOG(0) << "Inserted 13: " << j;
272 db2.affectedRows(
273 [](my_ulonglong affectedRows) -> void {
274 VLOG(0) << "AffectedRows 14: " << affectedRows;
275 },
276 [](const std::string& errorString, unsigned int errorNumber) -> void {
277 VLOG(0) << "Error 14: " << errorString << " : " << errorNumber;
278 });
279 },
280 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
281 VLOG(0) << "Error 13: " << errorString << " : " << errorNumber;
282 stop();
283 })
284 .commit(
285 [](void) -> void {
286 VLOG(0) << "Commit success 15";
287 },
288 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
289 VLOG(0) << "Error 15: " << errorString << " : " << errorNumber;
290 stop();
291 })
292 .query(
293 "SELECT COUNT(*) FROM snodec",
294 [&db2, j, stop](const MYSQL_ROW row) -> void {
295 if (row != nullptr) {
296 VLOG(0) << "Row Result count(*) 16: " << row[0];
297 if (std::atoi(row[0]) != j + 1) { // NOLINT
298 VLOG(0) << "Wrong number of rows 16: " << std::atoi(row[0]) << " != " << j + 1; // NOLINT
299 // exit(1);
300 }
301 } else {
302 VLOG(0) << "Row Result count(*) 16: no result:";
303 db2.fieldCount(
304 [](unsigned int fieldCount) -> void {
305 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
306 },
307 [](const std::string& errorString, unsigned int errorNumber) -> void {
308 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
309 });
310 }
311 },
312 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
313 VLOG(0) << "Error 16: " << errorString << " : " << errorNumber;
314 stop();
315 })
316 .endTransactions(
317 [](void) -> void {
318 VLOG(0) << "Transactions deactivated 17";
319 },
320 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
321 VLOG(0) << "Error 17: " << errorString << " : " << errorNumber;
322 stop();
323 });
324 },
325 0.1);
326 }
327
328 return core::SNodeC::start();
329}
MariaDBClient(const MariaDBConnectionDetails &details)
int main(int argc, char *argv[])