crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Database.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 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  * Database.hpp
24  *
25  * Interface to be inherited by the module threads.
26  * Allows them access to the database by providing basic Database functionality as well as the option to add prepared SQL statements.
27  *
28  * Created on: Oct 22, 2018
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_DATABASE_HPP_
33 #define WRAPPER_DATABASE_HPP_
34 
35 #include "../Data/Data.hpp"
36 #include "../Helper/Portability/mysqlcppconn.h"
37 #include "../Module/Database.hpp"
38 #include "../Struct/DatabaseSettings.hpp"
39 #include "../Struct/ModuleOptions.hpp"
40 #include "../Struct/QueryProperties.hpp"
41 #include "../Struct/TableColumn.hpp"
42 #include "../Struct/TableProperties.hpp"
43 #include "../Struct/TargetTableProperties.hpp"
44 #include "../Wrapper/DatabaseLock.hpp"
45 #include "../Wrapper/DatabaseTryLock.hpp"
46 
47 #include <cppconn/prepared_statement.h>
48 
49 #include <mysql_connection.h>
50 
51 #include <cstddef> // std::size_t
52 #include <cstdint> // std::uint8_t, std::uint64_t
53 #include <functional> // std::function
54 #include <queue> // std::queue
55 #include <string> // std::string
56 #include <string_view> // std::string_view
57 #include <utility> // std::pair
58 
59 namespace crawlservpp::Wrapper {
60 
61  /*
62  * DECLARATION
63  */
64 
66 
72  class Database {
73  // for convenience
79 
80  using IdString = std::pair<std::uint64_t, std::string>;
81  using IsRunningCallback = std::function<bool()>;
82 
83  public:
85  template<class DB> friend class Wrapper::DatabaseLock;
86 
88  template<class DB> friend class Wrapper::DatabaseTryLock;
89 
92 
93  explicit Database(Module::Database& dbThread);
94 
96  virtual ~Database() = default;
97 
101 
102  void setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose);
103  void setSleepOnError(std::uint64_t seconds);
104  void setTimeOut(std::uint64_t milliseconds);
105 
109 
110  void log(std::uint8_t level, const std::string& logEntry);
111  void log(std::uint8_t level, std::queue<std::string>& logEntries);
112 
116 
117  [[nodiscard]] std::string getWebsiteDomain(std::uint64_t websiteId);
118 
122 
123  void getQueryProperties(std::uint64_t queryId, QueryProperties& queryPropertiesTo);
124 
128 
129  [[nodiscard]] std::string getConfiguration(std::uint64_t configId);
130 
134 
135  std::uint64_t addOrUpdateTargetTable(const TargetTableProperties& properties);
136  [[nodiscard]] std::queue<IdString> getTargetTables(const std::string& type, std::uint64_t listId);
137  [[nodiscard]] std::uint64_t getTargetTableId(
138  const std::string& type,
139  std::uint64_t listId,
140  const std::string& tableName
141  );
142  [[nodiscard]] std::string getTargetTableName(const std::string& type, std::uint64_t tableId);
143  void addTargetColumn(const std::string& tableName, const TableColumn& column);
144  void deleteTargetTable(const std::string& type, std::uint64_t tableId);
145 
149 
150  void beginNoLock();
151  void endNoLock();
152 
156 
157  [[nodiscard]] bool isTableEmpty(const std::string& tableName);
158  [[nodiscard]] bool isTableExists(const std::string& tableName);
159  [[nodiscard]] bool isColumnExists(const std::string& tableName, const std::string& columnName);
160  [[nodiscard]] std::string getColumnType(const std::string& tableName, const std::string& columnName);
161 
165 
166  void getCustomData(Data::GetValue& data);
167  void getCustomData(Data::GetFields& data);
169  void getCustomData(Data::GetColumn& data);
170  void getCustomData(Data::GetColumns& data);
172  void insertCustomData(const Data::InsertValue& data);
173  void insertCustomData(const Data::InsertFields& data);
174  void insertCustomData(const Data::InsertFieldsMixed& data);
175  void updateCustomData(const Data::UpdateValue& data);
176  void updateCustomData(const Data::UpdateFields& data);
177  void updateCustomData(const Data::UpdateFieldsMixed& data);
178 
182 
183  [[nodiscard]] static std::uint64_t getRequestCounter();
184 
186 
189 
192  Database(Database&) = delete;
193 
195  Database& operator=(Database&) = delete;
196 
198  Database(Database&&) = delete;
199 
201  Database& operator=(Database&&) = delete;
202 
204 
205  protected:
208 
211 
215 
216  [[nodiscard]] const ModuleOptions& getOptions() const;
217  [[nodiscard]] const std::string& getWebsiteIdString() const;
218  [[nodiscard]] const std::string& getUrlListIdString() const;
219  [[nodiscard]] std::uint8_t getLoggingMin() const;
220  [[nodiscard]] std::uint8_t getLoggingVerbose() const;
221  [[nodiscard]] std::uint64_t getMaxAllowedPacketSize() const;
222 
226 
227  void checkConnection();
228 
232 
233  // wrappers for managing prepared SQL statements
234  void reserveForPreparedStatements(std::size_t n);
235  void addPreparedStatement(const std::string& sqlQuery, std::size_t& id);
236  void clearPreparedStatement(std::size_t& id);
237  [[nodiscard]] sql::PreparedStatement& getPreparedStatement(std::size_t id);
238 
242 
243  [[nodiscard]] std::uint64_t getLastInsertedId();
244  static void addDatabaseLock(const std::string& name, const IsRunningCallback& isRunningCallback);
245  static bool tryDatabaseLock(const std::string& name);
246  static void removeDatabaseLock(const std::string& name);
247  void createTable(const TableProperties& properties);
248  void addColumn(const std::string& tableName, const TableColumn& column);
249  void dropTable(const std::string& tableName);
250  void compressTable(const std::string& tableName);
251 
255 
256  void setUrlListCaseSensitive(std::uint64_t listId, bool isCaseSensitive);
257 
261 
262  static void sqlException(const std::string& function, const sql::SQLException& e);
263 
267 
268  static bool sqlExecute(sql::PreparedStatement& sqlPreparedStatement);
269  static sql::ResultSet * sqlExecuteQuery(sql::PreparedStatement& sqlPreparedStatement);
270  static int sqlExecuteUpdate(sql::PreparedStatement& sqlPreparedStatement);
271 
273  };
274 
275  /*
276  * IMPLEMENTATION
277  */
278 
279  /*
280  * CONSTRUCTION AND DESTRUCTION
281  */
282 
284 
288  inline Database::Database(Module::Database& dbThread) : database(dbThread) {}
289 
290  /*
291  * SETTERS
292  */
293 
295  inline void Database::setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose) {
296  this->database.setLogging(level, min, verbose);
297  }
298 
300  inline void Database::setSleepOnError(std::uint64_t seconds) {
301  this->database.setSleepOnError(seconds);
302  }
303 
305  inline void Database::setTimeOut(std::uint64_t milliseconds) {
306  this->database.setTimeOut(milliseconds);
307  }
308 
309  /*
310  * LOGGING
311  */
312 
314  inline void Database::log(std::uint8_t level, const std::string& logEntry) {
315  this->database.log(level, logEntry);
316  }
317 
319  inline void Database::log(std::uint8_t level, std::queue<std::string>& logEntries) {
320  this->database.log(level, logEntries);
321  }
322 
323  /*
324  * WEBSITES
325  */
326 
328  inline std::string Database::getWebsiteDomain(std::uint64_t websiteId) {
329  return this->database.getWebsiteDomain(websiteId);
330  }
331 
332  /*
333  * QUERIES
334  */
335 
337  inline void Database::getQueryProperties(std::uint64_t queryId, QueryProperties& queryPropertiesTo) {
338  this->database.getQueryProperties(queryId, queryPropertiesTo);
339  }
340 
341  /*
342  * CONFIGURATIONS
343  */
344 
346  inline std::string Database::getConfiguration(std::uint64_t configId) {
347  return this->database.getConfiguration(configId);
348  }
349 
350  /*
351  * TARGET TABLES
352  */
353 
355  inline std::uint64_t Database::addOrUpdateTargetTable(const TargetTableProperties& properties) {
356  return this->database.addOrUpdateTargetTable(properties);
357  }
358 
360  inline std::queue<Database::IdString> Database::getTargetTables(const std::string& type, std::uint64_t listId) {
361  return this->database.getTargetTables(type, listId);
362  }
363 
365  inline std::uint64_t Database::getTargetTableId(
366  const std::string& type,
367  std::uint64_t listId,
368  const std::string& tableName
369  ) {
370  return this->database.getTargetTableId(type, listId, tableName);
371  }
372 
374  inline std::string Database::getTargetTableName(const std::string& type, std::uint64_t tableId) {
375  return this->database.getTargetTableName(type, tableId);
376  }
377 
379 
396  inline void Database::addTargetColumn(const std::string& tableName, const TableColumn& column) {
397  if(!(this->isColumnExists(tableName, column.name))) {
398  this->addColumn(tableName, column);
399  }
400  }
401 
403  inline void Database::deleteTargetTable(const std::string& type, std::uint64_t tableId) {
404  this->database.deleteTargetTable(type, tableId);
405  }
406 
407  /*
408  * LOCKING
409  */
410 
412  inline void Database::beginNoLock() {
413  this->database.beginNoLock();
414  }
415 
417  inline void Database::endNoLock() {
418  this->database.endNoLock();
419  }
420 
421  /*
422  * TABLES
423  */
424 
426  inline bool Database::isTableEmpty(const std::string& tableName) {
427  return this->database.isTableEmpty(tableName);
428  }
429 
431  inline bool Database::isTableExists(const std::string& tableName) {
432  return this->database.isTableExists(tableName);
433  }
434 
436  inline bool Database::isColumnExists(const std::string& tableName, const std::string& columnName) {
437  return this->database.isColumnExists(tableName, columnName);
438  }
439 
441  inline std::string Database::getColumnType(const std::string& tableName, const std::string& columnName) {
442  return this->database.getColumnType(tableName, columnName);
443  }
444 
445  /*
446  * CUSTOM DATA
447  */
448 
451  this->database.getCustomData(data);
452  }
453 
456  this->database.getCustomData(data);
457  }
458 
461  this->database.getCustomData(data);
462  }
463 
466  this->database.getCustomData(data);
467  }
468 
471  this->database.getCustomData(data);
472  }
473 
476  this->database.getCustomData(data);
477  }
478 
481  this->database.insertCustomData(data);
482  }
483 
486  this->database.insertCustomData(data);
487  }
488 
491  this->database.insertCustomData(data);
492  }
493 
496  this->database.updateCustomData(data);
497  }
498 
501  this->database.updateCustomData(data);
502  }
503 
506  this->database.updateCustomData(data);
507  }
508 
509  /*
510  * REQUEST COUNTER
511  */
512 
514  inline std::uint64_t Database::getRequestCounter() {
516  }
517 
518  /*
519  * GETTERS
520  */
521 
523 
530  return this->database.options;
531  }
532 
534 
539  inline const std::string& Database::getWebsiteIdString() const {
540  return this->database.websiteIdString;
541  }
542 
544 
549  inline const std::string& Database::getUrlListIdString() const {
550  return this->database.urlListIdString;
551  }
552 
554 
559  inline std::uint8_t Database::getLoggingMin() const {
560  return this->database.loggingMin;
561  }
562 
564 
568  inline std::uint8_t Database::getLoggingVerbose() const {
569  return this->database.loggingVerbose;
570  }
571 
573  inline std::uint64_t Database::getMaxAllowedPacketSize() const {
574  return this->database.getMaxAllowedPacketSize();
575  }
576 
577  /*
578  * VALIDATION
579  */
580 
583  this->database.checkConnection();
584  }
585 
586  /*
587  * HELPER FUNCTIONS FOR PREPARED SQL STATEMENTS
588  */
589 
591  inline void Database::reserveForPreparedStatements(std::size_t n) {
593  }
594 
596  inline void Database::addPreparedStatement(const std::string& sqlQuery, std::size_t& id) {
597  this->database.addPreparedStatement(sqlQuery, id);
598  }
599 
601  inline void Database::clearPreparedStatement(std::size_t& id) {
603  }
604 
606  inline sql::PreparedStatement& Database::getPreparedStatement(std::size_t id) {
607  return this->database.getPreparedStatement(id);
608  }
609 
610  /*
611  * DATABASE HELPER FUNCTIONS
612  */
613 
615  inline std::uint64_t Database::getLastInsertedId() {
616  return this->database.getLastInsertedId();
617  }
618 
620  inline void Database::addDatabaseLock(const std::string& name, const IsRunningCallback& isRunningCallback) {
621  Main::Database::addDatabaseLock(name, isRunningCallback);
622  }
623 
625  inline bool Database::tryDatabaseLock(const std::string& name) {
626  return Main::Database::tryDatabaseLock(name);
627  }
628 
630  inline void Database::removeDatabaseLock(const std::string& name) {
632  }
633 
635  inline void Database::createTable(const TableProperties& properties) {
636  this->database.createTable(properties);
637  }
638 
640  inline void Database::addColumn(const std::string& tableName, const TableColumn& column) {
641  this->database.addColumn(tableName, column);
642  }
643 
645  inline void Database::dropTable(const std::string& tableName) {
646  this->database.dropTable(tableName);
647  }
648 
650  inline void Database::compressTable(const std::string& tableName) {
651  this->database.compressTable(tableName);
652  }
653 
654  /*
655  * URL LIST HELPER FUNCTION
656  */
657 
659  inline void Database::setUrlListCaseSensitive(std::uint64_t listId, bool isCaseSensitive) {
660  this->database.setUrlListCaseSensitive(listId, isCaseSensitive);
661  }
662 
663  /*
664  * EXCEPTION HELPER FUNCTION
665  */
666 
668  inline void Database::sqlException(const std::string& function, const sql::SQLException& e) {
669  Main::Database::sqlException(function, e);
670  }
671 
673 
683  inline bool Database::sqlExecute(sql::PreparedStatement& sqlPreparedStatement) {
684  return Main::Database::sqlExecute(sqlPreparedStatement);
685  }
686 
687  /*
688  * HELPER FUNCTIONS FOR EXECUTING SQL QUERIES
689  */
690 
692 
700  inline sql::ResultSet * Database::sqlExecuteQuery(sql::PreparedStatement& sqlPreparedStatement) {
701  return Main::Database::sqlExecuteQuery(sqlPreparedStatement);
702  }
703 
705 
713  inline int Database::sqlExecuteUpdate(sql::PreparedStatement& sqlPreparedStatement) {
714  return Main::Database::sqlExecuteUpdate(sqlPreparedStatement);
715  }
716 
717 } /* namespace crawlservpp::Wrapper */
718 
719 #endif /* WRAPPER_DATABASE_HPP_ */
std::uint64_t addOrUpdateTargetTable(const TargetTableProperties &properties)
Adds a new target table or updates an existing target table in the database.
Definition: Database.cpp:6134
std::queue< IdString > getTargetTables(const std::string &type, std::uint64_t listId)
Gets the target tables of the specified type for a URL list from the database.
Definition: Database.hpp:360
bool isTableEmpty(const std::string &tableName)
Checks whether a table in the database is empty.
Definition: Database.hpp:426
Query properties containing its name, text, type, and result type(s).
Definition: QueryProperties.hpp:39
void setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose)
Sets the current, minimal, and verbose logging levels.
Definition: Database.cpp:144
std::uint64_t getTargetTableId(const std::string &type, std::uint64_t listId, const std::string &tableName)
Gets the ID of a target table from the database.
Definition: Database.hpp:365
sql::PreparedStatement & getPreparedStatement(std::size_t id)
Gets a reference to a prepared SQL statement.
Definition: Database.cpp:10272
const std::string & getUrlListIdString() const
Gets the ID of the URL list used by the thread as string.
Definition: Database.hpp:549
const std::string & getWebsiteIdString() const
Gets the ID of the website used by the thread as string.
Definition: Database.hpp:539
void getCustomData(Data::GetValue &data)
Gets a custom value from one column from a table row in the database.
Definition: Database.hpp:450
static int sqlExecuteUpdate(sql::PreparedStatement &sqlPreparedStatement)
Executes a prepared SQL statement and returns the number of affected rows.
Definition: Database.hpp:713
void beginNoLock()
Disables database locking by starting a new SQL transaction.
Definition: Database.hpp:412
static void sqlException(const std::string &function, const sql::SQLException &e)
Catches a SQL exception and re-throws it as a specific or a generic Database::Exception.
Definition: Database.hpp:668
void compressTable(const std::string &tableName)
Compresses a table in the database.
Definition: Database.cpp:10834
void clearPreparedStatement(std::size_t &id)
Clears a prepared SQL statement.
Definition: Database.cpp:10240
Structure for retrieving multiple table columns of different types.
Definition: Data.hpp:310
void dropTable(const std::string &tableName)
Deletes a table from the database.
Definition: Database.cpp:10691
void addColumn(const std::string &tableName, const TableColumn &column)
Adds a column to a table in the database.
Definition: Database.cpp:10751
bool isTableExists(const std::string &tableName)
Checks whether a table exists in the database.
Definition: Database.hpp:431
std::string getWebsiteDomain(std::uint64_t id)
Gets the domain of a website from the database.
Definition: Database.cpp:1617
Target table properties containing its type, website, URL list, table names, columns, and compression.
Definition: TargetTableProperties.hpp:44
void getQueryProperties(std::uint64_t queryId, QueryProperties &queryPropertiesTo)
Gets the properties of a query from the database.
Definition: Database.hpp:337
std::string getConfiguration(std::uint64_t configId)
Gets a configuration from the database.
Definition: Database.hpp:346
Class handling database access for threads.
Definition: Database.hpp:91
void reserveForPreparedStatements(std::size_t n)
Reserves memory for a specific number of additional prepared SQL statements.
Definition: Database.hpp:591
static void addDatabaseLock(const std::string &name, const IsRunningCallback &isRunningCallback)
Adds a lock to the database class, blocking execution.
Definition: Database.hpp:620
std::string name
Name of the table column.
Definition: TableColumn.hpp:44
void setSleepOnError(std::uint64_t seconds)
Sets the number of seconds to sleep before trying to reconnect after connection loss.
Definition: Database.cpp:128
void addPreparedStatement(const std::string &sqlQuery, std::size_t &id)
Prepares an additional SQL statement and sets its ID.
Definition: Database.cpp:10191
void clearPreparedStatement(std::size_t &id)
Clears a prepared SQL statement.
Definition: Database.hpp:601
void getCustomData(Data::GetValue &data)
Gets a custom value from one column from a table row in the database.
Definition: Database.cpp:8132
void updateCustomData(const Data::UpdateValue &data)
Updates a custom value in a table row.
Definition: Database.cpp:9607
void setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose)
Sets the current, minimal, and verbose logging levels.
Definition: Database.hpp:295
void endNoLock()
Re-enables database locking by ending the previous SQL transaction.
Definition: Database.hpp:417
void deleteTargetTable(const std::string &type, std::uint64_t tableId)
Deletes a target table from the database.
Definition: Database.cpp:6590
Structure for getting multiple values of different types from a table column.
Definition: Data.hpp:243
void addPreparedStatement(const std::string &sqlQuery, std::size_t &id)
Prepares an additional SQL statement and sets its ID.
Definition: Database.hpp:596
Structure for updating multiple values of different types in a table.
Definition: Data.hpp:408
static std::uint64_t getRequestCounter()
Gets the number of SQL requests performed since the start of the application.
Definition: Database.hpp:514
std::uint8_t getLoggingVerbose() const
Gets the level for verbose logging.
Definition: Database.hpp:568
static bool sqlExecute(T &statement, Args... args)
Template function for executing a SQL query.
Definition: Database.hpp:862
void setTimeOut(std::uint64_t milliseconds)
Sets the maximum execution time for MySQL queries, in milliseconds.
Definition: Database.cpp:145
void checkConnection()
Checks whether the connection to the database is still valid and tries to reconnect if necessary...
Definition: Database.cpp:6677
void deleteTargetTable(const std::string &type, std::uint64_t tableId)
Deletes a target table from the database.
Definition: Database.hpp:403
std::uint64_t getLastInsertedId()
Gets the last inserted ID from the database.
Definition: Database.cpp:10300
std::queue< IdString > getTargetTables(const std::string &type, std::uint64_t listId)
Gets the target tables of the specified type for a URL list from the database.
Definition: Database.cpp:6346
Structure for updating one value in a table.
Definition: Data.hpp:369
static std::uint64_t getRequestCounter()
Gets the number of SQL requests performed since the start of the application.
Definition: Database.hpp:651
static bool sqlExecute(sql::PreparedStatement &sqlPreparedStatement)
Executes a prepared SQL statement.
Definition: Database.hpp:683
std::uint64_t getLastInsertedId()
Gets the last inserted ID from the database.
Definition: Database.hpp:615
Module options containing the thread ID, as well as ID and namespace of website and URL list used by ...
Definition: ModuleOptions.hpp:40
Table properties containing its name, columns, data directory, and compression.
Definition: TableProperties.hpp:42
Structure for inserting multiple values of different types into a row.
Definition: Data.hpp:360
void insertCustomData(const Data::InsertValue &data)
Inserts a custom value into a table row in the database.
Definition: Database.cpp:8993
Structure for retrieving one value from a table column.
Definition: Data.hpp:207
Structure for table columns containing its name, type, reference, and indexing.
Definition: TableColumn.hpp:39
void log(std::uint8_t level, const std::string &logEntry)
Writes a thread-specific log entry to the database.
Definition: Database.hpp:314
void beginNoLock()
Disables database locking by starting a new SQL transaction.
Definition: Database.cpp:7436
void setSleepOnError(std::uint64_t seconds)
Sets the number of seconds to sleep before trying to reconnect after connection loss.
Definition: Database.hpp:300
std::string getColumnType(const std::string &tableName, const std::string &columnName)
Gets the type of a specific table column from the database.
Definition: Database.hpp:441
std::uint64_t addOrUpdateTargetTable(const TargetTableProperties &properties)
Adds a new target table or updates an existing target table in the database.
Definition: Database.hpp:355
Wrapper class providing the database functionality of Module::Database to its child classes...
Definition: Database.hpp:72
std::string getWebsiteDomain(std::uint64_t websiteId)
Gets the domain of a website from the database.
Definition: Database.hpp:328
void addTargetColumn(const std::string &tableName, const TableColumn &column)
Adds a column to the target table, if it does not exist already.
Definition: Database.hpp:396
Structure for updating multiple values of the same type in a table.
Definition: Data.hpp:390
void createTable(const TableProperties &properties)
Adds a table to the database.
Definition: Database.hpp:635
static bool tryDatabaseLock(const std::string &name)
Tries to add a lock to the database class, not blocking execution.
Definition: Database.hpp:625
bool isTableEmpty(const std::string &tableName)
Checks whether a table in the database is empty.
Definition: Database.cpp:7488
Template class for safe in-scope database locks.
Definition: DatabaseTryLock.hpp:51
std::uint64_t getMaxAllowedPacketSize() const
Gets the maximum allowed packet size for communicating with the MySQL server.
Definition: Database.hpp:573
void reserveForPreparedStatements(std::size_t n)
Reserves memory for a specific number of additional prepared SQL statements.
Definition: Database.cpp:10165
Structure for retrieving multiple table columns of the same type.
Definition: Data.hpp:284
static void removeDatabaseLock(const std::string &name)
Removes a lock from the database class.
Definition: Database.hpp:630
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
std::string getTargetTableName(std::string_view type, std::uint64_t tableId)
Gets the name of a target table from the database.
Definition: Database.cpp:6518
const ModuleOptions & getOptions() const
Gets the options of the module.
Definition: Database.hpp:529
static sql::ResultSet * sqlExecuteQuery(T &statement, Args... args)
Template function for executing a SQL query and returning the resulting set.
Definition: Database.hpp:920
void dropTable(const std::string &tableName)
Deletes a table from the database.
Definition: Database.hpp:645
Database(Module::Database &dbThread)
Constructor setting the database connection.
Definition: Database.hpp:288
static int sqlExecuteUpdate(T &statement, Args... args)
Template function for executing a SQL query and returning the number of affected rows.
Definition: Database.hpp:978
void insertCustomData(const Data::InsertValue &data)
Inserts a custom value into a table row in the database.
Definition: Database.hpp:480
Structure for retrieving multiple values of the same type from a table column.
Definition: Data.hpp:225
static void sqlException(const std::string &function, const sql::SQLException &e)
Catches a SQL exception and re-throws it as a specific or a generic Database::Exception.
Definition: Database.cpp:11200
void setTimeOut(std::uint64_t milliseconds)
Sets the maximum execution time for MySQL queries, in milliseconds.
Definition: Database.hpp:305
std::string getTargetTableName(const std::string &type, std::uint64_t tableId)
Gets the name of a target table from the database.
Definition: Database.hpp:374
Database & operator=(Database &)=delete
Deleted copy assignment operator.
void compressTable(const std::string &tableName)
Compresses a table in the database.
Definition: Database.hpp:650
void setUrlListCaseSensitive(std::uint64_t listId, bool isCaseSensitive)
Sets whether the specified URL list is case-sensitive.
Definition: Database.cpp:11143
std::uint64_t getTargetTableId(const std::string &type, std::uint64_t listId, const std::string &tableName)
Gets the ID of a target table from the database.
Definition: Database.cpp:6432
virtual ~Database()=default
Default destructor.
static void removeDatabaseLock(const std::string &name)
Removes a lock from the database class.
Definition: Database.cpp:10477
static sql::ResultSet * sqlExecuteQuery(sql::PreparedStatement &sqlPreparedStatement)
Executes a prepared SQL statement and returns the resulting set.
Definition: Database.hpp:700
bool isColumnExists(const std::string &tableName, const std::string &columnName)
Checks whether a table in the database contains a specific column.
Definition: Database.hpp:436
Structure for retrieving the values in a table column.
Definition: Data.hpp:258
Module::Database & database
Reference to the database connection for the thread.
Definition: Database.hpp:210
static void addDatabaseLock(const std::string &name, const IsRunningCallback &isRunningCallback)
Adds a lock to the database class, blocking execution.
Definition: Database.cpp:10399
bool isColumnExists(const std::string &tableName, const std::string &columnName)
Checks whether a table in the database contains a specific column.
Definition: Database.cpp:7618
void getQueryProperties(std::uint64_t queryId, QueryProperties &queryPropertiesTo)
Gets the properties of a query from the database.
Definition: Database.cpp:5433
void createTable(const TableProperties &properties)
Adds a table to the database.
Definition: Database.cpp:10570
std::uint64_t getMaxAllowedPacketSize() const
Gets the maximum allowed packet size for communicating with the MySQL server.
Definition: Database.cpp:206
void setUrlListCaseSensitive(std::uint64_t listId, bool isCaseSensitive)
Sets whether the specified URL list is case-sensitive.
Definition: Database.hpp:659
std::uint8_t getLoggingMin() const
Gets the minimal logging level.
Definition: Database.hpp:559
Structure for inserting one value into a table.
Definition: Data.hpp:333
Template class for safe in-scope database locks.
Definition: DatabaseLock.hpp:54
sql::PreparedStatement & getPreparedStatement(std::size_t id)
Gets a reference to a prepared SQL statement.
Definition: Database.hpp:606
Structure for inserting multiple values of the same type into a table.
Definition: Data.hpp:348
bool isTableExists(const std::string &tableName)
Checks whether a table exists in the database.
Definition: Database.cpp:7549
void log(std::uint8_t level, const std::string &logEntry)
Writes a thread-specific log entry to the database.
Definition: Database.cpp:251
void addColumn(const std::string &tableName, const TableColumn &column)
Adds a column to a table in the database.
Definition: Database.hpp:640
void updateCustomData(const Data::UpdateValue &data)
Updates a custom value in a table row.
Definition: Database.hpp:495
static bool tryDatabaseLock(const std::string &name)
Tries to add a lock to the database class, not blocking execution.
Definition: Database.cpp:10445
std::string getColumnType(const std::string &tableName, const std::string &columnName)
Gets the type of a specific table column from the database.
Definition: Database.cpp:7695
void checkConnection()
Checks whether the connection to the database is still valid and tries to reconnect if necessary...
Definition: Database.hpp:582
void endNoLock()
Re-enables database locking by ending the previous SQL transaction.
Definition: Database.cpp:7454
std::string getConfiguration(std::uint64_t configId)
Gets a configuration from the database.
Definition: Database.cpp:5867