Impvorements and updates

Improved DatabaseWrapper
This commit is contained in:
Valentino Orlandi 2024-02-04 21:57:41 +01:00
parent cbabccfe0c
commit e2c2b279d3
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
2 changed files with 24 additions and 7 deletions

View File

@ -17,7 +17,7 @@
////////////////////////// //////////////////////////
DatabaseWrapper::DatabaseWrapper( const QString& database, const QString& name, const bool readonly ) DatabaseWrapper::DatabaseWrapper( const QString& database, const QString& name, const bool readonly )
: db{ QSqlDatabase::database( database ) } : db{ QSqlDatabase::database( database, false ) }
, db_name{ name } , db_name{ name }
, ongoing_transaction{ false } , 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 ) void DatabaseWrapper::startTransaction(const bool explain_msg, const bool explain_err )
{ {
if ( ! this->db.transaction() ) { if ( ! this->db.transaction() ) {
@ -124,23 +133,23 @@ size_t QueryWrapper::size()
DatabaseHandler::DatabaseHandler() DatabaseHandler::DatabaseHandler()
{ {
QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::data ); QSqlDatabase::addDatabase( "QSQLITE", DatabasesConnections::data );
QSqlDatabase::addDatabase( "QSQLITE", DatabasesNames::hashes ); QSqlDatabase::addDatabase( "QSQLITE", DatabasesConnections::hashes );
} }
DatabaseHandler::~DatabaseHandler() DatabaseHandler::~DatabaseHandler()
{ {
QSqlDatabase::removeDatabase( DatabasesNames::data ); QSqlDatabase::removeDatabase( DatabasesConnections::data );
QSqlDatabase::removeDatabase( DatabasesNames::hashes ); QSqlDatabase::removeDatabase( DatabasesConnections::hashes );
} }
DatabaseWrapper DatabaseHandler::get( const DatabaseType db_type, const bool readonly ) DatabaseWrapper DatabaseHandler::get( const DatabaseType db_type, const bool readonly )
{ {
switch ( db_type ) { switch ( db_type ) {
case DatabaseType::Data: case DatabaseType::Data:
return DatabaseWrapper( DatabasesNames::data, QStringLiteral("collection.db"), readonly ); return DatabaseWrapper( DatabasesConnections::data, QString(DatabasesNames::data), readonly );
case DatabaseType::Hashes: case DatabaseType::Hashes:
return DatabaseWrapper( DatabasesNames::hashes, QStringLiteral("hashes.db"), readonly ); return DatabaseWrapper( DatabasesConnections::hashes, QString(DatabasesNames::hashes), readonly );
default: default:
throw DoNotCatchException( "Unexpected DatabaseType" ); throw DoNotCatchException( "Unexpected DatabaseType" );
} }

View File

@ -54,6 +54,14 @@ public:
*/ */
void open( const std::string& path, const bool explain_err ); 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 //! Starts an ACID transaction on the database
/*! /*!
Throws in case of failure Throws in case of failure