]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOSQLiteTreeHandler.cpp
Added attribute selection functionality.
[creaImageIO.git] / src2 / creaImageIOSQLiteTreeHandler.cpp
index 8dcdd1795dcda95800944e9934bddd3f7c33200f..596a92a17492e963abea3cabcd061ccb084549e4 100644 (file)
@@ -148,10 +148,8 @@ namespace creaImageIO
    tree::Node* parent=node->GetParent();
     if (parent)
       {
-       parent->RemoveChildrenFromList(node);
-       int nC=GetNumberOfChildren(parent);
-       nC=nC-1;
-       if(nC>0)
+       int nC = parent->RemoveChildrenFromList(node);
+       if(nC>0 && parent->GetLevel()>0)
        {       
                std::stringstream out;
                out <<nC;
@@ -171,10 +169,6 @@ namespace creaImageIO
     //    std::cout << "DELETE OK"<<std::endl;
     return true;
    }
-
-    //========================================================================
-
-  
   
   //===================================================================== 
 
@@ -869,10 +863,11 @@ namespace creaImageIO
     sql += GetTree().GetLevelDescriptor(n->GetLevel()).GetName();
     sql += " SET ";
     sql += key;
-    sql += "='";
+    sql += " = '";
     sql += value;
-    sql += "' WHERE ID=";
+    sql += "' WHERE ID = '";
     sql += n->GetAttribute("ID");
+       sql +="'";
     //    sql += " LIMIT 1";
     UPDATEDB(sql);
   }
@@ -897,6 +892,7 @@ namespace creaImageIO
        sql += " = '";
     sql += searchVal;
        sql += "'";
+       std::cout<<sql<<std::endl;
     UPDATEDB(sql);
   }
    //=====================================================================
@@ -916,13 +912,56 @@ namespace creaImageIO
                            <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName()
                            <<std::endl);
 
-    Node::ChildrenListType::iterator i;
-    for (i  = node->GetChildrenList().begin();
-        i != node->GetChildrenList().end();
-        i++)
-      {
-       DBRecursiveRemoveNode((*i));
-      }
+
+       if(node->GetNumberOfChildren()!=0)
+       {
+               Node::ChildrenListType::iterator i;
+               for (i  = node->GetChildrenList().begin();
+               i != node->GetChildrenList().end();
+               i++)
+               {
+               DBRecursiveRemoveNode((*i));
+               }
+       }
+       else if(node->GetLevel()<GetTree().GetNumberOfLevels()-1)
+       {
+               DBRecursiveRemoveNode(node->GetLevel()+1,node->GetAttribute("ID"));
+    }
+  }
+
+  //=====================================================================
+  void SQLiteTreeHandler::DBRecursiveRemoveNode(int level, std::string parentId)
+  {
+    std::stringstream out;
+       std::stringstream result;
+       out<<"SELECT ID FROM "<<GetTree().GetLevelDescriptor(level).GetName()<<" WHERE PARENT_ID='"<<parentId<<"'";
+               
+       CppSQLite3Query q;
+       QUERYDB(out.str(),q);
+       
+       while (!q.eof())
+         {
+           for (int fld = 0; fld < q.numFields(); fld++)
+             {
+                         result<<q.getStringField(fld)<<"#";
+             }
+           q.nextRow();
+         }
+         std::string res=result.str();
+         size_t ini=0;
+         size_t fin=0;
+         while(fin<res.size()-1)
+         {
+          fin=res.find('#',ini);
+          DBDelete(GetTree().GetLevelDescriptor(level).GetName(),"ID",res.substr(ini,fin-ini));
+         if(level<GetTree().GetNumberOfLevels()-1)
+         {
+               DBRecursiveRemoveNode(level+1,res.substr(ini,fin-ini));
+         } 
+          ini=fin+1;
+         }
+         
+    
   }
 
   //=====================================================================
@@ -933,7 +972,7 @@ namespace creaImageIO
        query<<"DELETE FROM "<<levelDescriptor<<" WHERE "<<key<<"='"<<value<<"';";
  
     UPDATEDB(query.str());
-       GimmickMessage(1," Deleting: Query: "<<query.str()<<std::endl);
+       GimmickDebugMessage(2," Deleting: Query: "<<query.str()<<std::endl);
   }
 
 
@@ -977,7 +1016,7 @@ namespace creaImageIO
     // Query DB
     int nb=0;
     int level = n->GetLevel();
-    
+
     if(level<GetTree().GetNumberOfLevels()&& level>0)
       {
        std::string query = "SELECT NumberOfChildren FROM ";
@@ -1009,6 +1048,66 @@ namespace creaImageIO
     return nb; 
   }
 
+  //===================================================================== 
+  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)
+         {
+               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;
+
+  }
+
+  //=====================================================================
+  void SQLiteTreeHandler::RemoveEntries(const std::string i_table, 
+               const std::string i_attribute, 
+               const std::string i_operand, 
+               const std::string i_val)
+    {
+        std::stringstream query;
+               query<<"DELETE  FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
+        UPDATEDB(query.str());
+       }
+
+       //=====================================================================
+  void SQLiteTreeHandler::BeginTransaction()
+    {
+               std::stringstream out;
+               out<<"begin transaction;";
+        UPDATEDB(out.str());
+       }
+
+       //=====================================================================
+  void SQLiteTreeHandler::EndTransaction()
+    {
+       std::stringstream out;
+               out<<"commit transaction;";
+        UPDATEDB(out.str());
+       }
+
   /*
   //=====================================================================
   bool SQLiteTreeHandler::DBInsert(Node* alien_node,