home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c10 / lab02 / baseline / mdbdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.9 KB  |  179 lines

  1. // MDBDoc.cpp : implementation of the CMDBDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MDB.h"
  6.  
  7. #include "MDBDoc.h"
  8. #include "MDBView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMDBDoc
  18.  
  19. IMPLEMENT_DYNCREATE(CMDBDoc, CDocument)
  20.  
  21. BEGIN_MESSAGE_MAP(CMDBDoc, CDocument)
  22.     //{{AFX_MSG_MAP(CMDBDoc)
  23.     ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
  24.     //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMDBDoc construction/destruction
  29.  
  30. CMDBDoc::CMDBDoc()
  31. {
  32.     // TODO: add one-time construction code here
  33.  
  34. }
  35.  
  36. CMDBDoc::~CMDBDoc()
  37. {
  38. }
  39.  
  40. BOOL CMDBDoc::OnNewDocument()
  41. {
  42.     if (!CDocument::OnNewDocument())
  43.         return FALSE;
  44.  
  45.     // TODO: add reinitialization code here
  46.     // (SDI documents will reuse this document)
  47.  
  48.     POSITION pos = GetFirstViewPosition( );
  49.     CTreeView * pView = ( CTreeView * ) GetNextView( pos );
  50.     CTreeCtrl & tree = pView->GetTreeCtrl( );
  51.     tree.DeleteAllItems( );
  52.     SetModifiedFlag( FALSE );
  53.  
  54.     return TRUE;
  55. }
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CMDBDoc serialization
  59.  
  60. void CMDBDoc::Serialize(CArchive& ar)
  61. {
  62.     if (ar.IsStoring())
  63.     {
  64.         // TODO: add storing code here
  65.     }
  66.     else
  67.     {
  68.         // TODO: add loading code here
  69.     }
  70. }
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CMDBDoc diagnostics
  74.  
  75. #ifdef _DEBUG
  76. void CMDBDoc::AssertValid() const
  77. {
  78.     CDocument::AssertValid();
  79. }
  80.  
  81. void CMDBDoc::Dump(CDumpContext& dc) const
  82. {
  83.     CDocument::Dump(dc);
  84. }
  85. #endif //_DEBUG
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CMDBDoc commands
  89.  
  90.  
  91. BOOL CMDBDoc::SaveModified() 
  92. {
  93.     // TODO: Add your specialized code here and/or call the base class
  94.     if ( ! IsModified( ) )
  95.         return TRUE;
  96.     int answer = AfxMessageBox( "Create database before closing?",
  97.                         MB_YESNOCANCEL | MB_ICONQUESTION );
  98.     if ( IDCANCEL == answer )
  99.         return FALSE;
  100.     if ( IDYES == answer )
  101.     {
  102.         OnFileSaveAs( );    //This handler knows what to do
  103.         return ! IsModified( );  //In case of exception
  104.     }
  105.     return TRUE;    //No, don't save it
  106. }
  107.  
  108. void CMDBDoc::OnFileSaveAs() 
  109. {
  110.     // TODO: Add your command handler code here
  111.     
  112.     POSITION pos = GetFirstViewPosition( );
  113.     CMDBView * pView = ( CMDBView * ) GetNextView( pos );
  114.     if ( 0 == pView->GetTreeCtrl( ).GetCount( ) )    //No tables in the tree
  115.         if ( IDNO == 
  116.             AfxMessageBox("Create empty database?", 
  117.                     MB_YESNO | MB_ICONQUESTION ) )
  118.         {
  119.             SetModifiedFlag( FALSE );
  120.             return;  
  121.         }
  122.  
  123.     CString strDatabaseName;
  124.     CFileDialog dlg( FALSE, "mdb", NULL, OFN_HIDEREADONLY,
  125.         "Access database|*.mdb||" );
  126.  
  127.     if ( IDOK != dlg.DoModal( ) )  //User cancelled, nothing to do
  128.         return;
  129.  
  130.     CString strTableName, strFieldName;
  131.     int nFieldType, nTextFieldLength;
  132.  
  133.     HTREEITEM hTableItem;
  134.  
  135.     try
  136.     {    
  137.         //Exercise 1: Create database
  138.         
  139.         while ( NULL !=            //While there is a table
  140.             ( hTableItem = pView->TableNameIterator( strTableName ) ) )
  141.         {
  142.             //Exercise 1: Define tabledef object
  143.             //Exercise 1: Create tabledef
  144.  
  145.             while ( NULL !=        //While there is a field
  146.                 pView->FieldInfoIterator( strFieldName, nFieldType, nTextFieldLength, hTableItem ) )
  147.             {
  148.  
  149.                 //Exercise 1: Determine data type constant to use
  150.                 //Exercise 1: Create a field in the table
  151.  
  152.             } //End while there is a field
  153.  
  154.             //Exercise 1: Append the table definition
  155.             //Exercise 1: Close the table definition
  156.         }//End while there is a table
  157.         
  158.         //Exercise 1: Close the database
  159.         //Exercise 1: Reset the modified flag
  160.     }
  161.     catch ( CDaoException * ex )
  162.     {
  163.         ex->ReportError( );
  164.          try
  165.         {
  166.             //Exercise 1: Close the database
  167.             //Exercise 1: Remove database file if not error 3204
  168.         }
  169.         catch ( CException * exInner )
  170.         {
  171.             exInner->ReportError( );
  172.             exInner->Delete(  );
  173.         }    
  174.         ex->Delete( );
  175.     }
  176. }
  177.  
  178.  
  179.