00001 #include <creaImageIOSynchron.h>
00002 #include <creaImageIOSystem.h>
00003 #include <boost/filesystem.hpp>
00004 #include <boost/algorithm/string.hpp>
00005
00006
00007
00008
00009
00010
00011
00012 namespace creaImageIO
00013 {
00014
00015
00016 #define QUERYSYNCDB(QUER,RES) \
00017 try \
00018 { \
00019 RES = mDB->execQuery(QUER.c_str()); \
00020 } \
00021 catch (CppSQLite3Exception& e) \
00022 { \
00023 GimmickError("SQLite query '"<<QUER<<"' Error : " \
00024 << e.errorCode() << ":" \
00025 << e.errorMessage() ); \
00026 }
00027
00028 #define UPDATESYNCDB(UP) \
00029 try \
00030 { \
00031 mDB->execDML(UP.c_str()); \
00032 } \
00033 catch (CppSQLite3Exception& e) \
00034 { \
00035 GimmickError("SQLite update '"<<UP<<"' Error : " \
00036 << e.errorCode() << ":" \
00037 << e.errorMessage() ); \
00038 }
00039
00040
00041 Synchronizer::Synchronizer(const std::string& path)
00042 {
00043 pathDB = path + "maintenance_database.db3";
00044 mDB = new CppSQLite3DB;
00045 Initialize();
00046 }
00047
00048
00049 Synchronizer::~Synchronizer()
00050 {
00051 delete mDB;
00052 }
00053
00054
00055 void Synchronizer::Initialize()
00056 {
00057 if (!boost::filesystem::exists(pathDB))
00058 {
00059 CreateDB();
00060 }
00061
00062
00063 else
00064 {
00065 try
00066 {
00067 mDB->open(pathDB.c_str());
00068 }
00069 catch (CppSQLite3Exception& e)
00070 {
00071 GimmickError("Opening '"<<pathDB<<"' : "
00072 << e.errorCode() << ":"
00073 << e.errorMessage());
00074 }
00075 }
00076
00077
00078 }
00079
00080
00081 void Synchronizer::CreateDB()
00082 {
00083 mDB->open(pathDB.c_str());
00084
00085 std::string command;
00086 command = "CREATE TABLE ";
00087 command += "ADD_OPS";
00088 command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
00089 command += ",\nPATH text";
00090 command += ",\nRECURSIVE boolean";
00091 command += ",\nFILES_ADDED int";
00092 command += ",\nREFERENCEDDB text";
00093 command += "\n)";
00094 UPDATESYNCDB(command);
00095
00096 command = "CREATE TABLE ";
00097 command += "IGNORED_FILES";
00098 command += "\n(\nID INTEGER PRIMARY KEY";
00099 command += ",\nADD_KEY integer";
00100 command += ",\nPATH text";
00101 command += ",\nREMOVE boolean";
00102 command += ",\nTIME datetext";
00103 command += "\n)";
00104 UPDATESYNCDB(command);
00105 }
00106
00107
00108 void Synchronizer::CleanName(std::string& str) const
00109 {
00110 size_t pos;
00111 do
00112 {
00113 pos = str.find('\\');
00114 if (pos!=-1)
00115 {
00116 str.replace(pos, 1, "/");
00117 }
00118 }
00119 while (pos!=-1);
00120 }
00121
00122
00123 void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
00124 {
00125 CleanList(refdb);
00126 list=mAddList;
00127 }
00128
00129
00130 void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
00131 {
00132 ignoreList=GetIgnoreList(key);
00133 }
00134
00135
00136 void Synchronizer::UpdateAddList(const std::string& refdb)
00137 {
00138 std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
00139 CppSQLite3Query res;
00140 QUERYSYNCDB(query, res);
00141 while (!res.eof())
00142 {
00143 AddList temp = AddList(res);
00144 mAddList.push_back(temp);
00145 res.nextRow();
00146 }
00147 }
00148
00150
00151
00152
00153
00155 void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
00156 {
00157 std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
00158 UPDATESYNCDB(query);
00159 }
00160
00162
00163
00164
00165
00166
00167
00169 void Synchronizer::RemoveEntries(const std::string i_table,
00170 const std::string i_attribute,
00171 const std::string i_operand,
00172 const std::string i_val)
00173 {
00174 std::stringstream query;
00175 query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
00176 UPDATESYNCDB(query.str());
00177 }
00178
00180
00181
00182
00184 void Synchronizer::CleanList(const std::string& refdb)
00185 {
00186 mAddList.clear();
00187 UpdateAddList(refdb);
00188 std::vector<AddList>::iterator it_add = mAddList.begin();
00189 for(;it_add <mAddList.end(); ++it_add)
00190 {
00191 if(it_add->nbFiles == "0")
00192 {
00193 RemoveEntry("ADD_OPS", it_add->key);
00194 RemoveEntry("IGNORED_FILES", it_add->key);
00195
00196 }
00197 }
00198 mAddList.clear();
00199 UpdateAddList(refdb);
00200 }
00201
00203
00204
00205
00206
00207
00208
00210 void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
00211 {
00212 std::string insert;
00213 std::string pat=path.c_str();
00214 CleanName(pat);
00215 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
00216 insert+=convert(pat)+"','";
00217 insert+=recursive+"',";
00218 insert+=nChildren+",'";
00219 insert+=refdb+"');";
00220 UPDATESYNCDB(insert);
00221 }
00222
00224
00225
00226
00227
00228
00229
00231
00232 void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
00233 {
00234 std::string pat=path.c_str();
00235 CleanName(pat);
00236 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
00237 if(id.compare("")==0)
00238 {
00239 std::string insert;
00240 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
00241 insert+=addKey+"','";
00242 insert+=convert(pat)+"','";
00243 insert+=remove+"',";
00244 insert+=time+");";
00245 UPDATESYNCDB(insert);
00246 }
00247 else
00248 {
00249
00250 std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
00251
00252 std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
00253
00254 if(parentDB.compare("")==0)
00255 {
00256 std::string insert;
00257 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
00258 insert+=addKey+"','";
00259 insert+=convert(pat)+"','";
00260 insert+=remove+"',";
00261 insert+=time+");";
00262 UPDATESYNCDB(insert);
00263 }
00264 else
00265 {
00266
00267 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
00268
00269 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
00270
00271 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
00272 }
00273 }
00274 }
00275
00277
00278
00279
00281 std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
00282 {
00283 mIgnoreList.clear();
00284 std::vector<std::string> i_names;
00285 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
00286 query+=i_key;
00287 CppSQLite3Query res;
00288 QUERYSYNCDB(query, res);
00289 while (!res.eof())
00290 {
00291 RemoveList temp = RemoveList(res);
00292 if(temp.remove.compare("0")==0)
00293 {
00294 mIgnoreList.push_back(temp);
00295 }
00296 res.nextRow();
00297 }
00298 std::vector<RemoveList>::iterator it;
00299
00300 for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
00301 {
00302 i_names.push_back((*it).path);
00303 }
00304 return i_names;
00305 }
00306
00308
00309
00310
00311
00312
00313
00315 std::string Synchronizer::GetAttribute(const std::string& attribute,
00316 const std::string& table,
00317 const std::string& searchParam,
00318 const std::string& searchValue,
00319 const std::string& refdb)
00320 {
00321 std::stringstream query;
00322 std::string result;
00323 std::string sVal=convert(searchValue.c_str());
00324 CleanName(sVal);
00325 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
00326 if(table.compare("ADD_OPS")==0)
00327 {
00328 query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
00329 }
00330 else
00331 {
00332 query<<"';";
00333 }
00334 CppSQLite3Query res;
00335 QUERYSYNCDB(query.str(), res);
00336 while (!res.eof())
00337 {
00338 result=res.getStringField(0);
00339 res.nextRow();
00340 }
00341 return result;
00342 }
00343
00345
00346
00347
00348
00349
00350
00351
00353 void Synchronizer::SetAttribute(const std::string& attribute,
00354 const std::string& table,
00355 const std::string& value,
00356 const std::string& searchParam,
00357 const std::string& searchValue,
00358 const std::string& refdb)
00359 {
00360 std::string val=value.c_str();
00361 std::string sVal=convert(searchValue.c_str());
00362 CleanName(val);
00363 CleanName(sVal);
00364 std::string sql = "UPDATE ";
00365 sql+=table;
00366 sql+=" SET ";
00367 sql += attribute;
00368 sql += " = '";
00369 sql += val;
00370 sql += "' WHERE ";
00371 sql += searchParam;
00372 sql += " = '";
00373 sql += sVal;
00374 if(table.compare("ADD_OPS")==0)
00375 {
00376 sql += "' AND REFERENCEDDB = '";
00377 sql += refdb;
00378 }
00379 sql += "';";
00380 UPDATESYNCDB(sql);
00381 }
00382
00383
00384
00386
00387
00388
00390 void Synchronizer::GetList(const std::string i_db)
00391 {
00392 mList.clear();
00393 std::vector<std::string> i_names;
00394 std::vector<std::string> keys;
00395 CppSQLite3Query res;
00396 std::string query ="SELECT ADD_KEY, REFERENCEDDB FROM ADD_OPS";
00397 QUERYSYNCDB(query, res);
00398 keys.clear();
00399 while (!res.eof())
00400 {
00401 std::string key(res.getStringField(0));
00402 std::string db(res.getStringField(1));
00403 if (db == i_db)
00404 {
00405 keys.push_back(key);
00406 }
00407 res.nextRow();
00408 }
00409 query = "SELECT PATH, REMOVE FROM IGNORED_FILES WHERE";
00410 if(keys.size() > 0)
00411 {
00412 for (int i=0; i < keys.size(); i++)
00413 {
00414 query += " ADD_KEY = " + keys[i];
00415 query += " AND";
00416 }
00417 query = query.substr(0,query.size() - 4);
00418 }
00419 else
00420 {
00421 query += " ADD_KEY = -1";
00422 }
00423 QUERYSYNCDB(query, res);
00424 while (!res.eof())
00425 {
00426 std::string file(res.getStringField(0));
00427 std::string ignore(res.getStringField(1));
00428 mList[file] = ignore == "0"? true : false;
00429 res.nextRow();
00430 }
00431 }
00432
00433 bool Synchronizer::isIndexed(const std::string filename)
00434 {
00435 bool valid = true;
00436 std::string name(filename);
00437 boost::algorithm::replace_all( name,"\\" , "/");
00438 std::map <std::string, bool>::iterator it_list = mList.begin();
00439 for(;it_list != mList.end(); it_list++)
00440 {
00441 if(it_list->first == name)
00442 {
00443 valid = false;
00444 break;
00445 }
00446 }
00447 return valid;
00448 }
00449 const std::string Synchronizer::convert(const std::string &i_word)
00450 {
00451 std::string temp = i_word;
00452 boost::algorithm::replace_all(temp,"'","''");
00453
00454 return temp.c_str();
00455 }
00456 }
00457
00458