1 #include <creaImageIOSynchron.h>
2 #include <creaImageIOSystem.h>
3 #include <boost/filesystem.hpp>
4 #include <boost/algorithm/string.hpp>
5 //namespace fs = boost::filesystem;
7 //=====================================================================
13 //=====================================================================
14 #define QUERYSYNCDB(QUER,RES) \
17 RES = mDB->execQuery(QUER.c_str()); \
19 catch (CppSQLite3Exception& e) \
21 GimmickError("SQLite query '"<<QUER<<"' Error : " \
22 << e.errorCode() << ":" \
23 << e.errorMessage() ); \
25 //=====================================================================
26 #define UPDATESYNCDB(UP) \
29 mDB->execDML(UP.c_str()); \
31 catch (CppSQLite3Exception& e) \
33 GimmickError("SQLite update '"<<UP<<"' Error : " \
34 << e.errorCode() << ":" \
35 << e.errorMessage() ); \
37 //=====================================================================
39 Synchronizer::Synchronizer(const std::string& path)
41 pathDB = path + "maintenance_database.db3";
42 mDB = new CppSQLite3DB;
46 //=====================================================================
47 Synchronizer::~Synchronizer()
52 //=====================================================================
53 void Synchronizer::Initialize()
55 if (!boost::filesystem::exists(pathDB))
65 mDB->open(pathDB.c_str());
67 catch (CppSQLite3Exception& e)
69 GimmickError("Opening '"<<pathDB<<"' : "
70 << e.errorCode() << ":"
74 // get the ADD operations List
75 //UpdateAddList(pathDB);
78 //=====================================================================
79 void Synchronizer::CreateDB()
81 mDB->open(pathDB.c_str());
84 command = "CREATE TABLE ";
86 command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
87 command += ",\nPATH text";
88 command += ",\nRECURSIVE boolean";
89 command += ",\nFILES_ADDED int";
90 command += ",\nREFERENCEDDB text";
92 UPDATESYNCDB(command);
94 command = "CREATE TABLE ";
95 command += "IGNORED_FILES";
96 command += "\n(\nID INTEGER PRIMARY KEY";
97 command += ",\nADD_KEY integer";
98 command += ",\nPATH text";
99 command += ",\nREMOVE boolean";
100 command += ",\nTIME datetext";
102 UPDATESYNCDB(command);
105 //=====================================================================
106 void Synchronizer::CleanName(std::string& str) const
111 pos = str.find('\\');
114 str.replace(pos, 1, "/");
120 //=====================================================================
121 void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
127 //=====================================================================
128 void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
130 ignoreList=GetIgnoreList(key);
133 //=====================================================================
134 void Synchronizer::UpdateAddList(const std::string& refdb)
136 std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
138 QUERYSYNCDB(query, res);
141 AddList temp = AddList(res);
142 mAddList.push_back(temp);
147 /////////////////////////////////////////////////////////////////////////////////////////////////
148 // remove an entry of the DB
149 //@param i_table : table where to do the remove
150 // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
152 /////////////////////////////////////////////////////////////////////////////////////////////////
153 void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
155 std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
159 /////////////////////////////////////////////////////////////////////////////////////////////////
160 // remove several entries of the DB
161 // @param i_table : table where to do the remove
162 // @param i_attribute: attribute to match
163 // @param i_operand : operand to use
164 // @param i_val : the reference
166 /////////////////////////////////////////////////////////////////////////////////////////////////
167 void Synchronizer::RemoveEntries(const std::string i_table,
168 const std::string i_attribute,
169 const std::string i_operand,
170 const std::string i_val)
172 std::stringstream query;
173 query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
174 UPDATESYNCDB(query.str());
177 /////////////////////////////////////////////////////////////////////////////////////////////////
178 // clean DataBase if an operation has no child anymore
179 // @param refdb: the database segement to clean
181 /////////////////////////////////////////////////////////////////////////////////////////////////
182 void Synchronizer::CleanList(const std::string& refdb)
185 UpdateAddList(refdb);
186 std::vector<AddList>::iterator it_add = mAddList.begin();
187 for(;it_add <mAddList.end(); ++it_add)
189 if(it_add->nbFiles == "0")
191 RemoveEntry("ADD_OPS", it_add->key);
192 RemoveEntry("IGNORED_FILES", it_add->key);
197 UpdateAddList(refdb);
200 /////////////////////////////////////////////////////////////////////////////////////////////////
201 // Inserts a new add operation in the database
202 // @param path: the path of the directory that was added
203 // @param recursive: shows if the action was called recursively or not
204 // @param nChildren: the number of files affected by the operation
205 // @param refdb: the referenced database
206 // @result : The operation has been added
207 /////////////////////////////////////////////////////////////////////////////////////////////////
208 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
211 std::string pat=path.c_str();
213 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
215 insert+=recursive+"',";
216 insert+=nChildren+",'";
218 UPDATESYNCDB(insert);
221 /////////////////////////////////////////////////////////////////////////////////////////////////
222 // Inserts a new ignored file in the database
223 // @param add_key: the key of the add_op to which it corresponds
224 // @param path: the path of the directory that was added
225 // @param remove: shows if the file was removed or not
226 // @param time: the time in which the file was removed
227 // @result : The file has been inserted
228 /////////////////////////////////////////////////////////////////////////////////////////////////
230 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
232 std::string pat=path.c_str();
234 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
235 if(id.compare("")==0)
238 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
239 insert+=addKey+"','";
243 UPDATESYNCDB(insert);
248 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
249 //gets the parent database to check if the file has been added to the current database
250 std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
251 //If there is no such entry, add it
252 if(parentDB.compare("")==0)
255 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
256 insert+=addKey+"','";
260 UPDATESYNCDB(insert);
264 //Sets the new add key attribute for the file
265 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
266 //Sets the new remove attribute for the file
267 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
268 //Sets the new time attribute for the file
269 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
274 /////////////////////////////////////////////////////////////////////////////////////////////////
275 // get the files name to ignore for a add operation synchronization
276 // @param : the add key
277 //@result : list (path) of ignore files
278 /////////////////////////////////////////////////////////////////////////////////////////////////
279 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
282 std::vector<std::string> i_names;
283 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
286 QUERYSYNCDB(query, res);
289 RemoveList temp = RemoveList(res);
290 if(temp.remove.compare("0")==0)
292 mIgnoreList.push_back(temp);
296 std::vector<RemoveList>::iterator it;
298 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
300 i_names.push_back((*it).path);
305 /////////////////////////////////////////////////////////////////////////////////////////////////
306 // Gets the required attribute in the required table
307 // @param attribute: the attribute to look for
308 // @param table: the table to look in
309 // @param searchParam: the search parameter
310 // @param searchValue: the search value
311 // @result : required attribute
312 /////////////////////////////////////////////////////////////////////////////////////////////////
313 std::string Synchronizer::GetAttribute(const std::string& attribute,
314 const std::string& table,
315 const std::string& searchParam,
316 const std::string& searchValue,
317 const std::string& refdb)
319 std::stringstream query;
321 std::string sVal=searchValue.c_str();
323 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
324 if(table.compare("ADD_OPS")==0)
326 query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
333 QUERYSYNCDB(query.str(), res);
336 result=res.getStringField(0);
342 /////////////////////////////////////////////////////////////////////////////////////////////////
343 // Sets the attribute value in the required table and column
344 // @param attribute: the attribute to look for
345 // @param table: the table to look in
346 // @param value: the value to set
347 // @param searchParam: the search parameter
348 // @param searchValue: the search value
349 // @result : attribute value changed
350 /////////////////////////////////////////////////////////////////////////////////////////////////
351 void Synchronizer::SetAttribute(const std::string& attribute,
352 const std::string& table,
353 const std::string& value,
354 const std::string& searchParam,
355 const std::string& searchValue,
356 const std::string& refdb)
358 std::string val=value.c_str();
359 std::string sVal=searchValue.c_str();
362 std::string sql = "UPDATE ";
372 if(table.compare("ADD_OPS")==0)
374 sql += "' AND REFERENCEDDB = '";
383 /////////////////////////////////////////////////////////////////////////////////////////////////
384 // get the files name to ignore for a add operation synchronization
385 // @param : the add key
386 //@result : list (path) of ignore files
387 /////////////////////////////////////////////////////////////////////////////////////////////////
388 void Synchronizer::GetList()
391 std::vector<std::string> i_names;
392 std::string query = "SELECT PATH, REMOVE FROM IGNORED_FILES";
394 QUERYSYNCDB(query, res);
397 std::string file(res.getStringField(0));
398 std::string ignore(res.getStringField(1));
399 mList[file] = ignore == "0"? true : false;
404 bool Synchronizer::isIndexed(const std::string filename)
407 std::string name(filename);
408 boost::algorithm::replace_all( name,"\\" , "/");
409 std::map <std::string, bool>::iterator it_list = mList.begin();
410 for(;it_list != mList.end(); it_list++)
412 if(it_list->first == name)