DataTable.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // DataTable.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "DataTable.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. // CDataTable
  14. IMPLEMENT_DYNAMIC(CDataTable, CDaoRecordset)
  15. CDataTable::CDataTable(CDaoDatabase* pdb)
  16. :CDaoRecordset(pdb)
  17. {
  18. //{{AFX_FIELD_INIT(CDataTable)
  19. m_lID = 0;
  20. m_lParentID = 0;
  21. m_strClipBoardFormat = _T("");
  22. m_nFields = 4;
  23. //}}AFX_FIELD_INIT
  24. m_nDefaultType = dbOpenDynaset;
  25. }
  26. CString CDataTable::GetDefaultDBName()
  27. {
  28. return GetDBName();
  29. }
  30. CString CDataTable::GetDefaultSQL()
  31. {
  32. return _T("[Data]");
  33. }
  34. void CDataTable::DoFieldExchange(CDaoFieldExchange* pFX)
  35. {
  36. //{{AFX_FIELD_MAP(CDataTable)
  37. pFX->SetFieldType(CDaoFieldExchange::outputColumn);
  38. DFX_Long(pFX, _T("[lID]"), m_lID);
  39. DFX_Long(pFX, _T("[lParentID]"), m_lParentID);
  40. DFX_Text(pFX, _T("[strClipBoardFormat]"), m_strClipBoardFormat);
  41. DFX_LongBinary(pFX, _T("[ooData]"), m_ooData);
  42. //}}AFX_FIELD_MAP
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CDataTable diagnostics
  46. #ifdef _DEBUG
  47. void CDataTable::AssertValid() const
  48. {
  49. CDaoRecordset::AssertValid();
  50. }
  51. void CDataTable::Dump(CDumpContext& dc) const
  52. {
  53. CDaoRecordset::Dump(dc);
  54. }
  55. #endif //_DEBUG
  56. BOOL CDataTable::DeleteAll()
  57. {
  58. BOOL bRet = FALSE;
  59. try
  60. {
  61. theApp.EnsureOpenDB();
  62. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  63. bRet = TRUE;
  64. }
  65. catch(CDaoException* e)
  66. {
  67. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  68. e->Delete();
  69. }
  70. return bRet;
  71. }
  72. BOOL CDataTable::SetData(HGLOBAL hgData)
  73. {
  74. //Get the data from the clipboard sent in
  75. LPVOID pvData = NULL;
  76. pvData = GlobalLock(hgData);
  77. if(!pvData)
  78. return FALSE;
  79. //Size
  80. UINT unSize = GlobalSize(hgData);
  81. //Realocate m_ooData.m_hData
  82. if(m_ooData.m_hData)
  83. m_ooData.m_hData = GlobalReAlloc(m_ooData.m_hData, unSize, GMEM_MOVEABLE);
  84. else
  85. m_ooData.m_hData = GlobalAlloc(GHND, unSize);
  86. //Get the data associated
  87. LPVOID pvNewData = NULL;
  88. pvNewData = GlobalLock(m_ooData.m_hData);
  89. if(!pvNewData)
  90. return FALSE;
  91. //Set the size
  92. m_ooData.m_dwDataLength = unSize;
  93. //Set the size
  94. memcpy(pvNewData, pvData, unSize);
  95. //Set the fields dirty
  96. SetFieldDirty(&m_ooData);
  97. SetFieldNull(&m_ooData,FALSE);
  98. return TRUE;
  99. }
  100. BOOL CDataTable::LoadData(COleDataSource *pData, UINT uiPastType)
  101. {
  102. BOOL fRetVal = FALSE;
  103. //Retrieve size of array
  104. ULONG ulBufLen = m_ooData.m_dwDataLength;
  105. if(ulBufLen > 0)
  106. {
  107. HGLOBAL hGlobal;
  108. hGlobal = GlobalAlloc(GMEM_ZEROINIT, ulBufLen);
  109. if(hGlobal != NULL)
  110. {
  111. LPVOID pvData = NULL;
  112. pvData = GlobalLock(hGlobal);
  113. if(!pvData)
  114. return FALSE;
  115. LPVOID pvData2 = NULL;
  116. pvData2 = GlobalLock(m_ooData.m_hData);
  117. if(!pvData2)
  118. return FALSE;
  119. memcpy(pvData, pvData2, ulBufLen);
  120. GlobalUnlock(hGlobal);
  121. GlobalUnlock(m_ooData.m_hData);
  122. pData->CacheGlobalData(uiPastType, hGlobal);
  123. fRetVal = TRUE;
  124. }
  125. else
  126. ASSERT(FALSE);
  127. }
  128. else
  129. ASSERT(FALSE);
  130. return fRetVal;
  131. }
  132. void CDataTable::Open(LPCTSTR lpszFormat,...)
  133. {
  134. m_pDatabase = theApp.EnsureOpenDB();
  135. CString csText;
  136. va_list vlist;
  137. ASSERT(AfxIsValidString(lpszFormat));
  138. va_start(vlist,lpszFormat);
  139. csText.FormatV(lpszFormat,vlist);
  140. va_end(vlist);
  141. CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  142. }
  143. void CDataTable::Open(int nOpenType, LPCTSTR lpszSql, int nOptions)
  144. {
  145. m_pDatabase = theApp.EnsureOpenDB();
  146. CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
  147. }
  148. BOOL CDataTable::DataEqual(HGLOBAL hgData)
  149. {
  150. int nRet;
  151. int sizeGiven = GlobalSize(hgData);
  152. int sizeSelf = GlobalSize(m_ooData.m_hData);
  153. //GlobalSize is allocated space and not necessarily the size of the data
  154. //Should we assume that the Data being compared are the same size?
  155. // so far in my tests:
  156. // GlobalSize( hgData ) == accurate size of the data contained
  157. // GlobalSize( m_ooData.m_hData ) == 0x00008000
  158. // - this is true even after locking the memory.
  159. // if( sizeGiven != m_ooData.m_dwDataLength )
  160. // {
  161. // ASSERT(FALSE);
  162. // return FALSE;
  163. // }
  164. LPVOID saved = NULL;
  165. saved = GlobalLock(m_ooData.m_hData);
  166. if(!saved)
  167. {
  168. ASSERT(FALSE);
  169. return FALSE;
  170. }
  171. LPVOID data = NULL;
  172. data = GlobalLock(hgData);
  173. if(!data)
  174. {
  175. GlobalUnlock(m_ooData.m_hData);
  176. ASSERT(FALSE);
  177. return FALSE;
  178. }
  179. nRet = (memcmp(data, saved, m_ooData.m_dwDataLength) == 0);
  180. // unlock after the compare
  181. GlobalUnlock(hgData);
  182. GlobalUnlock(m_ooData.m_hData);
  183. return nRet;
  184. }