40 #include <boost/algorithm/string.hpp>
51 SQLiteTreeHandler::SQLiteTreeHandler(
const std::string& filename)
148 std::stringstream out;
172 const std::string& key,
173 const std::string& value)
182 const std::string& key,
183 const std::string& value,
184 const std::string& searchParam,
185 const std::string& searchVal)
192 std::string key, std::string value)
194 DBDelete(levelDescriptor,key,value);
220 return sqlite3_mprintf(
"%q",s.c_str());
226 #define QUERYDB(QUER,RES) \
229 GimmickMessage(2,"SQL query: '"<<QUER<<"'"<<std::endl); \
230 RES = mDB->execQuery(QUER.c_str()); \
232 catch (CppSQLite3Exception& e) \
234 GimmickError("SQLite query '"<<QUER<<"' : " \
235 << e.errorCode() << ":" \
236 << e.errorMessage() ); \
242 #define UPDATEDB(UP) \
245 GimmickMessage(2,"SQL update: '"<<UP<<"'"<<std::endl); \
246 mDB->execDML(UP.c_str()); \
248 catch (CppSQLite3Exception& e) \
250 GimmickError("SQLite update '"<<UP<<"' Error : " \
251 << e.errorCode() << ":" \
252 << e.errorMessage() ); \
261 <<
"' ... "<<std::endl);
287 <<
"' ... OK"<<std::endl);
296 <<
"' ... "<<std::endl);
301 <<
"file already exists");
322 command =
"create table LEVELS\n";
323 command +=
"( Name text )\n";
329 command =
"INSERT INTO LEVELS (Name) VALUES ('";
337 command =
"CREATE TABLE ";
339 command +=
"\n(\nID INTEGER PRIMARY KEY";
342 command +=
",\nPARENT_ID int not null";
347 command +=
",\nconstraint FK_PARENT foreign key (PARENT_ID) references ";
349 command +=
"(ID) on delete restrict on update restrict";
359 "Database Identifier",
369 "Database Parent Identifier",
379 command =
"CREATE TABLE ";
381 command +=
"_Attributes\n(\n";
382 command +=
"Key text,\n";
383 command +=
"Name text,\n";
384 command +=
"DicomGroup int,\n";
385 command +=
"DicomElement int,\n";
386 command +=
"Flags int\n";
391 LevelDescriptor::AttributeDescriptorListType::const_iterator i;
392 for (i =
GetTree().GetAttributeDescriptorList(l).begin();
397 std::stringstream insert;
398 insert <<
"INSERT INTO "
400 <<
"_Attributes (Key,Name,DicomGroup,DicomElement,Flags) "
402 << i->GetKey() <<
"','"
403 << i->GetName() <<
"',"
404 << i->GetGroup() <<
","
405 << i->GetElement() <<
","
406 << i->GetFlags() <<
");";
420 <<
"' ... OK"<<std::endl);
429 LevelDescriptor::AttributeDescriptorListType::const_iterator i;
430 for (i =
GetTree().GetAttributeDescriptorList(level).begin();
462 std::string query =
"SELECT * FROM LEVELS";
475 for (
int level = 0; level < nblevel; ++level )
478 table +=
"_Attributes";
487 std::string query =
"SELECT * FROM ";
493 <<
GetTree().GetLevelDescriptor(level).GetName()
497 bool ID_found =
false;
498 bool PARENT_ID_found =
false;
499 if (level==0) ID_found =
true;
500 if (level<=1) PARENT_ID_found =
true;
519 if ( key ==
"PARENT_ID" )
521 PARENT_ID_found =
true;
526 if ( ! (ID_found || PARENT_ID_found ) )
529 <<
"' does not contain mandatory attribute ID or PARENT_ID"
539 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
540 for (a =
GetTree().GetAttributeDescriptorList(0).begin();
550 query =
"SELECT * FROM ";
554 for (
int fld = 0; fld < q.
numFields(); fld++)
560 GimmickMessage(1,
"Importing tree description from database ... OK"
571 boost::algorithm::replace_all(i_str,
"'",
"''");
573 int i,size=(int)i_str.size();
578 i_str = i_str.substr(0,i);
592 std::string values=
"";
594 tree::Node::AttributeMapType::iterator i;
604 atts +=
"'" + i->first +
"'";
606 values +=
"'" + out +
"'";
609 GimmickMessage(4,
"'"<<i->first<<
"' = '"<<i->second<<
"'"<<std::endl);
611 atts[atts.size()-1]=
' ';
612 values[values.size()-1]=
' ';
614 str =
"("+atts+
") VALUES ("+values+
")";
630 tree::Node::ChildrenListType::const_iterator i;
635 if ( (*i)->Matches( attr ) )
665 tree::Node::ChildrenListType::iterator i;
682 std::string query =
"SELECT * FROM ";
687 query +=
" WHERE PARENT_ID='" + node->
GetAttribute(
"ID")
702 for (
int fld = 0; fld < q.
numFields(); fld++)
708 if ( numberoflevels != 1 )
734 std::string insert(
"INSERT INTO ");
736 insert +=
" " + val +
";";
742 std::stringstream ri;
756 for (
int level = parent->
GetLevel()+1;
768 std::stringstream out;
790 const std::string& key,
791 const std::string& value)
794 "' "<<key<<
"='"<<value<<
"'"<<std::endl);
797 std::string sql =
"UPDATE ";
803 sql +=
"' WHERE ID = '";
814 const std::string& key,
815 const std::string& value,
816 const std::string& searchParam,
817 const std::string& searchVal)
820 std::string sql =
"UPDATE ";
821 sql += levelDescriptor;
831 std::cout<<sql<<std::endl;
838 std::string query =
"DELETE FROM ";
840 query +=
" WHERE ID='"+ node->
GetAttribute(
"ID") +
"';";
845 Node::ChildrenListType::iterator i;
862 std::stringstream out;
863 std::stringstream result;
871 for (
int fld = 0; fld < q.
numFields(); fld++)
877 std::string res=result.str();
880 while(fin<res.size()-1)
882 fin=res.find(
'#',ini);
883 DBDelete(
GetTree().GetLevelDescriptor(level).GetName(),
"ID",res.substr(ini,fin-ini));
898 std::stringstream query;
899 query<<
"DELETE FROM "<<levelDescriptor<<
" WHERE "<<key<<
"='"<<value<<
"';";
908 std::string searchParam,
909 std::string searchVal,
913 std::stringstream out;
914 std::stringstream results;
915 out<<
"SELECT "<<key<<
" FROM "<<levelDescriptor;
918 out<<
" WHERE "<<searchParam<<
"='"<<searchVal<<
"'";
927 for (
int fld = 0; fld < q.
numFields(); fld++)
937 result=results.str();
947 if(level<
GetTree().GetNumberOfLevels()&& level>0)
949 std::string query =
"SELECT NumberOfChildren FROM ";
962 for (
int fld = 0; fld < q.
numFields(); fld++)
984 std::string search = i_filename;
985 std::string param =
"FullFileName";
989 std::set<std::string> pid;
990 std::vector<AttributeDescriptor> attr;
991 std::vector<AttributeDescriptor>::iterator it_attr;
992 std::vector<std::string> values;
993 std::vector<std::string>::iterator it_val;
1003 std::vector<std::string> values;
1006 for(it_attr = attr.begin(), it_val = values.begin(); it_attr != attr.end(); it_attr++, it_val++)
1008 i_results[(*it_attr).GetKey()] = (*it_val).c_str();
1022 std::stringstream out;
1023 std::stringstream results;
1025 tree::LevelDescriptor::AttributeDescriptorListType::iterator it = i_attr.begin();
1026 std::string query =
"";
1027 for(; it != i_attr.end(); it++)
1029 query += (*it).GetKey();
1032 query = query.substr(0, query.size()-1);
1034 out <<
"FROM "<<name;
1035 out<<
" WHERE "<<i_id <<
"='"<<i_value<<
"'";
1040 for (
int fld = 0; fld < q.
numFields(); fld++)
1050 std::string sp=searchParam.c_str();
1051 std::string sv=searchValue.c_str();
1052 std::stringstream out;
1053 std::stringstream results;
1055 out<<
" WHERE "<<sp<<
"='"<<sv<<
"'";
1060 for (
int fld = 0; fld < q.
numFields(); fld++)
1066 parent_id = results.str();
1075 std::string sp=searchParam.c_str();
1076 std::string sv=searchValue.c_str();
1112 const std::string i_attribute,
1113 const std::string i_operand,
1114 const std::string i_val)
1116 std::stringstream query;
1117 query<<
"DELETE FROM "<<i_table<<
" WHERE "<<i_attribute<<
" "<<i_operand<<
" '"<<i_val<<
"'";
1124 std::stringstream out;
1125 out<<
"begin transaction;";
1132 std::stringstream out;
1133 out<<
"commit transaction;";
1138 std::string temp = i_word;
1139 boost::algorithm::replace_all(temp,
"'",
"''");
1140 return temp.c_str();