home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / vcoledb / consumer / catdb / catdbdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  7.8 KB  |  350 lines

  1. // CatDBDoc.cpp : implementation of the CCatDBDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes and
  4. // Templates (MFC&T).
  5. // Copyright (C) 1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // MFC&T Reference and related electronic documentation provided
  10. // with the library.  See these sources for detailed information
  11. // regarding the MFC&T product.
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "CatDB.h"
  16.  
  17. #include "tablepag.h"
  18. #include "colpage.h"
  19. #include "CatDBDoc.h"
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CCatDBDoc
  29.  
  30. IMPLEMENT_DYNCREATE(CCatDBDoc, CDocument)
  31.  
  32. BEGIN_MESSAGE_MAP(CCatDBDoc, CDocument)
  33.     //{{AFX_MSG_MAP(CCatDBDoc)
  34.     ON_COMMAND(ID_VIEW_SETTINGS, OnViewSettings)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CCatDBDoc construction/destruction
  40.  
  41. CCatDBDoc::CCatDBDoc()
  42. {
  43.     m_pTableset = NULL;
  44.     m_pColumnset = NULL;
  45.     m_strTableName = _T("");
  46. }
  47.  
  48. CCatDBDoc::~CCatDBDoc()
  49. {
  50. }
  51.  
  52. BOOL CCatDBDoc::OnNewDocument()
  53. {
  54.     if (!CDocument::OnNewDocument())
  55.         return FALSE;
  56.  
  57.     // initialize current view level
  58.     m_nLevel = levelNone;
  59.  
  60.     // initialize table settings
  61.     m_bSystemTables = GetProfileValue(_T("TableSettings"),_T("SystemTables"));
  62.     m_bViews        = GetProfileValue(_T("TableSettings"),_T("Views"));
  63.     m_bSynonyms     = GetProfileValue(_T("TableSettings"),_T("SystemTables"));
  64.  
  65.     // initialize column info settings
  66.     m_bLength       = GetProfileValue(_T("ColumnInfoSettings"),_T("Length"));
  67.     m_bPrecision    = GetProfileValue(_T("ColumnInfoSettings"),_T("Precision"));
  68.     m_bNullability  = GetProfileValue(_T("ColumnInfoSettings"),_T("Nullability"));
  69.  
  70.     return TRUE;
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CCatDBDoc serialization
  75.  
  76. void CCatDBDoc::Serialize(CArchive& ar)
  77. {
  78.     if (ar.IsStoring())
  79.     {
  80.         // TODO: add storing code here
  81.     }
  82.     else
  83.     {
  84.         // TODO: add loading code here
  85.     }
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CCatDBDoc diagnostics
  90.  
  91. #ifdef _DEBUG
  92. void CCatDBDoc::AssertValid() const
  93. {
  94.     CDocument::AssertValid();
  95. }
  96.  
  97. void CCatDBDoc::Dump(CDumpContext& dc) const
  98. {
  99.     CDocument::Dump(dc);
  100. }
  101. #endif //_DEBUG
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CCatDBDoc commands
  105.  
  106. void CCatDBDoc::SetLevel(Level nLevel)
  107. {
  108.     m_nLevel = nLevel;
  109.     UpdateAllViews(NULL);
  110. }
  111.  
  112. CString CCatDBDoc::GetDSN()
  113. {
  114.     // Check to see if the database is open
  115.     if (!m_pTableset)
  116.         return _T("[No Data Source Selected]");
  117.  
  118.     // pull DSN from database connect string
  119.     return m_strConnect;
  120. }
  121.  
  122. void CCatDBDoc::FetchColumnInfo(LPCSTR lpszName)
  123. {
  124.     if (m_pColumnset)
  125.     {
  126.         delete m_pColumnset;
  127.         m_pColumnset = NULL;
  128.     }
  129.  
  130.     m_pColumnset = new CColumns;
  131.     HRESULT hr = m_pColumnset->Open(m_session, NULL, NULL, lpszName);
  132.     if (FAILED(hr))
  133.     {
  134.         AfxMessageBox(_T("Couldn't open column rowset"));
  135.         delete m_pColumnset;
  136.         m_pColumnset = NULL;
  137.     }
  138. }
  139.  
  140. BOOL CCatDBDoc::FetchTableInfo()
  141. {
  142.     if (m_pTableset != NULL)
  143.     {
  144.         delete m_pTableset;
  145.         m_pTableset = NULL;
  146.     }
  147.     m_pTableset = new CTables;
  148.  
  149.     // Must use char array for ODBC interface
  150.     // (can simply hard code max size)
  151.     char lpszType[64];
  152.  
  153.     strcpy(lpszType, "TABLE");
  154.     if (m_bViews)
  155.         strcat(lpszType, ",VIEW");
  156.     if (m_bSystemTables)
  157.         strcat(lpszType, ",SYSTEM TABLE");
  158.     if (m_bSynonyms)
  159.         strcat(lpszType, ",ALIAS,SYNONYM");
  160.  
  161.     if (m_pTableset->Open(m_session, NULL, NULL, NULL, lpszType) != S_OK)
  162.     {
  163.         delete m_pTableset;
  164.         m_pTableset = NULL;
  165.         return FALSE;
  166.     }
  167.  
  168.     return TRUE;
  169. }
  170.  
  171. BOOL CCatDBDoc::OnOpenDocument()
  172. {
  173.     USES_CONVERSION;
  174.  
  175.     // close and delete any open recordsets
  176.     if (m_pTableset)
  177.     {
  178.         delete m_pTableset;
  179.         m_pTableset = NULL;
  180.     }
  181.  
  182.     if (m_pColumnset)
  183.     {
  184.         delete m_pColumnset;
  185.         m_pColumnset = NULL;
  186.     }
  187.  
  188.     if (m_session.m_spOpenRowset != NULL)
  189.         m_session.m_spOpenRowset.Release();
  190.  
  191.     // close the database
  192.     if (!m_strConnect.IsEmpty())
  193.         m_strConnect = "";
  194.  
  195.     // open the database
  196.     if (m_source.Open(AfxGetMainWnd()->GetSafeHwnd()) != S_OK)
  197.     {
  198.         AfxMessageBox(_T("Couldn't connect to data source"));
  199.         m_strConnect = _T("");
  200.         return FALSE;
  201.     }
  202.     else
  203.     {
  204.         USES_CONVERSION;
  205.         if (m_session.Open(m_source) != S_OK)
  206.         {
  207.             AfxMessageBox(_T("Couldn't create session on provider"));
  208.             return FALSE;
  209.         }
  210.         CComVariant var;
  211.         m_source.GetProperty(DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
  212.         m_strConnect = OLE2T(var.bstrVal);
  213.     }
  214.  
  215.     if (FetchTableInfo())
  216.         return TRUE;
  217.     else
  218.         return FALSE;
  219. }
  220.  
  221. void CCatDBDoc::OnViewSettings()
  222. {
  223.     CPropertySheet  sheet(_T("Settings"));
  224.     CTablePage      pageTable;
  225.     CColumnPage     pageColumn;
  226.  
  227.     // initialize and add table settings page
  228.     sheet.AddPage(&pageTable);
  229.     pageTable.m_bSystemTables = m_bSystemTables;
  230.     pageTable.m_bViews = m_bViews;
  231.     pageTable.m_bSynonyms = m_bSynonyms;
  232.  
  233.     // initialize and add column info settings page
  234.     sheet.AddPage(&pageColumn);
  235.     pageColumn.m_bLength = m_bLength;
  236.     pageColumn.m_bPrecision = m_bPrecision;
  237.     pageColumn.m_bNullability = m_bNullability;
  238.  
  239.     // execte property sheet and update settings
  240.     if (sheet.DoModal() == IDOK)
  241.     {
  242.         BOOL    bTableModified = FALSE;
  243.         BOOL    bColumnModified = FALSE;
  244.  
  245.         if (m_bSystemTables != pageTable.m_bSystemTables)
  246.         {
  247.             m_bSystemTables = pageTable.m_bSystemTables;
  248.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  249.                 _T("SystemTables"),m_bSystemTables);
  250.             bTableModified = TRUE;
  251.         }
  252.  
  253.         if (m_bViews != pageTable.m_bViews)
  254.         {
  255.             m_bViews = pageTable.m_bViews;
  256.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  257.                 _T("Views"),m_bViews);
  258.             bTableModified = TRUE;
  259.         }
  260.  
  261.         if (m_bSynonyms != pageTable.m_bSynonyms)
  262.         {
  263.             m_bSynonyms = pageTable.m_bSynonyms;
  264.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  265.                 _T("Synonyms"),m_bSynonyms);
  266.             bTableModified = TRUE;
  267.         }
  268.  
  269.         if (m_bLength != pageColumn.m_bLength)
  270.         {
  271.             m_bLength = pageColumn.m_bLength;
  272.             AfxGetApp()->WriteProfileInt(_T("ColumnInfoSettings"),
  273.                 _T("Length"),m_bLength);
  274.             bColumnModified = TRUE;
  275.         }
  276.  
  277.         if (m_bPrecision != pageColumn.m_bPrecision)
  278.         {
  279.             m_bPrecision = pageColumn.m_bPrecision;
  280.             AfxGetApp()->WriteProfileInt(_T("ColumnInfoSettings"),
  281.                 _T("Precision"),m_bPrecision);
  282.             bColumnModified = TRUE;
  283.         }
  284.  
  285.         if (m_bNullability != pageColumn.m_bNullability)
  286.         {
  287.             m_bNullability = pageColumn.m_bNullability;
  288.             AfxGetApp()->WriteProfileInt(_T("ColumnInfoSettings"),
  289.                 _T("Nullability"),m_bNullability);
  290.             bColumnModified = TRUE;
  291.         }
  292.  
  293.         // check for table modification first
  294.         if (bTableModified && (m_nLevel == CCatDBDoc::levelTable))
  295.         {
  296.             // close and delete any open recordsets
  297.             if (m_pTableset)
  298.             {
  299.                 delete m_pTableset;
  300.                 m_pTableset = 0;
  301.             }
  302.  
  303.             if (m_pColumnset)
  304.             {
  305.                 delete m_pColumnset;
  306.                 m_pColumnset = 0;
  307.             }
  308.  
  309.             // refresh table data
  310.             FetchTableInfo();
  311.             UpdateAllViews(NULL);
  312.         }
  313.  
  314.         // if table settings not modified, check column info
  315.         else if (bColumnModified && (m_nLevel == CCatDBDoc::levelColumn))
  316.         {
  317.             FetchColumnInfo(m_strTableName);
  318.             UpdateAllViews(NULL);
  319.         }
  320.     }
  321. }
  322.  
  323. int CCatDBDoc::GetProfileValue(LPCTSTR lpszSection,LPCTSTR lpszItem)
  324. {
  325.     int nValue = AfxGetApp()->GetProfileInt(lpszSection,lpszItem,-1);
  326.     if (nValue == -1)
  327.     {
  328.         nValue = 0;
  329.         AfxGetApp()->WriteProfileInt(lpszSection,lpszItem,nValue);
  330.     }
  331.     return nValue;
  332. }
  333.  
  334. void CCatDBDoc::OnCloseDocument()
  335. {
  336.     if (m_pTableset)
  337.     {
  338.         delete m_pTableset;
  339.         m_pTableset = 0;
  340.     }
  341.  
  342.     if (m_pColumnset)
  343.     {
  344.         delete m_pColumnset;
  345.         m_pColumnset = 0;
  346.     }
  347.  
  348.     CDocument::OnCloseDocument();
  349. }
  350.