From 93fe4a94b29515cbc31c7d4afc4e4ae33fb647a4 Mon Sep 17 00:00:00 2001 From: krivoshein Date: Wed, 13 Aug 2025 10:50:55 +0300 Subject: [PATCH] QMutexLocker --- DataBaseLMS/databaselms.cpp | 43 ------------ DataBaseLMS/databaselms.h | 7 +- DataBaseLMS/interfacedatabaselms.cpp | 98 +++++++++++++++++++--------- DataBaseLMS/interfacedatabaselms.h | 1 + 4 files changed, 69 insertions(+), 80 deletions(-) diff --git a/DataBaseLMS/databaselms.cpp b/DataBaseLMS/databaselms.cpp index c9979fb..b1a0771 100644 --- a/DataBaseLMS/databaselms.cpp +++ b/DataBaseLMS/databaselms.cpp @@ -24,8 +24,6 @@ DataBaseLMS::~DataBaseLMS() bool DataBaseLMS::createConnection() { - mtxAccess.lock(); - dbSettings = getDataBaseSettings(); dbSettings.connectionName = "Connection_" + dbSettings.dbName; @@ -41,7 +39,6 @@ bool DataBaseLMS::createConnection() if(!res) { - mtxAccess.unlock(); deleteConnection(); return false; } @@ -49,15 +46,12 @@ bool DataBaseLMS::createConnection() { bool flHas = db->driver()->hasFeature(QSqlDriver::Transactions); qDebug() << "db->driver()->hasFeature(QSqlDriver::Transactions): " << flHas; - mtxAccess.unlock(); return true; } } void DataBaseLMS::deleteConnection() { - mtxAccess.lock(); - if(transactionBegined) QSqlDatabase::database().rollback(); @@ -69,29 +63,21 @@ void DataBaseLMS::deleteConnection() delete db; db = nullptr; } - - mtxAccess.unlock(); } bool DataBaseLMS::isConnected() { - mtxAccess.lock(); - if(db == nullptr) { - mtxAccess.unlock(); return false; } else { if(db->isOpen()) { - mtxAccess.unlock(); return true; } } - - mtxAccess.unlock(); return false; } @@ -153,33 +139,6 @@ DataBaseSettings DataBaseLMS::getDataBaseSettings() return settings; } -bool DataBaseLMS::transactionBegin() -{ - /* - if(transactionBegined) - {//Отмена предыдущей незаконченной - if(QSqlDatabase::database().rollback()) - transactionBegined = false; - else - return false; - } - return (transactionBegined = QSqlDatabase::database().transaction()); - */ - return true; -} - -bool DataBaseLMS::transactionEnd() -{ - /* - if(transactionBegined) - { - return !(transactionBegined = !QSqlDatabase::database().commit()); - } - return false; - */ - return true; -} - int DataBaseLMS::queryExecInt(QString queryStr) { QSqlQuery query = QSqlQuery(*db); @@ -222,10 +181,8 @@ bool DataBaseLMS::queryExecBool(QString queryStr) bool DataBaseLMS::queryExec(QString queryStr, QSqlQuery *query) { bool res = false; - mtxAccess.lock(); if(! (res = query->exec(queryStr)) ) messageWarningErrorQuery(queryStr, query); - mtxAccess.unlock(); return res; } diff --git a/DataBaseLMS/databaselms.h b/DataBaseLMS/databaselms.h index b001a8a..c15fdd3 100644 --- a/DataBaseLMS/databaselms.h +++ b/DataBaseLMS/databaselms.h @@ -39,10 +39,6 @@ protected: void deleteConnection(); bool isConnected(); - //Транзакции - bool transactionBegin(); - bool transactionEnd(); - //Юзер int selectUserID(QString type, QString login, QString password = QStringLiteral("")); QString selectUserNameByLogin(QString type, QString login); @@ -115,8 +111,7 @@ protected: DataBaseSettings dbSettings; private: - bool transactionBegined; - QMutex mtxAccess; + bool transactionBegined; QWidget* ownerWidget; }; diff --git a/DataBaseLMS/interfacedatabaselms.cpp b/DataBaseLMS/interfacedatabaselms.cpp index f9ace74..8812829 100644 --- a/DataBaseLMS/interfacedatabaselms.cpp +++ b/DataBaseLMS/interfacedatabaselms.cpp @@ -21,6 +21,7 @@ void InterfaceDataBaseLMS::slot_LanguageChanged(QString language) bool InterfaceDataBaseLMS::ConnectionToDB() { + QMutexLocker mtxLocker(&mtxAccess); if(!createConnection()) { QMessageBox::critical(ownerWidget, dbSettings.dbName, tr("Connection error: ") + db->lastError().text()); @@ -34,7 +35,8 @@ bool InterfaceDataBaseLMS::ConnectionToDB() } bool InterfaceDataBaseLMS::DisConnectionFromDB() -{ +{ + QMutexLocker mtxLocker(&mtxAccess); deleteConnection(); //QMessageBox::information(ownerWidget, dbName, tr("Disconnection is successful!")); return true; @@ -42,6 +44,7 @@ bool InterfaceDataBaseLMS::DisConnectionFromDB() bool InterfaceDataBaseLMS::DBisConnected() { + QMutexLocker mtxLocker(&mtxAccess); return isConnected(); } @@ -50,76 +53,77 @@ bool InterfaceDataBaseLMS::DBisConnected() bool InterfaceDataBaseLMS::AuthorizationInstructor(QString login, QString password) { - if(! transactionBegin()) - return false; + QMutexLocker mtxLocker(&mtxAccess); if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login, password)) { - if(isArchivedInstructor(id) || isLoggedInInstructor(id)) - { - transactionEnd(); + if(selectUserArchived(DataBaseLMS::TypeUserDBInstructor, id) || selectUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id)) return false; - } - if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, true)) - return transactionEnd(); - } - transactionEnd(); + if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, true)) + return true; + } return false; } bool InterfaceDataBaseLMS::deAuthorizationInstructor(QString login) { - if(! transactionBegin()) - return false; + QMutexLocker mtxLocker(&mtxAccess); if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login)) { if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, false)) - return transactionEnd(); + return true; } - - transactionEnd(); return false; } bool InterfaceDataBaseLMS::deAuthorizationAllInstructors() { + QMutexLocker mtxLocker(&mtxAccess); return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBInstructor,false); } QString InterfaceDataBaseLMS::getNameInstructorByLogin(QString login) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserNameByLogin(DataBaseLMS::TypeUserDBInstructor, login); } int InterfaceDataBaseLMS::getIdInstructorByLogin(QString login) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserID(DataBaseLMS::TypeUserDBInstructor, login); } QList InterfaceDataBaseLMS::getListInstructors() { + QMutexLocker mtxLocker(&mtxAccess); return selectAllInstructors(); } Instructor InterfaceDataBaseLMS::getInstructor(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectInstructor(id); } int InterfaceDataBaseLMS::newInstructor() { + QMutexLocker mtxLocker(&mtxAccess); return insertInstructor(); } int InterfaceDataBaseLMS::delInstructor(int id) { + QMutexLocker mtxLocker(&mtxAccess); return deleteInstructor(id); } int InterfaceDataBaseLMS::editInstructor(Instructor instructor) { + QMutexLocker mtxLocker(&mtxAccess); + //Проверка корректности логина, имени, пароля QList listInstructors = selectAllInstructors(); for(Instructor exist_instructor : listInstructors) @@ -165,16 +169,19 @@ int InterfaceDataBaseLMS::editInstructor(Instructor instructor) bool InterfaceDataBaseLMS::isAdminInstructor(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectInstructorIsAdmin(id); } bool InterfaceDataBaseLMS::isArchivedInstructor(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserArchived(DataBaseLMS::TypeUserDBInstructor, id); } bool InterfaceDataBaseLMS::isLoggedInInstructor(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id); } @@ -183,46 +190,41 @@ bool InterfaceDataBaseLMS::isLoggedInInstructor(int id) bool InterfaceDataBaseLMS::AuthorizationTrainee(QString login, QString password, QString classroom_name, QString computer_name) { - if(! transactionBegin()) - return false; + QMutexLocker mtxLocker(&mtxAccess); if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login, password)) { - if(isArchivedTrainee(id) || isLoggedInTrainee(id)) - { - transactionEnd(); + if(selectUserArchived(DataBaseLMS::TypeUserDBTrainee, id) || selectUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id)) return false; - } - if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, true)) - return transactionEnd(); - } - transactionEnd(); + if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, true)) + return true; + } return false; } bool InterfaceDataBaseLMS::deAuthorizationTrainee(QString login) { - if(! transactionBegin()) - return false; + QMutexLocker mtxLocker(&mtxAccess); if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login)) { if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, false)) - return transactionEnd(); + return true; } - - transactionEnd(); return false; } bool InterfaceDataBaseLMS::deAuthorizationAllTrainees() { + QMutexLocker mtxLocker(&mtxAccess); return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBTrainee, false); } int InterfaceDataBaseLMS::entryTraineeOnSimulator(int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); + TimingOfTrainee timing(id_trainee); timing = selectTimingTrainee(id_trainee); @@ -243,6 +245,8 @@ int InterfaceDataBaseLMS::entryTraineeOnSimulator(int id_trainee) int InterfaceDataBaseLMS::exitTraineeFromSimulator(int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); + TimingOfTrainee timing(id_trainee); timing = selectTimingTrainee(id_trainee); @@ -263,61 +267,74 @@ int InterfaceDataBaseLMS::exitTraineeFromSimulator(int id_trainee) QString InterfaceDataBaseLMS::getNameTraineeOnComputer(QString computer_name) { + QMutexLocker mtxLocker(&mtxAccess); return selectTraineeNameOnComputer(computer_name); } Trainee InterfaceDataBaseLMS::getTraineeOnComputer(QString computer_name) { + QMutexLocker mtxLocker(&mtxAccess); return selectTraineeOnComputer(computer_name); } QString InterfaceDataBaseLMS::getNameTraineeByLogin(QString login) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserNameByLogin(DataBaseLMS::TypeUserDBTrainee, login); } int InterfaceDataBaseLMS::getIdTraineeByLogin(QString login) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserID(DataBaseLMS::TypeUserDBTrainee, login); } QList InterfaceDataBaseLMS::getListTraineesInGroup(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectAllTraineesInGroup(id); } QList InterfaceDataBaseLMS::getListGroups() { + QMutexLocker mtxLocker(&mtxAccess); return selectAllGroups(); } QList InterfaceDataBaseLMS::getListTrainees() { + QMutexLocker mtxLocker(&mtxAccess); return selectAllTrainees(); } Trainee InterfaceDataBaseLMS::getTrainee(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectTrainee(id); } Group InterfaceDataBaseLMS::getGroup(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectGroup(id); } int InterfaceDataBaseLMS::newGroup() { + QMutexLocker mtxLocker(&mtxAccess); return insertGroup(); } int InterfaceDataBaseLMS::delGroup(int id) { + QMutexLocker mtxLocker(&mtxAccess); return deleteGroup(id); } int InterfaceDataBaseLMS::editGroup(Group group) { + QMutexLocker mtxLocker(&mtxAccess); + //Проверка корректности имени QList listGroups = selectAllGroups(); for(Group exist_group : listGroups) @@ -342,57 +359,68 @@ int InterfaceDataBaseLMS::editGroup(Group group) int InterfaceDataBaseLMS::newTaskAMM(TaskAmmFim task, int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); return insertTaskAMM(task, id_trainee); } int InterfaceDataBaseLMS::delTaskAMM(int id) { + QMutexLocker mtxLocker(&mtxAccess); return deleteTaskAMM(id); } int InterfaceDataBaseLMS::editTaskAMM(TaskAmmFim task) { + QMutexLocker mtxLocker(&mtxAccess); return updateTaskAMM(task); } QList InterfaceDataBaseLMS::getListTasksAMMofTrainee(int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); return selectTasksAMMofTrainee(id_trainee); } QList InterfaceDataBaseLMS::getListTasksFIMofTrainee(int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); return selectTasksFIMofTrainee(id_trainee); } TaskAmmFim InterfaceDataBaseLMS::getTaskAMMbyID(int id_task) { + QMutexLocker mtxLocker(&mtxAccess); return selectTaskAMMbyID(id_task); } TaskAmmFim InterfaceDataBaseLMS::getTaskFIMbyID(int id_task) { + QMutexLocker mtxLocker(&mtxAccess); return selectTaskFIMbyID(id_task); } int InterfaceDataBaseLMS::newTaskFIM(TaskAmmFim task, int id_trainee) { + QMutexLocker mtxLocker(&mtxAccess); return insertTaskFIM(task, id_trainee); } int InterfaceDataBaseLMS::delTaskFIM(int id) { + QMutexLocker mtxLocker(&mtxAccess); deleteReportFIM(id); return deleteTaskFIM(id); } int InterfaceDataBaseLMS::editTaskFIM(TaskAmmFim task) { + QMutexLocker mtxLocker(&mtxAccess); return updateTaskFIM(task); } int InterfaceDataBaseLMS::replaceReportFIM(TaskAmmFim task) { + QMutexLocker mtxLocker(&mtxAccess); deleteReportFIM(task.getID()); if(int report_id = insertReportFIM(task)) @@ -406,26 +434,32 @@ int InterfaceDataBaseLMS::replaceReportFIM(TaskAmmFim task) int InterfaceDataBaseLMS::changeStatusTaskFIM(int id_task, QString status) { + QMutexLocker mtxLocker(&mtxAccess); return updateStatusTaskFIM(id_task, status); } int InterfaceDataBaseLMS::changeStatusTaskAMM(int id_task, QString status) { + QMutexLocker mtxLocker(&mtxAccess); return updateStatusTaskAMM(id_task, status); } int InterfaceDataBaseLMS::newTrainee(int id_group) { + QMutexLocker mtxLocker(&mtxAccess); return insertTrainee(id_group); } int InterfaceDataBaseLMS::delTrainee(int id) { + QMutexLocker mtxLocker(&mtxAccess); return deleteTrainee(id); } int InterfaceDataBaseLMS::editTrainee(Trainee trainee) { + QMutexLocker mtxLocker(&mtxAccess); + //Проверка корректности логина, имени, пароля QList listTrainees = selectAllTrainees(); for(Trainee exist_trainee : listTrainees) @@ -471,10 +505,12 @@ int InterfaceDataBaseLMS::editTrainee(Trainee trainee) bool InterfaceDataBaseLMS::isArchivedTrainee(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserArchived(DataBaseLMS::TypeUserDBTrainee, id); } bool InterfaceDataBaseLMS::isLoggedInTrainee(int id) { + QMutexLocker mtxLocker(&mtxAccess); return selectUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id); } diff --git a/DataBaseLMS/interfacedatabaselms.h b/DataBaseLMS/interfacedatabaselms.h index 9f0bf0e..765b521 100644 --- a/DataBaseLMS/interfacedatabaselms.h +++ b/DataBaseLMS/interfacedatabaselms.h @@ -105,6 +105,7 @@ public: private: QTranslator qtLanguageTranslator; QWidget* ownerWidget; + QMutex mtxAccess; }; #endif // INTERFACEDATABASELMS_H