home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / STDREG.PAK / STDREG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.5 KB  |  463 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-1995 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.         exit;
  90.  
  91.     CString strDSN;
  92.     strDSN.LoadString(IDS_DEFAULT_DATA_SOURCE_NAME);
  93.  
  94.     #ifndef _MAC
  95.     if (SQLCreateDataSource(m_pMainWnd->m_hWnd, strDSN))
  96.     #else
  97.     if (SQLCreateDataSource(GetWrapperWindow(m_pMainWnd->m_hWnd), strDSN))
  98.     #endif
  99.         AfxMessageBox(IDS_DATA_SOURCE_ADDED);
  100.     else
  101.         AfxMessageBox(IDS_DATA_SOURCE_NOT_ADDED);
  102. }
  103.  
  104. BOOL CStdRegSetupApp::GetColumnSyntax()
  105. {
  106.     CColSyntaxDlg dlgColSyntax;
  107.     dlgColSyntax.m_pMapSQLTypeToSyntax = &m_mapSQLTypeToSyntax;
  108.     if (dlgColSyntax.DoModal() != IDOK)
  109.         return FALSE;
  110.     return TRUE;
  111. }
  112.  
  113. void CStdRegSetupApp::InitializeData()
  114. {
  115.     CString strDSN;
  116.     strDSN.LoadString(IDS_DEFAULT_DATA_SOURCE_NAME);
  117.  
  118.     if (!m_db.Open(strDSN))
  119.     {
  120.         AfxMessageBox(IDS_CANNOT_OPEN_DATA_SOURCE);
  121.         return;
  122.     }
  123.  
  124.     if (!GetColumnSyntax())
  125.             return;
  126.  
  127.     BeginWaitCursor();
  128.  
  129.     if (AddCourseTable()
  130.         && AddStudentTable()
  131.         && AddInstructorTable()
  132.         && AddSectionTable()
  133.         && AddDynabindSectionTable()
  134.         && AddEnrollmentTable())
  135.     {
  136.         AfxMessageBox(IDS_INITIALIZATION_COMPLETE);
  137.     }
  138.     else
  139.     {
  140.         AfxMessageBox(IDS_DATA_SOURCE_NOT_INITIALIZED);
  141.     }
  142.  
  143.     ShowProgress(0);
  144.  
  145.     m_db.Close();
  146.  
  147.     EndWaitCursor();
  148. }
  149.  
  150. void CStdRegSetupApp::ShowProgress(int nTablesDone)
  151. {
  152.     CStdRegSetupDlg* pDlg = (CStdRegSetupDlg*)m_pMainWnd;
  153.  
  154.     CString strProgress;
  155.  
  156.     if (nTablesDone > 0)
  157.     {
  158.         CString strProgressFormat;
  159.         strProgressFormat.LoadString(IDS_PROGRESS);
  160.         strProgress.Format(strProgressFormat, nTablesDone, 6);
  161.     }
  162.  
  163.     pDlg->m_ctlProgress.SetWindowText(strProgress);
  164. }
  165.  
  166.  
  167. BOOL CStdRegSetupApp::ExecuteSQLAndReportFailure(const CString& strSQL)
  168. {
  169.     TRY
  170.     {
  171.         m_db.ExecuteSQL(strSQL);
  172.     }
  173.     CATCH(CDBException, e)
  174.     {
  175.         CString strMsg;
  176.         strMsg.LoadString(IDS_EXECUTE_SQL_FAILED);
  177.         strMsg += strSQL;
  178.         return FALSE;
  179.     }
  180.     END_CATCH
  181.     return TRUE;
  182. }
  183.  
  184. BOOL CStdRegSetupApp::DropThenAddTable(const CString& strTableName, const CString& strColumns)
  185. {
  186.     CString strSQL;
  187.     TRY
  188.     {
  189.         strSQL = "DROP TABLE ";
  190.         strSQL += strTableName;
  191.         m_db.ExecuteSQL(strSQL);  // failure is ok, for example, if table doesn't already exist
  192.     }
  193.     CATCH(CDBException,e)
  194.     {
  195.         // It is ok if table does not already exist.
  196.     }
  197.     END_CATCH
  198.  
  199.     strSQL = "CREATE TABLE ";
  200.     strSQL += strTableName;
  201.     strSQL += '(';
  202.     strSQL += strColumns;
  203.     strSQL += ')';
  204.     return ExecuteSQLAndReportFailure(strSQL);
  205. }
  206.  
  207. void CStdRegSetupApp::AddColumn(CString& strColumns, LPCSTR lpszColumnName, SWORD fSqlType,
  208.     LPCSTR lpszColLength)
  209. {
  210.     if (!strColumns.IsEmpty())
  211.         strColumns += ',';
  212.     strColumns += lpszColumnName;
  213.  
  214.     CString strDataType;
  215.     VERIFY(m_mapSQLTypeToSyntax.Lookup(fSqlType,strDataType));
  216.  
  217.     if (fSqlType == SQL_VARCHAR)
  218.     {
  219.         // The Column Syntax dialog instructed the user to specify
  220.         // the syntax for a varchar with a maximum length of 50.
  221.         // Replace the "50" with the column-specific length.
  222.         BOOL b50Found = FALSE;
  223.  
  224.         ASSERT(lpszColLength != NULL);
  225.         CString strDataTypeWith50Replaced;
  226.         for (int nPos = 0; nPos < strDataType.GetLength(); nPos++)
  227.         {
  228.             if (strDataType[nPos] == '5' && strDataType[nPos+1] == '0')
  229.             {
  230.                 strDataTypeWith50Replaced += lpszColLength;
  231.                 nPos += 1;
  232.                 b50Found = TRUE;
  233.             }
  234.             else
  235.             {
  236.                 strDataTypeWith50Replaced += strDataType[nPos];
  237.             }
  238.         }
  239.         if (b50Found)
  240.             strDataType = strDataTypeWith50Replaced;
  241.     }
  242.  
  243.     strColumns += ' ';
  244.     strColumns += strDataType;
  245. }
  246.  
  247. BOOL CStdRegSetupApp::AddCourseTable()
  248. {
  249.     ShowProgress(1);
  250.  
  251.     CString strTableName = "COURSE";
  252.     CString strColumns;
  253.  
  254.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  255.     AddColumn(strColumns, "CourseTitle", SQL_VARCHAR, "50");
  256.     AddColumn(strColumns, "Hours", SQL_SMALLINT);
  257.  
  258.     if (!DropThenAddTable(strTableName, strColumns))
  259.         return FALSE;
  260.  
  261.     CCourseSet setCourse(&m_db);
  262.     setCourse.Open();
  263.  
  264.     for (int nIndex = 0; nIndex < sizeof(courseData)/sizeof(CCourseData); nIndex++)
  265.     {
  266.         setCourse.AddNew();
  267.  
  268.         setCourse.m_CourseID    = courseData[nIndex].m_CourseID;
  269.         setCourse.m_CourseTitle = courseData[nIndex].m_CourseTitle;
  270.         setCourse.m_Hours       = courseData[nIndex].m_Hours;
  271.  
  272.         setCourse.Update();
  273.     }
  274.  
  275.     setCourse.Close();
  276.  
  277.     return TRUE;
  278. }
  279.  
  280. BOOL CStdRegSetupApp::AddStudentTable()
  281. {
  282.     ShowProgress(2);
  283.  
  284.     CString strTableName = "STUDENT";
  285.     CString strColumns;
  286.  
  287.     AddColumn(strColumns, "StudentID", SQL_INTEGER);
  288.     AddColumn(strColumns, "Name", SQL_VARCHAR, "40");
  289.     AddColumn(strColumns, "GradYear", SQL_SMALLINT);
  290.  
  291.     if (!DropThenAddTable(strTableName, strColumns))
  292.         return FALSE;
  293.  
  294.     CStudentSet setStudent(&m_db);
  295.     setStudent.Open();
  296.  
  297.     for (int nIndex = 0; nIndex < sizeof(studentData)/sizeof(CStudentData); nIndex++)
  298.     {
  299.         setStudent.AddNew();
  300.  
  301.         setStudent.m_StudentID   = studentData[nIndex].m_StudentID;
  302.         setStudent.m_Name        = studentData[nIndex].m_Name;
  303.         setStudent.m_GradYear    = studentData[nIndex].m_GradYear;
  304.  
  305.         setStudent.Update();
  306.     }
  307.  
  308.     setStudent.Close();
  309.  
  310.     return TRUE;
  311. }
  312.  
  313. BOOL CStdRegSetupApp::AddInstructorTable()
  314. {
  315.     ShowProgress(3);
  316.  
  317.     CString strTableName = "INSTRUCTOR";
  318.     CString strColumns;
  319.  
  320.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  321.     AddColumn(strColumns, "Name", SQL_VARCHAR, "40");
  322.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  323.  
  324.     if (!DropThenAddTable(strTableName, strColumns))
  325.         return FALSE;
  326.  
  327.     CInstructorSet setInstructor(&m_db);
  328.     setInstructor.Open();
  329.  
  330.     for (int nIndex = 0; nIndex < sizeof(instructorData)/sizeof(CInstructorData); nIndex++)
  331.     {
  332.         setInstructor.AddNew();
  333.  
  334.         setInstructor.m_InstructorID = instructorData[nIndex].m_InstructorID;
  335.         setInstructor.m_Name         = instructorData[nIndex].m_Name;
  336.         setInstructor.m_RoomNo       = instructorData[nIndex].m_RoomNo;
  337.  
  338.         setInstructor.Update();
  339.     }
  340.  
  341.     setInstructor.Close();
  342.  
  343.  
  344.     return TRUE;
  345. }
  346.  
  347. BOOL CStdRegSetupApp::AddSectionTable()
  348. {
  349.     ShowProgress(4);
  350.  
  351.     CString strTableName = "SECTION";
  352.     CString strColumns;
  353.  
  354.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  355.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  356.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  357.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  358.     AddColumn(strColumns, "Schedule", SQL_VARCHAR, "24");
  359.     AddColumn(strColumns, "Capacity", SQL_SMALLINT);
  360.  
  361.     if (!DropThenAddTable(strTableName, strColumns))
  362.         return FALSE;
  363.  
  364.     CSectionSet setSection(&m_db);
  365.     setSection.Open();
  366.  
  367.     for (int nIndex = 0; nIndex < sizeof(sectionData)/sizeof(CSectionData); nIndex++)
  368.     {
  369.         setSection.AddNew();
  370.  
  371.         setSection.m_CourseID       =sectionData[nIndex].m_CourseID;
  372.         setSection.m_SectionNo      =sectionData[nIndex].m_SectionNo;
  373.         setSection.m_InstructorID   =sectionData[nIndex].m_InstructorID;
  374.         setSection.m_RoomNo         =sectionData[nIndex].m_RoomNo;
  375.         setSection.m_Schedule       =sectionData[nIndex].m_Schedule;
  376.         setSection.m_Capacity       =sectionData[nIndex].m_Capacity;
  377.  
  378.         setSection.Update();
  379.     }
  380.  
  381.     setSection.Close();
  382.  
  383.     return TRUE;
  384. }
  385.  
  386. BOOL CStdRegSetupApp::AddDynabindSectionTable()
  387. {
  388.     ShowProgress(5);
  389.  
  390.     CString strTableName = "DYNABIND_SECTION";
  391.     CString strColumns;
  392.  
  393.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  394.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  395.     AddColumn(strColumns, "InstructorID", SQL_VARCHAR, "8");
  396.     AddColumn(strColumns, "RoomNo", SQL_VARCHAR, "10");
  397.     AddColumn(strColumns, "Schedule", SQL_VARCHAR, "24");
  398.     AddColumn(strColumns, "Capacity", SQL_SMALLINT);
  399.     AddColumn(strColumns, "LabRoomNo", SQL_VARCHAR, "10");
  400.     AddColumn(strColumns, "LabSchedule", SQL_VARCHAR, "24");
  401.  
  402.     if (!DropThenAddTable(strTableName,strColumns))
  403.         return FALSE;
  404.  
  405.     CDynabindSectionSet setDynabindSection(&m_db);
  406.     setDynabindSection.Open();
  407.  
  408.     for (int nIndex = 0; nIndex < sizeof(dynabindSectionData)/sizeof(CDynabindSectionData); nIndex++)
  409.     {
  410.         setDynabindSection.AddNew();
  411.  
  412.         setDynabindSection.m_CourseID     = dynabindSectionData[nIndex].m_CourseID;
  413.         setDynabindSection.m_SectionNo    = dynabindSectionData[nIndex].m_SectionNo;
  414.         setDynabindSection.m_InstructorID = dynabindSectionData[nIndex].m_InstructorID;
  415.         setDynabindSection.m_RoomNo       = dynabindSectionData[nIndex].m_RoomNo;
  416.         setDynabindSection.m_Schedule     = dynabindSectionData[nIndex].m_Schedule;
  417.         setDynabindSection.m_Capacity     = dynabindSectionData[nIndex].m_Capacity;
  418.         setDynabindSection.m_LabRoomNo    = dynabindSectionData[nIndex].m_LabRoomNo;
  419.         setDynabindSection.m_LabSchedule  = dynabindSectionData[nIndex].m_LabSchedule;
  420.  
  421.         setDynabindSection.Update();
  422.     }
  423.  
  424.     setDynabindSection.Close();
  425.  
  426.     return TRUE;
  427. }
  428.  
  429. BOOL CStdRegSetupApp::AddEnrollmentTable()
  430. {
  431.     ShowProgress(6);
  432.  
  433.     CString strTableName = "ENROLLMENT";
  434.     CString strColumns;
  435.  
  436.     AddColumn(strColumns, "StudentID", SQL_INTEGER);
  437.     AddColumn(strColumns, "CourseID", SQL_VARCHAR, "8");
  438.     AddColumn(strColumns, "SectionNo", SQL_VARCHAR, "4");
  439.     AddColumn(strColumns, "Grade", SQL_VARCHAR, "1");
  440.  
  441.     if (!DropThenAddTable(strTableName,strColumns))
  442.         return FALSE;
  443.  
  444.     CEnrollmentSet setEnrollment(&m_db);
  445.     setEnrollment.Open();
  446.  
  447.     for (int nIndex = 0; nIndex < sizeof(enrollmentData)/sizeof(CEnrollmentData); nIndex++)
  448.     {
  449.         setEnrollment.AddNew();
  450.  
  451.         setEnrollment.m_StudentID      = enrollmentData[nIndex].m_StudentID;
  452.         setEnrollment.m_CourseID       = enrollmentData[nIndex].m_CourseID;
  453.         setEnrollment.m_SectionNo      = enrollmentData[nIndex].m_SectionNo;
  454.         setEnrollment.m_Grade          = enrollmentData[nIndex].m_Grade;
  455.  
  456.         setEnrollment.Update();
  457.     }
  458.  
  459.     setEnrollment.Close();
  460.  
  461.     return TRUE;
  462. }
  463.