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

  1. // AddIndexDlg.cpp : implementation file--dialog to let user specify
  2. // querydefs
  3. //
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 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 "DAOTable.h"
  16. #include "AddQyDlg.h"
  17. #include "querydef.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CAddQueryDlg dialog
  27.  
  28. // default constructor
  29. CAddQueryDlg::CAddQueryDlg(CWnd* pParent /*=NULL*/)
  30.     : CDialog(CAddQueryDlg::IDD, pParent)
  31. {
  32.     // call centralized initialization function
  33.     initializer();
  34. }
  35.  
  36.  
  37. // constructor that will generally be called to create the dialog
  38. //
  39. // IN: pDatabase--pointer to an open database object
  40. // IN: strQueryName--name of query to add or view
  41. // IN: pParent--pointer to parent of the dialog
  42. //
  43. CAddQueryDlg::CAddQueryDlg(CDaoDatabase *pDatabase, CString strQueryName, CWnd* pParent)
  44.     : CDialog(CAddQueryDlg::IDD, pParent)
  45. {
  46.     // call centralized initialization function
  47.     initializer();
  48.  
  49.     // initialize and set members to incoming parameters
  50.     m_pDatabase = pDatabase;
  51.     m_pQueryDef = NULL;
  52.     m_QI.m_strName = strQueryName;
  53. }
  54.  
  55. // initialize members of the class and the querydef info struct
  56. void CAddQueryDlg::initializer()
  57. {
  58.     //{{AFX_DATA_INIT(CAddQueryDlg)
  59.     //}}AFX_DATA_INIT
  60.  
  61.     // querydef info struct
  62.     m_QI.m_strName = _T("");
  63.     m_QI.m_bUpdatable = FALSE;
  64.     m_QI.m_strSQL = _T("");
  65. }
  66.  
  67.  
  68. void CAddQueryDlg::DoDataExchange(CDataExchange* pDX)
  69. {
  70.     CDialog::DoDataExchange(pDX);
  71.     //{{AFX_DATA_MAP(CAddQueryDlg)
  72.     //}}AFX_DATA_MAP
  73.  
  74.     // since mapping directly to query info struct, must be
  75.     // outside of wizard block
  76.     DDX_Text(pDX, IDC_TABLE_NAME, m_QI.m_strName);
  77.     DDX_Check(pDX, IDC_UPDATABLE, m_QI.m_bUpdatable);
  78.     DDX_Text(pDX, IDC_SQL, m_QI.m_strSQL);
  79. }
  80.  
  81.  
  82. BEGIN_MESSAGE_MAP(CAddQueryDlg, CDialog)
  83.     //{{AFX_MSG_MAP(CAddQueryDlg)
  84.     ON_BN_CLICKED(IDC_ADD_QUERY_DEF, OnAddQueryDef)
  85.     ON_BN_CLICKED(IDC_DELETE_QUERYDEF, OnDeleteQuerydef)
  86.     ON_BN_CLICKED(IDOK, OnDone)
  87.     ON_BN_CLICKED(IDC_MODIFY_QUERY_DEF, OnModifyQueryDef)
  88.     //}}AFX_MSG_MAP
  89. END_MESSAGE_MAP()
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CAddQueryDlg message handlers
  93.  
  94. BOOL CAddQueryDlg::OnInitDialog()
  95. {
  96.     CDialog::OnInitDialog();
  97.  
  98.     // user either specified new query or existing query
  99.     if(IsExistentQuery(m_pDatabase, m_QI.m_strName))
  100.     {
  101.         // user specified existing query, so populate
  102.         // dialog with information from the query
  103.         //
  104.         // get querydef info by name
  105.         getQueryInfo(m_pDatabase, &m_QI, -1, FALSE);
  106.  
  107.         // now try to open the querydef
  108.         openQueryDef(m_pDatabase, &m_pQueryDef, m_QI.m_strName);
  109.  
  110.         UpdateData(FALSE);
  111.     }
  112.  
  113.     // set focus to the SQL edit box
  114.     CEdit *pEdit = (CEdit *)GetDlgItem(IDC_SQL);
  115.     pEdit->SetFocus();
  116.  
  117.     return FALSE;  // return TRUE unless you set the focus to a control
  118. }
  119.  
  120. // user selected to add the specified querydef to the collection of
  121. // querydefs
  122. void CAddQueryDlg::OnAddQueryDef()
  123. {
  124.     // get data from the dialog
  125.     UpdateData(TRUE);
  126.  
  127.     // cache any existing querydef
  128.     CDaoQueryDef *pCacheQueryDef = m_pQueryDef;
  129.     if (m_pQueryDef != NULL)
  130.         m_pQueryDef = NULL;
  131.  
  132.     // create the querydef and append it to the collection
  133.     m_pQueryDef = new CDaoQueryDef(m_pDatabase);
  134.  
  135.     // if success on creating and saving new querydef, then proceed with
  136.     // this querydef, otherwise restore the original querydef
  137.     if(createSetAndSaveNewQueryDef (m_pDatabase, &m_pQueryDef, &m_QI))
  138.     {
  139.         // get the information on the new query and display
  140.         getQueryInfo(m_pDatabase, &m_QI, -1, FALSE);
  141.         UpdateData(FALSE);
  142.  
  143.         // don't need cached object
  144.         if (pCacheQueryDef != NULL)
  145.             delete pCacheQueryDef;
  146.     }
  147.     else
  148.     {
  149.         // delete the newly allocated querydef object and reset pointer
  150.         // to cached object
  151.         delete m_pQueryDef;
  152.         m_pQueryDef = pCacheQueryDef;
  153.     }
  154. }
  155. // user selected to delete the current query--prompt for acceptance
  156. void CAddQueryDlg::OnDeleteQuerydef()
  157. {
  158.     // get values from control
  159.     UpdateData(TRUE);
  160.  
  161.     // can only delete existing queries
  162.     if (IsExistentQuery(m_pDatabase, m_QI.m_strName))
  163.     {
  164.         // is user sure?
  165.         if (IDYES == AfxMessageBox(_T("Delete current query?"), MB_YESNO))
  166.         {
  167.             // only react if field is deleted!
  168.             if (deleteQuery(m_pDatabase, m_QI.m_strName))
  169.             {
  170.                 // set the initial state
  171.                 initializer();
  172.  
  173.                 // update the dialog controls to erase deleted info
  174.                 UpdateData(FALSE);
  175.             }
  176.         }
  177.     }
  178.  
  179.     // since query has been delete, cleanup and exit the dialog
  180.     delete m_pQueryDef;
  181.  
  182.     EndDialog(0);
  183. }
  184.  
  185. // user is done viewing or specifying the query--prompt so new query
  186. // that hasn't been added will not be lost--based solely on SQL string
  187. // having changed
  188. void CAddQueryDlg::OnDone()
  189. {
  190.     // by default, simply exit
  191.     int retCode = IDYES;
  192.  
  193.     // if user has entered a new sql string, then warn them they will lose
  194.     // it if it is not explicitly added
  195.     //
  196.     // compare the current sql string with the original
  197.     CString sql;
  198.     CEdit *pEdit = (CEdit *)GetDlgItem(IDC_SQL);
  199.     pEdit->GetWindowText(sql);
  200.     // if sql in edit and in querydef info differ, then prompt
  201.     if (sql != m_QI.m_strSQL)
  202.     {
  203.         CString prompt;
  204.  
  205.         // what to say in the prompt depends on if this is an existing
  206.         // query or a new one
  207.         if(IsExistentQuery(m_pDatabase, m_QI.m_strName))
  208.         {
  209.             // existing queries -- will lose any modifications
  210.             prompt = _T("SQL modifications will be ignored unless you select Modify.  Continue anyway?");
  211.         }
  212.         else
  213.         {
  214.             // new queries -- will lose unless added
  215.             prompt = _T("New query will be ignored unless you select Add.  Continue anyway?");
  216.         }
  217.  
  218.         retCode = AfxMessageBox(prompt, MB_YESNO);
  219.     }
  220.     // either there never was a change in the sql or the user has
  221.     // chosen not to record the change
  222.     if (retCode == IDYES)
  223.     {
  224.         // cleanup
  225.         delete m_pQueryDef;
  226.  
  227.         // end the dialog
  228.         CDialog::EndDialog(0);
  229.     }
  230. }
  231.  
  232. // even after appending to a collection, querydef properties are updatable
  233. // user has selected to update
  234. void CAddQueryDlg::OnModifyQueryDef()
  235. {
  236.     // cache old name (i.e. user may have modified name and we need to use
  237.     // name to determine if this a new or existing query)
  238.     CString oldName = m_QI.m_strName;
  239.  
  240.     // get new data from dialog (possibly including new name)
  241.     UpdateData(TRUE);
  242.  
  243.     // only proceed if this is an existing query (use old name to determine this)
  244.     if (!IsExistentQuery(m_pDatabase, oldName))
  245.     {
  246.         AfxMessageBox(_T("Can not modify a query that has not been added to the collection."));
  247.  
  248.         // return since can't modify non-existent query
  249.         return;
  250.     }
  251.  
  252.     // attempt to modify the querydef with the new information
  253.     // only if the modification succeeds do we allow the name to change,
  254.     // else reset it to the old name to avoid "losing" this query
  255.     // by setting its name to an invalid value
  256.     if (!modifyQueryDef (m_pDatabase, m_pQueryDef, &m_QI))
  257.         m_QI.m_strName = oldName;
  258.  
  259. }
  260.