DataTable.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // caller must free
  57. // takes m_ooData's HGLOBAL (do not update recset after calling this)
  58. // This should be faster than making a copy, but is this SAFE ?????
  59. HGLOBAL CDataTable::TakeData()
  60. {
  61. // if there is nothing to take
  62. if( m_ooData.m_hData == 0 || m_ooData.m_dwDataLength == 0 )
  63. return 0;
  64. // Unlock the handle that was locked by DaoLongBinaryAllocCallback()
  65. // (through DFX_LongBinary()).
  66. ::GlobalUnlock( m_ooData.m_hData );
  67. // we have to do a realloc in order to make the hGlobal m_dwDataLength
  68. HGLOBAL hGlobal = ::GlobalReAlloc(m_ooData.m_hData, m_ooData.m_dwDataLength, GMEM_MOVEABLE );
  69. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  70. {
  71. TRACE0( GetErrorString(::GetLastError()) );
  72. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  73. ASSERT(FALSE);
  74. }
  75. // no longer valid
  76. m_ooData.m_hData = 0;
  77. m_ooData.m_dwDataLength = 0;
  78. return hGlobal;
  79. }
  80. // this takes ownership of hgData, freeing m_ooData if necessary
  81. // This should be faster than making a copy, but is this SAFE?
  82. // looks like it is safe based upon:
  83. // http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q152/5/33.asp&NoWebContent=1
  84. // http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q119/7/65.asp&NoWebContent=1
  85. BOOL CDataTable::ReplaceData( HGLOBAL hgData, UINT len )
  86. {
  87. if( m_ooData.m_hData )
  88. ::GlobalFree( m_ooData.m_hData );
  89. m_ooData.m_hData = hgData;
  90. m_ooData.m_dwDataLength = len;
  91. //Set the fields dirty
  92. SetFieldDirty(&m_ooData);
  93. SetFieldNull(&m_ooData,FALSE);
  94. return TRUE;
  95. }
  96. // copies hgData into m_ooData using ::GlobalSize(hgData) for the size
  97. BOOL CDataTable::SetData( HGLOBAL hgData )
  98. {
  99. UINT unSize = GlobalSize(hgData);
  100. //Reallocate m_ooData.m_hData
  101. if(m_ooData.m_hData)
  102. m_ooData.m_hData = GlobalReAlloc(m_ooData.m_hData, unSize, GMEM_MOVEABLE);
  103. else
  104. m_ooData.m_hData = GlobalAlloc(GHND, unSize);
  105. m_ooData.m_dwDataLength = unSize;
  106. ::CopyToGlobalHH( m_ooData.m_hData, hgData, unSize );
  107. //Set the fields dirty
  108. SetFieldDirty(&m_ooData);
  109. SetFieldNull(&m_ooData,FALSE);
  110. return TRUE;
  111. }
  112. // allocates a new copy of the data
  113. HGLOBAL CDataTable::LoadData()
  114. {
  115. HGLOBAL hGlobal;
  116. ULONG ulBufLen = m_ooData.m_dwDataLength; //Retrieve size of array
  117. if( ulBufLen == 0 || m_ooData.m_hData == 0 )
  118. return 0;
  119. hGlobal = NewGlobalH( m_ooData.m_hData, ulBufLen );
  120. return hGlobal;
  121. }
  122. bool CDataTable::DeleteParent( long lParentID )
  123. {
  124. CString csDataSQL;
  125. bool bRet = false;
  126. csDataSQL.Format( "DELETE FROM Data WHERE lParentID = %d", lParentID );
  127. try
  128. {
  129. theApp.EnsureOpenDB();
  130. theApp.m_pDatabase->Execute(csDataSQL, dbFailOnError);
  131. bRet = TRUE;
  132. }
  133. catch(CDaoException* e)
  134. {
  135. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  136. e->Delete();
  137. }
  138. return bRet;
  139. }
  140. BOOL CDataTable::DeleteAll()
  141. {
  142. BOOL bRet = FALSE;
  143. try
  144. {
  145. theApp.EnsureOpenDB();
  146. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  147. bRet = TRUE;
  148. }
  149. catch(CDaoException* e)
  150. {
  151. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  152. e->Delete();
  153. }
  154. return bRet;
  155. }
  156. void CDataTable::Open(LPCTSTR lpszFormat,...)
  157. {
  158. m_pDatabase = theApp.EnsureOpenDB();
  159. CString csText;
  160. va_list vlist;
  161. ASSERT(AfxIsValidString(lpszFormat));
  162. va_start(vlist,lpszFormat);
  163. csText.FormatV(lpszFormat,vlist);
  164. va_end(vlist);
  165. CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  166. }
  167. void CDataTable::Open(int nOpenType, LPCTSTR lpszSql, int nOptions)
  168. {
  169. m_pDatabase = theApp.EnsureOpenDB();
  170. CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
  171. }
  172. BOOL CDataTable::DataEqual(HGLOBAL hgData)
  173. {
  174. return ::CompareGlobalHH( hgData, m_ooData.m_hData, m_ooData.m_dwDataLength ) == 0;
  175. }