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