diff --git a/CMakeLists.txt b/CMakeLists.txt index f1e67b9..852875f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ target_link_libraries( ${CMAKE_PROJECT_NAME} Qt5::Core Qt5::Gui + Qt5::Sql Qt5::Widgets ) diff --git a/deps/tables.ddl b/deps/tables.ddl index c796cd8..e89bed5 100644 --- a/deps/tables.ddl +++ b/deps/tables.ddl @@ -10,6 +10,3 @@ CREATE TABLE IF NOT EXISTS clients ( yourCompany TEXT NOT NULL, comment TEXT ); - -COMMENT ON TABLE clients IS ''; -COMMENT ON FIELD clients.id IS ''; diff --git a/src/LicenseModel/LicenseModel.cpp b/src/LicenseModel/LicenseModel.cpp index 4cd7a29..4ade85a 100644 --- a/src/LicenseModel/LicenseModel.cpp +++ b/src/LicenseModel/LicenseModel.cpp @@ -7,6 +7,8 @@ #include // Self +#include + #include "../def.h" const static int COLUMN_COUNT = 9; @@ -14,13 +16,13 @@ const static int COLUMN_COUNT = 9; LicenseModel::LicenseModel(QObject* parent) : QAbstractTableModel(parent) { - QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); - db.setDatabaseName(DB_PATH); + m_db = QSqlDatabase::addDatabase("QSQLITE"); + m_db.setDatabaseName(DB_PATH); - if (!db.open()) + if (!m_db.open()) { m_status = Status::DbExistError; - m_errors.append("Database connection failed: " + db.lastError().text()); + m_errors.append("Database connection failed: " + m_db.lastError().text()); } else { @@ -100,7 +102,7 @@ QVariant LicenseModel::headerData(int section, Qt::Orientation orientation, int LicenseModel::Status LicenseModel::getStatus() { - return Status::Ok; + return m_status; } QString LicenseModel::getStatusText() @@ -119,3 +121,29 @@ bool LicenseModel::checkTables() if (!clienttableExist) return false; } + +bool LicenseModel::prepareDatabase() +{ + QFile tablesFile(QStringLiteral(":/deps/tables.ddl")); + if (!tablesFile.open(QIODevice::ReadOnly | QIODevice::Text)) + return false; + + m_status = Status::None; + m_errors.clear(); + + for (auto item : QString(tablesFile.readAll()).split(';')) + { + if (item.trimmed().isEmpty()) + continue; + QSqlQuery query(item.trimmed() + ";", m_db); + if (!query.exec()) + { + m_status = Status::DbStructError; + qDebug() << item.trimmed() + ";" << query.lastError().text(); + m_errors.append(query.lastError().text()); + return false; + } + } + + return true; +} diff --git a/src/LicenseModel/LicenseModel.h b/src/LicenseModel/LicenseModel.h index 3d5a3de..11df3b1 100644 --- a/src/LicenseModel/LicenseModel.h +++ b/src/LicenseModel/LicenseModel.h @@ -42,6 +42,8 @@ public: Status getStatus(); QString getStatusText(); + bool prepareDatabase(); + private: bool checkTables(); diff --git a/src/MainWidget/MainWidget.cpp b/src/MainWidget/MainWidget.cpp index da09aae..a719731 100644 --- a/src/MainWidget/MainWidget.cpp +++ b/src/MainWidget/MainWidget.cpp @@ -1,12 +1,15 @@ #include "MainWidget.h" // Qt +#include +#include #include #include #include #include #include #include +#include #include #include #include @@ -22,6 +25,25 @@ MainWidget::MainWidget(QWidget *parent) setWindowTitle("LicenseManager"); resize({800, 600}); + // Model init + { + if (m_licenseModel->getStatus() == LicenseModel::Status::DbStructError) + { + if (!m_licenseModel->prepareDatabase()) + { + QTimer::singleShot(0, [&]() { + QMessageBox messageBox; + messageBox.setIcon(QMessageBox::Critical); + messageBox.setWindowTitle(tr("Error")); + messageBox.setText(tr("Error with prepare database")); + messageBox.setDetailedText(m_licenseModel->getStatusText()); + messageBox.exec(); + QApplication::quit(); + }); + } + } + } + // Ui { m_tableView = new QTableView(this);