From e2c2b279d3625ee2e92a9efc6fbe5fcfa3d58f96 Mon Sep 17 00:00:00 2001 From: Valentino Orlandi Date: Sun, 4 Feb 2024 21:57:41 +0100 Subject: [PATCH] Impvorements and updates Improved DatabaseWrapper --- logdoctor/modules/database/database.cpp | 23 ++++++++++++++++------- logdoctor/modules/database/database.h | 8 ++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/logdoctor/modules/database/database.cpp b/logdoctor/modules/database/database.cpp index 7c8c044e..07771bc2 100644 --- a/logdoctor/modules/database/database.cpp +++ b/logdoctor/modules/database/database.cpp @@ -17,7 +17,7 @@ ////////////////////////// DatabaseWrapper::DatabaseWrapper( const QString& database, const QString& name, const bool readonly ) - : db{ QSqlDatabase::database( database ) } + : db{ QSqlDatabase::database( database, false ) } , db_name{ name } , ongoing_transaction{ false } { @@ -50,6 +50,15 @@ void DatabaseWrapper::open( const std::string& path, const bool explain_err ) } } +void DatabaseWrapper::openNew( const std::string& path ) +{ + this->db.setDatabaseName( QString::fromStdString( path )); + if ( ! this->db.open() ) { + DialogSec::errDatabaseFailedOpening( this->db_name, db.lastError().text() ); + throw LogDoctorException(); + } +} + void DatabaseWrapper::startTransaction(const bool explain_msg, const bool explain_err ) { if ( ! this->db.transaction() ) { @@ -124,23 +133,23 @@ size_t QueryWrapper::size() DatabaseHandler::DatabaseHandler() { - QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::data ); - QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::hashes ); + QSqlDatabase::addDatabase( "QSQLITE", DatabasesConnections::data ); + QSqlDatabase::addDatabase( "QSQLITE", DatabasesConnections::hashes ); } DatabaseHandler::~DatabaseHandler() { - QSqlDatabase::removeDatabase( DatabasesNames::data ); - QSqlDatabase::removeDatabase( DatabasesNames::hashes ); + QSqlDatabase::removeDatabase( DatabasesConnections::data ); + QSqlDatabase::removeDatabase( DatabasesConnections::hashes ); } DatabaseWrapper DatabaseHandler::get( const DatabaseType db_type, const bool readonly ) { switch ( db_type ) { case DatabaseType::Data: - return DatabaseWrapper( DatabasesNames::data, QStringLiteral("collection.db"), readonly ); + return DatabaseWrapper( DatabasesConnections::data, QString(DatabasesNames::data), readonly ); case DatabaseType::Hashes: - return DatabaseWrapper( DatabasesNames::hashes, QStringLiteral("hashes.db"), readonly ); + return DatabaseWrapper( DatabasesConnections::hashes, QString(DatabasesNames::hashes), readonly ); default: throw DoNotCatchException( "Unexpected DatabaseType" ); } diff --git a/logdoctor/modules/database/database.h b/logdoctor/modules/database/database.h index 68e22f0d..b871f145 100644 --- a/logdoctor/modules/database/database.h +++ b/logdoctor/modules/database/database.h @@ -54,6 +54,14 @@ public: */ void open( const std::string& path, const bool explain_err ); + //! Opens the database file at the given path + /*! + Used when creating a new database file. + Throws if opening fails. + \throw LogDoctorException + */ + void openNew( const std::string& path ); + //! Starts an ACID transaction on the database /*! Throws in case of failure