DataTable.cpp 5.0 KB

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