#include <deque>
-#include "wx/wx.h"
-#include <wx/dir.h>
-#include <wx/filename.h>
-
-
//#include <icons/close.xpm>
#include <creaWx.h>
//=============================================================
- //=============================================================
- // void SQLiteTreeHandler::Print() const
- // {
- /*
- std::cout << "-> '"<<GetName()<< "' - '"
- << GetFileName()<<"'"<<std::endl;
- ChildrenListType::const_iterator i;
- for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
- {
- (*i)->Print();
- }
- */
- // }
- //=============================================================
-
- //=====================================================================
- /*
- bool SQLiteTreeHandler::LocationIsValid()
- {
- // TO DO
- return true;
- }
- */
//=====================================================================
if (parent)
{
int nC = parent->RemoveChildrenFromList(node);
- if(nC>0)
+ if(nC>0 && parent->GetLevel()>0)
{
std::stringstream out;
out <<nC;
// std::cout << "DELETE OK"<<std::endl;
return true;
}
-
- //========================================================================
-
-
//=====================================================================
}
//=====================================================================
- // sqlite3_exec(db, zSQL, 0, 0, 0);
- // sqlite3_free(zSQL);
- // char* CHAIN = format_sql(QUER); \
-// sqlite3_free(CHAIN); \
-
+
//=====================================================================
#define QUERYDB(QUER,RES) \
try \
command = "create table LEVELS\n";
command += "( Name text )\n";
UPDATEDB(command);
-
+ int l;
// Iterate the Levels
- for (int l=0; l<GetTree().GetNumberOfLevels(); ++l)
+ for (l=0; l<GetTree().GetNumberOfLevels(); l++)
{
command = "INSERT INTO LEVELS (Name) VALUES ('";
command += GetTree().GetLevelDescriptor(l).GetName();
//=====================================================================
bool SQLiteTreeHandler::DBImportTreeDescription()
{
- GimmickMessage(1,"Importing tree description from database ..."
+ GimmickMessage(1,"Importing tree description for database ..."
<<std::endl);
// Test table 'LEVELS' existence
a!= GetTree().GetAttributeDescriptorList(0).end();
++a)
{
- /*
- std::string v;
- AttributeMapType::const_iterator i = attr.find(a->GetKey());
- if ( i != attr.end() )
- {
- v = i->second;
- }
- */
+
GetTree().UnsafeSetAttribute( a->GetKey(), "" );
}
}
//=====================================================================
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//========================================================================
///
std::string& SQLformat(std::string& str)
{
continue;
}
- // std::cout << "("<<i->first<<","<<i->second<<")"<<std::endl;
+
atts += "'" + i->first + "'";
values += "'" + SQLformat(i->second) + "'";
atts += ",";
}
//=====================================================================
-
//=====================================================================
int SQLiteTreeHandler::DBLoadChildren(tree::Node* node,
int numberoflevels)
query += GetTree().GetLevelDescriptor(level+1).GetName();
if (level>0)
{
- query += " WHERE PARENT_ID='" + node->UnsafeGetAttribute("ID")
+ query += " WHERE PARENT_ID='" + node->GetAttribute("ID")
+ "'";
}
CppSQLite3Query q;
{
n->UnsafeSetAttribute(q.fieldName(fld),q.getStringField(fld));
}
- /*
- // Index
- TypeId ti;
- ti.type = type;
- ti.id = n->GetFieldValue("ID");
- mTypeIdToNodeMap[ti] = n;
- */
+
// recurse
if ( numberoflevels != 1 )
{
// Insert in DB
DBInsert(child);
- /*
- std::string val;
- SQLAppendAttributesValues(child,val);
- std::string insert("INSERT INTO ");
- insert += GetTree().GetLevelDescriptor(child->GetLevel()).GetName();
- insert += " " + val + ";";
- UPDATEDB(insert);
- // Store DB id of newly created node;
- long lastrow = mDB->lastRowId();
- std::stringstream ri;
- ri << mDB->lastRowId();
- child->SetAttribute("ID",ri.str());
- */
// Down one level
parent = child;
-
- /*
- // Insert in TypeId map
- TypeId ti;
- ti.type = node->GetType();
- ti.id = node_id;
- mTypeIdToNodeMap[ti] = node;
- // std::cout << "== Insert TypeId ("<<ti.type<<","<<ti.id<<")"<<std::endl;
- //
- msw[2].Pause();
-
- if (node->GetType()==Node::Patient) summary.added_patients++;
- if (node->GetType()==Node::Study) summary.added_studies++;
- if (node->GetType()==Node::Series) summary.added_series++;
- if (node->GetType()==Node::Image) summary.added_images++;
- */
- }
+ }
}
//======================================================================
sql +="'";
// sql += " LIMIT 1";
UPDATEDB(sql);
+ return true;
}
//=====================================================================
sql += " = '";
sql += searchVal;
sql += "'";
+ std::cout<<sql<<std::endl;
UPDATEDB(sql);
}
//=====================================================================
<<"' 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;
+ }
+
+
}
//=====================================================================
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);
}
query += GetTree().GetLevelDescriptor(level).GetName();
if (level>0)
{
- query += " WHERE ID='" + n->UnsafeGetAttribute("ID")
+ query += " WHERE ID='" + n->GetAttribute("ID")
+ "'";
}
CppSQLite3Query q;
return nb;
}
- /*
- //=====================================================================
- bool SQLiteTreeHandler::DBInsert(Node* alien_node,
- UpdateSummary& summary)
- {
- // std::cout << "SQLiteTreeHandler::Insert('"<<alien_node->GetLabel()
- // <<"')"<<std::endl;
-
- // if (!ChildrenLoaded()) DBLoadChildren(this,Node::Database);
-
- // Find parent
- Node* parent;
- std::string parent_id;
- parent = DBGetOrCreateParent(alien_node,parent_id,summary);
-
- // Insert
- DBRecursiveGetOrCreateNode(alien_node,parent,parent_id,summary);
- return true;
- }
- //=====================================================================
-
-
- //=====================================================================
- Node* SQLiteTreeHandler::DBGetOrCreateParent(Node* alien_node,
- std::string& parent_id,
- UpdateSummary& summary)
- {
- // std::cout << "DBGetOrCreateParent '" << alien_node->GetLabel()<<"'"
- // << std::endl;
- // Load the patients if not already done
- DBLoadChildren(this,Node::Patient);
-
- parent_id = "";
- Node* parent = this;
-
- // The chain of ancestors
- std::deque<Node*> chain;
- Node* cur = alien_node->GetParent();
- for (int type=Node::Patient;
- type<alien_node->GetType();++type)
- {
- chain.push_front(cur);
- cur = cur->GetParent();
- }
-
- // create the nodes if do not exist
- std::deque<Node*>::iterator i;
- for (i=chain.begin();i!=chain.end();++i)
- {
- // std::cout << " cur = '"<<(*i)->GetLabel()<<"'"<<std::endl;
- // std::string cur_id = DBGetNodeId(*i,parent_id);
- // if (cur_id.size()==0)
- // {
- // Node does not exist : create it
- std::string cur_id;
- parent = DBGetOrCreateNode(*i,
- parent,
- parent_id,
- cur_id,
- summary
- );
- DBLoadChildren(parent,parent->GetType()+1);
-
- parent_id = cur_id;
- }
- return parent;
- }
- //=====================================================================
-
-
-
- //=====================================================================
- void SQLiteTreeHandler::DBRecursiveGetOrCreateNode(Node* alien_node,
- Node* parent,
- const std::string& parent_id,
- UpdateSummary& summary)
+ //=====================================================================
+ void SQLiteTreeHandler::GetTopLevelNodeId(const std::string& searchParam, const std::string& searchValue, std::string& parent_id)
{
- // std::cout << "SQLiteTreeHandler::RecursiveGetOrCreateNode('"
- // <<alien_node->GetLabel()
- // <<"','"<<parent<<"','"<<parent_id<<"')"<<std::endl;
- if (parent != 0)
- {
- // std::cout << " -- Parent = '"<<parent->GetLabel()<<"'"<<std::endl;
- }
- std::string new_id;
- Node* new_node = DBGetOrCreateNode(alien_node,
- parent,
- parent_id,
- new_id,
- summary);
- Node::ChildrenListType::iterator i;
- for (i = alien_node->GetChildrenList().begin();
- i != alien_node->GetChildrenList().end();
- i++)
- {
- DBRecursiveGetOrCreateNode((*i),new_node,new_id,summary);
- }
- }
- //=====================================================================
-
+ int level=GetTree().GetNumberOfLevels()-1;
+ std::string sp=searchParam.c_str();
+ std::string sv=searchValue.c_str();
- //=====================================================================
- Node* SQLiteTreeHandler::DBGetOrCreateNode(Node* alien_node,
- Node* internal_parent,
- std::string parent_id,
- std::string& node_id,
- UpdateSummary& summary)
- {
- // std::cout << "DBGetOrCreateNode('"<<alien_node->GetLabel()<<"','"
- // << internal_parent << "','"<< parent_id<<"')"<<std::endl;
- if (internal_parent != 0)
- {
- // std::cout << " -- Parent = '"<<internal_parent->GetLabel()<<"'"<<std::endl;
- }
- // Node Exists ? return it
- // First try among children of internal parent
- Node* node = GetChildrenLike(internal_parent,alien_node);
- if (node>0)
- {
- node_id = node->UnsafeGetFieldValue("ID");
- return node;
- }
- // Second : try in DB
-
- // Does not exist : Create new one
- node = new Node(alien_node->GetType(),this,internal_parent);
- node->SetChildrenLoaded(true);
- // Copy fields values from alien
- Node::FieldValueMapType::iterator i,j;
- for (i = node->GetFieldValueMap().begin();
- i != node->GetFieldValueMap().end();
- i++)
- {
- j = alien_node->GetFieldValueMap().find(i->first);
- if (j != alien_node->GetFieldValueMap().end() )
- {
- i->second = j->second;
- }
- }
-
- msw[2].Resume();
- if (node->GetType()!=Node::Patient)
- node->SetFieldValue("PARENT_ID",parent_id);
-
- // Insert in DB
- std::string val;
- BuildSQLFieldsValues(node,val);
- std::string insert("INSERT INTO ");
- insert += std::string(SQLiteTreeHandlerStructure::Table(node->GetType()))
- + " " + val + ";";
- // std::cout << "** SQL = '"<<insert<<"'"<<std::endl;
- UPDATEDB(insert);
- // std::cout << "** SQL OK"<<std::endl;
-
- // Store DB id of newly created node;
- long lastrow = mDB->lastRowId();
- std::stringstream ri;
- ri << mDB->lastRowId();
- node_id = ri.str();
- // std::cout << "LastRowId='"<<mDB->lastRowId()<<"' vs '"<<created_id<<"'"<<std::endl;
-
- node->SetFieldValue("ID",node_id);
- // Insert in TypeId map
- TypeId ti;
- ti.type = node->GetType();
- ti.id = node_id;
- mTypeIdToNodeMap[ti] = node;
- // std::cout << "== Insert TypeId ("<<ti.type<<","<<ti.id<<")"<<std::endl;
- //
- msw[2].Pause();
-
- if (node->GetType()==Node::Patient) summary.added_patients++;
- if (node->GetType()==Node::Study) summary.added_studies++;
- if (node->GetType()==Node::Series) summary.added_series++;
- if (node->GetType()==Node::Image) summary.added_images++;
-
- return node;
- }
- //=====================================================================
-
- //=====================================================================
- Node* SQLiteTreeHandler::GetChildrenLike(Node* parent,
- Node* alien_node)
- {
- Node::ChildrenListType::iterator i;
- for (i = parent->GetChildrenList().begin();
- i != parent->GetChildrenList().end();
- i++)
- {
- Node::Type type = alien_node->GetType();
- bool ok = true;
- for (int j=0;
- j<SQLiteTreeHandlerStructure::NbQueryFields(type);
- j++)
- {
- if (
- alien_node->GetFieldValue(SQLiteTreeHandlerStructure::
- QueryField(type,j).key ) !=
- (*i)->GetFieldValue(SQLiteTreeHandlerStructure::
- QueryField(type,j).key ) )
- {
- ok = false;
- break;
- }
- }
- if (ok)
+ while(level>1)
{
- return (*i);
+ 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();
}
- }
- return 0;
- }
- //=====================================================================
-
- //=====================================================================
- std::string SQLiteTreeHandler::DBGetNodeId(Node* node,
- const std::string& parent_id)
- {
- // std::cout << "SQLiteTreeHandler::DBGetNodeId('"<<node->GetLabel()
- // <<"','"<<parent_id<<"')"
- // <<std::endl;
- msw[2].Resume();
- int type = node->GetType();
-
- std::string table = SQLiteTreeHandlerStructure::Table(type);
- std::string where = "WHERE ";
-
- if (type!=Node::Patient)
- {
- where += "PARENT_ID='" + parent_id
- //node->GetFieldValue("PARENT_ID")
- + "' AND ";
- }
-
- for (int i=0;i<SQLiteTreeHandlerStructure::NbQueryFields(type);i++)
- {
- where += SQLiteTreeHandlerStructure::QueryField(type,i).key + "='"
- + node->GetFieldValue(SQLiteTreeHandlerStructure::QueryField(type,i).key) + "' ";
- if (i<SQLiteTreeHandlerStructure::NbQueryFields(type)-1)
- where += "AND ";
- }
-
- std::string query = "SELECT ID FROM " + table + " " + where + ";";
- // std::cout << "** SQL = '"<<query<<"'"<<std::endl;
- CppSQLite3Query q;
- QUERYDB(query,q);
-
- if (!q.eof())
- {
-
- // std::cout << " - Node exists " << std::endl;
- std::string id = q.getStringField(0);
- // std::cout << " id = '"<<id<<"'"<<std::endl;
- msw[2].Pause();
- return id;
- }
- msw[2].Pause();
- return "";
- }
- //=====================================================================
-
-
-
- //=====================================================================
- Node* SQLiteTreeHandler::GetNodeFromTypeId(Node::Type type,
- const std::string& id)
- {
- // std::cout << "GetNodeFromTypeId("<<type<<","<<id<<")"<<std::endl;
+ parent_id=sv;
- TypeId ti;
- ti.type = type;
- ti.id = id;
-
- TypeIdToNodeMapType::iterator i = mTypeIdToNodeMap.find(ti);
- if (i == mTypeIdToNodeMap.end())
- {
-
- std::cout << "Internal error : mTypeIdToNodeMap does not contain key"
- << std::endl;
- creaError("Internal error : mTypeIdToNodeMap does not contain key");
- // }
- }
-
- // std::cout << " ** Node = "<<i->second<<std::endl;
- return i->second;
- }
-
- //=====================================================================
-
- //=====================================================================
- bool SQLiteTreeHandler::Remove(Node* node)
- {
-
- //DBRecursiveRemoveNode(node);
-
- // std::cout << "DELETE"<<std::endl;
- if (node->GetParent())
- {
- node->GetParent()->RemoveChildrenFromList(node);
- }
- delete node;
- // std::cout << "DELETE OK"<<std::endl;
- return true;
}
-
-
- //========================================================================
//=====================================================================
- void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node)
- {
- // std::cout << "SQLiteTreeHandler::DBRecursiveRemoveNode('"
- // <<node->GetLabel()<<"')"<<std::endl;
-
- std::string query = "DELETE FROM ";
- query += SQLiteTreeHandlerStructure::Table(node->GetType());
- query += " WHERE ID='"+ node->GetFieldValue("ID") + "';";
-
- UPDATEDB(query);
-
- Node::ChildrenListType::iterator i;
- for (i = node->GetChildrenList().begin();
- i != node->GetChildrenList().end();
- i++)
- {
- DBRecursiveRemoveNode((*i));
- }
- }
-
- //=====================================================================
-
- //=====================================================================
- int SQLiteTreeHandler::DBQueryNumberOfChildren(Node* node)
- {
- std::string query = "SELECT COUNT (ID) FROM ";
- query += SQLiteTreeHandlerStructure::Table(node->GetType()+1);
- if (node->GetType() != Node::Database)
- {
- query += " WHERE PARENT_ID='"+ node->GetFieldValue("ID")+"'";
- }
- query += ";";
-
- // std::cout << "**SQL = "<< query << std::endl;
-
- CppSQLite3Query q;
- QUERYDB(query,q);
-
- // std::cout << "**RES = "<< q.getIntField(0) <<std::endl;
-
- return q.getIntField(0);
- }
- //=====================================================================
-
-
-
-*/
+ 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());
+ }
} // namespace creaImageIO