home *** CD-ROM | disk | FTP | other *** search
- // DAOReadDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "DAORead.h"
- #include "DAORdDlg.h"
- #include <dbdaoerr.h>
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // Implementation
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOReadDlg dialog
-
- CDAOReadDlg::CDAOReadDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CDAOReadDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDAOReadDlg)
- m_lCacheSize = 100;
- m_strTableQuery = _T("");
- m_lNumRows = 10;
- m_bOpenRecordset = TRUE;
- m_bTable = TRUE;
- m_bDynaSet = TRUE;
- m_bDynaSetCache = TRUE;
- m_bSnapshot = TRUE;
- m_bGetRows = TRUE;
- m_strDatabase = _T("");
- m_strConnect = _T("");
- m_bExclusive = FALSE;
- m_bReadOnly = FALSE;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CDAOReadDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDAOReadDlg)
- DDX_Text(pDX, IDC_CACHESIZE, m_lCacheSize);
- DDV_MinMaxLong(pDX, m_lCacheSize, 0, 1200);
- DDX_Text(pDX, IDC_TABLEQUERYEDIT, m_strTableQuery);
- DDX_Text(pDX, IDC_NUMROWS, m_lNumRows);
- DDV_MinMaxLong(pDX, m_lNumRows, 1, 32000);
- DDX_Check(pDX, IDC_OPENRECORDSET, m_bOpenRecordset);
- DDX_Check(pDX, IDC_TABLE, m_bTable);
- DDX_Check(pDX, IDC_DYNASET, m_bDynaSet);
- DDX_Check(pDX, IDC_DYNASETCACHE, m_bDynaSetCache);
- DDX_Check(pDX, IDC_SNAPSHOT, m_bSnapshot);
- DDX_Check(pDX, IDC_GETROWS, m_bGetRows);
- DDX_Text(pDX, IDC_DATABASENAME, m_strDatabase);
- DDV_MaxChars(pDX, m_strDatabase, 256);
- DDX_Text(pDX, IDC_CONNECT, m_strConnect);
- DDV_MaxChars(pDX, m_strConnect, 256);
- DDX_Check(pDX, IDC_EXCLUSIVE, m_bExclusive);
- DDX_Check(pDX, IDC_READONLY, m_bReadOnly);
- //}}AFX_DATA_MAP
-
- //Do the manual checks
-
- //Check the database name parameter
- if(pDX->m_bSaveAndValidate)
- {
- pDX->PrepareEditCtrl(IDC_DATABASENAME);
- if (m_strDatabase.IsEmpty())
- {
- MessageBox(_T("Missing Jet database name."), _T("DAO Test App Error"), MB_ICONEXCLAMATION );
- pDX->Fail();
- }
- }
-
-
- //Check the Table/Query parameter
- if(pDX->m_bSaveAndValidate)
- {
- pDX->PrepareEditCtrl(IDC_TABLEQUERYEDIT);
- if (m_strTableQuery.IsEmpty())
- {
- MessageBox(_T("Missing table,query or SQL."), _T("DAO Test App Error"), MB_ICONEXCLAMATION );
- pDX->Fail();
- }
- }
- }
-
- BEGIN_MESSAGE_MAP(CDAOReadDlg, CDialog)
- //{{AFX_MSG_MAP(CDAOReadDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_DATABASE, OnDatabase)
- ON_BN_CLICKED(ID_EXECUTE, OnExecute)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOReadDlg message handlers
-
- BOOL CDAOReadDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
-
- //Set default selections
- SetDlgItemText(IDC_CACHESIZE, _T("100"));
- SetDlgItemText(IDC_EDIT9, _T("1"));
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CDAOReadDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CDAOReadDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CDAOReadDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- void CDAOReadDlg::OnDatabase()
- {
- CFileDialog* pcBrowse; //Need a file open dialog
-
- pcBrowse = (CFileDialog*)new CFileDialog(TRUE,_T("mdb"),NULL,NULL,_T("Jet Database Files | *.mdb ||"), NULL) ;
-
- pcBrowse->DoModal();
-
- (GetDlgItem(IDC_DATABASENAME))->SetWindowText(pcBrowse->GetPathName());
-
- delete pcBrowse;
- }
-
-
-
- //When the Execute button is pressed, run through the selected methods and perform each
- //reporting an approximate execution time and listing the first field returned (only if
- //it's a string or numeric)
-
- void CDAOReadDlg::OnExecute()
- {
- DWORD dwStart, dwEnd;
- DWORD dwDuration;
- LONG lIndex;
- COleVariant dbVar;
- CListBox *pList = ((CListBox *)(GetDlgItem(IDC_ROWSLIST)));
-
- //Update the parameters
- if (!UpdateData(TRUE))
- {
- return;
- }
-
- // This could take a while,
- CWaitCursor wait;
-
- //Clear the list
- pList->ResetContent();
-
-
- // Following is a series of database table retrieves using variations
- // on the Recordset object. This isn't a great programming structure,
- // but it shows each method clearly.
-
- //Execute each the requested read methods, recording a time for each
- try
- {
- //Open the database
- (GetDlgItem(IDC_DATABASENAME))->GetWindowText(m_strDatabase);
-
- m_dbDatabase = m_dbEngine.OpenDatabase( m_strDatabase,
- m_bExclusive,
- m_bReadOnly,
- m_strConnect);
-
- //Run through the selected methods, recording each time
-
- if(m_bOpenRecordset)
- {
- //Recordset as a Table
- if(m_bTable)
- {
- CdbRecordset RSetTable;
- pList->AddString(_T("Recordset/Table"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on a table
- try
- {
- RSetTable = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenTable);
- }
- //If the object was not found, assume it was something not openable
- //as a "table"
- catch(CdbException dbError)
- {
- if(dbError.m_hr != E_DAO_ObjectNotFound)
- throw dbError;
- }
- //Check that recordset was created
- if (RSetTable.Exists())
- {
- //Get requested number of records
- for (lIndex = 0L; !RSetTable.GetEOF() && lIndex < m_lNumRows; lIndex++)
- {
- //Get the first returned column
- dbVar = RSetTable.GetField(0L);
- AddFieldToList(lIndex, &dbVar);
-
- RSetTable.MoveNext();
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_TABLETIME, IDC_UNITTABLE, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_TABLETIME))->SetWindowText(_T("ERROR"));
- }
- }
-
- //Recordset as a Dynaset
- if(m_bDynaSet)
- {
- CdbRecordset RSetDyna;
- pList->AddString(_T("Recordset/Dynaset"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on as a Dynaset
- RSetDyna = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenDynaset);
-
- //Check that recordset was created
- if (RSetDyna.Exists())
- {
- //Get requested number of records
- for (lIndex = 0L; !RSetDyna.GetEOF() && lIndex < m_lNumRows; lIndex++)
- {
- //Get the first returned column
- dbVar = RSetDyna.GetField(0L);
- AddFieldToList(lIndex, &dbVar);
- RSetDyna.MoveNext();
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_DYNASETTIME, IDC_UNITDYNA, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_DYNASETTIME))->SetWindowText(_T("ERROR"));
- }
- }
-
- //Recordset as a Dynaset with cacheing
- if(m_bDynaSet && m_bDynaSetCache)
- {
- CdbRecordset RSetDynaCache;
- CdbBookmark DynaSetBookmark;
-
- pList->AddString(_T("Recordset/Dynaset cached"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on as a Dynaset
- RSetDynaCache = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenDynaset);
-
- //Check that recordset was created
- if (RSetDynaCache.Exists())
- {
- //Set the cache size to value supplied by user.
- RSetDynaCache.SetCacheSize(m_lCacheSize);
- RSetDynaCache.FillCache();
-
- //Get requested number of records
- for (lIndex = 0L; !RSetDynaCache.GetEOF() && lIndex < m_lNumRows; lIndex++)
- {
- //Get the first returned column
- dbVar = RSetDynaCache.GetField(0L);
- AddFieldToList(lIndex, &dbVar);
- RSetDynaCache.MoveNext();
-
- //Watch for cache run-out
- if (lIndex % m_lCacheSize)
- {
- DynaSetBookmark = RSetDynaCache.GetBookmark();
- RSetDynaCache.SetCacheStart(DynaSetBookmark);
- RSetDynaCache.FillCache();
- }
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_CACHETIME, IDC_UNITCACHE, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_CACHETIME))->SetWindowText(_T("ERROR"));
- }
- }
-
- //Recordset as a Snapshot
- if(m_bSnapshot)
- {
- // Bidirectional Snapshot
- CdbRecordset RSetSnap;
- pList->AddString(_T("Recordset/Snapshot"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on as a Snapshot
- RSetSnap = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenSnapshot);
-
- //Check that recordset was created
- if (RSetSnap.Exists())
- {
- //Get requested number of records
- for (lIndex = 0L; !RSetSnap.GetEOF() && lIndex < m_lNumRows; lIndex++)
- {
- //Get the first returned column
- dbVar = RSetSnap.GetField(0L);
- AddFieldToList(lIndex, &dbVar);
- RSetSnap.MoveNext();
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_SNAPSHOTTIME, IDC_UNITSNAP, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_SNAPSHOTTIME))->SetWindowText(_T("ERROR"));
- }
-
- //Forward Only snapshot
- CdbRecordset RSetSnapForward;
- pList->AddString(_T("Recordset/Snapshot forward only"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on as a Forward only Snapshot
- RSetSnapForward = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenSnapshot, dbForwardOnly);
-
- //Check that recordset was created
- if (RSetSnapForward.Exists())
- {
- //Get requested number of records
- for (lIndex = 0L; !RSetSnapForward.GetEOF() && lIndex < m_lNumRows; lIndex++)
- {
- //Get the first returned column
- dbVar = RSetSnapForward.GetField(0L);
- AddFieldToList(lIndex, &dbVar);
- RSetSnapForward.MoveNext();
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_FORWARDTIME, IDC_UNITSNAPF, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_FORWARDTIME))->SetWindowText(_T("ERROR"));
- }
-
- }
-
- //Getrows
- if(m_bGetRows)
- {
- CdbRecordset RSetGetRows;
- COleVariant cRows; // Returned rows (in SafeArray)
- LONG lActual;
- LONG lGetRowsIndex[2];
-
- pList->AddString(_T("GetRows"));
- pList->AddString(_T("=========================="));
-
- dwStart = timeGetTime();
-
- //Open recordset on as a Forward only Snapshot
- RSetGetRows = m_dbDatabase.OpenRecordset(m_strTableQuery, dbOpenSnapshot, dbForwardOnly);
-
- //Check that recordset was created
- if (RSetGetRows.Exists())
- {
- cRows = RSetGetRows.GetRows(m_lNumRows); // get requested rows
-
- //Find out how many records were actually retrieved
- SafeArrayGetUBound(cRows.parray, 1, &lActual);
- if(lActual > m_lNumRows)
- lActual = m_lNumRows;
-
- lGetRowsIndex[0] = 0L;
-
- for (lGetRowsIndex[1] = 0; lGetRowsIndex[1] < lActual; lGetRowsIndex[1]++)
- {
-
- //Use OLE safe array function to access fields
- SafeArrayGetElement(cRows.parray, &lGetRowsIndex[0], &dbVar);
- AddFieldToList(lGetRowsIndex[1], &dbVar);
- }
-
- //Report the estimated execute time (in milliseconds)
- dwEnd = timeGetTime();
- dwDuration = dwEnd - dwStart;
- DisplayQueryTime(IDC_GETROWSTIME, IDC_UNITGETROWS, dwDuration);
- }
- else
- {
- (GetDlgItem(IDC_GETROWSTIME))->SetWindowText(_T("ERROR"));
- }
- }
- }
-
- //Disconnect the database
- }
-
- catch (CdbException dbError)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbError.m_hr),
- (LPCTSTR) exError.GetDescription());
- AfxMessageBox(szBuf);
- }
-
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Other functions
-
- void CDAOReadDlg::DisplayQueryTime(int iControlID, int iUnitsID, DWORD dwDuration)
- {
- CString strDisp;
- CString strUnits(_T("ms"));
- double fDuration = dwDuration;
-
- //Did it go into minutes?
- if(fDuration >= 60000.0)
- {
- fDuration = fDuration/60000.0;
- strUnits = _T("M");
- }
- //Did it go to seconds?
- else if(fDuration >= 1000.0)
- {
- fDuration = fDuration/1000.0;
- strUnits = _T("S");
- }
-
- strDisp.Format(_T("%.2f"), fDuration);
-
- (GetDlgItem(iUnitsID))->SetWindowText(strUnits);
- (GetDlgItem(iControlID))->SetWindowText(strDisp);
-
- }
-
- void CDAOReadDlg::AddFieldToList(LONG lRecordNum, COleVariant *pdbVar)
- {
- CString strDisp;
- CString strScratch;
- CListBox *pList = ((CListBox *)(GetDlgItem(IDC_ROWSLIST)));
-
- //Add record number
- strDisp.Format(_T("%d - "), lRecordNum);
-
- //NOTE: This only works for strings/numerics
-
- switch(pdbVar->vt)
- {
- case VT_BSTR:
- {
- strDisp += (LPCTSTR)pdbVar->bstrVal;
- break;
- }
-
- case VT_I4:
- {
- strScratch.Format(_T("%d"), pdbVar->lVal);
- strDisp += strScratch;
- break;
- }
-
- case VT_UI1:
- {
- strScratch.Format(_T("%u"), pdbVar->bVal);
- strDisp += strScratch;
- break;
- }
-
- case VT_I2:
- {
- strScratch.Format(_T("%d"), pdbVar->iVal);
- strDisp += strScratch;
- break;
- }
-
- case VT_R4:
- {
- strScratch.Format(_T("%f"), pdbVar->fltVal);
- strDisp += strScratch;
- break;
- }
-
- case VT_R8 :
- {
- strScratch.Format(_T("%f"), pdbVar->dblVal);
- strDisp += strScratch;
- break;
- }
-
- default: //
- {
- strDisp += _T("Field type not supported by sample code");
- }
-
- }
- pList->AddString(strDisp);
-
- }
-