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/*
22 * MIT License
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 */
42
43#include "core/SNodeC.h"
44#include "core/timer/Timer.h"
45#include "database/mariadb/MariaDBClient.h"
46#include "database/mariadb/MariaDBCommandSequence.h"
47#include "utils/Config.h"
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51#include "log/Logger.h"
52
53#include <cstdlib>
54#include <functional>
55#include <mysql.h>
56#include <string>
57
58#endif /* DOXYGEN_SHOULD_SKIP_THIS */
59
60int main(int argc, char* argv[]) {
61 utils::Config::addStringOption("--db-host", "Hostname of IP-Address of Server", "[hostname|IP-address]", "localhost", true);
62
63 core::SNodeC::init(argc, argv);
64
65 const database::mariadb::MariaDBConnectionDetails details = {
66 .hostname = utils::Config::getStringOptionValue("--db-host"),
67 .username = "snodec",
68 .password = "pentium5",
69 .database = "snodec",
70 .port = 3306,
71 .socket = "/run/mysqld/mysqld.sock",
72 .flags = 0,
73 };
74
75 // CREATE DATABASE snodec;
76 // CREATE TABLE snodec (username text NOT NULL, password text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
77 // CREATE USER 'snodec'@localhost IDENTIFIED BY 'pentium5';
78 // GRANT ALL PRIVILEGES ON *.* TO 'snodec'@localhost;
79 // GRANT ALL PRIVILEGES ON snodec.snodec TO 'snodec'@localhost;
80 // FLUSH PRIVILEGES;
81
82 database::mariadb::MariaDBClient db1(details);
83
84 int r = 0;
85
86 db1.exec(
87 "DELETE FROM `snodec`",
88 [&db1](void) -> void {
89 VLOG(0) << "********** OnQuery 0;";
90 db1.affectedRows(
91 [](my_ulonglong affectedRows) -> void {
92 VLOG(0) << "********** AffectedRows 1: " << affectedRows;
93 },
94 [](const std::string& errorString, unsigned int errorNumber) -> void {
95 VLOG(0) << "Error 1: " << errorString << " : " << errorNumber;
96 });
97 },
98 [](const std::string& errorString, unsigned int errorNumber) -> void {
99 VLOG(0) << "********** Error 0: " << errorString << " : " << errorNumber;
100 })
101
102 .exec(
103 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
104 [&db1](void) -> void {
105 VLOG(0) << "********** OnQuery 1: ";
106 db1.affectedRows(
107 [](my_ulonglong affectedRows) -> void {
108 VLOG(0) << "********** AffectedRows 2: " << affectedRows;
109 },
110 [](const std::string& errorString, unsigned int errorNumber) -> void {
111 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
112 });
113 },
114 [](const std::string& errorString, unsigned int errorNumber) -> void {
115 VLOG(0) << "********** Error 1: " << errorString << " : " << errorNumber;
116 })
117 .query(
118 "SELECT * FROM snodec",
119 [&r](const MYSQL_ROW row) -> void {
120 if (row != nullptr) {
121 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
122 r++;
123 } else {
124 VLOG(0) << "********** Row Result 2: " << r;
125 }
126 },
127 [](const std::string& errorString, unsigned int errorNumber) -> void {
128 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
129 })
130 .query(
131 "SELECT * FROM snodec",
132 [&r](const MYSQL_ROW row) -> void {
133 if (row != nullptr) {
134 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
135 r++;
136 } else {
137 VLOG(0) << "********** Row Result 2: " << r;
138 }
139 },
140 [](const std::string& errorString, unsigned int errorNumber) -> void {
141 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
142 });
143
144 database::mariadb::MariaDBClient db2(details);
145 {
146 db2.query(
147 "SELECT * FROM snodec",
148 [](const MYSQL_ROW row) -> void {
149 if (row != nullptr) {
150 VLOG(0) << "Row Result 3: " << row[0] << " : " << row[1];
151 } else {
152 VLOG(0) << "Row Result 3:";
153 }
154 },
155 [](const std::string& errorString, unsigned int errorNumber) -> void {
156 VLOG(0) << "Error 3: " << errorString << " : " << errorNumber;
157 });
158
159 int r1 = 0;
160 int r2 = 0;
161
162 db2.query(
163 "SELECT * FROM snodec",
164 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
165 if (row != nullptr) {
166 VLOG(0) << "Row Result 4: " << row[0] << " : " << row[1];
167 } else {
168 VLOG(0) << "Row Result 4:";
169
170 db2.query(
171 "SELECT * FROM snodec",
172 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
173 if (row != nullptr) {
174 VLOG(0) << "Row Result 5: " << row[0] << " : " << row[1];
175 } else { // After all results have been fetched
176 VLOG(0) << "Row Result 5:";
177
178 core::timer::Timer dbTimer1 = core::timer::Timer::intervalTimer(
179 [&db2, &r1](const std::function<void()>& stop) -> void {
180 static int i = 0;
181 VLOG(0) << "Tick 2: " << i++;
182
183 r1 = 0;
184 db2.query(
185 "SELECT * FROM snodec",
186 [&r1](const MYSQL_ROW row) -> void {
187 if (row != nullptr) {
188 VLOG(0) << "Row Result 6: " << row[0] << " : " << row[1];
189 r1++;
190 } else {
191 VLOG(0) << "Row Result 6: " << r1;
192 }
193 },
194 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
195 VLOG(0) << "Error 6: " << errorString << " : " << errorNumber;
196 stop();
197 });
198 },
199 2);
200
201 core::timer::Timer dbTimer2 = core::timer::Timer::intervalTimer(
202 [&db2, &r2](const std::function<void()>& stop) -> void {
203 static int i = 0;
204 VLOG(0) << "Tick 0.7: " << i++;
205
206 r2 = 0;
207 db2.query(
208 "SELECT * FROM snodec",
209 [&db2, &r2](const MYSQL_ROW row) -> void {
210 if (row != nullptr) {
211 VLOG(0) << "Row Result 7: " << row[0] << " : " << row[1];
212 r2++;
213 } else {
214 VLOG(0) << "Row Result 7: " << r2;
215 db2.fieldCount(
216 [](unsigned int fieldCount) -> void {
217 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
218 },
219 [](const std::string& errorString, unsigned int errorNumber) -> void {
220 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
221 });
222 }
223 },
224 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
225 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
226 stop();
227 })
228 .fieldCount(
229 [](unsigned int fieldCount) -> void {
230 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
231 },
232 [](const std::string& errorString, unsigned int errorNumber) -> void {
233 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
234 });
235 },
236 0.7);
237 }
238 },
239 [](const std::string& errorString, unsigned int errorNumber) -> void {
240 VLOG(0) << "Error 5: " << errorString << " : " << errorNumber;
241 });
242 }
243 },
244 [](const std::string& errorString, unsigned int errorNumber) -> void {
245 VLOG(0) << "Error 4: " << errorString << " : " << errorNumber;
246 });
247
248 core::timer::Timer dbTimer = core::timer::Timer::intervalTimer(
249 [&db2](const std::function<void()>& stop) -> void {
250 static int i = 0;
251 VLOG(0) << "Tick 0.1: " << i++;
252
253 if (i >= 60000) {
254 VLOG(0) << "Stop Stop";
255 stop();
256 }
257
258 int j = i;
259 db2.startTransactions(
260 [](void) -> void {
261 VLOG(0) << "Transactions activated 10:";
262 },
263 [](const std::string& errorString, unsigned int errorNumber) -> void {
264 VLOG(0) << "Error 8: " << errorString << " : " << errorNumber;
265 })
266 .exec(
267 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
268 [&db2, j](void) -> void {
269 VLOG(0) << "Inserted 10: " << j;
270 db2.affectedRows(
271 [](my_ulonglong affectedRows) -> void {
272 VLOG(0) << "AffectedRows 11: " << affectedRows;
273 },
274 [](const std::string& errorString, unsigned int errorNumber) -> void {
275 VLOG(0) << "Error 11: " << errorString << " : " << errorNumber;
276 });
277 },
278 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
279 VLOG(0) << "Error 10: " << errorString << " : " << errorNumber;
280 stop();
281 })
282 .rollback(
283 [](void) -> void {
284 VLOG(0) << "Rollback success 11";
285 },
286 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
287 VLOG(0) << "Error 12: " << errorString << " : " << errorNumber;
288 stop();
289 })
290 .exec(
291 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
292 [&db2, j](void) -> void {
293 VLOG(0) << "Inserted 13: " << j;
294 db2.affectedRows(
295 [](my_ulonglong affectedRows) -> void {
296 VLOG(0) << "AffectedRows 14: " << affectedRows;
297 },
298 [](const std::string& errorString, unsigned int errorNumber) -> void {
299 VLOG(0) << "Error 14: " << errorString << " : " << errorNumber;
300 });
301 },
302 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
303 VLOG(0) << "Error 13: " << errorString << " : " << errorNumber;
304 stop();
305 })
306 .commit(
307 [](void) -> void {
308 VLOG(0) << "Commit success 15";
309 },
310 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
311 VLOG(0) << "Error 15: " << errorString << " : " << errorNumber;
312 stop();
313 })
314 .query(
315 "SELECT COUNT(*) FROM snodec",
316 [&db2, j, stop](const MYSQL_ROW row) -> void {
317 if (row != nullptr) {
318 VLOG(0) << "Row Result count(*) 16: " << row[0];
319 if (std::atoi(row[0]) != j + 1) { // NOLINT
320 VLOG(0) << "Wrong number of rows 16: " << std::atoi(row[0]) << " != " << j + 1; // NOLINT
321 // exit(1);
322 }
323 } else {
324 VLOG(0) << "Row Result count(*) 16: no result:";
325 db2.fieldCount(
326 [](unsigned int fieldCount) -> void {
327 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
328 },
329 [](const std::string& errorString, unsigned int errorNumber) -> void {
330 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
331 });
332 }
333 },
334 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
335 VLOG(0) << "Error 16: " << errorString << " : " << errorNumber;
336 stop();
337 })
338 .endTransactions(
339 [](void) -> void {
340 VLOG(0) << "Transactions deactivated 17";
341 },
342 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
343 VLOG(0) << "Error 17: " << errorString << " : " << errorNumber;
344 stop();
345 });
346 },
347 0.1);
348 }
349
350 return core::SNodeC::start();
351}
MariaDBClient(const MariaDBConnectionDetails &details)
int main(int argc, char *argv[])