creaImageIO_lib
creaImageIO::TimestampDatabaseHandler Class Reference

Concrete TreeHandler which manages a Tree stored in a sqlite database. More...

#include <creaImageIOTimestampDatabaseHandler.h>

Collaboration diagram for creaImageIO::TimestampDatabaseHandler:

Public Member Functions

 TimestampDatabaseHandler (const std::string &filename)
 Ctor with database file name. More...
 
virtual ~TimestampDatabaseHandler ()
 Dtor. More...
 
const std::string & GetFileName () const
 Returns the sqlite db file name. More...
 
bool Open ()
 Opens an existing 'source'. More...
 
bool Close ()
 Closes the 'source'. More...
 
bool Create ()
 Creates a new 'source'. More...
 
bool Destroy ()
 Destroys the 'source'. More...
 
std::string IsIndexed (const std::string &path, const std::string &refdb)
 Returns the id of the path if it's indexed, blank otherwise. More...
 
bool AddDirectory (const std::string &parent, const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Sets the current path's parent. More...
 
void AddFile (const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Adds a new file to the database without a parent. More...
 
void AddFile (const std::string &parentId, const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Adds a new file to the database with a parent. More...
 
void SetAttribute (const std::string &attName, const std::string &attValue, const std::string &searchParam, const std::string &searchValue)
 Sets the attribute to the value passed as parameter where the searchParameter is searchValue. More...
 
void RemoveNode (const std::string &searchAtt, const tree::Node *node, const std::string &refdb)
 Removes the given node. More...
 
void RemoveFile (const std::string &searchAtt, const std::string &searchVal, const std::string &refdb)
 Removes the filename with the given pathname. More...
 
void CleanPath (std::string &str) const
 Cleans the path name. More...
 
bool CheckTimestamp (const std::string pathId, const time_t lastModif, const std::string &refdb)
 Checks the timestamp in the database and compares it with the given one. More...
 
void RemoveEntries (const std::string i_table, const std::string i_attribute, const std::string i_operand, const std::string i_val)
 Removes the entries that match the given parameters. More...
 

Protected Member Functions

bool DBOpen ()
 Open the database. More...
 
bool DBCreate ()
 Creates a new database on disk and the tables. More...
 
void DBRemove (const std::string &searchAtt, const std::string &searchVal, const std::string &refdb)
 

Private Attributes

CppSQLite3DBmDB
 The DB. More...
 
std::string mFileName
 The physical location associated to the DicomDatabase (directory, db file...) More...
 

Detailed Description

Concrete TreeHandler which manages a Tree stored in a sqlite database.

Definition at line 41 of file creaImageIOTimestampDatabaseHandler.h.

Constructor & Destructor Documentation

creaImageIO::TimestampDatabaseHandler::TimestampDatabaseHandler ( const std::string &  filename)

Ctor with database file name.

Definition at line 41 of file creaImageIOTimestampDatabaseHandler.cpp.

References GimmickMessage, mDB, and CppSQLite3DB::SQLiteVersion().

42  : mFileName(filename)
43  {
44  mDB = new CppSQLite3DB;
45  GimmickMessage(1,"SQLite version : "
46  <<std::string(mDB->SQLiteVersion())<< std::endl);
47  }

Here is the call graph for this function:

creaImageIO::TimestampDatabaseHandler::~TimestampDatabaseHandler ( )
virtual

Dtor.

Definition at line 51 of file creaImageIOTimestampDatabaseHandler.cpp.

References mDB.

52  {
53  delete mDB;
54  }

Member Function Documentation

bool creaImageIO::TimestampDatabaseHandler::AddDirectory ( const std::string &  parent,
const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Sets the current path's parent.

Definition at line 218 of file creaImageIOTimestampDatabaseHandler.cpp.

References AddFile(), CheckTimestamp(), CleanPath(), IsIndexed(), and SetAttribute().

223  {
224  bool valid=false;
225  std::string par=parent.c_str();
226  std::string pat=path.c_str();
227  CleanPath(par);
228  CleanPath(pat);
229 
230  std::string pathId=IsIndexed(pat,refdb);
231  //Case: It is a root parent
232  if(parent.compare("")==0)
233  {
234  if(pathId.compare("")==0)
235  {
236  AddFile(pat,lastModif,lastRead,refdb);
237  valid=true;
238  }
239  else
240  {
241  valid=CheckTimestamp(pathId, lastModif, refdb);
242  }
243  }
244  else
245  {
246  std::string parentId=IsIndexed(par,refdb);
247  //Case: Parent is not in database
248  if(parentId.compare("")==0)
249  {
250  AddFile(par,lastModif,lastRead,refdb);
251  parentId=IsIndexed(par,refdb);
252  }
253 
254  //Case path is not in database
255  if(pathId.compare("")==0)
256  {
257  AddFile(parentId,pat,lastModif,lastRead,refdb);
258  valid=true;
259  }
260  //Parent and path are in the database
261  else
262  {
263  SetAttribute("PARENT_ID",parentId,"ID", pathId);
264  valid=CheckTimestamp(pathId, lastModif, refdb);
265  }
266  }
267  return valid;
268 
269  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::AddFile ( const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Adds a new file to the database without a parent.

Definition at line 273 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

Referenced by AddDirectory().

274  {
275  std::stringstream out;
276  out<<"INSERT INTO FILES (PARENT_ID,PATH,LastModified,LastRead,ReferencedDB) VALUES(0,'"<<path<<"',";
277  out<<lastModif<<","<<lastRead<<",'"<<refdb<<"');";
278  UPDATETIMESTAMPDB(out.str());
279 
280  }

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::AddFile ( const std::string &  parentId,
const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Adds a new file to the database with a parent.

Definition at line 284 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

289  {
290  std::stringstream out;
291  out<<"INSERT INTO FILES (PARENT_ID,PATH,LastModified,LastRead,ReferencedDB) VALUES("<<parentId<<",'"<<path<<"',";
292  out<<lastModif<<","<<lastRead<<",'"<<refdb<<"');";
293  UPDATETIMESTAMPDB(out.str());
294  }
bool creaImageIO::TimestampDatabaseHandler::CheckTimestamp ( const std::string  pathId,
const time_t  lastModif,
const std::string &  refdb 
)

Checks the timestamp in the database and compares it with the given one.

Definition at line 419 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Query::eof(), CppSQLite3Query::getFloatField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), QUERYTIMESTAMPDB, and SetAttribute().

Referenced by AddDirectory().

420  {
421  std::string sel="SELECT LastModified FROM FILES WHERE ID='"+pathId+"' AND REFERENCEDDB='"+refdb+"';";
422  CppSQLite3Query q;
423  QUERYTIMESTAMPDB(sel,q);
424  double timestamp;
425 
426  while (!q.eof())
427  {
428  for (int fld = 0; fld < q.numFields(); fld++)
429  {
430  timestamp=q.getFloatField(fld);
431  }
432  q.nextRow();
433  }
434 
435 
436  std::stringstream lm;
437  lm<<lastModif;
438  double modif=atof((lm.str()).c_str());
439  if(timestamp<modif)
440  {
441  SetAttribute("LastModified",lm.str(),"ID",pathId);
442  return true;
443  }
444  return false;
445  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::CleanPath ( std::string &  str) const

Cleans the path name.

Definition at line 203 of file creaImageIOTimestampDatabaseHandler.cpp.

Referenced by AddDirectory(), IsIndexed(), and SetAttribute().

204  {
205  size_t pos;
206  do
207  {
208  pos = str.find('\\');
209  if ((int)pos!=-1)
210  {
211  str.replace(pos, 1, "/");
212  }
213  }
214  while ((int)pos!=-1);
215  }

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Close ( )

Closes the 'source'.

Definition at line 71 of file creaImageIOTimestampDatabaseHandler.cpp.

72  {
73  return true;
74  }
bool creaImageIO::TimestampDatabaseHandler::Create ( )

Creates a new 'source'.

Definition at line 63 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBCreate().

64  {
65  return DBCreate();
66  }

Here is the call graph for this function:

bool creaImageIO::TimestampDatabaseHandler::DBCreate ( )
protected

Creates a new database on disk and the tables.

Definition at line 151 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Exception::errorCode(), CppSQLite3Exception::errorMessage(), GetFileName(), GimmickError, GimmickMessage, mDB, CppSQLite3DB::open(), and UPDATETIMESTAMPDB.

Referenced by Create().

152  {
153  GimmickMessage(1,"Creating SQLite database '"<<GetFileName()
154  <<"' ... "<<std::endl);
155 
156  if (boost::filesystem::exists(GetFileName()))
157  {
158  GimmickError(GetFileName()<<"' : "
159  << "file already exists");
160  return false;
161  }
162 
163  // OPENING
164  try
165  {
166  mDB->open(GetFileName().c_str());
167  }
168  catch (CppSQLite3Exception& e)
169  {
170  GimmickError(e.errorCode() << ":"
171  << e.errorMessage() <<std::endl);
172  return false;
173  }
174 
175 
176  // CREATING TABLES
177 
178  std::string command;
179 
180 
181  command = "CREATE TABLE ";
182  command += "FILES";
183  command += "\n(\nID INTEGER PRIMARY KEY";
184  command += ",\nPARENT_ID int not null";
185  command += ",\nPATH text";
186  command += ",\nLastModified datetext";
187  command += ",\nLastRead datetext";
188  command += ",\nTopLevelNodeId text";
189  command += ",\nReferencedDB text";
190  command += ",\nconstraint FK_PARENT foreign key (PARENT_ID) references ";
191  command += "FILES";
192  command += "(ID) on delete restrict on update restrict";
193 
194  command += "\n)";
195  UPDATETIMESTAMPDB(command);
196 
197  return true;
198  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::DBOpen ( )
protected

Open the database.

Definition at line 122 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Exception::errorCode(), CppSQLite3Exception::errorMessage(), GetFileName(), GimmickDebugMessage, GimmickError, GimmickMessage, mDB, and CppSQLite3DB::open().

Referenced by Open().

123  {
124  GimmickMessage(1,"Opening SQLite database '"<<GetFileName()
125  <<"' ... "<<std::endl);
126  // OPENING FILE
127  if (!boost::filesystem::exists(GetFileName()))
128  {
129  return false;
130  }
131 
132  try
133  {
134  mDB->open(GetFileName().c_str());
135  }
136  catch (CppSQLite3Exception& e)
137  {
138  GimmickError("Opening '"<<GetFileName()<<"' : "
139  << e.errorCode() << ":"
140  << e.errorMessage());
141  return false;
142  }
143 
144  GimmickDebugMessage(1,"Opening SQLite database '"<<GetFileName()
145  <<"' ... OK"<<std::endl);
146  return true;
147  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::DBRemove ( const std::string &  searchAtt,
const std::string &  searchVal,
const std::string &  refdb 
)
protected

Definition at line 411 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

Referenced by RemoveFile(), and RemoveNode().

412  {
413 
414  std::string query = "DELETE FROM FILES WHERE "+searchAtt+"='"+ searchVal + "' AND REFERENCEDDB='"+refdb+"';";
415  UPDATETIMESTAMPDB(query);
416  }

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Destroy ( )

Destroys the 'source'.

Definition at line 79 of file creaImageIOTimestampDatabaseHandler.cpp.

80  {
81  return false;
82  }
const std::string& creaImageIO::TimestampDatabaseHandler::GetFileName ( ) const
inline

Returns the sqlite db file name.

Definition at line 53 of file creaImageIOTimestampDatabaseHandler.h.

Referenced by DBCreate(), and DBOpen().

53 { return mFileName; }

Here is the caller graph for this function:

std::string creaImageIO::TimestampDatabaseHandler::IsIndexed ( const std::string &  path,
const std::string &  refdb 
)

Returns the id of the path if it's indexed, blank otherwise.

Definition at line 297 of file creaImageIOTimestampDatabaseHandler.cpp.

References CleanPath(), CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), and QUERYTIMESTAMPDB.

Referenced by AddDirectory().

298  {
299  std::string pat=path.c_str();
300  CleanPath(pat);
301  std::stringstream out;
302  std::stringstream result;
303  out<<"SELECT ID FROM FILES WHERE PATH='"<<pat<<"' AND REFERENCEDDB='"<<refdb<<"';";
304 
305  CppSQLite3Query q;
306  QUERYTIMESTAMPDB(out.str(),q);
307 
308 
309  while (!q.eof())
310  {
311  for (int fld = 0; fld < q.numFields(); fld++)
312  {
313  result<<q.getStringField(fld);
314  }
315  q.nextRow();
316  }
317 
318  return result.str();
319  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Open ( )

Opens an existing 'source'.

Definition at line 57 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBOpen().

58  {
59  return DBOpen();
60  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::RemoveEntries ( const std::string  i_table,
const std::string  i_attribute,
const std::string  i_operand,
const std::string  i_val 
)

Removes the entries that match the given parameters.

Definition at line 448 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

452  {
453  std::stringstream query;
454  query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
455  UPDATETIMESTAMPDB(query.str());
456  }
void creaImageIO::TimestampDatabaseHandler::RemoveFile ( const std::string &  searchAtt,
const std::string &  searchVal,
const std::string &  refdb 
)

Removes the filename with the given pathname.

Definition at line 369 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBRemove(), CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), and QUERYTIMESTAMPDB.

Referenced by RemoveNode().

370  {
371 
372  std::stringstream result;
373  std::string sel="SELECT PARENT_ID FROM FILES WHERE "+searchAtt+"='"+searchVal+"' AND REFERENCEDDB='"+refdb+"';";
374 
375  CppSQLite3Query q;
376  QUERYTIMESTAMPDB(sel,q);
377 
378  while (!q.eof())
379  {
380  for (int fld = 0; fld < q.numFields(); fld++)
381  {
382  result<<q.getStringField(fld);
383  }
384  q.nextRow();
385  }
386  DBRemove(searchAtt,searchVal,refdb);
387 
388  int nChildren=0;
389  sel="SELECT ID FROM FILES WHERE PARENT_ID='"+result.str()+"'";
390  CppSQLite3Query q2;
391  QUERYTIMESTAMPDB(sel,q2);
392  while (!q2.eof())
393  {
394  nChildren++;
395  q2.nextRow();
396  }
397  if(nChildren<1)
398  {
399  if(!result.str().compare("0"))
400  {
401  RemoveFile("ID",result.str(),refdb);
402  }
403  else
404  {
405  DBRemove("ID",result.str(),refdb);
406  }
407  }
408  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::RemoveNode ( const std::string &  searchAtt,
const tree::Node node,
const std::string &  refdb 
)

Removes the given node.

Definition at line 345 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBRemove(), creaImageIO::tree::Node::GetAttribute(), creaImageIO::tree::Node::GetChildrenList(), creaImageIO::tree::Node::GetLevel(), creaImageIO::tree::Node::GetNumberOfChildren(), and RemoveFile().

346  {
347  int n=node->GetNumberOfChildren();
348  if(n>0)
349  {
350  std::vector<tree::Node*> children=node->GetChildrenList();
351  std::vector<tree::Node*>::iterator it;
352  for(it=children.begin();it!=children.end();++it)
353  {
354  RemoveNode(searchAtt,(*it),refdb);
355  }
356  }
357  else if(node->GetLevel()==3)
358  {
359  RemoveFile(searchAtt,node->GetAttribute("FullFileName"),refdb);
360  }
361  else
362  {
363  DBRemove("TopLevelNodeId",node->GetAttribute("ID"),refdb);
364  }
365 
366 
367  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::SetAttribute ( const std::string &  attName,
const std::string &  attValue,
const std::string &  searchParam,
const std::string &  searchValue 
)

Sets the attribute to the value passed as parameter where the searchParameter is searchValue.

Definition at line 322 of file creaImageIOTimestampDatabaseHandler.cpp.

References CleanPath(), and UPDATETIMESTAMPDB.

Referenced by AddDirectory(), and CheckTimestamp().

326  {
327  std::string av=attValue.c_str();
328  std::string sv=searchValue.c_str();
329  CleanPath(av);
330  CleanPath(sv);
331 
332  std::string sql = "UPDATE FILES SET ";
333  sql += attName;
334  sql += " = '";
335  sql += av;
336  sql += "' WHERE ";
337  sql += searchParam;
338  sql += " = '";
339  sql += sv;
340  sql += "'";
341  UPDATETIMESTAMPDB(sql);
342  }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

CppSQLite3DB* creaImageIO::TimestampDatabaseHandler::mDB
private
std::string creaImageIO::TimestampDatabaseHandler::mFileName
private

The physical location associated to the DicomDatabase (directory, db file...)

Definition at line 128 of file creaImageIOTimestampDatabaseHandler.h.


The documentation for this class was generated from the following files: