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 .connectionName = "testconnection",
67 .hostname = utils::Config::getStringOptionValue("--db-host"),
68 .username = "snodec",
69 .password = "pentium5",
70 .database = "snodec",
71 .port = 3306,
72 .socket = "/run/mysqld/mysqld.sock",
73 .flags = 0,
74 };
75
76 // CREATE DATABASE snodec;
77 // CREATE TABLE snodec (username text NOT NULL, password text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
78 // CREATE USER 'snodec'@localhost IDENTIFIED BY 'pentium5';
79 // GRANT ALL PRIVILEGES ON *.* TO 'snodec'@localhost;
80 // GRANT ALL PRIVILEGES ON snodec.snodec TO 'snodec'@localhost;
81 // FLUSH PRIVILEGES;
82
83 database::mariadb::MariaDBClient db1(details, [](const database::mariadb::MariaDBState& state) {
84 if (state.error != 0) {
85 VLOG(0) << "MySQL error: " << state.errorMessage << " [" << state.error << "]";
86 } else if (state.connected) {
87 VLOG(0) << "MySQL connected";
88 } else {
89 VLOG(0) << "MySQL disconnected";
90 }
91 });
92
93 int r = 0;
94
95 db1.exec(
96 "DELETE FROM `snodec`",
97 [&db1](void) -> void {
98 VLOG(0) << "********** OnQuery 0;";
99 db1.affectedRows(
100 [](my_ulonglong affectedRows) -> void {
101 VLOG(0) << "********** AffectedRows 1: " << affectedRows;
102 },
103 [](const std::string& errorString, unsigned int errorNumber) -> void {
104 VLOG(0) << "Error 1: " << errorString << " : " << errorNumber;
105 });
106 },
107 [](const std::string& errorString, unsigned int errorNumber) -> void {
108 VLOG(0) << "********** Error 0: " << errorString << " : " << errorNumber;
109 })
110
111 .exec(
112 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
113 [&db1](void) -> void {
114 VLOG(0) << "********** OnQuery 1: ";
115 db1.affectedRows(
116 [](my_ulonglong affectedRows) -> void {
117 VLOG(0) << "********** AffectedRows 2: " << affectedRows;
118 },
119 [](const std::string& errorString, unsigned int errorNumber) -> void {
120 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
121 });
122 },
123 [](const std::string& errorString, unsigned int errorNumber) -> void {
124 VLOG(0) << "********** Error 1: " << errorString << " : " << errorNumber;
125 })
126 .query(
127 "SELECT * FROM snodec",
128 [&r](const MYSQL_ROW row) -> void {
129 if (row != nullptr) {
130 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
131 r++;
132 } else {
133 VLOG(0) << "********** Row Result 2: " << r;
134 }
135 },
136 [](const std::string& errorString, unsigned int errorNumber) -> void {
137 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
138 })
139 .query(
140 "SELECT * FROM snodec",
141 [&r](const MYSQL_ROW row) -> void {
142 if (row != nullptr) {
143 VLOG(0) << "********** Row Result 2: " << row[0] << " : " << row[1];
144 r++;
145 } else {
146 VLOG(0) << "********** Row Result 2: " << r;
147 }
148 },
149 [](const std::string& errorString, unsigned int errorNumber) -> void {
150 VLOG(0) << "********** Error 2: " << errorString << " : " << errorNumber;
151 });
152
153 database::mariadb::MariaDBClient db2(details, [](const database::mariadb::MariaDBState& state) {
154 if (state.error != 0) {
155 VLOG(0) << "MySQL error: " << state.errorMessage << " [" << state.error << "]";
156 } else if (state.connected) {
157 VLOG(0) << "MySQL connected";
158 } else {
159 VLOG(0) << "MySQL disconnected";
160 }
161 });
162 {
163 db2.query(
164 "SELECT * FROM snodec",
165 [](const MYSQL_ROW row) -> void {
166 if (row != nullptr) {
167 VLOG(0) << "Row Result 3: " << row[0] << " : " << row[1];
168 } else {
169 VLOG(0) << "Row Result 3:";
170 }
171 },
172 [](const std::string& errorString, unsigned int errorNumber) -> void {
173 VLOG(0) << "Error 3: " << errorString << " : " << errorNumber;
174 });
175
176 int r1 = 0;
177 int r2 = 0;
178
179 db2.query(
180 "SELECT * FROM snodec",
181 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
182 if (row != nullptr) {
183 VLOG(0) << "Row Result 4: " << row[0] << " : " << row[1];
184 } else {
185 VLOG(0) << "Row Result 4:";
186
187 db2.query(
188 "SELECT * FROM snodec",
189 [&db2, &r1, &r2](const MYSQL_ROW row) -> void {
190 if (row != nullptr) {
191 VLOG(0) << "Row Result 5: " << row[0] << " : " << row[1];
192 } else { // After all results have been fetched
193 VLOG(0) << "Row Result 5:";
194
195 core::timer::Timer dbTimer1 = core::timer::Timer::intervalTimer(
196 [&db2, &r1](const std::function<void()>& stop) -> void {
197 static int i = 0;
198 VLOG(0) << "Tick 2: " << i++;
199
200 r1 = 0;
201 db2.query(
202 "SELECT * FROM snodec",
203 [&r1](const MYSQL_ROW row) -> void {
204 if (row != nullptr) {
205 VLOG(0) << "Row Result 6: " << row[0] << " : " << row[1];
206 r1++;
207 } else {
208 VLOG(0) << "Row Result 6: " << r1;
209 }
210 },
211 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
212 VLOG(0) << "Error 6: " << errorString << " : " << errorNumber;
213 stop();
214 });
215 },
216 2);
217
218 core::timer::Timer dbTimer2 = core::timer::Timer::intervalTimer(
219 [&db2, &r2](const std::function<void()>& stop) -> void {
220 static int i = 0;
221 VLOG(0) << "Tick 0.7: " << i++;
222
223 r2 = 0;
224 db2.query(
225 "SELECT * FROM snodec",
226 [&db2, &r2](const MYSQL_ROW row) -> void {
227 if (row != nullptr) {
228 VLOG(0) << "Row Result 7: " << row[0] << " : " << row[1];
229 r2++;
230 } else {
231 VLOG(0) << "Row Result 7: " << r2;
232 db2.fieldCount(
233 [](unsigned int fieldCount) -> void {
234 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
235 },
236 [](const std::string& errorString, unsigned int errorNumber) -> void {
237 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
238 });
239 }
240 },
241 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
242 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
243 stop();
244 })
245 .fieldCount(
246 [](unsigned int fieldCount) -> void {
247 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
248 },
249 [](const std::string& errorString, unsigned int errorNumber) -> void {
250 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
251 });
252 },
253 0.7);
254 }
255 },
256 [](const std::string& errorString, unsigned int errorNumber) -> void {
257 VLOG(0) << "Error 5: " << errorString << " : " << errorNumber;
258 });
259 }
260 },
261 [](const std::string& errorString, unsigned int errorNumber) -> void {
262 VLOG(0) << "Error 4: " << errorString << " : " << errorNumber;
263 });
264
265 core::timer::Timer dbTimer = core::timer::Timer::intervalTimer(
266 [&db2](const std::function<void()>& stop) -> void {
267 static int i = 0;
268 VLOG(0) << "Tick 0.1: " << i++;
269
270 if (i >= 60000) {
271 VLOG(0) << "Stop Stop";
272 stop();
273 }
274
275 int j = i;
276 db2.startTransactions(
277 [](void) -> void {
278 VLOG(0) << "Transactions activated 10:";
279 },
280 [](const std::string& errorString, unsigned int errorNumber) -> void {
281 VLOG(0) << "Error 8: " << errorString << " : " << errorNumber;
282 })
283 .exec(
284 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
285 [&db2, j](void) -> void {
286 VLOG(0) << "Inserted 10: " << j;
287 db2.affectedRows(
288 [](my_ulonglong affectedRows) -> void {
289 VLOG(0) << "AffectedRows 11: " << affectedRows;
290 },
291 [](const std::string& errorString, unsigned int errorNumber) -> void {
292 VLOG(0) << "Error 11: " << errorString << " : " << errorNumber;
293 });
294 },
295 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
296 VLOG(0) << "Error 10: " << errorString << " : " << errorNumber;
297 stop();
298 })
299 .rollback(
300 [](void) -> void {
301 VLOG(0) << "Rollback success 11";
302 },
303 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
304 VLOG(0) << "Error 12: " << errorString << " : " << errorNumber;
305 stop();
306 })
307 .exec(
308 "INSERT INTO `snodec`(`username`, `password`) VALUES ('Annett','Hallo')",
309 [&db2, j](void) -> void {
310 VLOG(0) << "Inserted 13: " << j;
311 db2.affectedRows(
312 [](my_ulonglong affectedRows) -> void {
313 VLOG(0) << "AffectedRows 14: " << affectedRows;
314 },
315 [](const std::string& errorString, unsigned int errorNumber) -> void {
316 VLOG(0) << "Error 14: " << errorString << " : " << errorNumber;
317 });
318 },
319 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
320 VLOG(0) << "Error 13: " << errorString << " : " << errorNumber;
321 stop();
322 })
323 .commit(
324 [](void) -> void {
325 VLOG(0) << "Commit success 15";
326 },
327 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
328 VLOG(0) << "Error 15: " << errorString << " : " << errorNumber;
329 stop();
330 })
331 .query(
332 "SELECT COUNT(*) FROM snodec",
333 [&db2, j, stop](const MYSQL_ROW row) -> void {
334 if (row != nullptr) {
335 VLOG(0) << "Row Result count(*) 16: " << row[0];
336 if (std::atoi(row[0]) != j + 1) { // NOLINT
337 VLOG(0) << "Wrong number of rows 16: " << std::atoi(row[0]) << " != " << j + 1; // NOLINT
338 // exit(1);
339 }
340 } else {
341 VLOG(0) << "Row Result count(*) 16: no result:";
342 db2.fieldCount(
343 [](unsigned int fieldCount) -> void {
344 VLOG(0) << "************ FieldCount ************ = " << fieldCount;
345 },
346 [](const std::string& errorString, unsigned int errorNumber) -> void {
347 VLOG(0) << "Error 7: " << errorString << " : " << errorNumber;
348 });
349 }
350 },
351 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
352 VLOG(0) << "Error 16: " << errorString << " : " << errorNumber;
353 stop();
354 })
355 .endTransactions(
356 [](void) -> void {
357 VLOG(0) << "Transactions deactivated 17";
358 },
359 [stop](const std::string& errorString, unsigned int errorNumber) -> void {
360 VLOG(0) << "Error 17: " << errorString << " : " << errorNumber;
361 stop();
362 });
363 },
364 0.1);
365 }
366
367 return core::SNodeC::start();
368}
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
static std::string getStringOptionValue(const std::string &name)
Definition Config.cpp:980
int main(int argc, char *argv[])
Definition Timer.h:59
Definition Config.h:59