32 #ifndef WRAPPER_DATABASE_HPP_ 33 #define WRAPPER_DATABASE_HPP_ 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" 47 #include <cppconn/prepared_statement.h> 49 #include <mysql_connection.h> 56 #include <string_view> 80 using IdString = std::pair<std::uint64_t, std::string>;
81 using IsRunningCallback = std::function<bool()>;
102 void setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose);
110 void log(std::uint8_t level,
const std::string& logEntry);
111 void log(std::uint8_t level, std::queue<std::string>& logEntries);
136 [[nodiscard]] std::queue<IdString>
getTargetTables(
const std::string& type, std::uint64_t listId);
138 const std::string& type,
139 std::uint64_t listId,
140 const std::string& tableName
142 [[nodiscard]] std::string
getTargetTableName(
const std::string& type, std::uint64_t tableId);
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);
244 static void addDatabaseLock(
const std::string& name,
const IsRunningCallback& isRunningCallback);
249 void dropTable(
const std::string& tableName);
262 static void sqlException(
const std::string&
function,
const sql::SQLException& e);
268 static bool sqlExecute(sql::PreparedStatement& sqlPreparedStatement);
269 static sql::ResultSet *
sqlExecuteQuery(sql::PreparedStatement& sqlPreparedStatement);
314 inline void Database::log(std::uint8_t level,
const std::string& logEntry) {
319 inline void Database::log(std::uint8_t level, std::queue<std::string>& logEntries) {
366 const std::string& type,
367 std::uint64_t listId,
368 const std::string& tableName
540 return this->
database.websiteIdString;
550 return this->
database.urlListIdString;
569 return this->
database.loggingVerbose;
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