feat: add sql init
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user