1 #include <creaImageIOSynchron.h>
2 #include <creaImageIOSystem.h>
3 #include <boost/filesystem.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 (!fs::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 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 // clean DataBase if an operation has no child anymore
161 // @param refdb: the database segement to clean
163 /////////////////////////////////////////////////////////////////////////////////////////////////
164 void Synchronizer::CleanList(const std::string& refdb)
167 UpdateAddList(refdb);
168 std::vector<AddList>::iterator it_add = mAddList.begin();
169 for(;it_add <mAddList.end(); ++it_add)
171 if(it_add->nbFiles == "0")
173 RemoveEntry("ADD_OPS", it_add->key);
174 RemoveEntry("IGNORED_FILES", it_add->key);
179 UpdateAddList(refdb);
182 /////////////////////////////////////////////////////////////////////////////////////////////////
183 // Inserts a new add operation in the database
184 // @param path: the path of the directory that was added
185 // @param recursive: shows if the action was called recursively or not
186 // @param nChildren: the number of files affected by the operation
187 // @param refdb: the referenced database
188 // @result : The operation has been added
189 /////////////////////////////////////////////////////////////////////////////////////////////////
190 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
193 std::string pat=path.c_str();
195 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
197 insert+=recursive+"',";
198 insert+=nChildren+",'";
200 UPDATESYNCDB(insert);
203 /////////////////////////////////////////////////////////////////////////////////////////////////
204 // Inserts a new ignored file in the database
205 // @param add_key: the key of the add_op to which it corresponds
206 // @param path: the path of the directory that was added
207 // @param remove: shows if the file was removed or not
208 // @param time: the time in which the file was removed
209 // @result : The file has been inserted
210 /////////////////////////////////////////////////////////////////////////////////////////////////
212 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
214 std::string pat=path.c_str();
216 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
217 if(id.compare("")==0)
220 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
221 insert+=addKey+"','";
225 UPDATESYNCDB(insert);
230 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
231 //gets the parent database to check if the file has been added to the current database
232 std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
233 //If there is no such entry, add it
234 if(parentDB.compare("")==0)
237 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
238 insert+=addKey+"','";
242 UPDATESYNCDB(insert);
246 //Sets the new add key attribute for the file
247 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
248 //Sets the new remove attribute for the file
249 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
250 //Sets the new time attribute for the file
251 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
256 /////////////////////////////////////////////////////////////////////////////////////////////////
257 // get the files name to ignore for a add operation synchronization
258 // @param : the add key
259 //@result : list (path) of ignore files
260 /////////////////////////////////////////////////////////////////////////////////////////////////
261 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
264 std::vector<std::string> i_names;
265 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
268 QUERYSYNCDB(query, res);
271 RemoveList temp = RemoveList(res);
272 if(temp.remove.compare("0")==0)
274 mIgnoreList.push_back(temp);
278 std::vector<RemoveList>::iterator it;
280 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
282 i_names.push_back((*it).path);
287 /////////////////////////////////////////////////////////////////////////////////////////////////
288 // Gets the required attribute in the required table
289 // @param attribute: the attribute to look for
290 // @param table: the table to look in
291 // @param searchParam: the search parameter
292 // @param searchValue: the search value
293 // @result : required attribute
294 /////////////////////////////////////////////////////////////////////////////////////////////////
295 std::string Synchronizer::GetAttribute(const std::string& attribute,
296 const std::string& table,
297 const std::string& searchParam,
298 const std::string& searchValue,
299 const std::string& refdb)
301 std::stringstream query;
303 std::string sVal=searchValue.c_str();
305 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
306 if(table.compare("ADD_OPS")==0)
308 query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
315 QUERYSYNCDB(query.str(), res);
318 result=res.getStringField(0);
324 /////////////////////////////////////////////////////////////////////////////////////////////////
325 // Sets the attribute value in the required table and column
326 // @param attribute: the attribute to look for
327 // @param table: the table to look in
328 // @param value: the value to set
329 // @param searchParam: the search parameter
330 // @param searchValue: the search value
331 // @result : attribute value changed
332 /////////////////////////////////////////////////////////////////////////////////////////////////
333 void Synchronizer::SetAttribute(const std::string& attribute,
334 const std::string& table,
335 const std::string& value,
336 const std::string& searchParam,
337 const std::string& searchValue,
338 const std::string& refdb)
340 std::string val=value.c_str();
341 std::string sVal=searchValue.c_str();
344 std::string sql = "UPDATE ";
354 if(table.compare("ADD_OPS")==0)
356 sql += "' AND REFERENCEDDB = '";