DatabaseUtilities.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // DatabaseUtilites.cpp: implementation of the CDatabaseUtilites class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "DatabaseUtilities.h"
  7. #include "io.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CString GetDBName()
  12. {
  13. return CGetSetOptions::GetDBPath();
  14. }
  15. CString GetDefaultDBName()
  16. {
  17. CString csDefaultPath;
  18. LPMALLOC pMalloc;
  19. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  20. {
  21. LPITEMIDLIST pidlPrograms;
  22. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  23. char string[MAX_PATH];
  24. SHGetPathFromIDList(pidlPrograms, string);
  25. pMalloc->Free(pidlPrograms);
  26. pMalloc->Release();
  27. csDefaultPath = string;
  28. csDefaultPath += "\\Ditto\\";
  29. if(_access(csDefaultPath, 0) == -1)
  30. CreateDirectory(csDefaultPath, NULL);
  31. csDefaultPath += DEFAULT_DB_NAME;
  32. }
  33. return csDefaultPath;
  34. }
  35. BOOL CheckDBExists(CString csDBPath)
  36. {
  37. if(_access(csDBPath, 0) == -1)
  38. {
  39. //Database didn't exist
  40. CGetSetOptions::SetDBPath("");
  41. // -- create a new one
  42. CreateDB(GetDefaultDBName());
  43. return TRUE;
  44. }
  45. BOOL bRet = FALSE;
  46. if(ValidDB(csDBPath) == FALSE)
  47. {
  48. theApp.CloseDB();
  49. CGetSetOptions::SetDBPath("");
  50. //Db existed but was bad
  51. CString csMarkAsBad;
  52. csMarkAsBad = csDBPath;
  53. csMarkAsBad.Replace(".", "_BAD.");
  54. CString csPath = GetDefaultDBName();
  55. CString cs;
  56. cs.Format("Unrecognized Database Format \"%s\",\n"
  57. "the file will be renamed \"%s\",\n"
  58. "and a new database will be created,\n"
  59. "\"%s\"", csDBPath, csMarkAsBad, csPath);
  60. AfxMessageBox(cs);
  61. CFile::Rename(csDBPath, csMarkAsBad);
  62. bRet = CreateDB(csPath);
  63. }
  64. else
  65. bRet = TRUE;
  66. return bRet;
  67. }
  68. BOOL ValidDB(CString csPath)
  69. {
  70. try
  71. {
  72. CDaoDatabase db;
  73. db.Open(csPath);
  74. CDaoTableDef table(&db);
  75. CDaoFieldInfo info;
  76. table.Open("Main");
  77. table.GetFieldInfo("lID", info);
  78. table.GetFieldInfo("lDate", info);
  79. table.GetFieldInfo("strType", info);
  80. table.GetFieldInfo("strText", info);
  81. table.GetFieldInfo("lShortCut", info);
  82. table.GetFieldInfo("lDontAutoDelete", info);
  83. table.GetFieldInfo("lTotalCopySize", info);
  84. table.Close();
  85. table.Open("Data");
  86. table.GetFieldInfo("lID", info);
  87. table.GetFieldInfo("lParentID", info);
  88. table.GetFieldInfo("strClipBoardFormat", info);
  89. table.GetFieldInfo("ooData", info);
  90. table.Close();
  91. table.Open("Types");
  92. table.GetFieldInfo("ID", info);
  93. table.GetFieldInfo("TypeText", info);
  94. table.Close();
  95. }
  96. catch(CDaoException* e)
  97. {
  98. e->Delete();
  99. ASSERT(FALSE);
  100. return FALSE;
  101. }
  102. return TRUE;
  103. }
  104. BOOL CreateDB(CString csPath)
  105. {
  106. CDaoDatabase db;
  107. try
  108. {
  109. if(_access(csPath, 0) == 0)
  110. DeleteFile(csPath);
  111. db.Create(csPath);
  112. CDaoTableDefEx table(&db);
  113. //Creat the Main table
  114. table.Create("Main");
  115. table.CreateField("lID", dbLong, 4, dbAutoIncrField);
  116. table.CreateIndex(TRUE, "lID");
  117. table.CreateField("lDate", dbLong, 4, 0, "0");
  118. table.CreateIndex(FALSE, "lDate");
  119. table.CreateField("strType", dbText, 50, dbVariableField);
  120. table.CreateField("strText", dbText, 255, dbVariableField);
  121. table.CreateField("lShortCut", dbLong, 4, 0, "0");
  122. table.CreateIndex(FALSE, "lShortCut");
  123. table.CreateField("lDontAutoDelete", dbLong, 4, 0, "0");
  124. table.CreateField("lTotalCopySize", dbLong, 4, 0, "0");
  125. table.Append();
  126. table.Close();
  127. //Create the Data Table
  128. table.Create("Data");
  129. table.CreateField("lID", dbLong, 4, dbAutoIncrField);
  130. table.CreateIndex(TRUE, "lID");
  131. table.CreateField("lParentID", dbLong, 4, 0, "0");
  132. table.CreateIndex(FALSE, "lParentID");
  133. table.CreateField("strClipBoardFormat", dbText, 50, dbVariableField);
  134. table.CreateField("ooData", dbLongBinary, 0);
  135. table.Append();
  136. table.Close();
  137. //Create the Types table
  138. table.Create("Types");
  139. table.CreateField("ID", dbLong, 4, dbAutoIncrField);
  140. table.CreateField("TypeText", dbText, 50, dbVariableField);
  141. table.Append();
  142. table.Close();
  143. db.Close();
  144. return TRUE;
  145. }
  146. catch(CDaoException *e)
  147. {
  148. ASSERT(FALSE);
  149. e->Delete();
  150. }
  151. return FALSE;
  152. }
  153. BOOL CompactDatabase()
  154. {
  155. if(!theApp.CloseDB())
  156. return FALSE;
  157. CString csDBName = GetDBName();
  158. CString csTempDBName = csDBName;
  159. csTempDBName.Replace(".mdb", "TempDBName.mdb");
  160. //Compact the database
  161. try
  162. {
  163. CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
  164. }
  165. catch(CDaoException* e)
  166. {
  167. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  168. DeleteFile(csTempDBName);
  169. e->Delete();
  170. return FALSE;
  171. }
  172. catch(CMemoryException* e)
  173. {
  174. AfxMessageBox("Memory Exception");
  175. DeleteFile(csTempDBName);
  176. e->Delete();
  177. return FALSE;
  178. }
  179. //Since compacting the database creates a new db delete the old one and replace it
  180. //with the compacted db
  181. if(DeleteFile(csDBName))
  182. {
  183. try
  184. {
  185. CFile::Rename(csTempDBName, csDBName);
  186. }
  187. catch(CFileException *e)
  188. {
  189. e->ReportError();
  190. e->Delete();
  191. return FALSE;
  192. }
  193. }
  194. else
  195. AfxMessageBox("Error Compacting Database");
  196. return TRUE;
  197. }
  198. BOOL RepairDatabase()
  199. {
  200. if(!theApp.CloseDB())
  201. return FALSE;
  202. try
  203. {
  204. CDaoWorkspace::RepairDatabase(GetDBName());
  205. }
  206. catch(CDaoException *e)
  207. {
  208. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  209. e->Delete();
  210. return FALSE;
  211. }
  212. return TRUE;
  213. }
  214. BOOL RemoveOldEntries()
  215. {
  216. if(CGetSetOptions::GetCheckForMaxEntries())
  217. {
  218. long lMax = CGetSetOptions::GetMaxEntries();
  219. CMainTable recset;
  220. recset.Open("SELECT * FROM Main ORDER BY lDate DESC");
  221. if(!recset.IsEOF())
  222. {
  223. recset.MoveLast();
  224. long lCount = recset.GetRecordCount();
  225. ARRAY IDs;
  226. while((lCount > lMax) && (!recset.IsBOF()))
  227. {
  228. //Don't delete entries that have shorcuts or the flag set
  229. if(recset.m_lDontAutoDelete <= 0)
  230. IDs.Add(recset.m_lID);
  231. lCount--;
  232. recset.MovePrev();
  233. }
  234. recset.DeleteRows(IDs);
  235. }
  236. }
  237. if(CGetSetOptions::GetCheckForExpiredEntries())
  238. {
  239. long lExpire = CGetSetOptions::GetExpiredEntries();
  240. if(lExpire)
  241. {
  242. CTime now = CTime::GetCurrentTime();
  243. now -= CTimeSpan(lExpire, 0, 0, 0);
  244. CMainTable recset;
  245. recset.Open("SELECT * FROM Main "
  246. "WHERE lDate < %d AND "
  247. "lShortCut <= 0 AND lDontAutoDelete <= 0", now.GetTime());
  248. ARRAY IDs;
  249. while(!recset.IsEOF())
  250. {
  251. IDs.Add(recset.m_lID);
  252. recset.MoveNext();
  253. }
  254. recset.DeleteRows(IDs);
  255. }
  256. }
  257. return TRUE;
  258. }