DataTable.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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( m_ooData.m_hData == 0 || m_ooData.m_dwDataLength == 0 )
  62. return 0;
  63. // we have to do a realloc in order to make the hGlobal m_dwDataLength
  64. HGLOBAL hGlobal = ::GlobalReAlloc(m_ooData.m_hData, m_ooData.m_dwDataLength, GMEM_MOVEABLE );
  65. if( !hGlobal || ::GlobalSize(hGlobal) == 0 )
  66. {
  67. TRACE0( GetErrorString(::GetLastError()) );
  68. //::_RPT0( _CRT_WARN, GetErrorString(::GetLastError()) );
  69. ASSERT(FALSE);
  70. }
  71. // no longer valid
  72. m_ooData.m_hData = 0;
  73. m_ooData.m_dwDataLength = 0;
  74. return hGlobal;
  75. }
  76. // this takes ownership of hgData, freeing m_ooData if necessary
  77. // This should be faster than making a copy, but is this SAFE ?????
  78. BOOL CDataTable::ReplaceData( HGLOBAL hgData, UINT len )
  79. {
  80. if( m_ooData.m_hData &&
  81. m_ooData.m_dwDataLength &&
  82. (m_ooData.m_hData = ::GlobalFree( m_ooData.m_hData )) )
  83. {
  84. ASSERT(FALSE);
  85. return FALSE;
  86. }
  87. m_ooData.m_hData = hgData;
  88. m_ooData.m_dwDataLength = len;
  89. //Set the fields dirty
  90. SetFieldDirty(&m_ooData);
  91. SetFieldNull(&m_ooData,FALSE);
  92. return TRUE;
  93. }
  94. // copies hgData into m_ooData using ::GlobalSize(hgData) for the size
  95. BOOL CDataTable::SetData( HGLOBAL hgData )
  96. {
  97. UINT unSize = GlobalSize(hgData);
  98. //Reallocate m_ooData.m_hData
  99. if(m_ooData.m_hData)
  100. m_ooData.m_hData = GlobalReAlloc(m_ooData.m_hData, unSize, GMEM_MOVEABLE);
  101. else
  102. m_ooData.m_hData = GlobalAlloc(GHND, unSize);
  103. m_ooData.m_dwDataLength = unSize;
  104. ::CopyToGlobalHH( m_ooData.m_hData, hgData, unSize );
  105. //Set the fields dirty
  106. SetFieldDirty(&m_ooData);
  107. SetFieldNull(&m_ooData,FALSE);
  108. return TRUE;
  109. }
  110. // allocates a new copy of the data
  111. HGLOBAL CDataTable::LoadData()
  112. {
  113. HGLOBAL hGlobal;
  114. ULONG ulBufLen = m_ooData.m_dwDataLength; //Retrieve size of array
  115. if(ulBufLen == 0)
  116. return 0;
  117. hGlobal = NewGlobalH( m_ooData.m_hData, ulBufLen );
  118. return hGlobal;
  119. }
  120. BOOL CDataTable::DeleteAll()
  121. {
  122. BOOL bRet = FALSE;
  123. try
  124. {
  125. theApp.EnsureOpenDB();
  126. theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
  127. bRet = TRUE;
  128. }
  129. catch(CDaoException* e)
  130. {
  131. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  132. e->Delete();
  133. }
  134. return bRet;
  135. }
  136. void CDataTable::Open(LPCTSTR lpszFormat,...)
  137. {
  138. m_pDatabase = theApp.EnsureOpenDB();
  139. CString csText;
  140. va_list vlist;
  141. ASSERT(AfxIsValidString(lpszFormat));
  142. va_start(vlist,lpszFormat);
  143. csText.FormatV(lpszFormat,vlist);
  144. va_end(vlist);
  145. CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  146. }
  147. void CDataTable::Open(int nOpenType, LPCTSTR lpszSql, int nOptions)
  148. {
  149. m_pDatabase = theApp.EnsureOpenDB();
  150. CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
  151. }
  152. BOOL CDataTable::DataEqual(HGLOBAL hgData)
  153. {
  154. return ::CompareGlobalHH( hgData, m_ooData.m_hData, m_ooData.m_dwDataLength ) == 0;
  155. }