From 62ae86702deb51e049f86df7627a0f67b7551722 Mon Sep 17 00:00:00 2001
From: Riccardo Maria Bianchi <riccardo.maria.bianchi@cern.ch>
Date: Thu, 13 Jun 2024 02:58:08 +0200
Subject: [PATCH] Write Parent-Child connections into the new DB schema

---
 .../GeoModelDBManager/GMDBManager.h           |  3 +--
 .../GeoModelDBManager/src/GMDBManager.cpp     | 13 ++++++-------
 .../GeoModelWrite/WriteGeoModel.h             |  4 +++-
 .../GeoModelWrite/src/WriteGeoModel.cpp       | 19 ++++++++++++-------
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/GeoModelIO/GeoModelDBManager/GeoModelDBManager/GMDBManager.h b/GeoModelIO/GeoModelDBManager/GeoModelDBManager/GMDBManager.h
index 420624224..404eab0f6 100644
--- a/GeoModelIO/GeoModelDBManager/GeoModelDBManager/GMDBManager.h
+++ b/GeoModelIO/GeoModelDBManager/GeoModelDBManager/GMDBManager.h
@@ -227,8 +227,7 @@ class GMDBManager {
         const DBRowEntry
             records);
 
-    bool addListOfChildrenPositions(
-        const std::vector<std::vector<std::string>> &records);
+    bool addListOfChildrenPositions(const DBRowsList &records);
 
     /**
      * @brief Save the list of 'published' GeoAlignableTransform nodes to the
diff --git a/GeoModelIO/GeoModelDBManager/src/GMDBManager.cpp b/GeoModelIO/GeoModelDBManager/src/GMDBManager.cpp
index 8baf752dd..499d25fb3 100644
--- a/GeoModelIO/GeoModelDBManager/src/GMDBManager.cpp
+++ b/GeoModelIO/GeoModelDBManager/src/GMDBManager.cpp
@@ -660,7 +660,7 @@ std::deque<double> GMDBManager::getTableFromTableName_DequeDouble(std::string ta
 //}
 
 bool GMDBManager::addListOfChildrenPositions(
-    const std::vector<std::vector<std::string>>& records) {
+    const DBRowsList& records) {
     if (records.size() > 0) {
         // NOTE: Choose the right function for your version of SQLite!!
         return addListOfRecordsToTable("ChildrenPositions",
@@ -1582,12 +1582,11 @@ bool GMDBManager::createTables() {
     tab.push_back("childCopyNumber");
     storeTableColumnNames(tab);
     queryStr = fmt::format(
-        "create table {0}({1} integer primary key, {2} integer, {3} "
-        "integer "
-        "not null REFERENCES GeoNodesTypes(id), {4} integer, {5} integer, "
-        "{6} "
-        "integer not null REFERENCES GeoNodesTypes(id), {7} integer not "
-        "null, "
+        "create table {0}({1} integer primary key, {2} integer, "
+        "{3} integer not null REFERENCES GeoNodesTypes(id), "
+        "{4} integer, {5} integer, "
+        "{6} integer not null REFERENCES GeoNodesTypes(id), "
+        "{7} integer not null, "
         "{8} integer not null)",
         tab[0], tab[1], tab[2], tab[3], tab[4], tab[5], tab[6], tab[7], tab[8]);
     rc = execQuery(queryStr);
diff --git a/GeoModelIO/GeoModelWrite/GeoModelWrite/WriteGeoModel.h b/GeoModelIO/GeoModelWrite/GeoModelWrite/WriteGeoModel.h
index 686cbd86b..28f09ab3e 100644
--- a/GeoModelIO/GeoModelWrite/GeoModelWrite/WriteGeoModel.h
+++ b/GeoModelIO/GeoModelWrite/GeoModelWrite/WriteGeoModel.h
@@ -405,7 +405,9 @@ class WriteGeoModel : public GeoNodeAction {
     // std::vector<std::string> m_rootVolume;
     std::pair<std::string, unsigned> m_rootVolume;
 
-    std::vector<std::vector<std::string>> m_childrenPositions;
+    // std::vector<std::vector<std::string>> m_childrenPositions;
+    DBRowsList m_childrenPositions;
+
     std::vector<std::vector<std::string>> m_publishedAlignableTransforms_String;
     std::vector<std::vector<std::string>> m_publishedFullPhysVols_String;
 
diff --git a/GeoModelIO/GeoModelWrite/src/WriteGeoModel.cpp b/GeoModelIO/GeoModelWrite/src/WriteGeoModel.cpp
index a3f0301c2..042a8539d 100644
--- a/GeoModelIO/GeoModelWrite/src/WriteGeoModel.cpp
+++ b/GeoModelIO/GeoModelWrite/src/WriteGeoModel.cpp
@@ -2587,20 +2587,25 @@ void WriteGeoModel::addChildPosition(const unsigned int& parentId,
                                      const unsigned int& parentCopyN,
                                      const unsigned int& childPos,
                                      const std::string& childType,
-                                     const unsigned int& childCopyN) {
-    std::vector<std::vector<std::string>>* container = &m_childrenPositions;
+                                     const unsigned int& childCopyN)
+{
+    // std::vector<std::vector<std::string>>* container = &m_childrenPositions;
+    DBRowsList* container = &m_childrenPositions;
+
     const unsigned int parentTableID = getIdFromNodeType(parentType);
     const unsigned int childTableID = getIdFromNodeType(childType);
 
-    std::vector<std::string> values;
+    // std::vector<std::string> values;
+    DBRowEntry values;
+
     //  values << parentId.toString() << parentTableID <<
     //  QString::number(parentCopyN) << QString::number(childPos) <<
     //  childTableID << childId.toString() << QString::number(childCopyN);
     values.insert(values.begin(),
-                  {std::to_string(parentId), std::to_string(parentTableID),
-                   std::to_string(parentCopyN), std::to_string(childPos),
-                   std::to_string(childTableID), std::to_string(childId),
-                   std::to_string(childCopyN)});  // INT
+                  {parentId, parentTableID,
+                   parentCopyN, childPos,
+                   childTableID, childId,
+                   childCopyN});
     addRecord(container, values);
     return;
 }
-- 
GitLab