MainTable.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // MainTable.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainTable.h"
  6. #include "DatabaseUtilities.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMainTable
  14. IMPLEMENT_DYNAMIC(CMainTable, CDaoRecordset)
  15. CMainTable::CMainTable(CDaoDatabase* pdb)
  16. : CDaoRecordset(pdb)
  17. {
  18. //{{AFX_FIELD_INIT(CMainTable)
  19. m_lID = 0;
  20. m_lDate = 0;
  21. m_strType = _T("");
  22. m_strText = _T("");
  23. m_lShortCut = 0;
  24. m_lDontAutoDelete = 0;
  25. m_lTotalCopySize = 0;
  26. m_nFields = 7;
  27. //}}AFX_FIELD_INIT
  28. m_nDefaultType = dbOpenDynaset;
  29. }
  30. CString CMainTable::GetDefaultDBName()
  31. {
  32. return GetDBName();
  33. }
  34. CString CMainTable::GetDefaultSQL()
  35. {
  36. return _T("[Main]");
  37. }
  38. void CMainTable::DoFieldExchange(CDaoFieldExchange* pFX)
  39. {
  40. //{{AFX_FIELD_MAP(CMainTable)
  41. pFX->SetFieldType(CDaoFieldExchange::outputColumn);
  42. DFX_Long(pFX, _T("[lID]"), m_lID);
  43. DFX_Long(pFX, _T("[lDate]"), m_lDate);
  44. DFX_Text(pFX, _T("[strType]"), m_strType);
  45. DFX_Text(pFX, _T("[strText]"), m_strText);
  46. DFX_Long(pFX, _T("[lShortCut]"), m_lShortCut);
  47. DFX_Long(pFX, _T("[lDontAutoDelete]"), m_lDontAutoDelete);
  48. DFX_Long(pFX, _T("[lTotalCopySize]"), m_lTotalCopySize);
  49. //}}AFX_FIELD_MAP
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CMainTable diagnostics
  53. #ifdef _DEBUG
  54. void CMainTable::AssertValid() const
  55. {
  56. CDaoRecordset::AssertValid();
  57. }
  58. void CMainTable::Dump(CDumpContext& dc) const
  59. {
  60. CDaoRecordset::Dump(dc);
  61. }
  62. #endif //_DEBUG
  63. // deletes from both Main and Data
  64. BOOL CMainTable::DeleteClip( int id )
  65. {
  66. CString csMainSQL;
  67. CString csDataSQL;
  68. BOOL bRet = FALSE;
  69. csMainSQL.Format( "DELETE FROM Main WHERE lID = %d", id );
  70. csDataSQL.Format( "DELETE FROM Data WHERE lParentID = %d", id );
  71. try
  72. {
  73. theApp.EnsureOpenDB();
  74. theApp.m_pDatabase->Execute(csMainSQL, dbFailOnError);
  75. theApp.m_pDatabase->Execute(csDataSQL, dbFailOnError);
  76. bRet = TRUE;
  77. }
  78. catch(CDaoException* e)
  79. {
  80. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  81. e->Delete();
  82. }
  83. return bRet;
  84. }
  85. BOOL CMainTable::DeleteClips(ARRAY &IDs)
  86. {
  87. int count = IDs.GetSize();
  88. if(count <= 0)
  89. return FALSE;
  90. BOOL bRet = TRUE;
  91. // delete one at a time rather than all in one query
  92. // in order to avoid the "query too large" error for large deletes
  93. for( int i=0; i < count && bRet; i++ )
  94. bRet = bRet && DeleteClip( IDs[i] );
  95. return bRet;
  96. }
  97. BOOL CMainTable::DeleteAllClips()
  98. {
  99. BOOL bRet = FALSE;
  100. try
  101. {
  102. theApp.EnsureOpenDB();
  103. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  104. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  105. bRet = TRUE;
  106. }
  107. catch(CDaoException* e)
  108. {
  109. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  110. e->Delete();
  111. }
  112. return bRet;
  113. }
  114. BOOL CMainTable::DeleteAll()
  115. {
  116. BOOL bRet = FALSE;
  117. try
  118. {
  119. theApp.EnsureOpenDB();
  120. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  121. bRet = TRUE;
  122. }
  123. catch(CDaoException* e)
  124. {
  125. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  126. e->Delete();
  127. }
  128. return bRet;
  129. }
  130. HACCEL CMainTable::LoadAcceleratorKeys()
  131. {
  132. CMainTable recset;
  133. try
  134. {
  135. recset.Open("SELECT * FROM Main WHERE lShortCut > 0");
  136. CArray<ACCEL, ACCEL> keys;
  137. while(!recset.IsEOF())
  138. {
  139. ACCEL me;
  140. me.cmd = (USHORT)recset.m_lID;
  141. me.fVirt = 0;
  142. if( HIBYTE(recset.m_lShortCut) & HOTKEYF_SHIFT ) me.fVirt |= FSHIFT;
  143. if( HIBYTE(recset.m_lShortCut) & HOTKEYF_CONTROL ) me.fVirt |= FCONTROL;
  144. if( HIBYTE(recset.m_lShortCut) & HOTKEYF_ALT ) me.fVirt |= FALT;
  145. me.fVirt |= FVIRTKEY;
  146. me.key = LOBYTE(recset.m_lShortCut);
  147. keys.Add(me);
  148. recset.MoveNext();
  149. }
  150. if(keys.GetSize() > 0)
  151. return CreateAcceleratorTable(keys.GetData(), keys.GetSize());
  152. }
  153. catch(CDaoException* e)
  154. {
  155. e->Delete();
  156. }
  157. return NULL;
  158. }
  159. void CMainTable::Open(LPCTSTR lpszFormat,...)
  160. {
  161. m_pDatabase = theApp.EnsureOpenDB();
  162. CString csText;
  163. va_list vlist;
  164. ASSERT(AfxIsValidString(lpszFormat));
  165. va_start(vlist,lpszFormat);
  166. csText.FormatV(lpszFormat,vlist);
  167. va_end(vlist);
  168. CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  169. }