home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daoview / listview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  13.9 KB  |  581 lines

  1. // samplvw.cpp : implementation of the CDaoListView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "DaoView.h"
  15. #include "DlgParam.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDaoListView
  25.  
  26. IMPLEMENT_DYNCREATE(CDaoListView, CListView)
  27.  
  28. BEGIN_MESSAGE_MAP(CDaoListView, CListView)
  29.     //{{AFX_MSG_MAP(CDaoListView)
  30.     ON_WM_CREATE()
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CDaoListView construction/destruction
  36.  
  37. CDaoListView::CDaoListView()
  38. {
  39. }
  40.  
  41. CDaoListView::~CDaoListView()
  42. {
  43. }
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CDaoListView drawing
  47.  
  48. void CDaoListView::OnDraw(CDC* pDC)
  49. {
  50.     CDaoViewDoc* pDoc = GetDocument();
  51.     ASSERT_VALID(pDoc);
  52.  
  53.     // TODO: add draw code for native data here
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CDaoListView diagnostics
  58.  
  59. #ifdef _DEBUG
  60. void CDaoListView::AssertValid() const
  61. {
  62.     CListView::AssertValid();
  63. }
  64.  
  65. void CDaoListView::Dump(CDumpContext& dc) const
  66. {
  67.     CListView::Dump(dc);
  68. }
  69.  
  70. #endif //_DEBUG
  71. CDaoViewDoc* CDaoListView::GetDocument() // non-debug version is inline
  72. {
  73.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDaoViewDoc)));
  74.     return (CDaoViewDoc*)m_pDocument;
  75. }
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CDaoListView message handlers
  79.  
  80. int CDaoListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  81. {
  82.     lpCreateStruct->style |= LVS_REPORT;
  83.     if (CListView::OnCreate(lpCreateStruct) == -1)
  84.         return -1;
  85.  
  86.     // Give the document a pointer to this view
  87.     GetDocument()->m_pListView = this;
  88.  
  89.     return 0;
  90. }
  91.  
  92. void CDaoListView::EraseList()
  93. {
  94.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  95.     ctlList.DeleteAllItems();
  96.     while(ctlList.DeleteColumn(0));
  97.     UpdateWindow();
  98. }
  99.  
  100. void CDaoListView::ShowDatabase()
  101. {
  102.     ASSERT(m_pDB);
  103.     ASSERT(m_pDB->IsOpen());
  104.  
  105.     CDaoDatabaseInfo dbInfo;
  106.  
  107.     m_bVertical = TRUE;
  108.     DisplayColumnHeadings(IDS_COL_DATABASE);
  109.  
  110.     try
  111.     {
  112.         m_pDB->m_pWorkspace->GetDatabaseInfo(m_pDB->GetName(),dbInfo,AFX_DAO_ALL_INFO);
  113.         ShowDatabaseInfo(0,dbInfo);
  114.     }
  115.     catch(CDaoException* e)
  116.     {
  117.         e->Delete();
  118.     }
  119.     AdjustColumnWidths();
  120. }
  121.  
  122. void CDaoListView::ShowTableData(LPCTSTR strTableName)
  123. {
  124.     ASSERT(m_pDB);
  125.     ASSERT(m_pDB->IsOpen());
  126.  
  127.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  128.  
  129.     CDaoFieldInfo fieldInfo;
  130.     int nFields;
  131.     // Attempt to open the table (which may fail with a security violation)
  132.  
  133.     EraseList();
  134.  
  135.     CDaoTableDef td(m_pDB);
  136.     try
  137.     {
  138.         td.Open(strTableName);
  139.         nFields = td.GetFieldCount();
  140.         for (int j=0; j < nFields; j++)
  141.         {
  142.             td.GetFieldInfo(j,fieldInfo);
  143.             ctlList.AddColumn(fieldInfo.m_strName,j);
  144.         }
  145.     }
  146.     catch (CDaoException* e)
  147.     {
  148.         DisplayDaoException(e);
  149.         e->Delete();
  150.     }
  151.     td.Close();
  152.     CDaoRecordset rs(m_pDB);
  153.     int nItem = 0;
  154.     int nLoaded = 0;
  155.     BOOL MAXRECORDS = ((CDaoViewApp *)AfxGetApp())->m_nMaxRecords;
  156.     try
  157.     {
  158.         CString strSelect(_T("Select * From ["));
  159.         strSelect += strTableName;
  160.         strSelect += _T("]");
  161.         rs.Open(dbOpenDynaset,strSelect);
  162.         while (!rs.IsEOF()) {
  163.             if (nItem < MAXRECORDS)\
  164.             {
  165.                 nLoaded++;
  166.                 COleVariant var;
  167.                 for (int i=0; i < nFields; i++)
  168.                 {
  169.                     var = rs.GetFieldValue(i);
  170.                     ctlList.AddItem(nItem,i,CCrack::strVARIANT(var));
  171.                 }
  172.             }
  173.             nItem++;
  174.             rs.MoveNext();
  175.         }
  176.     }
  177.     catch (CDaoException* e)
  178.     {
  179.         DisplayDaoException(e);
  180.         e->Delete();
  181.         return;
  182.     }
  183.     CString strRecCount;
  184.     strRecCount.Format(_T("Loaded %d of %d total records"),nLoaded,nItem);
  185.     UpdateWindow();
  186.     if (nItem>=MAXRECORDS)
  187.         MessageBox(strRecCount);
  188.     ((CFrameWnd *) AfxGetMainWnd())->SetMessageText(strRecCount);
  189.     rs.Close();
  190. }
  191.  
  192. void CDaoListView::ShowTableSchema(LPCTSTR strTableDefName)
  193. {
  194.     ASSERT(m_pDB);
  195.     ASSERT(m_pDB->IsOpen());
  196.  
  197.     CDaoTableDefInfo tabInfo;
  198.  
  199.     m_bVertical = strTableDefName != NULL;
  200.     DisplayColumnHeadings(IDS_COL_TABLE);
  201.  
  202.     int nItem = 0;
  203.     int nTableDefCount = m_pDB->GetTableDefCount();
  204.     BOOL bShowSystemObjects = ((CDaoViewApp *)AfxGetApp())->m_bShowSystemObjects;
  205.     for (int j=0; j < nTableDefCount; j++)
  206.     {
  207.         try
  208.         {
  209.             m_pDB->GetTableDefInfo(j,tabInfo,AFX_DAO_ALL_INFO);
  210.             if (!bShowSystemObjects)
  211.                 if (tabInfo.m_lAttributes & dbSystemObject)
  212.                     continue;
  213.             if (strTableDefName == NULL || tabInfo.m_strName == strTableDefName)
  214.                 ShowTableDefInfo(nItem++,tabInfo);
  215.         }
  216.         catch(CDaoException* e)
  217.         {
  218.             e->Delete();
  219.         }
  220.     }
  221.     AdjustColumnWidths();
  222. }
  223.  
  224. void CDaoListView::ShowRelations(LPCTSTR strRelationName)
  225. {
  226.     ASSERT(m_pDB);
  227.     ASSERT(m_pDB->IsOpen());
  228.  
  229.     CDaoRelationInfo Info;
  230.  
  231.     m_bVertical = strRelationName != NULL;
  232.     DisplayColumnHeadings(IDS_COL_RELATION);
  233.  
  234.     int nItem = 0;
  235.     int nTableDefCount = m_pDB->GetTableDefCount();
  236.     BOOL bShowSystemObjects = ((CDaoViewApp *)AfxGetApp())->m_bShowSystemObjects;
  237.     for (int j=0; j < nTableDefCount; j++)
  238.     {
  239.         try
  240.         {
  241.             m_pDB->GetRelationInfo(j,Info,AFX_DAO_ALL_INFO);
  242.             if (!bShowSystemObjects)
  243.                 if (Info.m_lAttributes & dbSystemObject)
  244.                     continue;
  245.             if (strRelationName == NULL || Info.m_strName == strRelationName)
  246.                 ShowRelationInfo(nItem++,Info);
  247.         }
  248.         catch(CDaoException* e)
  249.         {
  250.             e->Delete();
  251.         }
  252.     }
  253.     AdjustColumnWidths();
  254. }
  255.  
  256. void CDaoListView::ShowIndexes(LPCTSTR strTableName,LPCTSTR strIndexName)
  257. {
  258.     ASSERT(m_pDB);
  259.     ASSERT(m_pDB->IsOpen());
  260.  
  261.     CDaoTableDef td(m_pDB);
  262.     CDaoIndexInfo Info;
  263.  
  264.     m_bVertical = strIndexName != NULL;
  265.     DisplayColumnHeadings(IDS_COL_INDEX);
  266.  
  267.     try
  268.     {
  269.         td.Open(strTableName);
  270.         int nItem = 0;
  271.         int nIndexCount = td.GetIndexCount();
  272.         for (int j=0; j < nIndexCount; j++)
  273.         {
  274.             try
  275.             {
  276.                 td.GetIndexInfo(j,Info,AFX_DAO_ALL_INFO);
  277.                 if (strIndexName == NULL || Info.m_strName == strIndexName)
  278.                     ShowIndexInfo(nItem++,Info);
  279.             }
  280.             catch(CDaoException* e)
  281.             {
  282.                 e->Delete();
  283.             }
  284.         }
  285.         AdjustColumnWidths();
  286.     }
  287.     catch(CDaoException* e)
  288.     {
  289.         e->Delete();
  290.     }
  291.     td.Close();
  292.     AdjustColumnWidths();
  293. }
  294.  
  295. void CDaoListView::ShowQuerySchema(LPCTSTR strQueryDefName)
  296. {
  297.     ASSERT(m_pDB);
  298.     ASSERT(m_pDB->IsOpen());
  299.  
  300.     CDaoQueryDefInfo qdInfo;
  301.  
  302.     m_bVertical = strQueryDefName != NULL;
  303.     DisplayColumnHeadings(IDS_COL_QUERYDEF);
  304.  
  305.     int nItem = 0;
  306.     int nQueryDefCount = m_pDB->GetQueryDefCount();
  307.     BOOL bShowSystemObjects = ((CDaoViewApp *)AfxGetApp())->m_bShowSystemObjects;
  308.     for (int j=0; j < nQueryDefCount; j++)
  309.     {
  310.         try
  311.         {
  312.             m_pDB->GetQueryDefInfo(j,qdInfo,AFX_DAO_ALL_INFO);
  313.             if (!bShowSystemObjects)
  314.                 if (qdInfo.m_nType == 5)
  315.                     continue;
  316.             if (strQueryDefName == NULL || qdInfo.m_strName == strQueryDefName)
  317.                 ShowQueryDefInfo(nItem++,qdInfo);
  318.         }
  319.         catch (CDaoException* e)
  320.         {
  321.             e->Delete();
  322.         }
  323.     }
  324.     AdjustColumnWidths();
  325. }
  326.  
  327. void CDaoListView::RunQueryDef(LPCTSTR strQueryDefName)
  328. {
  329.     ASSERT(m_pDB);
  330.     ASSERT(m_pDB->IsOpen());
  331.  
  332.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  333.  
  334.     CDaoFieldInfo fieldInfo;
  335.     int nFields;
  336.  
  337.     CDaoQueryDef qd(m_pDB);
  338.  
  339.     EraseList();
  340.  
  341.     try
  342.     {
  343.         qd.Open(strQueryDefName);
  344.         if (qd.GetParameterCount() > 0)
  345.         {
  346.             CDlgParams dlgParams;
  347.             dlgParams.SetInfo(m_pDB,strQueryDefName);
  348.             dlgParams.DoModal();
  349.         }
  350.         nFields = qd.GetFieldCount();
  351.         for (int j=0; j < nFields; j++)
  352.         {
  353.             qd.GetFieldInfo(j,fieldInfo);
  354.             ctlList.AddColumn(fieldInfo.m_strName,j);
  355.         }
  356.     }
  357.     catch(CDaoException* e)
  358.     {
  359.         e->Delete();
  360.     }
  361.     //qd.Close();
  362.     CDaoRecordset rs(m_pDB);
  363.     int nItem = 0;
  364.     int nLoaded = 0;
  365.     BOOL MAXRECORDS = ((CDaoViewApp *)AfxGetApp())->m_nMaxRecords;
  366.     try
  367.     {
  368.         rs.Open(&qd,dbOpenSnapshot,dbReadOnly);
  369.         while (!rs.IsEOF()) {
  370.             if (nItem<MAXRECORDS)
  371.             {
  372.                 nLoaded++;
  373.                 COleVariant var;
  374.                 for (int i=0; i < nFields; i++)
  375.                 {
  376.                     var = rs.GetFieldValue(i);
  377.                     ctlList.AddItem(nItem,i,CCrack::strVARIANT(var));
  378.                 }
  379.             }
  380.             nItem++;
  381.             rs.MoveNext();
  382.         }
  383.     }
  384.     catch(CDaoException* e)
  385.     {
  386.         DisplayDaoException(e);
  387.         e->Delete();
  388.     }
  389.     CString strRecCount;
  390.     strRecCount.Format(_T("Loaded %d of %d total records"),nLoaded,nItem);
  391.     UpdateWindow();
  392.     if (nItem>=MAXRECORDS)
  393.         MessageBox(strRecCount);
  394.     ((CFrameWnd *) AfxGetMainWnd())->SetMessageText(strRecCount);
  395.     if (rs.IsOpen())
  396.         rs.Close();
  397. }
  398.  
  399. void CDaoListView::ShowFields(LPCTSTR strTableName,LPCTSTR strFieldName)
  400. {
  401.     ASSERT(m_pDB);
  402.     ASSERT(m_pDB->IsOpen());
  403.  
  404.     CDaoFieldInfo fieldInfo;
  405.  
  406.     m_bVertical = strFieldName != NULL;
  407.     DisplayColumnHeadings(IDS_COL_FIELD);
  408.  
  409.     // Attempt to open the table (which may fail with a security violation)
  410.     CDaoTableDef td(m_pDB);
  411.     try
  412.     {
  413.         td.Open(strTableName);
  414.         int nItem = 0;
  415.         int nFieldCount = td.GetFieldCount();
  416.         for (int j=0; j < nFieldCount; j++)
  417.         {
  418.             td.GetFieldInfo(j,fieldInfo,AFX_DAO_ALL_INFO);
  419.             if (strFieldName == NULL || fieldInfo.m_strName == strFieldName)
  420.             {
  421.                 ShowFieldInfo(nItem++,fieldInfo);
  422.             }
  423.         }
  424.         AdjustColumnWidths();
  425.     }
  426.     catch(CDaoException* e)
  427.     {
  428.         e->Delete();
  429.     }
  430.     td.Close();
  431. }
  432.  
  433. void CDaoListView::ShowFieldInfo(int nItem,CDaoFieldInfo& Info)
  434. {
  435.     CString strSize;
  436.     CString strCollatingOrder;
  437.     strSize.Format(_T("%ld"),Info.m_lSize);
  438.     strCollatingOrder.Format(_T("%ld"),Info.m_lCollatingOrder);
  439.  
  440.     AddItem(nItem,0,Info.m_strName);
  441.     AddItem(nItem,1,CCrack::strFieldType(Info.m_nType));
  442.     AddItem(nItem,2,strSize);
  443.     AddItem(nItem,3,CCrack::strBOOL(Info.m_bRequired));
  444.     AddItem(nItem,4,CCrack::strBOOL(Info.m_bAllowZeroLength));
  445.     AddItem(nItem,5,strCollatingOrder);
  446.     AddItem(nItem,6,Info.m_strForeignName);
  447.     AddItem(nItem,7,Info.m_strValidationRule);
  448.     AddItem(nItem,8,Info.m_strValidationText);
  449.     AddItem(nItem,9,Info.m_strDefaultValue);
  450. }
  451.  
  452. void CDaoListView::ShowDatabaseInfo(int nItem,CDaoDatabaseInfo& Info)
  453. {
  454.     AddItem(nItem,0,Info.m_strName);
  455.     AddItem(nItem,1,CCrack::strBOOL(Info.m_bUpdatable));
  456.     AddItem(nItem,2,CCrack::strBOOL(Info.m_bTransactions));
  457.     AddItem(nItem,3,Info.m_strVersion);
  458.     AddItem(nItem,4,CCrack::strVARIANT(COleVariant(Info.m_lCollatingOrder)));
  459.     AddItem(nItem,5,CCrack::strVARIANT(COleVariant(Info.m_nQueryTimeout)));
  460.     AddItem(nItem,6,Info.m_strConnect);
  461. }
  462.  
  463. void CDaoListView::ShowQueryDefInfo(int nItem,CDaoQueryDefInfo& Info)
  464. {
  465.     CString strODBCTimeout;
  466.     strODBCTimeout.Format(_T("%hd"),Info.m_nODBCTimeout);
  467.  
  468.     AddItem(nItem,0,Info.m_strName);
  469.     AddItem(nItem,1,CCrack::strQueryDefType(Info.m_nType));
  470.     AddItem(nItem,2,Info.m_dateCreated.Format());
  471.     AddItem(nItem,3,Info.m_dateLastUpdated.Format());
  472.     AddItem(nItem,4,CCrack::strBOOL(Info.m_bUpdatable));
  473.     AddItem(nItem,5,CCrack::strBOOL(Info.m_bReturnsRecords));
  474.     AddItem(nItem,6,Info.m_strSQL);
  475.     AddItem(nItem,7,Info.m_strConnect);
  476.     AddItem(nItem,8,strODBCTimeout);
  477. }
  478.  
  479. void CDaoListView::ShowTableDefInfo(int nItem,CDaoTableDefInfo& Info)
  480. {
  481.     CString strRecordCount;
  482.     strRecordCount.Format(_T("%ld"),Info.m_lRecordCount);
  483.  
  484.     AddItem(nItem,0,Info.m_strName);
  485.     AddItem(nItem,1,CCrack::strBOOL(Info.m_bUpdatable));
  486.     AddItem(nItem,2,Info.m_dateCreated.Format());
  487.     AddItem(nItem,3,Info.m_dateLastUpdated.Format());
  488.     AddItem(nItem,4,Info.m_strSrcTableName);
  489.     AddItem(nItem,5,Info.m_strConnect);
  490.     AddItem(nItem,6,Info.m_strValidationRule);
  491.     AddItem(nItem,7,Info.m_strValidationText);
  492.     AddItem(nItem,8,strRecordCount);
  493. }
  494.  
  495. void CDaoListView::ShowRelationInfo(int nItem,CDaoRelationInfo& Info)
  496. {
  497.     CString strAttributes;
  498.     strAttributes.Format(_T("%ld"),Info.m_lAttributes);
  499.  
  500.     AddItem(nItem,0,Info.m_strName);
  501.     AddItem(nItem,1,Info.m_strTable);
  502.     AddItem(nItem,2,Info.m_strForeignTable);
  503.     AddItem(nItem,3,strAttributes);
  504. }
  505.  
  506. void CDaoListView::ShowIndexInfo(int nItem,CDaoIndexInfo& Info)
  507. {
  508.     CString strFieldInfo;
  509.     for (int nIndex = 0; nIndex < Info.m_nFields; nIndex++)
  510.     {
  511.         strFieldInfo += Info.m_pFieldInfos[nIndex].m_bDescending ? _T("-") : _T("+");
  512.         strFieldInfo += Info.m_pFieldInfos[nIndex].m_strName;
  513.         if (nIndex < Info.m_nFields - 1)
  514.             strFieldInfo += _T("; ");
  515.     }
  516.     CString strDistinctCount;
  517.     strDistinctCount.Format(_T("%ld"),Info.m_lDistinctCount);
  518.  
  519.     AddItem(nItem,0,Info.m_strName);
  520.     AddItem(nItem,1,strFieldInfo);
  521.     AddItem(nItem,2,CCrack::strBOOL(Info.m_bPrimary));
  522.     AddItem(nItem,3,CCrack::strBOOL(Info.m_bUnique));
  523.     AddItem(nItem,4,CCrack::strBOOL(Info.m_bClustered));
  524.     AddItem(nItem,5,CCrack::strBOOL(Info.m_bIgnoreNulls));
  525.     AddItem(nItem,6,CCrack::strBOOL(Info.m_bRequired));
  526.     AddItem(nItem,7,CCrack::strBOOL(Info.m_bForeign));
  527.     AddItem(nItem,8,strDistinctCount);
  528. }
  529.  
  530. void CDaoListView::DisplayColumnHeadings(UINT nStringID)
  531. {
  532.     CString strHeadings;
  533.     strHeadings.LoadString(nStringID);
  534.  
  535.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  536.  
  537.     int nPos;
  538.     int nCount = 0;
  539.  
  540.     EraseList();
  541.  
  542.     if (m_bVertical)
  543.     {
  544.         ctlList.AddColumn(_T("Property     "),0);
  545.         ctlList.AddColumn(_T("Value     "),1);
  546.         m_nColumns = 2;
  547.     }
  548.     while ((nPos = strHeadings.Find(_T(","))) != -1){
  549.         CString strItem;
  550.         strItem = strHeadings.Left(nPos);
  551.         if (m_bVertical)
  552.             ctlList.AddItem(nCount++,0,strItem);
  553.         else
  554.             ctlList.AddColumn(strItem,nCount++);
  555.         strItem = strHeadings.Mid(nPos + 1);
  556.         strHeadings = strItem;
  557.     }
  558.     if (m_bVertical)
  559.         ctlList.AddItem(nCount,0,strHeadings);
  560.     else
  561.         ctlList.AddColumn(strHeadings,nCount);
  562.     m_nColumns = nCount;
  563. }
  564.  
  565. void CDaoListView::AdjustColumnWidths()
  566. {
  567.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  568.  
  569.     ctlList.SetColumnWidth(-1,-3);
  570. }
  571.  
  572. void CDaoListView::AddItem(int nItem,int nSubItem,LPCTSTR strItem)
  573. {
  574.     CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
  575.  
  576.     if (m_bVertical)
  577.         ctlList.AddItem(nSubItem,1,strItem);
  578.     else
  579.         ctlList.AddItem(nItem,nSubItem,strItem);
  580. }
  581.