From e3338f9d3b1ba3b7c851b7206a696777f47d04ca Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 1 Dec 2020 14:53:32 +0100 Subject: [PATCH] Now the postgres and sqlite connection also inherits from Database, and made main.cpp register them if they are present. --- database/mysql/mysql_connection.h | 2 +- database/postgres/pgsql_connection.h | 36 +++++++++++++++++++--------- database/sqlite/sqlite3_connection.h | 20 ++++++++++++++-- main.cpp | 15 +++++++++++- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/database/mysql/mysql_connection.h b/database/mysql/mysql_connection.h index cf46d37..2580187 100644 --- a/database/mysql/mysql_connection.h +++ b/database/mysql/mysql_connection.h @@ -26,7 +26,7 @@ public: DatabaseManager::_unregister_db_creation_func("mysql"); } - MysqlConnection() { + MysqlConnection() : Database() { mysql = new MYSQL(); } ~MysqlConnection() { diff --git a/database/postgres/pgsql_connection.h b/database/postgres/pgsql_connection.h index f3f6615..0cea512 100644 --- a/database/postgres/pgsql_connection.h +++ b/database/postgres/pgsql_connection.h @@ -1,6 +1,9 @@ #ifndef PGSQL_CONNECTION #define PGSQL_CONNECTION +#include "core/database.h" +#include "core/database_manager.h" + //Brynet has it aswell, and because of using namespace it is defined here aswell //later this will be fixed better //#ifdef IS_NUM @@ -9,17 +12,28 @@ #include -class PGSQLConnection { - public: - PGSQLConnection() { - conn = PQconnectStart(""); - } - ~PGSQLConnection() - { - PQfinish(conn); - } - - PGconn *conn; +class PGSQLConnection : public Database { +public: + static Database *_creation_func() { + return new PGSQLConnection(); + } + + static void _register() { + DatabaseManager::_register_db_creation_func("pgsql", PGSQLConnection::_creation_func); + } + + static void _unregister() { + DatabaseManager::_unregister_db_creation_func("pgsql"); + } + + PGSQLConnection() : Database() { + conn = PQconnectStart(""); + } + ~PGSQLConnection() { + PQfinish(conn); + } + + PGconn *conn; }; //#undef IS_NUM diff --git a/database/sqlite/sqlite3_connection.h b/database/sqlite/sqlite3_connection.h index fc878ee..cfce88e 100644 --- a/database/sqlite/sqlite3_connection.h +++ b/database/sqlite/sqlite3_connection.h @@ -1,6 +1,9 @@ #ifndef SQLITE3_CONNECTION #define SQLITE3_CONNECTION +#include "core/database.h" +#include "core/database_manager.h" + //Brynet has it aswell, and because of using namespace it is defined here aswell //later this will be fixed better //#ifdef IS_NUM @@ -11,9 +14,22 @@ #include -class SQLite3Connection { +class SQLite3Connection : public Database { public: - SQLite3Connection() { + static Database *_creation_func() { + return new SQLite3Connection(); + } + + static void _register() { + DatabaseManager::_register_db_creation_func("sqlite", SQLite3Connection::_creation_func); + } + + static void _unregister() { + DatabaseManager::_unregister_db_creation_func("sqlite"); + } + + SQLite3Connection() : + Database() { int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD); if (ret != SQLITE_OK) { diff --git a/main.cpp b/main.cpp index 41d170c..521a826 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,19 @@ #include "modules/message_page/message_page.h" +#include "core/database_manager.h" + +#if MYSQL_PRESENT #include "database/mysql/mysql_connection.h" +#endif + +#if PGSQL_PRESENT +#include "database/postgres/pgsql_connection.h" +#endif + +#if SQLITE_PRESENT +#include "database/sqlite/sqlite3_connection.h" +#endif #define MAIN_CLASS RDNApplication @@ -17,16 +29,17 @@ int main(int argc, char **argv) { #if MYSQL_PRESENT printf("mysql present\n"); - MysqlConnection::_register(); #endif #if PGSQL_PRESENT printf("pgsql present\n"); + PGSQLConnection::_register(); #endif #if SQLITE_PRESENT printf("sqlite present\n"); + SQLite3Connection::_register(); #endif FileCache *file_cache = new FileCache(true);