feat: added autocreate tables
This commit is contained in:
@@ -37,6 +37,7 @@ target_link_libraries(
|
|||||||
${CMAKE_PROJECT_NAME}
|
${CMAKE_PROJECT_NAME}
|
||||||
Qt5::Core
|
Qt5::Core
|
||||||
Qt5::Gui
|
Qt5::Gui
|
||||||
|
Qt5::Sql
|
||||||
Qt5::Widgets
|
Qt5::Widgets
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
3
deps/tables.ddl
vendored
3
deps/tables.ddl
vendored
@@ -10,6 +10,3 @@ CREATE TABLE IF NOT EXISTS clients (
|
|||||||
yourCompany TEXT NOT NULL,
|
yourCompany TEXT NOT NULL,
|
||||||
comment TEXT
|
comment TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
COMMENT ON TABLE clients IS '';
|
|
||||||
COMMENT ON FIELD clients.id IS '';
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
// Self
|
// Self
|
||||||
|
#include <qfile.h>
|
||||||
|
|
||||||
#include "../def.h"
|
#include "../def.h"
|
||||||
|
|
||||||
const static int COLUMN_COUNT = 9;
|
const static int COLUMN_COUNT = 9;
|
||||||
@@ -14,13 +16,13 @@ const static int COLUMN_COUNT = 9;
|
|||||||
LicenseModel::LicenseModel(QObject* parent)
|
LicenseModel::LicenseModel(QObject* parent)
|
||||||
: QAbstractTableModel(parent)
|
: QAbstractTableModel(parent)
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||||
db.setDatabaseName(DB_PATH);
|
m_db.setDatabaseName(DB_PATH);
|
||||||
|
|
||||||
if (!db.open())
|
if (!m_db.open())
|
||||||
{
|
{
|
||||||
m_status = Status::DbExistError;
|
m_status = Status::DbExistError;
|
||||||
m_errors.append("Database connection failed: " + db.lastError().text());
|
m_errors.append("Database connection failed: " + m_db.lastError().text());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -100,7 +102,7 @@ QVariant LicenseModel::headerData(int section, Qt::Orientation orientation, int
|
|||||||
|
|
||||||
LicenseModel::Status LicenseModel::getStatus()
|
LicenseModel::Status LicenseModel::getStatus()
|
||||||
{
|
{
|
||||||
return Status::Ok;
|
return m_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LicenseModel::getStatusText()
|
QString LicenseModel::getStatusText()
|
||||||
@@ -119,3 +121,29 @@ bool LicenseModel::checkTables()
|
|||||||
if (!clienttableExist)
|
if (!clienttableExist)
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ public:
|
|||||||
Status getStatus();
|
Status getStatus();
|
||||||
QString getStatusText();
|
QString getStatusText();
|
||||||
|
|
||||||
|
bool prepareDatabase();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool checkTables();
|
bool checkTables();
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
#include "MainWidget.h"
|
#include "MainWidget.h"
|
||||||
|
|
||||||
// Qt
|
// Qt
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QDebug>
|
||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
|
#include <QTimer>
|
||||||
#include <QToolBar>
|
#include <QToolBar>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QContextMenuEvent>
|
#include <QContextMenuEvent>
|
||||||
@@ -22,6 +25,25 @@ MainWidget::MainWidget(QWidget *parent)
|
|||||||
setWindowTitle("LicenseManager");
|
setWindowTitle("LicenseManager");
|
||||||
resize({800, 600});
|
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
|
// Ui
|
||||||
{
|
{
|
||||||
m_tableView = new QTableView(this);
|
m_tableView = new QTableView(this);
|
||||||
|
|||||||
Reference in New Issue
Block a user