Improve update of the project database

This commit is contained in:
Claveau Joshua
2020-11-01 21:27:40 +01:00
parent 41c7cde8ca
commit b514c39883
4 changed files with 94 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
/*
/*
Copyright 2006-2020 QElectroTech Team
This file is part of QElectroTech.
@@ -42,6 +42,28 @@ projectDataBase::projectDataBase(QETProject *project, QObject *parent) :
m_project(project)
{
createDataBase();
connect(m_project, &QETProject::diagramAdded, [this](QETProject *, Diagram *diagram) {
this->addDiagram(diagram);
});
connect(m_project, &QETProject::diagramRemoved, [this](QETProject *, Diagram *diagram) {
this->removeDiagram(diagram);
});
connect(m_project, &QETProject::projectDiagramsOrderChanged, [this]()
{
for (auto diagram : m_project->diagrams())
{
m_diagram_order_changed.bindValue(":pos", m_project->folioIndex(diagram)+1);
m_diagram_order_changed.bindValue(":uuid", diagram->uuid());
m_diagram_order_changed.exec();
m_diagram_info_order_changed.bindValue(":folio", diagram->border_and_titleblock.titleblockInformation().value("folio"));
m_diagram_info_order_changed.bindValue(":uuid", diagram->uuid());
m_diagram_info_order_changed.exec();
}
emit dataBaseUpdated();
});
}
/**
@@ -151,27 +173,12 @@ void projectDataBase::elementInfoChanged(Element *element)
void projectDataBase::addDiagram(Diagram *diagram)
{
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram));
m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)+1);
if(!m_insert_diagram_query.exec()) {
qDebug() << "projectDataBase::addDiagram insert error : " << m_insert_diagram_query.lastError();
}
m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid());
auto infos = diagram->border_and_titleblock.titleblockInformation();
for (auto key : QETApp::diagramInfoKeys())
{
if (key == "date")
{
m_insert_diagram_info_query.bindValue( ":date",
QLocale::system().toDate(infos.value("date").toString(),
QLocale::ShortFormat));
} else {
auto value = infos.value(key);
auto bind = key.prepend(":");
m_insert_diagram_info_query.bindValue(bind, value);
}
}
bindDiagramInfoValues(m_insert_diagram_info_query, diagram);
if (!m_insert_diagram_info_query.exec()) {
qDebug() << "projectDataBase::addDiagram insert info error : " << m_insert_diagram_info_query.lastError();
@@ -190,6 +197,21 @@ void projectDataBase::removeDiagram(Diagram *diagram)
}
}
void projectDataBase::diagramInfoChanged(Diagram *diagram)
{
bindDiagramInfoValues(m_update_diagram_info_query, diagram);
if (!m_update_diagram_info_query.exec()) {
qDebug() << "projectDataBase::diagramInfoChanged update error : " << m_update_diagram_info_query.lastError();
} else {
emit dataBaseUpdated();
}
}
void projectDataBase::diagramOrderChanged()
{
}
/**
@brief projectDataBase::createDataBase
Create the data base
@@ -342,7 +364,7 @@ void projectDataBase::populateDiagramTable()
for (auto diagram : m_project->diagrams())
{
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram));
m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)+1);
if(!m_insert_diagram_query.exec()) {
qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError();
}
@@ -417,21 +439,7 @@ void projectDataBase::populateDiagramInfoTable()
for (auto *diagram : m_project->diagrams())
{
m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid());
auto infos = diagram->border_and_titleblock.titleblockInformation();
for (auto key : QETApp::diagramInfoKeys())
{
if (key == "date") {
m_insert_diagram_info_query.bindValue( ":date",
QLocale::system().toDate(infos.value("date").toString(),
QLocale::ShortFormat));
} else {
auto value = infos.value(key);
auto bind = key.prepend(":");
m_insert_diagram_info_query.bindValue(bind, value);
}
}
bindDiagramInfoValues(m_insert_diagram_info_query, diagram);
if (!m_insert_diagram_info_query.exec()) {
qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << m_insert_diagram_info_query.lastError();
@@ -462,6 +470,22 @@ void projectDataBase::prepareQuery()
")");
m_insert_diagram_info_query.prepare(insert_diag_info);
//UPDATE DIAGRAM INFO
QString update_diagram_str("UPDATE diagram_info SET ");
for (auto str : QETApp::diagramInfoKeys()) {
update_diagram_str.append(str + " = :" + str + ", ");
}
update_diagram_str.remove(update_diagram_str.length()-2, 2); //Remove the last ", "
update_diagram_str.append(" WHERE diagram_uuid = :uuid");
m_update_diagram_info_query = QSqlQuery(m_data_base);
m_update_diagram_info_query.prepare(update_diagram_str);
//UPDATE DIAGRAM ORDER
m_diagram_order_changed = QSqlQuery(m_data_base);
m_diagram_order_changed.prepare("UPDATE diagram SET pos = :pos WHERE uuid = :uuid");
m_diagram_info_order_changed = QSqlQuery(m_data_base);
m_diagram_info_order_changed.prepare("UPDATE diagram_info SET folio = :folio WHERE diagram_uuid = :uuid");
//INSERT ELEMENT
QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)");
m_insert_elements_query = QSqlQuery(m_data_base);
@@ -518,6 +542,25 @@ QHash<QString, QString> projectDataBase::elementInfoToString(Element *elmt)
return hash;
}
void projectDataBase::bindDiagramInfoValues(QSqlQuery &query, Diagram *diagram)
{
query.bindValue(":uuid", diagram->uuid());
auto infos = diagram->border_and_titleblock.titleblockInformation();
for (auto key : QETApp::diagramInfoKeys())
{
if (key == "date") {
query.bindValue( ":date",
QLocale::system().toDate(infos.value("date").toString(),
QLocale::ShortFormat));
} else {
auto value = infos.value(key);
auto bind = key.prepend(":");
query.bindValue(bind, value);
}
}
}
#ifdef QET_EXPORT_PROJECT_DB
/**
@brief projectDataBase::sqliteHandle

View File

@@ -47,11 +47,15 @@ class projectDataBase : public QObject
void updateDB();
QETProject *project() const;
QSqlQuery newQuery(const QString &query = QString());
void addElement (Element *element);
void removeElement (Element *element);
void elementInfoChanged (Element *element);
void addDiagram (Diagram *diagram);
void removeDiagram (Diagram *diagram);
void diagramInfoChanged (Diagram *diagram);
void diagramOrderChanged();
signals:
void dataBaseUpdated();
@@ -67,6 +71,7 @@ class projectDataBase : public QObject
void prepareQuery();
static QHash<QString, QString> elementInfoToString(
Element *elmt);
void bindDiagramInfoValues(QSqlQuery &query, Diagram *diagram);
private:
QPointer<QETProject> m_project;
@@ -77,7 +82,10 @@ class projectDataBase : public QObject
m_update_element_query,
m_insert_diagram_query,
m_remove_diagram_query,
m_insert_diagram_info_query;
m_insert_diagram_info_query,
m_update_diagram_info_query,
m_diagram_order_changed,
m_diagram_info_order_changed;
#ifdef QET_EXPORT_PROJECT_DB
public:

View File

@@ -1650,6 +1650,8 @@ void Diagram::setTitleBlockTemplate(const QString &template_name)
if (template_name != current_name)
emit(usedTitleBlockTemplateChanged(template_name));
project()->dataBase()->diagramInfoChanged(this);
}
/**

View File

@@ -1221,7 +1221,6 @@ void QETProject::removeDiagram(Diagram *diagram)
if (m_diagrams_list.removeAll(diagram))
{
m_data_base.removeDiagram(diagram);
emit(diagramRemoved(this, diagram));
diagram->deleteLater();
}
@@ -1652,7 +1651,7 @@ void QETProject::addDiagram(Diagram *diagram, int pos)
} else {
m_diagrams_list.insert(pos, diagram);
}
m_data_base.addDiagram(diagram);
updateDiagramsFolioData();
}