1
0

DataTable.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // make sure the given HGLOBAL is valid.
  87. if( hgData != NULL )
  88. {
  89. VERIFY( len > 0 );
  90. UINT size = ::GlobalSize( hgData );
  91. VERIFY( size >= len );
  92. VERIFY( IsValid(hgData) );
  93. }
  94. else
  95. VERIFY( len == 0 );
  96. // free the old HGLOBAL
  97. if( m_ooData.m_hData )
  98. ::GlobalFree( m_ooData.m_hData );
  99. m_ooData.m_hData = hgData;
  100. m_ooData.m_dwDataLength = len;
  101. //Set the fields dirty
  102. SetFieldDirty(&m_ooData);
  103. SetFieldNull(&m_ooData,FALSE);
  104. return TRUE;
  105. }
  106. // copies hgData into m_ooData
  107. BOOL CDataTable::SetData( HGLOBAL hgData, UINT size )
  108. {
  109. UINT unSize = (size < 0)? ::GlobalSize(hgData) : size;
  110. //Reallocate m_ooData.m_hData
  111. if(m_ooData.m_hData)
  112. m_ooData.m_hData = ::GlobalReAlloc(m_ooData.m_hData, unSize, GMEM_MOVEABLE);
  113. else
  114. m_ooData.m_hData = ::GlobalAlloc(GHND, unSize);
  115. m_ooData.m_dwDataLength = unSize;
  116. ::CopyToGlobalHH( m_ooData.m_hData, hgData, unSize );
  117. //Set the fields dirty
  118. SetFieldDirty(&m_ooData);
  119. SetFieldNull(&m_ooData,FALSE);
  120. return TRUE;
  121. }
  122. // allocates a new copy of the data
  123. HGLOBAL CDataTable::LoadData()
  124. {
  125. HGLOBAL hGlobal;
  126. ULONG ulBufLen = m_ooData.m_dwDataLength; //Retrieve size of array
  127. if( ulBufLen == 0 || m_ooData.m_hData == 0 )
  128. return 0;
  129. hGlobal = NewGlobalH( m_ooData.m_hData, ulBufLen );
  130. return hGlobal;
  131. }
  132. void CDataTable::CopyRec( CDataTable& src )
  133. {
  134. m_strClipBoardFormat = src.m_strClipBoardFormat;
  135. SetData( src.m_ooData.m_hData, src.m_ooData.m_dwDataLength );
  136. }
  137. BOOL CDataTable::DeleteAll()
  138. {
  139. BOOL bRet = FALSE;
  140. try
  141. {
  142. theApp.EnsureOpenDB();
  143. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  144. bRet = TRUE;
  145. }
  146. catch(CDaoException* e)
  147. {
  148. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  149. e->Delete();
  150. }
  151. return bRet;
  152. }
  153. void CDataTable::Open(LPCTSTR lpszFormat,...)
  154. {
  155. m_pDatabase = theApp.EnsureOpenDB();
  156. CString csText;
  157. va_list vlist;
  158. ASSERT(AfxIsValidString(lpszFormat));
  159. va_start(vlist,lpszFormat);
  160. csText.FormatV(lpszFormat,vlist);
  161. va_end(vlist);
  162. CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  163. }
  164. void CDataTable::Open(int nOpenType, LPCTSTR lpszSql, int nOptions)
  165. {
  166. m_pDatabase = theApp.EnsureOpenDB();
  167. CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
  168. }
  169. BOOL CDataTable::DataEqual(HGLOBAL hgData)
  170. {
  171. return ::CompareGlobalHH( hgData, m_ooData.m_hData, m_ooData.m_dwDataLength ) == 0;
  172. }
  173. /////////////////////////////////////////////////////////////////////////////
  174. // CDataTable diagnostics
  175. #ifdef _DEBUG
  176. void CDataTable::AssertValid() const
  177. {
  178. CDaoRecordset::AssertValid();
  179. }
  180. void CDataTable::Dump(CDumpContext& dc) const
  181. {
  182. CDaoRecordset::Dump(dc);
  183. }
  184. #endif //_DEBUG