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;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define LICENSEMANAGER_LICENSEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QtSql/QSqlDatabase>
|
||||
|
||||
class LicenseModel : public QAbstractTableModel
|
||||
{
|
||||
@@ -41,9 +42,14 @@ public:
|
||||
Status getStatus();
|
||||
QString getStatusText();
|
||||
|
||||
private:
|
||||
bool checkTables();
|
||||
|
||||
private:
|
||||
QList<LicenseItem> m_data;
|
||||
Status m_status = Status::None;
|
||||
QStringList m_errors;
|
||||
QSqlDatabase m_db;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,18 +4,14 @@
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QTableView>
|
||||
#include <QToolBar>
|
||||
#include <QVBoxLayout>
|
||||
#include <QContextMenuEvent>
|
||||
|
||||
// Qt
|
||||
#include <QAction>
|
||||
|
||||
// Self
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "LicenseModel/LicenseModel.h"
|
||||
|
||||
MainWidget::MainWidget(QWidget *parent)
|
||||
|
||||
Reference in New Issue
Block a user