]> Creatis software - creaImageIO.git/blobdiff - src/creaImageIOSQLiteTreeHandler.cpp
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOSQLiteTreeHandler.cpp
index 627db08f7465afd4321302687a5d527d3affee6d..e51bfa91ad9760cb0e3e8ce1246088f1bf8ffc21 100644 (file)
@@ -1,7 +1,34 @@
+/*
+# ---------------------------------------------------------------------
+#
+# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
+#                        pour la Santé)
+# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
+# Previous Authors : Laurent Guigues, Jean-Pierre Roux
+# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
+#
+#  This software is governed by the CeCILL-B license under French law and 
+#  abiding by the rules of distribution of free software. You can  use, 
+#  modify and/ or redistribute the software under the terms of the CeCILL-B 
+#  license as circulated by CEA, CNRS and INRIA at the following URL 
+#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
+#  or in the file LICENSE.txt.
+#
+#  As a counterpart to the access to the source code and  rights to copy,
+#  modify and redistribute granted by the license, users are provided only
+#  with a limited warranty  and the software's author,  the holder of the
+#  economic rights,  and the successive licensors  have only  limited
+#  liability. 
+#
+#  The fact that you are presently reading this means that you have had
+#  knowledge of the CeCILL-B license and that you accept its terms.
+# ------------------------------------------------------------------------
+*/
+
+
 #include <creaImageIOSQLiteTreeHandler.h>
 #include <creaImageIOSystem.h>
 #include <creaImageIOGimmick.h>
-#include <creaImageIOTree.h>
 
 #include "CppSQLite3.h"
 
@@ -44,7 +71,8 @@ namespace creaImageIO
   //=====================================================================
   bool SQLiteTreeHandler::Open(bool writable)
   {
-    //    std::cout << "***> SQLiteTreeHandler::Open('"<<GetFileName()<<"')"<<std::endl;
+
+         //sqlite3_exec(mDB, "PRAGMA synchronous=OFF ", NULL, 0, &errMsg); 
     SetWritable(writable);
     return DBOpen();
   }
@@ -77,7 +105,8 @@ namespace creaImageIO
 
   //===================================================================== 
   int SQLiteTreeHandler::LoadChildren(tree::Node* parent, int maxlevel)
-  {
+  {     
+       //mDB->execDML("PRAGMA synchronous=ON ");
     if (parent==0) parent = GetTree().GetTree();
     return DBLoadChildren(parent,maxlevel);
   }
@@ -239,6 +268,7 @@ namespace creaImageIO
     try
       {
        mDB->open(GetFileName().c_str());
+       mDB->execDML("pragma synchronous=off;");
       }
     catch (CppSQLite3Exception& e)
       {
@@ -283,7 +313,7 @@ namespace creaImageIO
                     << e.errorMessage() <<std::endl);
        return false;
       }
-    
+    mDB->execDML("pragma synchronous=off;");
      
     // CREATING TABLES
     
@@ -321,6 +351,7 @@ namespace creaImageIO
            command += "\n)";
            UPDATEDB(command);
            
+               GetTree().CopyAttributeDescriptorList(l);
            
            // Add Attribute 'ID' to Description
            GetTree().GetDescriptor().Add
@@ -500,6 +531,7 @@ namespace creaImageIO
            return false;
  
          }
+                GetTree().CopyAttributeDescriptorList(level);
       }
 
        
@@ -538,7 +570,7 @@ namespace creaImageIO
                // quote must be doubled
                boost::algorithm::replace_all(i_str,"'","''");
                // Found strange strings which contained NULL char INSIDE string 
