- /*
- //=====================================================================
- 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)
- {
- // 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);
- }
- }
- //=====================================================================
-
-
- //=====================================================================
- 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)
- {
- return (*i);
- }
- }
- 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;
-
- 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);
- }
- //=====================================================================
-
-
-
-*/
-
-
-