changeset 164:6f83b74373d3 optimized-routes

/optimized-routes/studies/../instances-metadatas: include empty dico if no metadata such that all instances are listed
author Alain Mazy <alain@mazy.be>
date Tue, 14 Jul 2020 09:47:46 +0200
parents 2ccde9c7311b
children 215782cadfc2
files Framework/Plugins/IndexBackend.cpp Framework/Plugins/OptimizedRoutes.cpp
diffstat 2 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/IndexBackend.cpp	Fri Jul 10 13:26:47 2020 +0200
+++ b/Framework/Plugins/IndexBackend.cpp	Tue Jul 14 09:47:46 2020 +0200
@@ -1978,13 +1978,7 @@
                                                std::list<int32_t> metadataTypes)
   {
     {
-      std::string sql = "SELECT instances.publicid, metadata.type, metadata.value FROM resources instances "
-                        "LEFT JOIN metadata ON metadata.id = instances.internalid "
-                        "INNER JOIN resources series ON instances.parentid = series.internalid "
-                        "INNER JOIN resources studies ON series.parentid = studies.internalid "
-                        "  WHERE studies.publicId = ${id} ";
-
-
+      std::string metadataSqlFilter = "";
       if (metadataTypes.size() != 0)
       {
         std::list<std::string> metadataTypesStrings;
@@ -1994,9 +1988,17 @@
         }
 
         std::string metadataTypesFilter = boost::algorithm::join(metadataTypesStrings, ",");
-        sql = sql + "    AND metadata.type IN (" + metadataTypesFilter + ")";
+        metadataSqlFilter = "WHERE metadata.type IN (" + metadataTypesFilter + ")";
       }
 
+      std::string sql = "SELECT instances.publicid, metadata.type, metadata.value "
+                        "FROM resources instances "
+                        "  LEFT JOIN (select * from metadata " + metadataSqlFilter +
+                        "                                                            ) AS metadata ON metadata.id = instances.internalId "
+                        "INNER JOIN resources series ON instances.parentid = series.internalid "
+                        "INNER JOIN resources studies ON series.parentid = studies.internalid "
+                        "  WHERE studies.publicId = ${id} ";
+
       DatabaseManager::StandaloneStatement statement(manager_, sql);
 
       statement.SetReadOnly(true);
@@ -2019,14 +2021,19 @@
         while (!statement.IsDone())
         {
           std::string instanceId = ReadString(statement, 0);
-          int32_t type = ReadInteger32(statement, 1);
-          std::string value = ReadString(statement, 2);
 
           if (target.find(instanceId) == target.end())
           {
             target[instanceId] = std::map<std::int32_t, std::string>();
           }
-          target[instanceId][type] = value;
+
+          if (statement.GetResultField(1).GetType() != ValueType_Null)
+          {
+            int32_t type = ReadInteger32(statement, 1);
+            std::string value = ReadString(statement, 2);
+
+            target[instanceId][type] = value;
+          }
 
           statement.Next();
         }
--- a/Framework/Plugins/OptimizedRoutes.cpp	Fri Jul 10 13:26:47 2020 +0200
+++ b/Framework/Plugins/OptimizedRoutes.cpp	Tue Jul 14 09:47:46 2020 +0200
@@ -66,7 +66,7 @@
     Json::Value response(Json::objectValue);
     for (std::map<std::string, std::map<int32_t, std::string>>::const_iterator itInstance = result.begin(); itInstance != result.end(); itInstance++)
     {
-      Json::Value instanceMetadatas;
+      Json::Value instanceMetadatas(Json::objectValue);
       for (std::map<int32_t, std::string>::const_iterator itMetadata = itInstance->second.begin(); itMetadata != itInstance->second.end(); itMetadata++)
       {
         std::string id = boost::lexical_cast<std::string>(itMetadata->first);