-               int i,size=i_str.size();
+               int i,size=(int)i_str.size();
                for (i=0;i<size;++i) 
                {
                        if (i_str[i]==0) 
@@ -659,7 +691,7 @@ GimmickDebugMessage(1,                                      "query : '"                         <<query                                         <<std::endl);
     CppSQLite3Query q;
     QUERYDB(query,q);
 
-       int p=0;
+    //int p=0;
     while (!q.eof())
       {
 
@@ -706,7 +738,7 @@ GimmickDebugMessage(1,                                      "query : '"                         <<query                                         <<std::endl);
     UPDATEDB(insert);
        
     // Store DB id of newly created node;
-    long lastrow = mDB->lastRowId();
+    //long lastrow = mDB->lastRowId();
     std::stringstream ri;
     ri << mDB->lastRowId();
     n->SetAttribute("ID",ri.str());
@@ -767,7 +799,7 @@ GimmickDebugMessage(1,                                      "query : '"                         <<query                                         <<std::endl);
     sql += " SET ";
     sql += key;
     sql += " = '";
-    sql += value;
+    sql += convert(value);
     sql += "' WHERE ID = '";
     sql += n->GetAttribute("ID");
        sql +="'";
@@ -804,21 +836,10 @@ GimmickDebugMessage(1,                                    "query : '"                         <<query                                         <<std::endl);
   {
        
     std::string query = "DELETE FROM ";
-   
-
-       query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
-       
+       query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
     query += " WHERE ID='"+ node->GetAttribute("ID") + "';";
-    UPDATEDB(query);
-       GimmickDebugMessage(2,
-                           " Deleting '"
-                               <<node->GetLabel()<<"' with ID '"
-                           <<node->GetAttribute("ID")
-                           <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName()
-                           <<std::endl);
-
-
+       UPDATEDB(query);
+       
        if(node->GetNumberOfChildren()!=0)
        {
                Node::ChildrenListType::iterator i;
@@ -954,36 +975,136 @@ GimmickDebugMessage(1,                                   "query : '"                         <<query                                         <<std::endl);
     return nb; 
   }
 
-  //===================================================================== 
-  void SQLiteTreeHandler::GetTopLevelNodeId(const std::string& searchParam, const std::string& searchValue, std::string& parent_id) 
+//===================================================================== 
+// get all attributes from database for a given file
+  void SQLiteTreeHandler::getAllAttributes(std::string i_filename, std::map<std::string, std::string> &i_results)
   {
          int level=GetTree().GetNumberOfLevels()-1;
-         std::string sp=searchParam.c_str();
-         std::string sv=searchValue.c_str();
 
-         while(level>1)
+         std::string search = i_filename;
+         std::string param = "FullFileName";
+         std::string name;
+
+         std::string id;
+         std::set<std::string> pid;
+         std::vector<AttributeDescriptor> attr;
+         std::vector<AttributeDescriptor>::iterator it_attr;
+         std::vector<std::string> values;
+         std::vector<std::string>::iterator it_val;
+        
+         while(level>=1)
          {
-               std::stringstream out;
-               std::stringstream results;
-               out<<"SELECT PARENT_ID FROM "<<GetTree().GetLevelDescriptor(level).GetName();
-               out<<" WHERE "<<sp<<"='"<<sv<<"'";      
-               CppSQLite3Query q;
-               QUERYDB(out.str(),q);
-               
-               
-               while (!q.eof())
+               attr = GetTree().GetAttributeDescriptorList(level,1);
+
+               name = GetTree().GetLevelDescriptor(level).GetName();
+               std::vector<std::string> values;
+
+//EED 2018-04-13                 GetUpLevelNodeId(level, param,search,id);  
+               if (level>1)
                {
+                       GetUpLevelNodeId(level, param,search,id);
+               }
+               GetAttributes(name, param,search,attr, values);
+               for(it_attr = attr.begin(), it_val = values.begin(); it_attr != attr.end(); it_attr++, it_val++)
+               {
+                       i_results[(*it_attr).GetKey()] = (*it_val).c_str();
+               } // for
+               search = id;
+               param = "ID";
+               level --;
+         } // while level
+  }
+
+
+//===================================================================== 
+// get selected attributes from database for a given file
+         void SQLiteTreeHandler::GetAttributes(std::string name, std::string i_id, std::string i_value, tree::LevelDescriptor::AttributeDescriptorListType i_attr, std::vector<std::string> &i_results)
+        {
+                //SELECT t1.ArtistName,CDs.Title FROM Artists t1, CDs WHERE t1.ArtistID=CDs.ArtistID    
+                 std::stringstream out;
+                 std::stringstream results;
+                 out<<"SELECT ";
+                 tree::LevelDescriptor::AttributeDescriptorListType::iterator it = i_attr.begin();
+                 std::string query ="";
+                 for(; it != i_attr.end(); it++)
+                 {
+                         query += (*it).GetKey();
+                         query +=" ,";
+                 }
+                 query = query.substr(0, query.size()-1);
+                 out << query;
+                 out << "FROM "<<name;
+                 out<<" WHERE "<<i_id <<"='"<<i_value<<"'";    
+                 CppSQLite3Query q;
+                 QUERYDB(out.str(),q);
+                 while (!q.eof())
+                 {
                        for (int fld = 0; fld < q.numFields(); fld++)
                        {
-                               results<<q.getStringField(fld);
+                               i_results.push_back(q.getStringField(fld));
                        }
                        q.nextRow();
+                 }
+       }
+
+   void SQLiteTreeHandler::GetUpLevelNodeId(int level, const std::string& searchParam, const std::string& searchValue, std::string& parent_id) 
+   {
+         std::string sp=searchParam.c_str();
+         std::string sv=searchValue.c_str();
+         std::stringstream out;
+         std::stringstream results;
+         out<<"SELECT PARENT_ID FROM "<<GetTree().GetLevelDescriptor(level).GetName();
+         out<<" WHERE "<<sp<<"='"<<sv<<"'";    
+         CppSQLite3Query q;
+         QUERYDB(out.str(),q);
+         while (!q.eof())
+         {
+               for (int fld = 0; fld < q.numFields(); fld++)
+               {
+                       results<<q.getStringField(fld);
                }
-               level=level-1;
-               sp="ID";
-               sv=results.str();
+               q.nextRow();
+         }
+         parent_id = results.str();
+  }
+
+
+  //===================================================================== 
+  void SQLiteTreeHandler::GetTopLevelNodeId(const std::string& searchParam, const std::string& searchValue, std::string& parent_id) 
+  {
+         int level=GetTree().GetNumberOfLevels()-1;
+         std::string sp=searchParam.c_str();
+         std::string sv=searchValue.c_str();
+
+         while(level>1)
+         {
+               GetUpLevelNodeId(level, sp, sv, parent_id);
+               level--;
+               sp = "ID";
+               sv = parent_id;
          }
-         parent_id=sv;
+///*
+//             std::stringstream out;
+//             std::stringstream results;
+//             out<<"SELECT PARENT_ID FROM "<<GetTree().GetLevelDescriptor(level).GetName();
+//             out<<" WHERE "<<sp<<"='"<<sv<<"'";      
+//             CppSQLite3Query q;
+//             QUERYDB(out.str(),q);
+//             
+//             
+//             while (!q.eof())
+//             {
+//                     for (int fld = 0; fld < q.numFields(); fld++)
+//                     {
+//                             results<<q.getStringField(fld);
+//                     }
+//                     q.nextRow();
+//             }*/
+//             level=level-1;
+//             sp="ID";
+//             sv=results.str();
+//       }
+//       parent_id=sv;
 
   }
 
@@ -1013,5 +1134,11 @@ GimmickDebugMessage(1,                                   "query : '"                         <<query                                         <<std::endl);
                out<<"commit transaction;";
         UPDATEDB(out.str());
        }
+  const std::string SQLiteTreeHandler::convert(const std::string &i_word)
+  {
+         std::string temp = i_word;
+         boost::algorithm::replace_all(temp,"'","''");
+         return temp.c_str();
+  }
 
 } // namespace creaImageIO