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

  1. // stdreg.cpp : Defines the class behaviors for the application.
  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 "stdreg.h"
  15. #include "typeinfo.h"
  16. #include "dialog.h"
  17. #include "columdlg.h"
  18. #include "coursset.h"
  19. #include "stdset.h"
  20. #include "instrset.h"
  21. #include "sectset.h"
  22. #include "dsectset.h"
  23. #include "enrolset.h"
  24. #include "initdata.h"
  25.  
  26. #ifdef _DEBUG
  27. #undef THIS_FILE
  28. static char BASED_CODE THIS_FILE[] = __FILE__;
  29. #endif
  30.  
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CStdRegSetupApp
  34.  
  35. BEGIN_MESSAGE_MAP(CStdRegSetupApp, CWinApp)
  36.     //{{AFX_MSG_MAP(CStdRegSetupApp)
  37.     //}}AFX_MSG
  38.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CStdRegSetupApp construction
  43.  
  44. CStdRegSetupApp::CStdRegSetupApp()
  45. {
  46. }
  47.  
  48. CStdRegSetupApp::~CStdRegSetupApp()
  49. {
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // The one and only CStdRegSetupApp object
  54.  
  55. CStdRegSetupApp theApp;
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CStdRegSetupApp initialization
  59.  
  60. BOOL CStdRegSetupApp::InitInstance()
  61. {
  62. #ifdef _AFXDLL
  63.     Enable3dControls();         // Call this when using MFC in a shared DLL
  64. #else
  65.     Enable3dControlsStatic();   // Call this when linking to MFC statically
  66. #endif
  67.     LoadStdProfileSettings();
  68.  
  69.     m_mapSQLTypeToSyntax[SQL_VARCHAR] = "varchar(50)";
  70.     m_mapSQLTypeToSyntax[SQL_INTEGER] = "int";
  71.     m_mapSQLTypeToSyntax[SQL_SMALLINT] = "smallint";
  72.  
  73.     CStdRegSetupDlg dlg;
  74.     m_pMainWnd = &dlg;
  75.     int nResponse = dlg.DoModal();
  76.     if (nResponse == IDOK)
  77.     {
  78.     }
  79.     else if (nResponse == IDCANCEL)
  80.     {
  81.     }
  82.  
  83.     return FALSE;
  84. }
  85.  
  86. void CStdRegSetupApp::AddDataSource()
  87. {
  88.     if (AfxMessageBox(IDS_CONFIRM_DB_ADDED_ALREADY, MB_YESNO) != IDYES)
  89.         return;
  90.  
  91.     CString strDSN;
  92.     strDSN.LoadString(IDS_DEFAULT_DATA_SOURCE_NAME);
  93.  
  94.     if (SQLCreateDataSource(m_pMainWnd->m_hWnd, strDSN))
  95.         AfxMessageBox(IDS_DATA_SOURCE_ADDED);
  96.     else
  97.         AfxMessageBox(IDS_DATA_SOURCE_NOT_ADDED);
  98. }
  99.  
  100. BOOL CStdRegSetupApp::GetColumnSyntax()
  101. {
  102.     CColSyntaxDlg dlgColSyntax;
  103.     dlgColSyntax.m_pMapSQLTypeToSyntax = &m_mapSQLTypeToSyntax;
  104.     if (dlgColSyntax.DoModal() != IDOK)
  105.         return FALSE;
  106.     return TRUE;
  107. }
  108.  
  109. void CStdRegSetupApp::InitializeData()
  110. {
  111.     CString strDSN;
  112.     strDSN.LoadString(IDS_DEFAULT_DATA_SOURCE_NAME);
  113.  
  114.     if (!m_db.Open(strDSN))
  115.     {
  116.         AfxMessageBox(IDS_CANNOT_OPEN_DATA_SOURCE);
  117.         return;
  118.     }
  119.  
  120.     if (!GetColumnSyntax())
  121.             return;
  122.  
  123.     BeginWaitCursor();
  124.  
  125.     if (AddCourseTable()
  126.         && AddStudentTable()
  127.         && AddInstructorTable()
  128.         && AddSectionTable()
  129.         && AddDynabindSectionTable()
  130.         && AddEnrollmentTable())
  131.     {
  132.         AfxMessageBox(IDS_INITIALIZATION_COMPLETE);
  133.     }
  134.     else
  135.     {
  136.         AfxMessageBox(IDS_DATA_SOURCE_NOT_INITIALIZED);
  137.     }
  138.  
  139.     ShowProgress(0);
  140.  
  141.     m_db.Close();
  142.  
  143.     EndWaitCursor();
  144. }
  145.  
  146. void CStdRegSetupApp::ShowProgress(int nTablesDone)
  147. {
  148.     CStdRegSetupDlg* pDlg = (CStdRegSetupDlg*)m_pMainWnd;
  149.  
  150.     CString strProgress;
  151.  
  152.     if (nTablesDone > 0)
  153.     {
  154.         CString strProgressFormat;
  155.         strProgressFormat.LoadString(IDS_PROGRESS);
  156.         strProgress.Format(strProgressFormat, nTablesDone, 6);
  157.     }
  158.  
  159.     pDlg->m_ctlProgress.SetWindowText(strProgress);
  160. }
  161.  
  162.  
  163. BOOL CStdRegSetupApp::ExecuteSQLAndReportFailure(const CString& strSQL)
  164. {
  165.     TRY
  166.     {
  167.         m_db.ExecuteSQL(strSQL);
  168.     }
  169.     CATCH(CDBException, e)
  170.     {
  171.         CString strMsg;
  172.         strMsg.LoadString(IDS_EXECUTE_SQL_FAILED);
  173.         strMsg += strSQL;
  174.         return FALSE;
  175.     }
  176.     END_CATCH
  177.     return TRUE;
  178. }
  179.  
  180. BOOL CStdRegSetupApp::DropThenAddTable(const CString& strTableName, const CString& strColumns)
  181. {
  182.     CString strSQL;
  183.     TRY
  184.     {
  185.         strSQL = "DROP TABLE ";
  186.         strSQL += strTableName;
  187.         m_db.ExecuteSQL(strSQL);  // failure is ok, for example, if table doesn't already exist
  188.     }
  189.     CATCH(CDBException,e)
  190.     {
  191.         // It is ok if table does not already exist.
  192.     }
  193.     END_CATCH
  194.  
  195.     strSQL = "CREATE TABLE ";
  196.     strSQL += strTableName;
  197.     strSQL += '(';
  198.     strSQL += strColumns;
  199.     strSQL += ')';
  200.     return ExecuteSQLAndReportFailure(strSQL);
  201. }
  202.  
  203. void CStdRegSetupApp::AddColumn(CString& strColumns, LPCSTR lpszColumnName, SWORD fSqlType,
  204.     LPCSTR lpszColLength)
  205. {
  206.     if (!strColumns.IsEmpty())
  207.         strColumns += ',';
  208.     strColumns += lpszColumnName;
  209.  
  210.     CString strDataType;
  211.     VERIFY(m_mapSQLTypeToSyntax.Lookup(fSqlType,strDataType));
  212.  
  213.     if (fSqlType == SQL_VARCHAR)
  214.     {
  215.         // The Column Syntax dialog instructed the user to specify
  216.         // the syntax for a varchar with a maximum length of 50.
  217.         // Replace the "50" with the column-specific length.
  218.         BOOL b50Found = FALSE;
  219.  
  220.         ASSERT(lpszColLength != NULL);
  221.         CString strDataTypeWith50Replaced;
  222.         for (int nPos = 0; nPos < strDataType.GetLength(); nPos++)
  223.         {
  224.             if (strDataType[nPos] == '5' && strDataType[nPos+1] == '0')
  225.             {
  226.                 strDataTypeWith50Replaced += lpszColLength;
  227.                 nPos += 1;
  228.                 b50Found = TRUE;
  229.             }
  230.             else
  231.             {
  232.                 strDataTypeWith50Replaced += strDataType[nPos];
  233.             }
  234.         }
  235.         if (b50Found)
  236.             strDataType = strDataTypeWith50Replaced;
  237.     }
  238.  
  239.     strColumns += ' ';
  240.     strColumns += strDataType;
  241. }
  242.  
  243. BOOL CStdRegSetupApp::AddCourseTable()
  244. {
  245.     ShowProgress(1);
  246.  
  247.     CString strTableName = "COURSE";
  248.     CString strColumns;
  249.  
  250.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  251.     AddColumn(strColumns, "CourseTitle", SQL_VARCHAR, "50");
  252.     AddColumn(strColumns, "Hours", SQL_SMALLINT);
  253.  
  254.     if (!DropThenAddTable(strTableName, strColumns))
  255.         return FALSE;
  256.  
  257.     CCourseSet setCourse(&m_db);
  258.     setCourse.Open();
  259.  
  260.     for (int nIndex = 0; nIndex < sizeof(courseData)/sizeof(CCourseData); nIndex++)
  261.     {
  262.         setCourse.AddNew();
  263.  
  264.         setCourse.m_CourseID    = courseData[nIndex].m_CourseID;
  265.         setCourse.m_CourseTitle = courseData[nIndex].m_CourseTitle;
  266.         setCourse.m_Hours       = courseData[nIndex].m_Hours;
  267.  
  268.         setCourse.Update();
  269.     }
  270.  
  271.     setCourse.Close();
  272.  
  273.     return TRUE;
  274. }
  275.  
  276. BOOL CStdRegSetupApp::AddStudentTable()
  277. {
  278.     ShowProgress(2);
  279.  
  280.     CString strTableName = "STUDENT";
  281.     CString strColumns;
  282.  
  283.     AddColumn(strColumns, "StudentID", SQL_INTEGER);
  284.     AddColumn(strColumns, "Name", SQL_VARCHAR, "40");
  285.     AddColumn(strColumns, "GradYear", SQL_SMALLINT);
  286.  
  287.     if (!DropThenAddTable(strTableName, strColumns))
  288.         return FALSE;
  289.  
  290.     CStudentSet setStudent(&m_db);
  291.     setStudent.Open();
  292.  
  293.     for (int nIndex = 0; nIndex < sizeof(studentData)/sizeof(CStudentData); nIndex++)
  294.     {
  295.         setStudent.AddNew();
  296.  
  297.         setStudent.m_StudentID   = studentData[nIndex].m_StudentID;
  298.         setStudent.m_Name        = studentData[nIndex].m_Name;
  299.         setStudent.m_GradYear    = studentData[nIndex].m_GradYear;
  300.  
  301.         setStudent.Update();
  302.     }
  303.  
  304.     setStudent.Close();
  305.  
  306.     return TRUE;
  307. }
  308.  
  309. BOOL CStdRegSetupApp::AddInstructorTable()
  310. {
  311.     ShowProgress(3);
  312.  
  313.     CString strTableName = "INSTRUCTOR";
  314.     CString strColumns;
  315.  
  316.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  317.     AddColumn(strColumns, "Name", SQL_VARCHAR, "40");
  318.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  319.  
  320.     if (!DropThenAddTable(strTableName, strColumns))
  321.         return FALSE;
  322.  
  323.     CInstructorSet setInstructor(&m_db);
  324.     setInstructor.Open();
  325.  
  326.     for (int nIndex = 0; nIndex < sizeof(instructorData)/sizeof(CInstructorData); nIndex++)
  327.     {
  328.         setInstructor.AddNew();
  329.  
  330.         setInstructor.m_InstructorID = instructorData[nIndex].m_InstructorID;
  331.         setInstructor.m_Name         = instructorData[nIndex].m_Name;
  332.         setInstructor.m_RoomNo       = instructorData[nIndex].m_RoomNo;
  333.  
  334.         setInstructor.Update();
  335.     }
  336.  
  337.     setInstructor.Close();
  338.  
  339.  
  340.     return TRUE;
  341. }
  342.  
  343. BOOL CStdRegSetupApp::AddSectionTable()
  344. {
  345.     ShowProgress(4);
  346.  
  347.     CString strTableName = "SECTION";
  348.     CString strColumns;
  349.  
  350.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  351.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  352.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  353.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  354.     AddColumn(strColumns, "Schedule", SQL_VARCHAR, "24");
  355.     AddColumn(strColumns, "Capacity", SQL_SMALLINT);
  356.  
  357.     if (!DropThenAddTable(strTableName, strColumns))
  358.         return FALSE;
  359.  
  360.     CSectionSet setSection(&m_db);
  361.     setSection.Open();
  362.  
  363.     for (int nIndex = 0; nIndex < sizeof(sectionData)/sizeof(CSectionData); nIndex++)
  364.     {
  365.         setSection.AddNew();
  366.  
  367.         setSection.m_CourseID       =sectionData[nIndex].m_CourseID;
  368.         setSection.m_SectionNo      =sectionData[nIndex].m_SectionNo;
  369.         setSection.m_InstructorID   =sectionData[nIndex].m_InstructorID;
  370.         setSection.m_RoomNo         =sectionData[nIndex].m_RoomNo;
  371.         setSection.m_Schedule       =sectionData[nIndex].m_Schedule;
  372.         setSection.m_Capacity       =sectionData[nIndex].m_Capacity;
  373.  
  374.         setSection.Update();
  375.     }
  376.  
  377.     setSection.Close();
  378.  
  379.     return TRUE;
  380. }
  381.  
  382. BOOL CStdRegSetupApp::AddDynabindSectionTable()
  383. {
  384.     ShowProgress(5);
  385.  
  386.     CString strTableName = "DYNABIND_SECTION";
  387.     CString strColumns;
  388.  
  389.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  390.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  391.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  392.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  393.     AddColumn(strColumns, "Schedule", SQL_VARCHAR, "24");
  394.     AddColumn(strColumns, "Capacity", SQL_SMALLINT);
  395.     AddColumn(strColumns, "LabRoomNo", SQL_VARCHAR, "10");
  396.     AddColumn(strColumns, "LabSchedule", SQL_VARCHAR, "24");
  397.  
  398.     if (!DropThenAddTable(strTableName,strColumns))
  399.         return FALSE;
  400.  
  401.     CDynabindSectionSet setDynabindSection(&m_db);
  402.     setDynabindSection.Open();
  403.  
  404.     for (int nIndex = 0; nIndex < sizeof(dynabindSectionData)/sizeof(CDynabindSectionData); nIndex++)
  405.     {
  406.         setDynabindSection.AddNew();
  407.  
  408.         setDynabindSection.m_CourseID     = dynabindSectionData[nIndex].m_CourseID;
  409.         setDynabindSection.m_SectionNo    = dynabindSectionData[nIndex].m_SectionNo;
  410.         setDynabindSection.m_InstructorID = dynabindSectionData[nIndex].m_InstructorID;
  411.         setDynabindSection.m_RoomNo       = dynabindSectionData[nIndex].m_RoomNo;
  412.         setDynabindSection.m_Schedule     = dynabindSectionData[nIndex].m_Schedule;
  413.         setDynabindSection.m_Capacity     = dynabindSectionData[nIndex].m_Capacity;
  414.         setDynabindSection.m_LabRoomNo    = dynabindSectionData[nIndex].m_LabRoomNo;
  415.         setDynabindSection.m_LabSchedule  = dynabindSectionData[nIndex].m_LabSchedule;
  416.  
  417.         setDynabindSection.Update();
  418.     }
  419.  
  420.     setDynabindSection.Close();
  421.  
  422.     return TRUE;
  423. }
  424.  
  425. BOOL CStdRegSetupApp::AddEnrollmentTable()
  426. {
  427.     ShowProgress(6);
  428.  
  429.     CString strTableName = "ENROLLMENT";
  430.     CString strColumns;
  431.  
  432.     AddColumn(strColumns, "StudentID", SQL_INTEGER);
  433.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  434.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  435.     AddColumn(strColumns, "Grade", SQL_VARCHAR, "1");
  436.  
  437.     if (!DropThenAddTable(strTableName,strColumns))
  438.         return FALSE;
  439.  
  440.     CEnrollmentSet setEnrollment(&m_db);
  441.     setEnrollment.Open();
  442.  
  443.     for (int nIndex = 0; nIndex < sizeof(enrollmentData)/sizeof(CEnrollmentData); nIndex++)
  444.     {
  445.         setEnrollment.AddNew();
  446.  
  447.         setEnrollment.m_StudentID      = enrollmentData[nIndex].m_StudentID;
  448.         setEnrollment.m_CourseID       = enrollmentData[nIndex].m_CourseID;
  449.         setEnrollment.m_SectionNo      = enrollmentData[nIndex].m_SectionNo;
  450.         setEnrollment.m_Grade          = enrollmentData[nIndex].m_Grade;
  451.  
  452.         setEnrollment.Update();
  453.     }
  454.  
  455.     setEnrollment.Close();
  456.  
  457.     return TRUE;
  458. }
  459.