diff --git a/src/LicenseModel/LicenseModel.cpp b/src/LicenseModel/LicenseModel.cpp index a36e69a..4cd7a29 100644 --- a/src/LicenseModel/LicenseModel.cpp +++ b/src/LicenseModel/LicenseModel.cpp @@ -1,16 +1,44 @@ #include "LicenseModel.h" +// Qt +#include +#include +#include +#include + +// Self +#include "../def.h" + const static int COLUMN_COUNT = 9; LicenseModel::LicenseModel(QObject* parent) : QAbstractTableModel(parent) { + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(DB_PATH); + if (!db.open()) + { + m_status = Status::DbExistError; + m_errors.append("Database connection failed: " + db.lastError().text()); + } + else + { + if (!checkTables()) + { + m_status = Status::DbStructError; + m_errors.append("Database tables are not valid"); + } + else + { + m_status = Status::Ok; + } + } } LicenseModel::~LicenseModel() { - + m_db.close(); } int LicenseModel::rowCount(const QModelIndex &parent) const @@ -77,5 +105,17 @@ LicenseModel::Status LicenseModel::getStatus() QString LicenseModel::getStatusText() { - return m_errors.join('n'); + return m_errors.join('\n'); +} + +bool LicenseModel::checkTables() +{ + bool clienttableExist = false; + for (const auto &table : m_db.tables()) + { + if (table == "clients") + return true; + } + if (!clienttableExist) + return false; } diff --git a/src/LicenseModel/LicenseModel.h b/src/LicenseModel/LicenseModel.h index 3bb7fd4..3d5a3de 100644 --- a/src/LicenseModel/LicenseModel.h +++ b/src/LicenseModel/LicenseModel.h @@ -2,6 +2,7 @@ #define LICENSEMANAGER_LICENSEMODEL_H #include +#include class LicenseModel : public QAbstractTableModel { @@ -41,9 +42,14 @@ public: Status getStatus(); QString getStatusText(); +private: + bool checkTables(); + private: QList m_data; + Status m_status = Status::None; QStringList m_errors; + QSqlDatabase m_db; }; diff --git a/src/MainWidget/MainWidget.cpp b/src/MainWidget/MainWidget.cpp index e4fafeb..da09aae 100644 --- a/src/MainWidget/MainWidget.cpp +++ b/src/MainWidget/MainWidget.cpp @@ -4,18 +4,14 @@ #include #include #include +#include #include #include #include #include #include -// Qt -#include - // Self -#include - #include "LicenseModel/LicenseModel.h" MainWidget::MainWidget(QWidget *parent) diff --git a/src/def.h b/src/def.h index e542b6b..366de7a 100644 --- a/src/def.h +++ b/src/def.h @@ -3,6 +3,6 @@ #include -const static QStringView DB_PATH = "./db.sqlite"; +const static QString DB_PATH = "./db.sqlite"; #endif //LICENSEMANAGER_DEF_H \ No newline at end of file