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