From 8d5c164d09c7bb52b0c2191f0e7a6e3c6cead4ad Mon Sep 17 00:00:00 2001 From: ispyisail Date: Tue, 20 Oct 2026 14:02:14 +1300 Subject: [PATCH] Move a folio's numbering counters into the project database QETZ-DB-FIRST stage 2b-4: the unit/ten/hundred-folio sequential counters Diagram tracks per numbering scheme (insertFolioSeqHash()) now write through to a new folio_counter table, keyed by (folio_uuid, owner, level, title, ord). The hashes stay the source for now, same write-through pattern as 2b-1's numbering rows. renameElementAutoNum()/AutoNumSchemeCommand::rename() carry the rows along on a scheme rename. Co-Authored-By: Claude Sonnet 5 --- sources/autoNum/autonumschemecommand.cpp | 1 + sources/dataBase/projectdatabase.cpp | 103 +++++++++++++++++++++++ sources/dataBase/projectdatabase.h | 12 +++ sources/diagram.cpp | 26 ++++-- sources/diagram.h | 3 +- sources/qetproject.cpp | 1 + 6 files changed, 138 insertions(+), 8 deletions(-) diff --git a/sources/autoNum/autonumschemecommand.cpp b/sources/autoNum/autonumschemecommand.cpp index 7eb96d3dd..956837c41 100644 --- a/sources/autoNum/autonumschemecommand.cpp +++ b/sources/autoNum/autonumschemecommand.cpp @@ -245,6 +245,7 @@ void AutoNumSchemeCommand::rename(const QString &from, const QString &to) } } } + m_project->dataBase()->renameFolioCounterScheme(QStringLiteral("conductor"), from, to); } else { m_project->removeFolioAutoNum(from); m_project->addFolioAutoNum(to, context); diff --git a/sources/dataBase/projectdatabase.cpp b/sources/dataBase/projectdatabase.cpp index 2cf59498c..dd848a9cf 100644 --- a/sources/dataBase/projectdatabase.cpp +++ b/sources/dataBase/projectdatabase.cpp @@ -2715,6 +2715,21 @@ bool projectDataBase::createDataBase() } } + //2b-4: a folio's own unit/ten/hundred-folio counters, one row per + //(folio, owner, level, scheme title, ord). owner is "element" or + //"conductor"; level keeps Diagram's own "unitfolio"/"tenfolio"/ + //"hundredfolio" strings. Not filled from the folio's XML here -- + //Diagram writes these rows itself, the same write-through pattern + //as the numbering tables above. + if (!query_.exec(QLatin1String( + "CREATE TABLE folio_counter (" + "folio_uuid VARCHAR(50) NOT NULL, owner VARCHAR(20) NOT NULL, " + "level VARCHAR(20) NOT NULL, title TEXT NOT NULL, ord INTEGER NOT NULL, " + "value TEXT, " + "PRIMARY KEY (folio_uuid, owner, level, title, ord))"))) { + qDebug() << " folio_counter table query : " << query_.lastError(); + } + //Create the terminal table. //Terminal::uuid() is the terminal-position id baked into the catalog //.elmt definition (e.g. "the top terminal") -- identical across every @@ -4427,6 +4442,94 @@ void projectDataBase::setNumberingFlag(const QString &name, bool value) setNumberingSetting(name, value ? QStringLiteral("true") : QStringLiteral("false")); } +/** + @brief projectDataBase::folioCounterValues + @return the folio counter values for @p folio_uuid / @p owner / + @p level / @p title, in @c ord order (Diagram::insertFolioSeqHash()'s + @c max list); empty if never written +*/ +QStringList projectDataBase::folioCounterValues(const QUuid &folio_uuid, const QString &owner, + const QString &level, const QString &title) const +{ + QSqlQuery query(m_data_base); + query.prepare(QStringLiteral( + "SELECT value FROM folio_counter WHERE folio_uuid = :folio_uuid " + "AND owner = :owner AND level = :level AND title = :title ORDER BY ord")); + query.bindValue(QStringLiteral(":folio_uuid"), folio_uuid.toString()); + query.bindValue(QStringLiteral(":owner"), owner); + query.bindValue(QStringLiteral(":level"), level); + query.bindValue(QStringLiteral(":title"), title); + QStringList values; + if (query.exec()) { + while (query.next()) { + values.append(query.value(0).toString()); + } + } else { + qDebug() << "projectDataBase::folioCounterValues error : " << query.lastError(); + } + return values; +} + +/** + @brief projectDataBase::setFolioCounterValues + The folio counter for @p folio_uuid / @p owner / @p level / @p title + is @p values from now on (one row per entry, in order) +*/ +void projectDataBase::setFolioCounterValues(const QUuid &folio_uuid, const QString &owner, + const QString &level, const QString &title, + const QStringList &values) +{ + QSqlQuery del(m_data_base); + del.prepare(QStringLiteral( + "DELETE FROM folio_counter WHERE folio_uuid = :folio_uuid " + "AND owner = :owner AND level = :level AND title = :title")); + del.bindValue(QStringLiteral(":folio_uuid"), folio_uuid.toString()); + del.bindValue(QStringLiteral(":owner"), owner); + del.bindValue(QStringLiteral(":level"), level); + del.bindValue(QStringLiteral(":title"), title); + if (!del.exec()) { + qDebug() << "projectDataBase::setFolioCounterValues delete error : " << del.lastError(); + return; + } + + QSqlQuery query(m_data_base); + query.prepare(QStringLiteral( + "INSERT INTO folio_counter (folio_uuid, owner, level, title, ord, value) " + "VALUES (:folio_uuid, :owner, :level, :title, :ord, :value)")); + for (int i = 0; i < values.size(); ++i) { + query.bindValue(QStringLiteral(":folio_uuid"), folio_uuid.toString()); + query.bindValue(QStringLiteral(":owner"), owner); + query.bindValue(QStringLiteral(":level"), level); + query.bindValue(QStringLiteral(":title"), title); + query.bindValue(QStringLiteral(":ord"), i); + query.bindValue(QStringLiteral(":value"), values.at(i)); + if (!query.exec()) { + qDebug() << "projectDataBase::setFolioCounterValues insert error : " << query.lastError(); + } + } +} + +/** + @brief projectDataBase::renameFolioCounterScheme + Every folio_counter row for @p owner under @p old_title moves to + @p new_title (renameNumberingScheme()'s counterpart for the rows a + folio keeps on its own) +*/ +void projectDataBase::renameFolioCounterScheme(const QString &owner, const QString &old_title, + const QString &new_title) +{ + QSqlQuery query(m_data_base); + query.prepare(QStringLiteral( + "UPDATE folio_counter SET title = :new_title " + "WHERE owner = :owner AND title = :old_title")); + query.bindValue(QStringLiteral(":new_title"), new_title); + query.bindValue(QStringLiteral(":owner"), owner); + query.bindValue(QStringLiteral(":old_title"), old_title); + if (!query.exec()) { + qDebug() << "projectDataBase::renameFolioCounterScheme error : " << query.lastError(); + } +} + /** @brief projectDataBase::savedLinks The links @p element saves, read from its link rows, in their order diff --git a/sources/dataBase/projectdatabase.h b/sources/dataBase/projectdatabase.h index 516800a1e..6bc7c9112 100644 --- a/sources/dataBase/projectdatabase.h +++ b/sources/dataBase/projectdatabase.h @@ -194,6 +194,18 @@ class projectDataBase : public QObject bool numberingFlag(const QString &name, bool default_value) const; void setNumberingSetting(const QString &name, const QString &value); void setNumberingFlag(const QString &name, bool value); + //2b-4: a folio's own numbering counters (unit/ten/hundred-folio + //sequentials), one row per (folio, owner, level, scheme, ord). + //owner is "element" or "conductor"; level is "unitfolio", + //"tenfolio" or "hundredfolio" (kept as Diagram's own strings, + //not renamed, so the two stay comparable in logs/greps). + QStringList folioCounterValues(const QUuid &folio_uuid, const QString &owner, + const QString &level, const QString &title) const; + void setFolioCounterValues(const QUuid &folio_uuid, const QString &owner, + const QString &level, const QString &title, + const QStringList &values); + void renameFolioCounterScheme(const QString &owner, const QString &old_title, + const QString &new_title); bool savedLinks(const Element *element, QList> *links); //The link rows are written (opening filled them): a symbol //built links from them (Element::initLink()) diff --git a/sources/diagram.cpp b/sources/diagram.cpp index de3e520ca..81ce1e21a 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -3026,7 +3026,8 @@ void Diagram::insertFolioSeqHash(QHash *hash, const QString& title, const QString& type, - NumerotationContext *nc) { + NumerotationContext *nc, + const QString &owner) { QStringList max; for (int i = 0; i < nc->size(); i++) { if (nc->itemAt(i).at(0) == type) { @@ -3038,6 +3039,11 @@ void Diagram::insertFolioSeqHash(QHashinsert(title,max); + //2b-4: the hash stays the source; the row follows it, same + //write-through pattern as the numbering tables (2b-1). + if (!owner.isEmpty() && project()) { + project()->dataBase()->setFolioCounterValues(uuid(), owner, type, title, max); + } } /** @@ -3160,7 +3166,8 @@ void Diagram::loadElmtFolioSeq() insertFolioSeqHash(&m_elmt_unitfolio_max, title, QStringLiteral("unitfolio"), - &nc); + &nc, + QStringLiteral("element")); project()->addElementAutoNum(title,nc); } } @@ -3185,7 +3192,8 @@ void Diagram::loadElmtFolioSeq() insertFolioSeqHash(&m_elmt_tenfolio_max, title, QStringLiteral("tenfolio"), - &nc); + &nc, + QStringLiteral("element")); project()->addElementAutoNum(title,nc); } } @@ -3210,7 +3218,8 @@ void Diagram::loadElmtFolioSeq() insertFolioSeqHash(&m_elmt_hundredfolio_max, title, QStringLiteral("hundredfolio"), - &nc); + &nc, + QStringLiteral("element")); project()->addElementAutoNum(title,nc); } } @@ -3247,7 +3256,8 @@ void Diagram::loadCndFolioSeq() insertFolioSeqHash(&m_cnd_unitfolio_max, title, QStringLiteral("unitfolio"), - &nc); + &nc, + QStringLiteral("conductor")); project()->addConductorAutoNum(title,nc); } } @@ -3270,7 +3280,8 @@ void Diagram::loadCndFolioSeq() insertFolioSeqHash(&m_cnd_tenfolio_max, title, QStringLiteral("tenfolio"), - &nc); + &nc, + QStringLiteral("conductor")); project()->addConductorAutoNum(title,nc); } } @@ -3293,7 +3304,8 @@ void Diagram::loadCndFolioSeq() insertFolioSeqHash(&m_cnd_hundredfolio_max, title, QStringLiteral("hundredfolio"), - &nc); + &nc, + QStringLiteral("conductor")); project()->addConductorAutoNum(title,nc); } } diff --git a/sources/diagram.h b/sources/diagram.h index 614e5a184..fde76b109 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -376,7 +376,8 @@ class Diagram : public QGraphicsScene void insertFolioSeqHash (QHash *hash, const QString& title, const QString& seq, - NumerotationContext *nc); + NumerotationContext *nc, + const QString &owner = QString()); void loadFolioSeqHash (QHash *hash, const QString& title, const QString& seq, NumerotationContext *nc); diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index c8dd66df0..1bfcbab64 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -1784,6 +1784,7 @@ bool QETProject::renameElementAutoNum(const QString &old_title, const QString &n } } } + m_data_base.renameFolioCounterScheme(QStringLiteral("element"), old_title, new_title); emit elementAutoNumRemoved(old_title); emit elementAutoNumAdded(new_title);