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
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";
91 UPDATESYNCDB(command);
93 command = "CREATE TABLE ";
94 command += "IGNORED_FILES";
95 command += "\n(\nID INTEGER PRIMARY KEY";
96 command += ",\nADD_KEY integer";
97 command += ",\nPATH text";
98 command += ",\nREMOVE boolean";
99 command += ",\nTIME datetext";
101 UPDATESYNCDB(command);
104 //=====================================================================
105 void Synchronizer::CleanName(std::string& str) const
110 pos = str.find('\\');
113 str.replace(pos, 1, "/");
119 //=====================================================================
120 void Synchronizer::GetFileList(std::vector<AddList> & list)
126 //=====================================================================
127 void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
129 ignoreList=GetIgnoreList(key);
132 //=====================================================================
133 void Synchronizer::UpdateAddList()
135 std::string query = "SELECT * FROM ADD_OPS";
137 QUERYSYNCDB(query, res);
140 AddList temp = AddList(res);
141 mAddList.push_back(temp);
146 /////////////////////////////////////////////////////////////////////////////////////////////////
147 // remove an entry of the DB
148 //@param i_table : table where do the remove
149 // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
151 /////////////////////////////////////////////////////////////////////////////////////////////////
152 void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
154 std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
158 /////////////////////////////////////////////////////////////////////////////////////////////////
159 // clean DataBase if an operation has no child anymore
161 /////////////////////////////////////////////////////////////////////////////////////////////////
162 void Synchronizer::CleanList()
166 std::vector<AddList>::iterator it_add = mAddList.begin();
167 for(;it_add <mAddList.end(); ++it_add)
169 if(it_add->nbFiles == "0")
171 RemoveEntry("ADD_OPS", it_add->key);
172 RemoveEntry("IGNORED_FILES", it_add->key);
180 /////////////////////////////////////////////////////////////////////////////////////////////////
181 // Inserts a new add operation in the database
182 // @param path: the path of the directory that was added
183 // @param recursive: shows if the action was called recursively or not
184 // @param nChildren: the number of files affected by the operation
185 // @result : The operation has been added
186 /////////////////////////////////////////////////////////////////////////////////////////////////
187 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren)
190 std::string pat=path.c_str();
192 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED) VALUES('";
194 insert+=recursive+"',";
195 insert+=nChildren+");";
196 UPDATESYNCDB(insert);
199 /////////////////////////////////////////////////////////////////////////////////////////////////
200 // Inserts a new ignored file in the database
201 // @param add_key: the key of the add_op to which it corresponds
202 // @param path: the path of the directory that was added
203 // @param remove: shows if the file was removed or not
204 // @param time: the time in which the file was removed
205 // @result : The file has been inserted
206 /////////////////////////////////////////////////////////////////////////////////////////////////
208 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time)
210 std::string pat=path.c_str();
212 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat);
213 if(id.compare("")==0)
216 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
217 insert+=addKey+"','";
221 UPDATESYNCDB(insert);
226 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id);
227 //Sets the new add key attribute for the file
228 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id);
229 //Sets the new remove attribute for the file
230 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id);
231 //Sets the new time attribute for the file
232 SetAttribute("TIME","IGNORED_FILES",time,"ID", id);
236 /////////////////////////////////////////////////////////////////////////////////////////////////
237 // get the files name to ignore for a add operation synchronization
238 // @param : the add key
239 //@result : list (path) of ignore files
240 /////////////////////////////////////////////////////////////////////////////////////////////////
241 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
244 std::vector<std::string> i_names;
245 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
248 QUERYSYNCDB(query, res);
251 RemoveList temp = RemoveList(res);
252 if(temp.remove.compare("0")==0)
254 mIgnoreList.push_back(temp);
258 std::vector<RemoveList>::iterator it;
260 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
262 i_names.push_back((*it).path);
267 /////////////////////////////////////////////////////////////////////////////////////////////////
268 // Gets the required attribute in the required table
269 // @param attribute: the attribute to look for
270 // @param table: the table to look in
271 // @param searchParam: the search parameter
272 // @param searchValue: the search value
273 // @result : required attribute
274 /////////////////////////////////////////////////////////////////////////////////////////////////
275 std::string Synchronizer::GetAttribute(const std::string& attribute,
276 const std::string& table,
277 const std::string& searchParam,
278 const std::string& searchValue)
280 std::stringstream query;
282 std::string sVal=searchValue.c_str();
284 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal<<"';";
286 QUERYSYNCDB(query.str(), res);
289 result=res.getStringField(0);
295 /////////////////////////////////////////////////////////////////////////////////////////////////
296 // Sets the attribute value in the required table and column
297 // @param attribute: the attribute to look for
298 // @param table: the table to look in
299 // @param value: the value to set
300 // @param searchParam: the search parameter
301 // @param searchValue: the search value
302 // @result : attribute value changed
303 /////////////////////////////////////////////////////////////////////////////////////////////////
304 void Synchronizer::SetAttribute(const std::string& attribute,
305 const std::string& table,
306 const std::string& value,
307 const std::string& searchParam,
308 const std::string& searchValue)
310 std::string val=value.c_str();
311 std::string sVal=searchValue.c_str();
314 std::string sql = "UPDATE ";