crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Data.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2022 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Data.hpp
24  *
25  * Custom data structures for database access by algorithms.
26  *
27  * Created on: Jan 16, 2019
28  * Author: ans
29  */
30 
31 #ifndef DATA_DATA_HPP_
32 #define DATA_DATA_HPP_
33 
34 #include <algorithm> // std::transform
35 #include <array> // std::array
36 #include <cctype> // std::tolower
37 #include <cstddef> // std::size_t
38 #include <cstdint> // std::int32_t, std::int64_t, std::uint32_t, std::uint64_t
39 #include <string> // std::string
40 #include <tuple> // std::tuple
41 #include <utility> // std::pair, std::swap
42 #include <vector> // std::vector
43 
44 namespace crawlservpp::Data {
45 
46  /*
47  * CONSTANTS
48  */
49 
52 
54  inline constexpr auto bytes32bit{4};
55 
57  inline constexpr auto bytes64bit{8};
58 
60 
61  /*
62  * DECLARATIONS
63  */
64 
66  enum Type {
69 
72 
75 
78 
81 
84 
87 
90  };
91 
93 
96  struct Value { //NOLINT(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
98  union {
100  bool _b;
101 
103  std::int32_t _i32;
104 
106  std::uint32_t _ui32;
107 
109  std::int64_t _i64;
110 
112  std::uint64_t _ui64{};
113 
115  double _d;
116  };
117 
119  std::string _s;
120 
122  bool _isnull{true};
123 
128 
131 
134 
137 
138  }
139 
141 
144  _overflow{_if_too_large::_error};
145 
147  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
148  Value() = default;
149 
151 
154  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
155  explicit Value(bool value) : _b(value), _isnull(false) {}
156 
158 
161  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
162  explicit Value(std::int32_t value) : _i32(value), _isnull(false) {}
163 
165 
168  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
169  explicit Value(std::uint32_t value) : _ui32(value), _isnull(false) {}
170 
172 
175  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
176  explicit Value(std::int64_t value) : _i64(value), _isnull(false) {}
177 
179 
182  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
183  explicit Value(std::uint64_t value) : _ui64(value), _isnull(false) {}
184 
186 
189  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
190  explicit Value(double value) : _d(value), _isnull(false) {}
191 
193 
197  //NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
198  explicit Value(const std::string& value) : _s(value), _isnull(false) {}
199 
201  void clear() {
202  *this = Value();
203  }
204  };
205 
207  struct GetValue {
209  std::string table;
210 
212  std::string column;
213 
215  Type type{_unknown};
216 
218  std::string condition;
219 
222  };
223 
225  struct GetFields {
227  std::string table;
228 
230  std::vector<std::string> columns;
231 
233  Type type{_unknown};
234 
236  std::string condition;
237 
239  std::vector<Value> values;
240  };
241 
243  struct GetFieldsMixed {
245  std::string table;
246 
248  std::vector<std::pair<std::string, Type>> columns_types;
249 
251  std::string condition;
252 
254  std::vector<Value> values;
255  };
256 
258  struct GetColumn {
260  std::string table;
261 
263  std::string column;
264 
266  Type type{_unknown};
267 
269  std::string condition;
270 
272 
277  std::vector<std::string> order;
278 
280  std::vector<Value> values;
281  };
282 
284  struct GetColumns {
286  std::string table;
287 
289  std::vector<std::string> columns;
290 
292  Type type{_unknown};
293 
295  std::string condition;
296 
298 
303  std::vector<std::string> order;
304 
306  std::vector<std::vector<Value>> values;
307  };
308 
312  std::string table;
313 
315  std::vector<std::pair<std::string, Type>> columns_types;
316 
318  std::string condition;
319 
321 
326  std::vector<std::string> order;
327 
329  std::vector<std::vector<Value>> values;
330  };
331 
333  struct InsertValue {
335  std::string table;
336 
338  std::string column;
339 
341  Type type{_unknown};
342 
345  };
346 
348  struct InsertFields {
350  std::string table;
351 
353  std::vector<std::pair<std::string, Value>> columns_values;
354 
356  Type type{_unknown};
357  };
358 
362  std::string table;
363 
365  std::vector<std::tuple<std::string, Type, Value>> columns_types_values;
366  };
367 
369  struct UpdateValue {
371  std::string table;
372 
374  std::string column;
375 
377  Type type{_unknown};
378 
380 
384 
386  std::string condition;
387  };
388 
390  struct UpdateFields {
392  std::string table;
393 
395 
398  std::vector<std::pair<std::string, Value>> columns_values;
399 
401  Type type{_unknown};
402 
404  std::string condition;
405  };
406 
410  std::string table;
411 
413 
416  std::vector<std::tuple<std::string, Type, Value>> columns_types_values;
417 
419  std::string condition;
420  };
421 
422  /*
423  * HELPER FUNCTION
424  */
425 
428 
430 
438  [[nodiscard]] inline Type parseSQLType(std::string sqlType) {
439  // constants classifying SQL types
440  constexpr std::array boolTypes{"bool", "boolean"};
441  constexpr std::array int32Types{"bit", "tinyint", "smallint", "mediumint", "int", "integer"};
442  constexpr std::array int64Types{"bigint"};
443  constexpr std::array doubleTypes{"float", "double", "double precision", "decimal", "dec"};
444  constexpr std::array stringTypes{
445  "char",
446  "varchar",
447  "binary",
448  "varbinary",
449  "tinyblob",
450  "tinytext",
451  "text",
452  "blob",
453  "mediumtext",
454  "longtext",
455  "longblob",
456  "enum",
457  "set"
458  };
459 
460  // convert data type to lower case
461  std::transform(
462  sqlType.begin(),
463  sqlType.end(),
464  sqlType.begin(),
465  [](const auto c) {
466  return std::tolower(c);
467  }
468  );
469 
470  // remove size from retrieved data type
471  const auto bracketPos{sqlType.find_first_of('(')};
472 
473  if(bracketPos != std::string::npos) {
474  const auto bracketEnd{sqlType.find_first_of(')', bracketPos + 1)};
475 
476  if(bracketEnd != std::string::npos) {
477  sqlType = sqlType.substr(0, bracketPos) + sqlType.substr(bracketEnd + 1);
478  }
479  }
480 
481  // detect types (unsigned ones first)
482  for(const auto& type : boolTypes) {
483  const std::string typeString{type};
484 
485  if(sqlType.substr(0, typeString.length()) == typeString) {
486  return Type::_bool;
487  }
488  }
489 
490  for(const auto& type : int32Types) {
491  const std::string typeString{type};
492  const auto unsignedType{typeString + " unsigned"};
493 
494  if(sqlType.substr(0, unsignedType.length()) == unsignedType) {
495  return Type::_uint32;
496  }
497 
498  if(sqlType.substr(0, typeString.length()) == typeString) {
499  return Type::_int32;
500  }
501  }
502 
503  for(const auto& type : int64Types) {
504  const std::string typeString{type};
505  const auto unsignedType{typeString + " unsigned"};
506 
507  if(sqlType.substr(0, unsignedType.length()) == unsignedType) {
508  return Type::_uint64;
509  }
510 
511  if(sqlType.substr(0, typeString.length()) == typeString) {
512  return Type::_int64;
513  }
514  }
515 
516  for(const auto& type : doubleTypes) {
517  const std::string typeString{type};
518 
519  if(sqlType.substr(0, typeString.length()) == typeString) {
520  return Type::_double;
521  }
522  }
523 
524  for(const auto& type : stringTypes) {
525  const std::string typeString{type};
526 
527  if(sqlType.substr(0, typeString.length()) == typeString) {
528  return Type::_string;
529  }
530  }
531 
532  return Type::_unknown;
533  }
534 
536 
537  /*
538  * TEMPLATE FUNCTIONS
539  */
540 
543 
544  template<int> Type getTypeOfSizeT();
545 
547  template<> inline Type getTypeOfSizeT<bytes32bit>() {
548  return Type::_uint32;
549  }
550 
552  template<> inline Type getTypeOfSizeT<bytes64bit>() {
553  return Type::_uint64;
554  }
555 
557  inline Type getTypeOfSizeT() {
558  return getTypeOfSizeT<sizeof(std::size_t)>();
559  }
560 
562 
563 } /* namespace crawlservpp::Data */
564 
565 #endif /* DATA_DATA_HPP_ */
std::vector< std::string > order
Optional order to be applied to the SQL query retrieving the values of the columns.
Definition: Data.hpp:303
32-bit integer.
Definition: Data.hpp:74
std::vector< std::string > columns
Vector containing the names of the columns to be retrieved.
Definition: Data.hpp:230
std::vector< std::tuple< std::string, Type, Value > > columns_types_values
Vector containing the names of the columns to be updated, their data types and the new values to be s...
Definition: Data.hpp:416
std::uint32_t _ui32
Unsigned 32-bit integer value.
Definition: Data.hpp:106
Type parseSQLType(std::string sqlType)
Parses the given SQL data type.
Definition: Data.hpp:438
std::string condition
The condition to be added to the SQL query updating the value.
Definition: Data.hpp:419
Use a null value instead.
Definition: Data.hpp:136
std::vector< std::tuple< std::string, Type, Value > > columns_types_values
Vector containing the names of the columns, their data types and the values to be inserted into them...
Definition: Data.hpp:365
Unknown data type.
Definition: Data.hpp:68
String.
Definition: Data.hpp:89
Type getTypeOfSizeT< bytes32bit >()
Identifies std::size_t as a 32-bit integer.
Definition: Data.hpp:547
std::string table
The name of the table.
Definition: Data.hpp:227
std::vector< std::string > columns
Vector containing the names of the columns.
Definition: Data.hpp:289
Boolean value.
Definition: Data.hpp:71
std::string condition
Condition to be added to the SQL query retrieving the values.
Definition: Data.hpp:251
std::vector< std::pair< std::string, Value > > columns_values
Vector containing the names of the columns to be updated and the new values to be set...
Definition: Data.hpp:398
Value(double value)
Constructor initializing a floating point value (with double precision).
Definition: Data.hpp:190
std::vector< std::pair< std::string, Value > > columns_values
Vector containing the names of the columns and the values to be inserted into them.
Definition: Data.hpp:353
Value value
The new value to be set.
Definition: Data.hpp:383
std::string table
The name of the table.
Definition: Data.hpp:362
Value(std::int32_t value)
Constructor initializing a 32-bit integer.
Definition: Data.hpp:162
Structure for retrieving multiple table columns of different types.
Definition: Data.hpp:310
std::string table
The name of the table.
Definition: Data.hpp:260
std::int64_t _i64
64-bit integer value.
Definition: Data.hpp:109
std::string column
The name of the column.
Definition: Data.hpp:212
std::vector< std::pair< std::string, Type > > columns_types
Vector containing the names and data types of the columns to be retrieved.
Definition: Data.hpp:315
std::string condition
Condition to be added to the SQL query retrieving the values.
Definition: Data.hpp:236
std::string condition
Optional condition to be added to the SQL query retrieving the values of the columns.
Definition: Data.hpp:318
std::string table
The name of the table.
Definition: Data.hpp:371
std::string condition
Optional condition to be added to the SQL query retrieving the values of the columns.
Definition: Data.hpp:295
std::string condition
The condition to be added to the SQL query updating the value.
Definition: Data.hpp:386
Unsigned 32-bit integer.
Definition: Data.hpp:77
void clear()
Clears the current value and re-initializes it as null value.
Definition: Data.hpp:201
Structure for getting multiple values of different types from a table column.
Definition: Data.hpp:243
Structure for updating multiple values of different types in a table.
Definition: Data.hpp:408
std::vector< Value > values
Vector containing the retrieved values.
Definition: Data.hpp:254
std::string table
The name of the table.
Definition: Data.hpp:392
constexpr auto bytes64bit
The number of bytes of a 64-bit value.
Definition: Data.hpp:57
Structure for updating one value in a table.
Definition: Data.hpp:369
Type
Data types.
Definition: Data.hpp:66
std::uint64_t _ui64
Unsigned 64-bit integer value.
Definition: Data.hpp:112
std::string table
The name of the table.
Definition: Data.hpp:286
bool _isnull
Null value.
Definition: Data.hpp:122
Type getTypeOfSizeT< bytes64bit >()
Identifies std::size_t as a 64-bit integer.
Definition: Data.hpp:552
Structure for inserting multiple values of different types into a row.
Definition: Data.hpp:360
Value value
The retrieved value.
Definition: Data.hpp:221
Value(bool value)
Constructor initializing a boolean value.
Definition: Data.hpp:155
Structure for retrieving one value from a table column.
Definition: Data.hpp:207
Throw a Database::Exception.
Definition: Data.hpp:127
Unsigned 64-bit integer.
Definition: Data.hpp:83
std::string column
The name of the column.
Definition: Data.hpp:263
double _d
Floating point value (with double precision).
Definition: Data.hpp:115
Structure for updating multiple values of the same type in a table.
Definition: Data.hpp:390
Floating point value (with double precision).
Definition: Data.hpp:86
Value(std::uint64_t value)
Constructor initializing an unsigned 64-bit integer.
Definition: Data.hpp:183
std::vector< std::vector< Value > > values
Vector containing the retrieved columns as vectors of the retrieved values.
Definition: Data.hpp:306
std::string table
The name of the table.
Definition: Data.hpp:350
std::string _s
String value.
Definition: Data.hpp:119
std::vector< Value > values
Vector containing the retrieved values.
Definition: Data.hpp:239
Structure for retrieving multiple table columns of the same type.
Definition: Data.hpp:284
std::vector< std::string > order
Optional order to be applied to the SQL query retrieving the values of the columns.
Definition: Data.hpp:326
Value(std::uint32_t value)
Constructor initializing an unsigned 32-bit integer.
Definition: Data.hpp:169
std::int32_t _i32
32-bit integer value.
Definition: Data.hpp:103
Type getTypeOfSizeT()
Resolves std::size_t into the appropriate data type.
Definition: Data.hpp:557
Use an empty string instead.
Definition: Data.hpp:133
std::string column
The name of the column.
Definition: Data.hpp:374
Structure for retrieving multiple values of the same type from a table column.
Definition: Data.hpp:225
std::string table
The name of the table.
Definition: Data.hpp:335
Value(std::int64_t value)
Constructor initializing a 64-bit integer.
Definition: Data.hpp:176
std::string condition
Optional condition to be added to the SQL query retrieving the values of the column.
Definition: Data.hpp:269
bool _b
Boolean value.
Definition: Data.hpp:100
std::string table
The name of the table.
Definition: Data.hpp:410
std::vector< std::string > order
Optional order to be applied to the SQL query retrieving the values of the column.
Definition: Data.hpp:277
Structure for retrieving the values in a table column.
Definition: Data.hpp:258
std::string condition
The condition to be added to the SQL query updating the value.
Definition: Data.hpp:404
64-bit integer.
Definition: Data.hpp:80
std::vector< std::pair< std::string, Type > > columns_types
Vector containing the names and data types of the columns to be retrieved.
Definition: Data.hpp:248
_if_too_large
Enumeration for the action that will be performed if a string is too large for the database...
Definition: Data.hpp:125
Value(const std::string &value)
Constructor initializing a string value.
Definition: Data.hpp:198
std::string condition
Condition to be added to the SQL query retrieving the value.
Definition: Data.hpp:218
Value()=default
Default constructor initializing a null value.
A generic value.
Definition: Data.hpp:96
constexpr auto bytes32bit
The number of bytes of a 32-bit value.
Definition: Data.hpp:54
Value value
The value to be inserted.
Definition: Data.hpp:344
Namespace for different types of data.
std::vector< Value > values
Vector containing the retrieved values.
Definition: Data.hpp:280
Structure for inserting one value into a table.
Definition: Data.hpp:333
std::string table
The name of the table.
Definition: Data.hpp:245
std::string column
The name of the column.
Definition: Data.hpp:338
Structure for inserting multiple values of the same type into a table.
Definition: Data.hpp:348
Trim the string to an acceptable size.
Definition: Data.hpp:130
std::vector< std::vector< Value > > values
Vector containing the retrieved columns as vectors of the retrieved values.
Definition: Data.hpp:329
std::string table
The name of the table.
Definition: Data.hpp:209
std::string table
The name of the table.
Definition: Data.hpp:312