1 #include <creaImageIOSQLiteTreeHandler.h>
2 #include <creaImageIOSystem.h>
4 #include "CppSQLite3.h"
8 //#include <creaImageIOSQLiteTreeHandlerStructure.h>
10 //#include <creaImageIOUtilities.h>
12 //#include <icons/database.xpm>
18 #include <wx/filename.h>
21 //#include <icons/close.xpm>
26 #include <boost/filesystem.hpp>
27 #include <boost/algorithm/string/replace.hpp>
34 //=============================================================
35 SQLiteTreeHandler::SQLiteTreeHandler(const std::string& filename)
38 mDB = new CppSQLite3DB;
39 GimmickMessage(1,"SQLite version : "
40 <<std::string(mDB->SQLiteVersion())<< std::endl);
42 //=============================================================
44 //=============================================================
45 SQLiteTreeHandler::~SQLiteTreeHandler()
49 //=============================================================
52 //=============================================================
53 // void SQLiteTreeHandler::Print() const
56 std::cout << "-> '"<<GetName()<< "' - '"
57 << GetFileName()<<"'"<<std::endl;
58 ChildrenListType::const_iterator i;
59 for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
65 //=============================================================
67 //=====================================================================
69 bool SQLiteTreeHandler::LocationIsValid()
75 //=====================================================================
78 //=====================================================================
79 bool SQLiteTreeHandler::Open(bool writable)
81 // std::cout << "***> SQLiteTreeHandler::Open('"<<GetFileName()<<"')"<<std::endl;
82 SetWritable(writable);
86 //=====================================================================
87 bool SQLiteTreeHandler::Create(bool writable)
89 // std::cout << "***> SQLiteTreeHandler::New('"<<GetFileName()<<"')"<<std::endl;
90 SetWritable(writable);
93 //=====================================================================
96 //=====================================================================
97 bool SQLiteTreeHandler::Close()
101 //=====================================================================
104 //=====================================================================
105 bool SQLiteTreeHandler::Destroy()
110 //=====================================================================
112 //=====================================================================
113 int SQLiteTreeHandler::LoadChildren(tree::Node* parent, int maxlevel)
115 if (parent==0) parent = GetTree().GetTree();
116 return DBLoadChildren(parent,maxlevel);
118 //=====================================================================
123 //=====================================================================
124 void SQLiteTreeHandler::UnLoad(tree::Node* n)
127 //=====================================================================
130 //=====================================================================
131 int SQLiteTreeHandler::AddBranch( const AttributeMapType& attr )
133 tree::Node* parent = DBGetParent(attr);
134 DBGraftToParent(parent,attr);
135 return (parent->GetLevel()+1);
137 //=====================================================================
140 //=====================================================================
141 bool SQLiteTreeHandler::Remove(tree::Node* node)
143 DBRecursiveRemoveNode(node);
145 // std::cout << "DELETE"<<std::endl;
146 if (node->GetParent())
148 node->GetParent()->RemoveChildrenFromList(node);
151 // std::cout << "DELETE OK"<<std::endl;
155 //========================================================================
159 //=====================================================================
161 //=====================================================================
162 /// Sets an attribute of a Node
163 bool SQLiteTreeHandler::SetAttribute(tree::Node* n,
164 const std::string& key,
165 const std::string& value)
167 if (n==0) n=GetTree().GetTree();
168 return DBSetAttribute(n,key,value);
170 //=====================================================================
184 //=====================================================================
185 // SQLite DB specific methods
186 //=====================================================================
191 //=====================================================================
192 char* format_sql(const std::string& s)
194 return sqlite3_mprintf("%q",s.c_str());
196 //=====================================================================
198 // sqlite3_exec(db, zSQL, 0, 0, 0);
199 // sqlite3_free(zSQL);
200 // char* CHAIN = format_sql(QUER); \
201 // sqlite3_free(CHAIN); \
203 //=====================================================================
204 #define QUERYDB(QUER,RES) \
207 GimmickMessage(2,"SQL query: '"<<QUER<<"'"<<std::endl); \
208 RES = mDB->execQuery(QUER.c_str()); \
210 catch (CppSQLite3Exception& e) \
212 GimmickError("SQLite query '"<<QUER<<"' : " \
213 << e.errorCode() << ":" \
214 << e.errorMessage() ); \
217 //=====================================================================
219 //=====================================================================
220 #define UPDATEDB(UP) \
223 GimmickMessage(2,"SQL update: '"<<UP<<"'"<<std::endl); \
224 mDB->execDML(UP.c_str()); \
226 catch (CppSQLite3Exception& e) \
228 GimmickError("SQLite update '"<<UP<<"' Error : " \
229 << e.errorCode() << ":" \
230 << e.errorMessage() ); \
232 //=====================================================================
235 //=====================================================================
236 bool SQLiteTreeHandler::DBOpen()
238 GimmickMessage(1,"Opening SQLite database '"<<GetFileName()
239 <<"' ... "<<std::endl);
241 if (!boost::filesystem::exists(GetFileName()))
248 mDB->open(GetFileName().c_str());
250 catch (CppSQLite3Exception& e)
252 GimmickError("Opening '"<<GetFileName()<<"' : "
253 << e.errorCode() << ":"
254 << e.errorMessage());
257 // IMPORT TREE DESCRIPTION (AND TEST DB VALIDITY)
258 if (!DBImportTreeDescription())
263 GimmickDebugMessage(1,"Opening SQLite database '"<<GetFileName()
264 <<"' ... OK"<<std::endl);
267 //=====================================================================
269 //=====================================================================
270 bool SQLiteTreeHandler::DBCreate()
272 GimmickMessage(1,"Creating SQLite database '"<<GetFileName()
273 <<"' ... "<<std::endl);
275 if (boost::filesystem::exists(GetFileName()))
277 GimmickError(GetFileName()<<"' : "
278 << "file already exists");
285 mDB->open(GetFileName().c_str());
287 catch (CppSQLite3Exception& e)
289 GimmickError(e.errorCode() << ":"
290 << e.errorMessage() <<std::endl);
298 // Create LEVELS table
299 command = "create table LEVELS\n";
300 command += "( Name text )\n";
303 // Iterate the Levels
304 for (int l=0; l<GetTree().GetNumberOfLevels(); ++l)
306 command = "INSERT INTO LEVELS (Name) VALUES ('";
307 command += GetTree().GetLevelDescriptor(l).GetName();
311 // Create table of level (for level>0, i.e. not Root)
314 command = "CREATE TABLE ";
315 command += GetTree().GetLevelDescriptor(l).GetName();
316 command += "\n(\nID INTEGER PRIMARY KEY";
319 command += ",\nPARENT_ID int not null";
321 SQLAppendAttributesDefinition(l,command);
324 command += ",\nconstraint FK_PARENT foreign key (PARENT_ID) references ";
325 command += GetTree().GetLevelDescriptor(l-1).GetName();
326 command += "(ID) on delete restrict on update restrict";
332 // Add Attribute 'ID' to Description
333 GetTree().GetLevelDescriptor(l).Add
334 (AttributeDescriptor( "ID",
335 "Database Identifier",
337 AttributeDescriptor::PRIVATE
342 // Add Attribute 'PARENT_ID' to Description
343 GetTree().GetLevelDescriptor(l).Add
344 (AttributeDescriptor( "PARENT_ID",
345 "Database Parent Identifier",
347 AttributeDescriptor::PRIVATE
353 // Create table *_ATTRIBUTES
355 command = "CREATE TABLE ";
356 command += GetTree().GetLevelDescriptor(l).GetName();
357 command += "_Attributes\n(\n";
358 command += "Key text,\n";
359 command += "Name text,\n";
360 command += "DicomGroup int,\n";
361 command += "DicomElement int,\n";
362 command += "Flags int\n";
367 // Fill the table *_ATTRIBUTES
368 LevelDescriptor::AttributeDescriptorListType::const_iterator i;
369 for (i = GetTree().GetAttributeDescriptorList(l).begin();
370 i != GetTree().GetAttributeDescriptorList(l).end();
374 std::stringstream insert;
375 insert << "INSERT INTO "
376 << GetTree().GetLevelDescriptor(l).GetName()
377 << "_Attributes (Key,Name,DicomGroup,DicomElement,Flags) "
379 << i->GetKey() << "','"
380 << i->GetName() << "',"
381 << i->GetGroup() << ","
382 << i->GetElement() << ","
383 << i->GetFlags() << ");";
385 UPDATEDB(insert.str());
390 // Initialize the root attributes
391 GetTree().InitializeAttributeMap();
392 // Insert the root in the level 0 table
393 DBInsert(GetTree().GetTree());
396 GetTree().SetChildrenLoaded(true);
397 GimmickMessage(1,"Creating SQLite database '"<<GetFileName()
398 <<"' ... OK"<<std::endl);
401 //=====================================================================
403 //=====================================================================
404 void SQLiteTreeHandler::SQLAppendAttributesDefinition(int level,
407 LevelDescriptor::AttributeDescriptorListType::const_iterator i;
408 for (i = GetTree().GetAttributeDescriptorList(level).begin();
409 i != GetTree().GetAttributeDescriptorList(level).end();
412 // if (i->second.flags==1) continue;
418 //=====================================================================
421 //=====================================================================
422 bool SQLiteTreeHandler::DBImportTreeDescription()
424 GimmickMessage(1,"Importing tree description from database ..."
427 // Test table 'LEVELS' existence
428 if ( ! mDB->tableExists("LEVELS") )
430 GimmickMessage(1,"!! ERROR : Table 'LEVELS' does not exist"
435 tree::Descriptor& desc = GetTree().GetDescriptor();
436 // clears the existing one
437 desc.GetLevelDescriptorList().clear();
440 std::string query = "SELECT * FROM LEVELS";
446 std::string name = q.getStringField(0);
447 GimmickMessage(2," * Importing level '"<<name<<"'"<<std::endl);
448 desc.GetLevelDescriptorList().push_back(LevelDescriptor(name));
453 for (int level = 0; level < nblevel; ++level )
455 std::string table = GetTree().GetLevelDescriptor(level).GetName();
456 table += "_Attributes";
457 // Test table 'LEVELS' existence
458 if ( ! mDB->tableExists(table.c_str()) )
460 GimmickMessage(1,"!! ERROR : Table '"<<table<<"' does not exist"
465 std::string query = "SELECT * FROM ";
470 GimmickMessage(2," * Level '"
471 <<GetTree().GetLevelDescriptor(level).GetName()
474 // Test that ID and PARENT_ID mandatory attributes exist
475 bool ID_found = false;
476 bool PARENT_ID_found = false;
477 if (level==0) ID_found = true;
478 if (level<=1) PARENT_ID_found = true;
482 std::string key(q.getStringField(0));
483 std::string name(q.getStringField(1));
484 GimmickMessage(2," - Importing attribute '"<<key<<"' '"<<name
486 desc.GetLevelDescriptor(level).Add
487 (AttributeDescriptor( key, // Key
489 q.getIntField(2), // Group
490 q.getIntField(3), // Element
491 q.getIntField(4) // Flags
497 if ( key == "PARENT_ID" )
499 PARENT_ID_found = true;
504 if ( ! (ID_found || PARENT_ID_found ) )
506 GimmickMessage(1,"!! ERROR : Table '"<<table
507 <<"' does not contain mandatory attribute ID or PARENT_ID"
515 // Create the attributes table for Root (i.e. Tree)
516 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
517 for (a = GetTree().GetAttributeDescriptorList(0).begin();
518 a!= GetTree().GetAttributeDescriptorList(0).end();
523 AttributeMapType::const_iterator i = attr.find(a->GetKey());
524 if ( i != attr.end() )
529 GetTree().UnsafeSetAttribute( a->GetKey(), "" );
532 // Reading Root attributes
534 query = "SELECT * FROM ";
535 query += GetTree().GetLevelDescriptor(0).GetName();
538 for (int fld = 0; fld < q.numFields(); fld++)
540 GetTree().UnsafeSetAttribute(q.fieldName(fld),
541 q.getStringField(fld));
544 GimmickMessage(1,"Importing tree description from database ... OK"
548 //=====================================================================
566 //========================================================================
568 std::string& SQLformat(std::string& str)
570 // quote must be doubled
571 boost::algorithm::replace_all(str,"'","''");
572 // Found strange strings which contained NULL char INSIDE string
573 int i,size=str.size();
578 str = str.substr(0,i);
585 //========================================================================
587 //=====================================================================
588 void SQLiteTreeHandler::SQLAppendAttributesValues(tree::Node* n,
591 GimmickMessage(4,"SQLAppendAttributesValues"<<std::endl);
593 std::string values="";
594 tree::Node::AttributeMapType::iterator i;
595 for (i = n->GetAttributeMap().begin();
596 i != n->GetAttributeMap().end();
603 // std::cout << "("<<i->first<<","<<i->second<<")"<<std::endl;
604 atts += "'" + i->first + "'";
605 values += "'" + SQLformat(i->second) + "'";
608 GimmickMessage(4,"'"<<i->first<<"' = '"<<i->second<<"'"<<std::endl);
610 atts[atts.size()-1]=' ';
611 values[values.size()-1]=' ';
613 str = "("+atts+") VALUES ("+values+")";
614 GimmickMessage(4,"Result = '"<<str<<"'"<<std::endl);
616 //=====================================================================
618 //=====================================================================
619 tree::Node* SQLiteTreeHandler::DBGetParent( const AttributeMapType& attr)
621 Node* parent = GetTree().GetTree();
626 // Load the children of the current parent
627 DBLoadChildren(parent);
628 // Iterate the children
629 tree::Node::ChildrenListType::const_iterator i;
630 for (i = parent->GetChildrenList().begin();
631 i!= parent->GetChildrenList().end();
634 if ( (*i)->Matches( attr ) )
645 //=====================================================================
648 //=====================================================================
649 int SQLiteTreeHandler::DBLoadChildren(tree::Node* node,
652 if (node->GetLevel()+1 >= node->GetTree()->GetNumberOfLevels() )
655 GimmickMessage(2,"Loading children of '"<<node->GetLabel()
659 // If children loaded we do not have to do it but we need to recurse
660 // in order to load the children's children if necessary, and so on...
661 if (node->GetChildrenLoaded())
663 // Iterate the children
664 tree::Node::ChildrenListType::iterator i;
665 for (i = node->GetChildrenList().begin();
666 i!= node->GetChildrenList().end();
669 nbloaded += DBLoadChildren(*i,numberoflevels-1);
674 /// If children not loaded : do it and recurse
677 int level = node->GetLevel();
678 std::string query = "SELECT * FROM ";
680 query += GetTree().GetLevelDescriptor(level+1).GetName();
683 query += " WHERE PARENT_ID='" + node->UnsafeGetAttribute("ID")
692 Node* n = new Node(node);
693 for (int fld = 0; fld < q.numFields(); fld++)
695 n->UnsafeSetAttribute(q.fieldName(fld),q.getStringField(fld));
701 ti.id = n->GetFieldValue("ID");
702 mTypeIdToNodeMap[ti] = n;
705 if ( numberoflevels != 1 )
708 nbloaded += DBLoadChildren(n, numberoflevels-1);
715 node->SetChildrenLoaded(true);
720 //=====================================================================
722 //======================================================================
723 void SQLiteTreeHandler::DBInsert(tree::Node* n)
725 GimmickMessage(2,"Inserting in DB '"<<n->GetLabel()
728 SQLAppendAttributesValues(n,val);
729 std::string insert("INSERT INTO ");
730 insert += GetTree().GetLevelDescriptor(n->GetLevel()).GetName();
731 insert += " " + val + ";";
735 // Store DB id of newly created node;
736 long lastrow = mDB->lastRowId();
737 std::stringstream ri;
738 ri << mDB->lastRowId();
739 n->SetAttribute("ID",ri.str());
741 //======================================================================
743 //======================================================================
744 /// Graft the branch defined by the attributes to the parent
745 void SQLiteTreeHandler::DBGraftToParent( tree::Node* parent,
746 const AttributeMapType& attr)
748 GimmickMessage(2,"Grafting to parent '"<<parent->GetLabel()
751 for (int level = parent->GetLevel()+1;
752 level < GetTree().GetNumberOfLevels();
756 tree::Node* child = new tree::Node(parent,attr);
758 // Set PARENT_ID if necessary
759 if ( parent->GetLevel()>0 )
760 child->SetAttribute("PARENT_ID",parent->GetAttribute("ID"));
766 SQLAppendAttributesValues(child,val);
767 std::string insert("INSERT INTO ");
768 insert += GetTree().GetLevelDescriptor(child->GetLevel()).GetName();
769 insert += " " + val + ";";
772 // Store DB id of newly created node;
773 long lastrow = mDB->lastRowId();
774 std::stringstream ri;
775 ri << mDB->lastRowId();
776 child->SetAttribute("ID",ri.str());
782 // Insert in TypeId map
784 ti.type = node->GetType();
786 mTypeIdToNodeMap[ti] = node;
787 // std::cout << "== Insert TypeId ("<<ti.type<<","<<ti.id<<")"<<std::endl;
791 if (node->GetType()==Node::Patient) summary.added_patients++;
792 if (node->GetType()==Node::Study) summary.added_studies++;
793 if (node->GetType()==Node::Series) summary.added_series++;
794 if (node->GetType()==Node::Image) summary.added_images++;
798 //======================================================================
801 //=====================================================================
802 /// Sets an attribute of a Node
803 bool SQLiteTreeHandler::DBSetAttribute(tree::Node* n,
804 const std::string& key,
805 const std::string& value)
807 GimmickMessage(3,"Setting Attribute of '"<<n->GetLabel()<<
808 "' "<<key<<"='"<<value<<"'"<<std::endl);
810 n->SetAttribute(key,value);
811 std::string sql = "UPDATE ";
812 sql += GetTree().GetLevelDescriptor(n->GetLevel()).GetName();
817 sql += "' WHERE ID=";
818 sql += n->GetAttribute("ID");
819 // sql += " LIMIT 1";
822 //=====================================================================
825 //=====================================================================
826 void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node)
829 std::string query = "DELETE FROM ";
831 query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
832 query += " WHERE ID='"+ node->GetAttribute("ID") + "';";
835 GimmickDebugMessage(2,
837 <<node->GetLabel()<<"' with ID '"
838 <<node->GetAttribute("ID")
839 <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName()
842 Node::ChildrenListType::iterator i;
843 for (i = node->GetChildrenList().begin();
844 i != node->GetChildrenList().end();
847 DBRecursiveRemoveNode((*i));
851 //=====================================================================
853 //=====================================================================
854 unsigned int SQLiteTreeHandler::GetNumberOfChildren(tree::Node* n)
858 int level = n->GetLevel();
859 if(GetTree().GetNumberOfLevels()!=(level+1))
861 std::string query = "SELECT * FROM ";
862 query += GetTree().GetLevelDescriptor(level+1).GetName();
865 query += " WHERE PARENT_ID='" + n->UnsafeGetAttribute("ID")
883 //=====================================================================
884 bool SQLiteTreeHandler::DBInsert(Node* alien_node,
885 UpdateSummary& summary)
887 // std::cout << "SQLiteTreeHandler::Insert('"<<alien_node->GetLabel()
888 // <<"')"<<std::endl;
890 // if (!ChildrenLoaded()) DBLoadChildren(this,Node::Database);
894 std::string parent_id;
895 parent = DBGetOrCreateParent(alien_node,parent_id,summary);
898 DBRecursiveGetOrCreateNode(alien_node,parent,parent_id,summary);
901 //=====================================================================
904 //=====================================================================
905 Node* SQLiteTreeHandler::DBGetOrCreateParent(Node* alien_node,
906 std::string& parent_id,
907 UpdateSummary& summary)
909 // std::cout << "DBGetOrCreateParent '" << alien_node->GetLabel()<<"'"
911 // Load the patients if not already done
912 DBLoadChildren(this,Node::Patient);
917 // The chain of ancestors
918 std::deque<Node*> chain;
919 Node* cur = alien_node->GetParent();
920 for (int type=Node::Patient;
921 type<alien_node->GetType();++type)
923 chain.push_front(cur);
924 cur = cur->GetParent();
927 // create the nodes if do not exist
928 std::deque<Node*>::iterator i;
929 for (i=chain.begin();i!=chain.end();++i)
931 // std::cout << " cur = '"<<(*i)->GetLabel()<<"'"<<std::endl;
932 // std::string cur_id = DBGetNodeId(*i,parent_id);
933 // if (cur_id.size()==0)
935 // Node does not exist : create it
937 parent = DBGetOrCreateNode(*i,
943 DBLoadChildren(parent,parent->GetType()+1);
949 //=====================================================================
953 //=====================================================================
954 void SQLiteTreeHandler::DBRecursiveGetOrCreateNode(Node* alien_node,
956 const std::string& parent_id,
957 UpdateSummary& summary)
959 // std::cout << "SQLiteTreeHandler::RecursiveGetOrCreateNode('"
960 // <<alien_node->GetLabel()
961 // <<"','"<<parent<<"','"<<parent_id<<"')"<<std::endl;
964 // std::cout << " -- Parent = '"<<parent->GetLabel()<<"'"<<std::endl;
967 Node* new_node = DBGetOrCreateNode(alien_node,
972 Node::ChildrenListType::iterator i;
973 for (i = alien_node->GetChildrenList().begin();
974 i != alien_node->GetChildrenList().end();
977 DBRecursiveGetOrCreateNode((*i),new_node,new_id,summary);
980 //=====================================================================
983 //=====================================================================
984 Node* SQLiteTreeHandler::DBGetOrCreateNode(Node* alien_node,
985 Node* internal_parent,
986 std::string parent_id,
987 std::string& node_id,
988 UpdateSummary& summary)
990 // std::cout << "DBGetOrCreateNode('"<<alien_node->GetLabel()<<"','"
991 // << internal_parent << "','"<< parent_id<<"')"<<std::endl;
992 if (internal_parent != 0)
994 // std::cout << " -- Parent = '"<<internal_parent->GetLabel()<<"'"<<std::endl;
996 // Node Exists ? return it
997 // First try among children of internal parent
998 Node* node = GetChildrenLike(internal_parent,alien_node);
1001 node_id = node->UnsafeGetFieldValue("ID");
1004 // Second : try in DB
1006 // Does not exist : Create new one
1007 node = new Node(alien_node->GetType(),this,internal_parent);
1008 node->SetChildrenLoaded(true);
1009 // Copy fields values from alien
1010 Node::FieldValueMapType::iterator i,j;
1011 for (i = node->GetFieldValueMap().begin();
1012 i != node->GetFieldValueMap().end();
1015 j = alien_node->GetFieldValueMap().find(i->first);
1016 if (j != alien_node->GetFieldValueMap().end() )
1018 i->second = j->second;
1023 if (node->GetType()!=Node::Patient)
1024 node->SetFieldValue("PARENT_ID",parent_id);
1028 BuildSQLFieldsValues(node,val);
1029 std::string insert("INSERT INTO ");
1030 insert += std::string(SQLiteTreeHandlerStructure::Table(node->GetType()))
1032 // std::cout << "** SQL = '"<<insert<<"'"<<std::endl;
1034 // std::cout << "** SQL OK"<<std::endl;
1036 // Store DB id of newly created node;
1037 long lastrow = mDB->lastRowId();
1038 std::stringstream ri;
1039 ri << mDB->lastRowId();
1041 // std::cout << "LastRowId='"<<mDB->lastRowId()<<"' vs '"<<created_id<<"'"<<std::endl;
1043 node->SetFieldValue("ID",node_id);
1044 // Insert in TypeId map
1046 ti.type = node->GetType();
1048 mTypeIdToNodeMap[ti] = node;
1049 // std::cout << "== Insert TypeId ("<<ti.type<<","<<ti.id<<")"<<std::endl;
1053 if (node->GetType()==Node::Patient) summary.added_patients++;
1054 if (node->GetType()==Node::Study) summary.added_studies++;
1055 if (node->GetType()==Node::Series) summary.added_series++;
1056 if (node->GetType()==Node::Image) summary.added_images++;
1060 //=====================================================================
1062 //=====================================================================
1063 Node* SQLiteTreeHandler::GetChildrenLike(Node* parent,
1066 Node::ChildrenListType::iterator i;
1067 for (i = parent->GetChildrenList().begin();
1068 i != parent->GetChildrenList().end();
1071 Node::Type type = alien_node->GetType();
1074 j<SQLiteTreeHandlerStructure::NbQueryFields(type);
1078 alien_node->GetFieldValue(SQLiteTreeHandlerStructure::
1079 QueryField(type,j).key ) !=
1080 (*i)->GetFieldValue(SQLiteTreeHandlerStructure::
1081 QueryField(type,j).key ) )
1094 //=====================================================================
1096 //=====================================================================
1097 std::string SQLiteTreeHandler::DBGetNodeId(Node* node,
1098 const std::string& parent_id)
1100 // std::cout << "SQLiteTreeHandler::DBGetNodeId('"<<node->GetLabel()
1101 // <<"','"<<parent_id<<"')"
1104 int type = node->GetType();
1106 std::string table = SQLiteTreeHandlerStructure::Table(type);
1107 std::string where = "WHERE ";
1109 if (type!=Node::Patient)
1111 where += "PARENT_ID='" + parent_id
1112 //node->GetFieldValue("PARENT_ID")
1116 for (int i=0;i<SQLiteTreeHandlerStructure::NbQueryFields(type);i++)
1118 where += SQLiteTreeHandlerStructure::QueryField(type,i).key + "='"
1119 + node->GetFieldValue(SQLiteTreeHandlerStructure::QueryField(type,i).key) + "' ";
1120 if (i<SQLiteTreeHandlerStructure::NbQueryFields(type)-1)
1124 std::string query = "SELECT ID FROM " + table + " " + where + ";";
1125 // std::cout << "** SQL = '"<<query<<"'"<<std::endl;
1132 // std::cout << " - Node exists " << std::endl;
1133 std::string id = q.getStringField(0);
1134 // std::cout << " id = '"<<id<<"'"<<std::endl;
1141 //=====================================================================
1145 //=====================================================================
1146 Node* SQLiteTreeHandler::GetNodeFromTypeId(Node::Type type,
1147 const std::string& id)
1149 // std::cout << "GetNodeFromTypeId("<<type<<","<<id<<")"<<std::endl;
1155 TypeIdToNodeMapType::iterator i = mTypeIdToNodeMap.find(ti);
1156 if (i == mTypeIdToNodeMap.end())
1159 std::cout << "Internal error : mTypeIdToNodeMap does not contain key"
1161 creaError("Internal error : mTypeIdToNodeMap does not contain key");
1165 // std::cout << " ** Node = "<<i->second<<std::endl;
1169 //=====================================================================
1171 //=====================================================================
1172 bool SQLiteTreeHandler::Remove(Node* node)
1175 //DBRecursiveRemoveNode(node);
1177 // std::cout << "DELETE"<<std::endl;
1178 if (node->GetParent())
1180 node->GetParent()->RemoveChildrenFromList(node);
1183 // std::cout << "DELETE OK"<<std::endl;
1188 //========================================================================
1190 //=====================================================================
1191 void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node)
1193 // std::cout << "SQLiteTreeHandler::DBRecursiveRemoveNode('"
1194 // <<node->GetLabel()<<"')"<<std::endl;
1196 std::string query = "DELETE FROM ";
1197 query += SQLiteTreeHandlerStructure::Table(node->GetType());
1198 query += " WHERE ID='"+ node->GetFieldValue("ID") + "';";
1202 Node::ChildrenListType::iterator i;
1203 for (i = node->GetChildrenList().begin();
1204 i != node->GetChildrenList().end();
1207 DBRecursiveRemoveNode((*i));
1211 //=====================================================================
1213 //=====================================================================
1214 int SQLiteTreeHandler::DBQueryNumberOfChildren(Node* node)
1216 std::string query = "SELECT COUNT (ID) FROM ";
1217 query += SQLiteTreeHandlerStructure::Table(node->GetType()+1);
1218 if (node->GetType() != Node::Database)
1220 query += " WHERE PARENT_ID='"+ node->GetFieldValue("ID")+"'";
1224 // std::cout << "**SQL = "<< query << std::endl;
1229 // std::cout << "**RES = "<< q.getIntField(0) <<std::endl;
1231 return q.getIntField(0);
1233 //=====================================================================
1241 } // namespace creaImageIO