home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CATALG2.PAK / CAT2DOC.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.6 KB  |  324 lines

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