feat: add sql init

This commit is contained in:
2026-01-17 15:25:18 +03:00
parent 89bd070613
commit 47348a1678
4 changed files with 50 additions and 8 deletions

View File

@@ -1,16 +1,44 @@
#include "LicenseModel.h" #include "LicenseModel.h"
// Qt
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
// Self
#include "../def.h"
const static int COLUMN_COUNT = 9; const static int COLUMN_COUNT = 9;
LicenseModel::LicenseModel(QObject* parent) LicenseModel::LicenseModel(QObject* parent)
: QAbstractTableModel(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() LicenseModel::~LicenseModel()
{ {
m_db.close();
} }
int LicenseModel::rowCount(const QModelIndex &parent) const int LicenseModel::rowCount(const QModelIndex &parent) const
@@ -77,5 +105,17 @@ LicenseModel::Status LicenseModel::getStatus()
QString LicenseModel::getStatusText() 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;
} }

View File

@@ -2,6 +2,7 @@
#define LICENSEMANAGER_LICENSEMODEL_H #define LICENSEMANAGER_LICENSEMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QtSql/QSqlDatabase>
class LicenseModel : public QAbstractTableModel class LicenseModel : public QAbstractTableModel
{ {
@@ -41,9 +42,14 @@ public:
Status getStatus(); Status getStatus();
QString getStatusText(); QString getStatusText();
private:
bool checkTables();
private: private:
QList<LicenseItem> m_data; QList<LicenseItem> m_data;
Status m_status = Status::None;
QStringList m_errors; QStringList m_errors;
QSqlDatabase m_db;
}; };

View File

@@ -4,18 +4,14 @@
#include <QHeaderView> #include <QHeaderView>
#include <QLabel> #include <QLabel>
#include <QMenu> #include <QMenu>
#include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QTableView> #include <QTableView>
#include <QToolBar> #include <QToolBar>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QContextMenuEvent> #include <QContextMenuEvent>
// Qt
#include <QAction>
// Self // Self
#include <QMessageBox>
#include "LicenseModel/LicenseModel.h" #include "LicenseModel/LicenseModel.h"
MainWidget::MainWidget(QWidget *parent) MainWidget::MainWidget(QWidget *parent)

View File

@@ -3,6 +3,6 @@
#include <QString> #include <QString>
const static QStringView DB_PATH = "./db.sqlite"; const static QString DB_PATH = "./db.sqlite";
#endif //LICENSEMANAGER_DEF_H #endif //LICENSEMANAGER_DEF_H