1 #include <creaImageIOSynchron.h>
2 #include <creaImageIOSystem.h>
3 #include <boost/filesystem.hpp>
4 #include <boost/algorithm/string.hpp>
7 //namespace fs = boost::filesystem;
9 //=====================================================================
15 //=====================================================================
16 #define QUERYSYNCDB(QUER,RES) \
19 RES = mDB->execQuery(QUER.c_str()); \
21 catch (CppSQLite3Exception& e) \
23 GimmickError("SQLite query '"<<QUER<<"' Error : " \
24 << e.errorCode() << ":" \
25 << e.errorMessage() ); \
27 //=====================================================================
28 #define UPDATESYNCDB(UP) \
31 mDB->execDML(UP.c_str()); \
33 catch (CppSQLite3Exception& e) \
35 GimmickError("SQLite update '"<<UP<<"' Error : " \
36 << e.errorCode() << ":" \
37 << e.errorMessage() ); \
39 //=====================================================================
41 Synchronizer::Synchronizer(const std::string& path)
43 pathDB = path + "maintenance_database.db3";
44 mDB = new CppSQLite3DB;
48 //=====================================================================
49 Synchronizer::~Synchronizer()
54 //=====================================================================
55 void Synchronizer::Initialize()
57 if (!boost::filesystem::exists(pathDB))
67 mDB->open(pathDB.c_str());
69 catch (CppSQLite3Exception& e)
71 GimmickError("Opening '"<<pathDB<<"' : "
72 << e.errorCode() << ":"
76 // get the ADD operations List
77 //UpdateAddList(pathDB);
80 //=====================================================================
81 void Synchronizer::CreateDB()
83 mDB->open(pathDB.c_str());
86 command = "CREATE TABLE ";
88 command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
89 command += ",\nPATH text";
90 command += ",\nRECURSIVE boolean";
91 command += ",\nFILES_ADDED int";
92 command += ",\nREFERENCEDDB text";
94 UPDATESYNCDB(command);
96 command = "CREATE TABLE ";
97 command += "IGNORED_FILES";
98 command += "\n(\nID INTEGER PRIMARY KEY";
99 command += ",\nADD_KEY integer";
100 command += ",\nPATH text";
101 command += ",\nREMOVE boolean";
102 command += ",\nTIME datetext";
104 UPDATESYNCDB(command);
107 //=====================================================================
108 void Synchronizer::CleanName(std::string& str) const
113 pos = str.find('\\');
116 str.replace(pos, 1, "/");
122 //=====================================================================
123 void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
129 //=====================================================================
130 void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
132 ignoreList=GetIgnoreList(key);
135 //=====================================================================
136 void Synchronizer::UpdateAddList(const std::string& refdb)
138 std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
140 QUERYSYNCDB(query, res);
143 AddList temp = AddList(res);
144 mAddList.push_back(temp);
149 /////////////////////////////////////////////////////////////////////////////////////////////////
150 // remove an entry of the DB
151 //@param i_table : table where to do the remove
152 // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
154 /////////////////////////////////////////////////////////////////////////////////////////////////
155 void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
157 std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
161 /////////////////////////////////////////////////////////////////////////////////////////////////
162 // remove several entries of the DB
163 // @param i_table : table where to do the remove
164 // @param i_attribute: attribute to match
165 // @param i_operand : operand to use
166 // @param i_val : the reference
168 /////////////////////////////////////////////////////////////////////////////////////////////////
169 void Synchronizer::RemoveEntries(const std::string i_table,
170 const std::string i_attribute,
171 const std::string i_operand,
172 const std::string i_val)
174 std::stringstream query;
175 query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
176 UPDATESYNCDB(query.str());
179 /////////////////////////////////////////////////////////////////////////////////////////////////
180 // clean DataBase if an operation has no child anymore
181 // @param refdb: the database segement to clean
183 /////////////////////////////////////////////////////////////////////////////////////////////////
184 void Synchronizer::CleanList(const std::string& refdb)
187 UpdateAddList(refdb);
188 std::vector<AddList>::iterator it_add = mAddList.begin();
189 for(;it_add <mAddList.end(); ++it_add)
191 if(it_add->nbFiles == "0")
193 RemoveEntry("ADD_OPS", it_add->key);
194 RemoveEntry("IGNORED_FILES", it_add->key);
199 UpdateAddList(refdb);
202 /////////////////////////////////////////////////////////////////////////////////////////////////
203 // Inserts a new add operation in the database
204 // @param path: the path of the directory that was added
205 // @param recursive: shows if the action was called recursively or not
206 // @param nChildren: the number of files affected by the operation
207 // @param refdb: the referenced database
208 // @result : The operation has been added
209 /////////////////////////////////////////////////////////////////////////////////////////////////
210 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
213 std::string pat=path.c_str();
215 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
217 insert+=recursive+"',";
218 insert+=nChildren+",'";
220 UPDATESYNCDB(insert);
223 /////////////////////////////////////////////////////////////////////////////////////////////////
224 // Inserts a new ignored file in the database
225 // @param add_key: the key of the add_op to which it corresponds
226 // @param path: the path of the directory that was added
227 // @param remove: shows if the file was removed or not
228 // @param time: the time in which the file was removed
229 // @result : The file has been inserted
230 /////////////////////////////////////////////////////////////////////////////////////////////////
232 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
234 std::string pat=path.c_str();
236 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
237 if(id.compare("")==0)
240 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
241 insert+=addKey+"','";
245 UPDATESYNCDB(insert);
250 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
251 //gets the parent database to check if the file has been added to the current database
252 std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
253 //If there is no such entry, add it
254 if(parentDB.compare("")==0)
257 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
258 insert+=addKey+"','";
262 UPDATESYNCDB(insert);
266 //Sets the new add key attribute for the file
267 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
268 //Sets the new remove attribute for the file
269 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
270 //Sets the new time attribute for the file
271 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
276 /////////////////////////////////////////////////////////////////////////////////////////////////
277 // get the files name to ignore for a add operation synchronization
278 // @param : the add key
279 //@result : list (path) of ignore files
280 /////////////////////////////////////////////////////////////////////////////////////////////////
281 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
284 std::vector<std::string> i_names;
285 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
288 QUERYSYNCDB(query, res);
291 RemoveList temp = RemoveList(res);
292 if(temp.remove.compare("0")==0)
294 mIgnoreList.push_back(temp);
298 std::vector<RemoveList>::iterator it;
300 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
302 i_names.push_back((*it).path);
307 /////////////////////////////////////////////////////////////////////////////////////////////////
308 // Gets the required attribute in the required table
309 // @param attribute: the attribute to look for
310 // @param table: the table to look in
311 // @param searchParam: the search parameter
312 // @param searchValue: the search value
313 // @result : required attribute
314 /////////////////////////////////////////////////////////////////////////////////////////////////
315 std::string Synchronizer::GetAttribute(const std::string& attribute,
316 const std::string& table,
317 const std::string& searchParam,
318 const std::string& searchValue,
319 const std::string& refdb)
321 std::stringstream query;
323 std::string sVal=searchValue.c_str();
325 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
326 if(table.compare("ADD_OPS")==0)
328 query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
335 QUERYSYNCDB(query.str(), res);
338 result=res.getStringField(0);
344 /////////////////////////////////////////////////////////////////////////////////////////////////
345 // Sets the attribute value in the required table and column
346 // @param attribute: the attribute to look for
347 // @param table: the table to look in
348 // @param value: the value to set
349 // @param searchParam: the search parameter
350 // @param searchValue: the search value
351 // @result : attribute value changed
352 /////////////////////////////////////////////////////////////////////////////////////////////////
353 void Synchronizer::SetAttribute(const std::string& attribute,
354 const std::string& table,
355 const std::string& value,
356 const std::string& searchParam,
357 const std::string& searchValue,
358 const std::string& refdb)
360 std::string val=value.c_str();
361 std::string sVal=searchValue.c_str();
364 std::string sql = "UPDATE ";
374 if(table.compare("ADD_OPS")==0)
376 sql += "' AND REFERENCEDDB = '";
385 /////////////////////////////////////////////////////////////////////////////////////////////////
386 // get the files name to ignore for a add operation synchronization
387 // @param : the add key
388 //@result : list (path) of ignore files
389 /////////////////////////////////////////////////////////////////////////////////////////////////
390 void Synchronizer::GetList(const std::string i_db)
393 std::vector<std::string> i_names;
394 std::vector<std::string> keys;
396 std::string query ="SELECT ADD_KEY, REFERENCEDDB FROM ADD_OPS";
397 QUERYSYNCDB(query, res);
401 std::string key(res.getStringField(0));
402 std::string db(res.getStringField(1));
409 query = "SELECT PATH, REMOVE FROM IGNORED_FILES WHERE";
412 for (int i=0; i < keys.size(); i++)
414 query += " ADD_KEY = " + keys[i];
417 query = query.substr(0,query.size() - 4);
421 query += " ADD_KEY = -1";
423 QUERYSYNCDB(query, res);
426 std::string file(res.getStringField(0));
427 std::string ignore(res.getStringField(1));
428 mList[file] = ignore == "0"? true : false;
433 bool Synchronizer::isIndexed(const std::string filename)
436 std::string name(filename);
437 boost::algorithm::replace_all( name,"\\" , "/");
438 std::map <std::string, bool>::iterator it_list = mList.begin();
439 for(;it_list != mList.end(); it_list++)
441 if(it_list->first == name)