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"
// Qt
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
// 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;
}