+/*
+# ---------------------------------------------------------------------
+#
+# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
+# pour la Santé)
+# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
+# Previous Authors : Laurent Guigues, Jean-Pierre Roux
+# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
+#
+# This software is governed by the CeCILL-B license under French law and
+# abiding by the rules of distribution of free software. You can use,
+# modify and/ or redistribute the software under the terms of the CeCILL-B
+# license as circulated by CEA, CNRS and INRIA at the following URL
+# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+# or in the file LICENSE.txt.
+#
+# As a counterpart to the access to the source code and rights to copy,
+# modify and redistribute granted by the license, users are provided only
+# with a limited warranty and the software's author, the holder of the
+# economic rights, and the successive licensors have only limited
+# liability.
+#
+# The fact that you are presently reading this means that you have had
+# knowledge of the CeCILL-B license and that you accept its terms.
+# ------------------------------------------------------------------------
+*/
+
+
#include <creaImageIOSQLiteTreeHandler.h>
#include <creaImageIOSystem.h>
#include <creaImageIOGimmick.h>
-#include <creaImageIOTree.h>
#include "CppSQLite3.h"
//=====================================================================
bool SQLiteTreeHandler::Open(bool writable)
{
- // std::cout << "***> SQLiteTreeHandler::Open('"<<GetFileName()<<"')"<<std::endl;
+
+ //sqlite3_exec(mDB, "PRAGMA synchronous=OFF ", NULL, 0, &errMsg);
SetWritable(writable);
return DBOpen();
}
//=====================================================================
int SQLiteTreeHandler::LoadChildren(tree::Node* parent, int maxlevel)
- {
+ {
+ //mDB->execDML("PRAGMA synchronous=ON ");
if (parent==0) parent = GetTree().GetTree();
return DBLoadChildren(parent,maxlevel);
}
try
{
mDB->open(GetFileName().c_str());
+ mDB->execDML("pragma synchronous=off;");
}
catch (CppSQLite3Exception& e)
{
<< e.errorMessage() <<std::endl);
return false;
}
-
+ mDB->execDML("pragma synchronous=off;");
// CREATING TABLES
command += "\n)";
UPDATEDB(command);
+ GetTree().CopyAttributeDescriptorList(l);
// Add Attribute 'ID' to Description
GetTree().GetDescriptor().Add
return false;
}
+ GetTree().CopyAttributeDescriptorList(level);
}
// quote must be doubled
boost::algorithm::replace_all(i_str,"'","''");
// Found strange strings which contained NULL char INSIDE string
- int i,size=i_str.size();
+ int i,size=(int)i_str.size();
for (i=0;i<size;++i)
{
if (i_str[i]==0)
CppSQLite3Query q;
QUERYDB(query,q);
- int p=0;
+ //int p=0;
while (!q.eof())
{
UPDATEDB(insert);
// Store DB id of newly created node;
- long lastrow = mDB->lastRowId();
+ //long lastrow = mDB->lastRowId();
std::stringstream ri;
ri << mDB->lastRowId();
n->SetAttribute("ID",ri.str());
sql += " SET ";
sql += key;
sql += " = '";
- sql += value;
+ sql += convert(value);
sql += "' WHERE ID = '";
sql += n->GetAttribute("ID");
sql +="'";
{
std::string query = "DELETE FROM ";
-
-
- query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
-
+ query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
query += " WHERE ID='"+ node->GetAttribute("ID") + "';";
-
- UPDATEDB(query);
- GimmickDebugMessage(2,
- " Deleting '"
- <<node->GetLabel()<<"' with ID '"
- <<node->GetAttribute("ID")
- <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName()
- <<std::endl);
-
-
+ UPDATEDB(query);
+
if(node->GetNumberOfChildren()!=0)
{
Node::ChildrenListType::iterator i;
return nb;
}
- //=====================================================================
- void SQLiteTreeHandler::GetTopLevelNodeId(const std::string& searchParam, const std::string& searchValue, std::string& parent_id)
+//=====================================================================
+// get all attributes from database for a given file
+ void SQLiteTreeHandler::getAllAttributes(std::string i_filename, std::map<std::string, std::string> &i_results)
{
int level=GetTree().GetNumberOfLevels()-1;
- std::string sp=searchParam.c_str();
- std::string sv=searchValue.c_str();
+
+ std::string search = i_filename;
+ std::string param = "FullFileName";
+ std::string name;
+ std::string id;
+ std::set<std::string> pid;
+ std::vector<AttributeDescriptor> attr;
+ std::vector<AttributeDescriptor>::iterator it_attr;
+ std::vector<std::string> values;
+ std::vector<std::string>::iterator it_val;
+
+
+
+
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())
- {
+ attr = GetTree().GetAttributeDescriptorList(level,1);
+
+ name = GetTree().GetLevelDescriptor(level).GetName();
+ std::vector<std::string> values;
+ GetUpLevelNodeId(level, param,search,id);
+ GetAttributes(name, param,search,attr, values);
+ for(it_attr = attr.begin(), it_val = values.begin(); it_attr != attr.end(); it_attr++, it_val++)
+ {
+ i_results[(*it_attr).GetKey()] = (*it_val).c_str();
+ }
+ search = id;
+ param = "ID";
+ level --;
+ }
+ }
+
+
+//=====================================================================
+// get selected attributes from database for a given file
+ void SQLiteTreeHandler::GetAttributes(std::string name, std::string i_id, std::string i_value, tree::LevelDescriptor::AttributeDescriptorListType i_attr, std::vector<std::string> &i_results)
+ {
+ //SELECT t1.ArtistName,CDs.Title FROM Artists t1, CDs WHERE t1.ArtistID=CDs.ArtistID
+ std::stringstream out;
+ std::stringstream results;
+ out<<"SELECT ";
+ tree::LevelDescriptor::AttributeDescriptorListType::iterator it = i_attr.begin();
+ std::string query ="";
+ for(; it != i_attr.end(); it++)
+ {
+ query += (*it).GetKey();
+ query +=" ,";
+ }
+ query = query.substr(0, query.size()-1);
+ out << query;
+ out << "FROM "<<name;
+ out<<" WHERE "<<i_id <<"='"<<i_value<<"'";
+ CppSQLite3Query q;
+ QUERYDB(out.str(),q);
+ while (!q.eof())
+ {
for (int fld = 0; fld < q.numFields(); fld++)
{
- results<<q.getStringField(fld);
+ i_results.push_back(q.getStringField(fld));
}
q.nextRow();
+ }
+ }
+
+ void SQLiteTreeHandler::GetUpLevelNodeId(int level, const std::string& searchParam, const std::string& searchValue, std::string& parent_id)
+ {
+ std::string sp=searchParam.c_str();
+ std::string sv=searchValue.c_str();
+ 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);
}
- level=level-1;
- sp="ID";
- sv=results.str();
+ q.nextRow();
}
- parent_id=sv;
+ parent_id = results.str();
+
+ }
+
+
+ //=====================================================================
+ 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)
+ {
+ GetUpLevelNodeId(level, sp, sv, parent_id);
+ level--;
+ sp = "ID";
+ sv = parent_id;
+ }
+///*
+// 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;
}
out<<"commit transaction;";
UPDATEDB(out.str());
}
+ const std::string SQLiteTreeHandler::convert(const std::string &i_word)
+ {
+ std::string temp = i_word;
+ boost::algorithm::replace_all(temp,"'","''");
+ return temp.c_str();
+ }
} // namespace creaImageIO