| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- // DataTable.cpp : implementation file
- //
- #include "stdafx.h"
- #include "CP_Main.h"
- #include "DataTable.h"
- #include "DatabaseUtilities.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDataTable
- IMPLEMENT_DYNAMIC(CDataTable, CDaoRecordset)
- CDataTable::CDataTable(CDaoDatabase* pdb)
- :CDaoRecordset(pdb)
- {
- //{{AFX_FIELD_INIT(CDataTable)
- m_lID = 0;
- m_lParentID = 0;
- m_strClipBoardFormat = _T("");
- m_nFields = 4;
- //}}AFX_FIELD_INIT
- m_nDefaultType = dbOpenDynaset;
- }
- CString CDataTable::GetDefaultDBName()
- {
- return GetDBName();
- }
- CString CDataTable::GetDefaultSQL()
- {
- return _T("[Data]");
- }
- void CDataTable::DoFieldExchange(CDaoFieldExchange* pFX)
- {
- //{{AFX_FIELD_MAP(CDataTable)
- pFX->SetFieldType(CDaoFieldExchange::outputColumn);
- DFX_Long(pFX, _T("[lID]"), m_lID);
- DFX_Long(pFX, _T("[lParentID]"), m_lParentID);
- DFX_Text(pFX, _T("[strClipBoardFormat]"), m_strClipBoardFormat);
- DFX_LongBinary(pFX, _T("[ooData]"), m_ooData);
- //}}AFX_FIELD_MAP
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDataTable diagnostics
- #ifdef _DEBUG
- void CDataTable::AssertValid() const
- {
- CDaoRecordset::AssertValid();
- }
- void CDataTable::Dump(CDumpContext& dc) const
- {
- CDaoRecordset::Dump(dc);
- }
- #endif //_DEBUG
- BOOL CDataTable::DeleteAll()
- {
- BOOL bRet = FALSE;
- try
- {
- theApp.EnsureOpenDB();
- theApp.m_pDatabase->Execute("DELETE * FROM Data", dbFailOnError);
- bRet = TRUE;
- }
- catch(CDaoException* e)
- {
- AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- e->Delete();
- }
- return bRet;
- }
- BOOL CDataTable::SetData(HGLOBAL hgData)
- {
- //Get the data from the clipboard sent in
- LPVOID pvData = NULL;
- pvData = GlobalLock(hgData);
- if(!pvData)
- return FALSE;
- //Size
- UINT unSize = GlobalSize(hgData);
- //Realocate m_ooData.m_hData
- if(m_ooData.m_hData)
- m_ooData.m_hData = GlobalReAlloc(m_ooData.m_hData, unSize, GMEM_MOVEABLE);
- else
- m_ooData.m_hData = GlobalAlloc(GHND, unSize);
- //Get the data associated
- LPVOID pvNewData = NULL;
- pvNewData = GlobalLock(m_ooData.m_hData);
- if(!pvNewData)
- return FALSE;
- //Set the size
- m_ooData.m_dwDataLength = unSize;
- //Set the size
- memcpy(pvNewData, pvData, unSize);
- //Set the fields dirty
- SetFieldDirty(&m_ooData);
- SetFieldNull(&m_ooData,FALSE);
- return TRUE;
- }
- BOOL CDataTable::LoadData(COleDataSource *pData, UINT uiPastType)
- {
- BOOL fRetVal = FALSE;
-
- //Retrieve size of array
- ULONG ulBufLen = m_ooData.m_dwDataLength;
- if(ulBufLen > 0)
- {
- HGLOBAL hGlobal;
- hGlobal = GlobalAlloc(GMEM_ZEROINIT, ulBufLen);
- if(hGlobal != NULL)
- {
- LPVOID pvData = NULL;
- pvData = GlobalLock(hGlobal);
- if(!pvData)
- return FALSE;
- LPVOID pvData2 = NULL;
- pvData2 = GlobalLock(m_ooData.m_hData);
- if(!pvData2)
- return FALSE;
- memcpy(pvData, pvData2, ulBufLen);
-
- GlobalUnlock(hGlobal);
- GlobalUnlock(m_ooData.m_hData);
- pData->CacheGlobalData(uiPastType, hGlobal);
- fRetVal = TRUE;
- }
- else
- ASSERT(FALSE);
- }
- else
- ASSERT(FALSE);
-
- return fRetVal;
- }
- void CDataTable::Open(LPCTSTR lpszFormat,...)
- {
- m_pDatabase = theApp.EnsureOpenDB();
- CString csText;
- va_list vlist;
- ASSERT(AfxIsValidString(lpszFormat));
- va_start(vlist,lpszFormat);
- csText.FormatV(lpszFormat,vlist);
- va_end(vlist);
-
- CDaoRecordset::Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
- }
- void CDataTable::Open(int nOpenType, LPCTSTR lpszSql, int nOptions)
- {
- m_pDatabase = theApp.EnsureOpenDB();
-
- CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
- }
- BOOL CDataTable::DataEqual(HGLOBAL hgData)
- {
- int nRet;
- int sizeGiven = GlobalSize(hgData);
- int sizeSelf = GlobalSize(m_ooData.m_hData);
- //GlobalSize is allocated space and not necessarily the size of the data
- //Should we assume that the Data being compared are the same size?
- // so far in my tests:
- // GlobalSize( hgData ) == accurate size of the data contained
- // GlobalSize( m_ooData.m_hData ) == 0x00008000
- // - this is true even after locking the memory.
- // if( sizeGiven != m_ooData.m_dwDataLength )
- // {
- // ASSERT(FALSE);
- // return FALSE;
- // }
- LPVOID saved = NULL;
- saved = GlobalLock(m_ooData.m_hData);
- if(!saved)
- {
- ASSERT(FALSE);
- return FALSE;
- }
- LPVOID data = NULL;
- data = GlobalLock(hgData);
- if(!data)
- {
- GlobalUnlock(m_ooData.m_hData);
- ASSERT(FALSE);
- return FALSE;
- }
- nRet = (memcmp(data, saved, m_ooData.m_dwDataLength) == 0);
- // unlock after the compare
- GlobalUnlock(hgData);
- GlobalUnlock(m_ooData.m_hData);
- return nRet;
- }
|