home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / catalog2 / cat2doc.cpp next >
C/C++ Source or Header  |  1998-03-26  |  8KB  |  323 lines

  1. // catalog2Doc.cpp : implementation of the CCatalog2Doc 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 "catalog2.h"
  15.  
  16. #include "catsets.h"
  17. #include "cat2Doc.h"
  18. #include "TabPage.h"
  19. #include "ColPage.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CCatalog2Doc
  28.  
  29. IMPLEMENT_DYNCREATE(CCatalog2Doc, CDocument)
  30.  
  31. BEGIN_MESSAGE_MAP(CCatalog2Doc, CDocument)
  32.     //{{AFX_MSG_MAP(CCatalog2Doc)
  33.     ON_COMMAND(ID_VIEW_SETTINGS, OnViewSettings)
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CCatalog2Doc construction/destruction
  39.  
  40. CCatalog2Doc::CCatalog2Doc()
  41. {
  42.     // initialize member recordset pointers
  43.     m_pTableset = 0;
  44.     m_pColumnset = 0;
  45. }
  46.  
  47. CCatalog2Doc::~CCatalog2Doc()
  48. {
  49. }
  50.  
  51. BOOL CCatalog2Doc::OnNewDocument()
  52. {
  53.     if (!CDocument::OnNewDocument())
  54.         return FALSE;
  55.  
  56.     // initialize current view level
  57.     m_nLevel = levelNone;
  58.  
  59.     // initialize table settings
  60.     m_bSystemTables = GetProfileValue(_T("TableSettings"),_T("SystemTables"));
  61.     m_bViews        = GetProfileValue(_T("TableSettings"),_T("Views"));
  62.     m_bSynonyms     = GetProfileValue(_T("TableSettings"),_T("SystemTables"));
  63.  
  64.     // initialize column info settings
  65.     m_bLength       = GetProfileValue(_T("ColumnInfoSettings"),_T("Length"));
  66.     m_bPrecision    = GetProfileValue(_T("ColumnInfoSettings"),_T("Precision"));
  67.     m_bNullability  = GetProfileValue(_T("ColumnInfoSettings"),_T("Nullability"));
  68.  
  69.     return TRUE;
  70. }
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CCatalog2Doc serialization
  74.  
  75. void CCatalog2Doc::Serialize(CArchive& ar)
  76. {
  77.     if (ar.IsStoring())
  78.     {
  79.         // TODO: add storing code here
  80.     }
  81.     else
  82.     {
  83.         // TODO: add loading code here
  84.     }
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CCatalog2Doc diagnostics
  89.  
  90. #ifdef _DEBUG
  91. void CCatalog2Doc::AssertValid() const
  92. {
  93.     CDocument::AssertValid();
  94. }
  95.  
  96. void CCatalog2Doc::Dump(CDumpContext& dc) const
  97. {
  98.     CDocument::Dump(dc);
  99. }
  100. #endif //_DEBUG
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CCatalog2Doc commands
  104.  
  105. int CCatalog2Doc::GetProfileValue(LPCTSTR lpszSection,LPCTSTR lpszItem)
  106. {
  107.     int nValue = AfxGetApp()->GetProfileInt(lpszSection,lpszItem,-1);
  108.     if (nValue == -1)
  109.     {
  110.         nValue = 0;
  111.         AfxGetApp()->WriteProfileInt(lpszSection,lpszItem,nValue);
  112.     }
  113.     return nValue;
  114. }
  115.  
  116. void CCatalog2Doc::SetLevel(Level nLevel)
  117. {
  118.     m_nLevel = nLevel;
  119.     UpdateAllViews(NULL);
  120. }
  121.  
  122. CString CCatalog2Doc::GetDSN()
  123. {
  124.     if (!m_Database.IsOpen())
  125.         return _T("[No Data Source Selected]");
  126.  
  127.     // pull DSN from database connect string
  128.     CString string = m_Database.GetConnect();
  129.     string = string.Right(string.GetLength() - (string.Find(_T("DSN=")) + 4));
  130.     string = string.Left(string.Find(_T(";")));
  131.     return string;
  132. }
  133.  
  134. BOOL CCatalog2Doc::OnOpenDocument()
  135. {
  136.     // close and delete any open recordsets
  137.     if (m_pTableset)
  138.     {
  139.         if (m_pTableset->IsOpen())
  140.             m_pTableset->Close();
  141.         delete m_pTableset;
  142.         m_pTableset = 0;
  143.     }
  144.     if (m_pColumnset)
  145.     {
  146.         if (m_pColumnset->IsOpen())
  147.             m_pColumnset->Close();
  148.         delete m_pColumnset;
  149.         m_pColumnset = 0;
  150.     }
  151.  
  152.     // close the database
  153.     if (m_Database.IsOpen())
  154.         m_Database.Close();
  155.  
  156.     // open the database
  157.     if (m_Database.Open(NULL,FALSE,TRUE))
  158.     {
  159.         if (FetchTableInfo())
  160.             return TRUE;
  161.     }
  162.     return FALSE;
  163. }
  164.  
  165. void CCatalog2Doc::OnCloseDocument()
  166. {
  167.     if (m_pTableset)
  168.     {
  169.         if (m_pTableset->IsOpen())
  170.             m_pTableset->Close();
  171.         delete m_pTableset;
  172.         m_pTableset = 0;
  173.     }
  174.     if (m_pColumnset)
  175.     {
  176.         if (m_pColumnset->IsOpen())
  177.             m_pColumnset->Close();
  178.         delete m_pColumnset;
  179.         m_pColumnset = 0;
  180.     }
  181.  
  182.     if (m_Database.IsOpen())
  183.         m_Database.Close();
  184.  
  185.     CDocument::OnCloseDocument();
  186. }
  187.  
  188. void CCatalog2Doc::FetchColumnInfo(LPCSTR lpszName)
  189. {
  190.     if (m_pColumnset)
  191.     {
  192.         if (m_pColumnset->IsOpen())
  193.             m_pColumnset->Close();
  194.         delete m_pColumnset;
  195.         m_pColumnset = 0;
  196.     }
  197.     m_pColumnset = new CColumns(&m_Database);
  198.     m_pColumnset->Open(NULL,NULL,lpszName,NULL,CRecordset::snapshot);
  199. }
  200.  
  201. BOOL CCatalog2Doc::FetchTableInfo()
  202. {
  203.     m_pTableset = new CTables(&m_Database);
  204.  
  205.     // Must use char array for ODBC interface
  206.     // (can simply hard code max size)
  207.     char lpszType[64];
  208.  
  209.     strcpy(lpszType, "'TABLE'");
  210.     if (m_bViews)
  211.         strcat(lpszType, ",'VIEW'");
  212.     if (m_bSystemTables)
  213.         strcat(lpszType, ",'SYSTEM TABLE'");
  214.     if (m_bSynonyms)
  215.         strcat(lpszType, ",'ALIAS','SYNONYM'");
  216.  
  217.     if (!m_pTableset->Open(NULL,NULL,NULL,lpszType,CRecordset::snapshot))
  218.     {
  219.         delete m_pTableset;
  220.         m_pTableset = NULL;
  221.         m_Database.Close();
  222.         return FALSE;
  223.     }
  224.     return TRUE;
  225. }
  226.  
  227. void CCatalog2Doc::OnViewSettings()
  228. {
  229.     CPropertySheet  sheet(_T("Settings"));
  230.     CTablePage      pageTable;
  231.     CColumnPage     pageColumn;
  232.  
  233.     // initialize and add table settings page
  234.     sheet.AddPage(&pageTable);
  235.     pageTable.m_bSystemTables = m_bSystemTables;
  236.     pageTable.m_bViews = m_bViews;
  237.     pageTable.m_bSynonyms = m_bSynonyms;
  238.  
  239.     // initialize and add column info settings page
  240.     sheet.AddPage(&pageColumn);
  241.     pageColumn.m_bLength = m_bLength;
  242.     pageColumn.m_bPrecision = m_bPrecision;
  243.     pageColumn.m_bNullability = m_bNullability;
  244.  
  245.     // execte property sheet and update settings
  246.     if (sheet.DoModal() == IDOK) {
  247.         BOOL    bTableModified = FALSE;
  248.         BOOL    bColumnModified = FALSE;
  249.  
  250.         if (m_bSystemTables != pageTable.m_bSystemTables)
  251.         {
  252.             m_bSystemTables = pageTable.m_bSystemTables;
  253.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  254.                 _T("SystemTables"),m_bSystemTables);
  255.             bTableModified = TRUE;
  256.         }
  257.         if (m_bViews != pageTable.m_bViews)
  258.         {
  259.             m_bViews = pageTable.m_bViews;
  260.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  261.                 _T("Views"),m_bViews);
  262.             bTableModified = TRUE;
  263.         }
  264.         if (m_bSynonyms != pageTable.m_bSynonyms)
  265.         {
  266.             m_bSynonyms = pageTable.m_bSynonyms;
  267.             AfxGetApp()->WriteProfileInt(_T("TableSettings"),
  268.                 _T("Synonyms"),m_bSynonyms);
  269.             bTableModified = TRUE;
  270.         }
  271.         if (m_bLength != pageColumn.m_bLength)
  272.         {
  273.             m_bLength = pageColumn.m_bLength;
  274.             AfxGetApp()->WriteProfileInt(_T("ColumnInfoSettings"),
  275.                 _T("Length"),m_bLength);
  276.             bColumnModified = TRUE;
  277.         }
  278.         if (m_bPrecision != pageColumn.m_bPrecision)
  279.         {
  280.             m_bPrecision = pageColumn.m_bPrecision;
  281.             AfxGetApp()->WriteProfileInt(_T("ColumnInfoSettings"),
  282.                 _T("Precision"),m_bPrecision);
  283.             bColumnModified = TRUE;
  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)
  295.         {
  296.             // close and delete any open recordsets
  297.             if (m_pTableset)
  298.             {
  299.                 if (m_pTableset->IsOpen())
  300.                     m_pTableset->Close();
  301.                 delete m_pTableset;
  302.                 m_pTableset = 0;
  303.             }
  304.             if (m_pColumnset)
  305.             {
  306.                 if (m_pColumnset->IsOpen())
  307.                     m_pColumnset->Close();
  308.                 delete m_pColumnset;
  309.                 m_pColumnset = 0;
  310.             }
  311.  
  312.             // refresh table data
  313.             FetchTableInfo();
  314.             SetLevel(levelTable);
  315.             UpdateAllViews(NULL);
  316.         }
  317.  
  318.         // if table settings not modified, check column info
  319.         else if (bColumnModified)
  320.             UpdateAllViews(NULL);
  321.     }
  322. }
  323.