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 / querydef.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  12KB  |  440 lines

  1. // querydef.cpp : MFC DAO QueryDef specific functions
  2. //
  3. // contains all querydef specific functions:
  4. //
  5. // This is a part of the Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992-1998 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and related
  11. // electronic documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14.  
  15. #include "stdafx.h"
  16. #include "querydef.h"
  17.  
  18. // check for duplicate query name in querydef collection
  19. // of database object
  20. // IN: pDatabase--pointer to database whose querydef collection we will access
  21. // IN: strQueryName--the name of the querydef we want to check for existence
  22. // RETURN: TRUE if the specified querydef already exists, FALSE otherwise
  23. BOOL IsExistentQuery(CDaoDatabase *pDatabase, CString strQueryName)
  24. {
  25.     // if no database, then done immediately
  26.     if (pDatabase == NULL)
  27.         return FALSE;
  28.  
  29.     // initialize status indicator
  30.     BOOL bDuplicateQueryName = TRUE;
  31.  
  32.     // see if there is a query by this name already--duplicate
  33.     // named querys are not accepted
  34.     CDaoQueryDefInfo queryInfo; // only needed for the call
  35.  
  36.     // MFC exception handler macros used
  37.     TRY
  38.     {
  39.         // this call will throw an exception if there is no
  40.         // query by the specified name--test for duplication
  41.         pDatabase->GetQueryDefInfo(strQueryName, queryInfo);
  42.     }
  43.     CATCH (CDaoException, e)
  44.     {
  45.         // if this is an 'Item not found' exception, we are
  46.         // cleared to create the query -- else this is
  47.         // a duplicate queryname and we got another exception
  48.         // which is irrelevant for our purposes
  49.         if (e->m_pErrorInfo->m_lErrorCode == 3265)
  50.             bDuplicateQueryName = FALSE;
  51.     }
  52.     AND_CATCH (CMemoryException, e)
  53.     {
  54.         // do nothing
  55.         ;
  56.     }
  57.     END_CATCH
  58.  
  59.     return bDuplicateQueryName;
  60. }
  61.  
  62. // wraps the CreateQueryDef call with an exception handler and duplicate
  63. // name check
  64. // IN: pDatabase--pointer to database whose querydef collection we will access
  65. // OUT: ppQueryDef--pointer to pointer of querydef we will create and return
  66. // RETURN: TRUE if creation succeeded, FALSE otherwise
  67. BOOL createNewQueryDef(CDaoDatabase * pDatabase,
  68.                                     CDaoQueryDef **ppQueryDef,
  69.                                     CString strQueryName)
  70. {
  71.     // if no database, then done immediately
  72.     if (pDatabase == NULL)
  73.         return FALSE;
  74.  
  75.     // check for existing query with this name just to be safe
  76.     if (IsExistentQuery(pDatabase, strQueryName))
  77.     {
  78.         AfxMessageBox(_T("A query by that name already exists."));
  79.  
  80.         // return FALSE since can't create duplicate query
  81.         return FALSE;
  82.     }
  83.  
  84.     // initialize creation failure indicator
  85.     BOOL bCreateFailed = FALSE;
  86.  
  87.     // cleanup any existing querydef
  88.     if (*ppQueryDef != NULL)
  89.         delete *ppQueryDef;
  90.  
  91.     // construct querydef
  92.     *ppQueryDef = new CDaoQueryDef(pDatabase);
  93.  
  94.     // failed to allocate so exit with failure
  95.     if ((*ppQueryDef) == NULL)
  96.         return FALSE;
  97.  
  98.     // no duplication, so create the querydef if possible
  99.     TRY
  100.     {
  101.         (*ppQueryDef)->Create(strQueryName);
  102.     }
  103.     CATCH (CDaoException, e)
  104.     {
  105.         // construct a meaningful message
  106.         CString strMessage = _T("Couldn't create querydef--Exception: ");
  107.         strMessage += e->m_pErrorInfo->m_strDescription;
  108.  
  109.         AfxMessageBox(strMessage);
  110.  
  111.         // indicate failure
  112.         bCreateFailed = TRUE;
  113.  
  114.         // delete the querydef on failure
  115.         if (*ppQueryDef != NULL)
  116.         {
  117.             delete *ppQueryDef;
  118.             *ppQueryDef = NULL;
  119.         }
  120.     }
  121.     AND_CATCH (CMemoryException, e)
  122.     {
  123.         // output status
  124.         AfxMessageBox(_T("Failed to create querydef--Memory exception thrown."));
  125.  
  126.         // indicate failure
  127.         bCreateFailed = TRUE;
  128.  
  129.         // delete the querydef on failure
  130.         if (*ppQueryDef != NULL)
  131.         {
  132.             delete *ppQueryDef;
  133.             *ppQueryDef = NULL;
  134.         }
  135.     }
  136.     END_CATCH
  137.  
  138.     // return TRUE if creation succeeds
  139.     return (!bCreateFailed);
  140. }
  141.  
  142. // wraps the OpenQueryDef call with an exception handler
  143. // IN: pDatabase--pointer to database whose querydef collection we will access
  144. // OUT: ppQueryDef--pointer to pointer to querydef we will open and return
  145. // IN: strQueryName--the name of the querydef we want to check for existence
  146. // RETURN: TRUE if the open succeeded, FALSE otherwise
  147. BOOL openQueryDef(CDaoDatabase * pDatabase, CDaoQueryDef **ppQueryDef,
  148.                   CString strQueryName)
  149. {
  150.     // if no database, then done immediately
  151.     if (pDatabase == NULL)
  152.         return FALSE;
  153.  
  154.     // initialize creation failure indicator
  155.     BOOL bOpenFailed = FALSE;
  156.  
  157.     // cleanup any existing querydef
  158.     if (*ppQueryDef != NULL)
  159.         delete *ppQueryDef;
  160.  
  161.     // construct querydef
  162.     *ppQueryDef = new CDaoQueryDef(pDatabase);
  163.  
  164.     // failed to allocate so exit with failure
  165.     if ((*ppQueryDef) == NULL)
  166.         return FALSE;
  167.  
  168.     // open the querydef if possible
  169.     TRY
  170.     {
  171.         (*ppQueryDef)->Open(strQueryName);
  172.     }
  173.     CATCH (CDaoException, e)
  174.     {
  175.         // construct a meaningful message
  176.         CString strMessage = _T("Couldn't open querydef--Exception: ");
  177.         strMessage += e->m_pErrorInfo->m_strDescription;
  178.  
  179.         AfxMessageBox(strMessage);
  180.  
  181.         // indicate failure
  182.         bOpenFailed = TRUE;
  183.  
  184.         // delete the querydef on failure
  185.         if (*ppQueryDef != NULL)
  186.         {
  187.             delete *ppQueryDef;
  188.             *ppQueryDef = NULL;
  189.         }
  190.     }
  191.     AND_CATCH (CMemoryException, e)
  192.     {
  193.         // output status
  194.         AfxMessageBox(_T("Failed to open querydef--Memory exception thrown."));
  195.  
  196.         // indicate failure
  197.         bOpenFailed = TRUE;
  198.  
  199.         // delete the querydef on failure
  200.         if (*ppQueryDef != NULL)
  201.         {
  202.             delete *ppQueryDef;
  203.             *ppQueryDef = NULL;
  204.         }
  205.     }
  206.     END_CATCH
  207.  
  208.     // return TRUE if open succeeds
  209.     return (!bOpenFailed);
  210. }
  211.  
  212. // append the specified query def to the collection of querydefs in the
  213. // database object
  214. //
  215. // IN: pDatabase--pointer to database whose querydef collection we will access
  216. // IN: pQueryDef--pointer to querydef we will append to the collection
  217. // RETURN: TRUE if querydef appended, FALSE otherwise
  218. BOOL appendQueryDef(CDaoDatabase *pDatabase, CDaoQueryDef *pQueryDef)
  219. {
  220.     // if no database, then done immediately
  221.     if (pDatabase == NULL)
  222.         return FALSE;
  223.  
  224.     // initialize success indicator
  225.     BOOL bSuccess = TRUE;
  226.  
  227.     // append the querydef to the collection
  228.     // catch exceptions using C++ style exceptions
  229.     TRY
  230.     {
  231.         pQueryDef->Append();
  232.     }
  233.     CATCH (CDaoException, e)
  234.     {
  235.         // construct informative message
  236.         CString strMessage = _T("Couldn't append QueryDef--Exception: ");
  237.         strMessage += e->m_pErrorInfo->m_strDescription;
  238.  
  239.         // output status
  240.         AfxMessageBox(strMessage);
  241.  
  242.         // failure
  243.         bSuccess = FALSE;
  244.     }
  245.     AND_CATCH (CMemoryException, e)
  246.     {
  247.         // output status
  248.         AfxMessageBox(_T("Failed to append querydef--Memory exception thrown."));
  249.  
  250.         // failure
  251.         bSuccess = FALSE;
  252.     }
  253.     END_CATCH
  254.  
  255.     // return status
  256.     return bSuccess;
  257. }
  258.  
  259. // fill a querydef info struct--handle exceptions
  260. // IN: pDatabase--pointer to database whose querydef collection we will access
  261. // IN/OUT: pQueryInfo--pointer to querydef info struct we want to fill in
  262. // IN: index--index into the collection of querydefs for the item we want
  263. //     pass a negative value if you want to use the name in pQueryInfo
  264. //     to look up the querydef
  265. // IN: bReportErrors--if TRUE, report any errors that occur, else silent
  266. //     TRUE by default
  267. // RETURN: TRUE if information obtained, FALSE otherwise
  268. BOOL getQueryInfo(CDaoDatabase *pDatabase, CDaoQueryDefInfo *pQueryInfo,
  269.                   int index, BOOL bReportErrors /*= TRUE*/)
  270. {
  271.     // if the database is non-existent, then the answer is obvious
  272.     if (pDatabase == NULL)
  273.         return FALSE;
  274.  
  275.     // initialize success indicator
  276.     BOOL bSuccess = TRUE;
  277.  
  278.     TRY
  279.     {
  280.         // try to get info on the querydef either by index or by name
  281.         if (index < 0)
  282.             pDatabase->GetQueryDefInfo(pQueryInfo->m_strName, *pQueryInfo,
  283.                                 AFX_DAO_ALL_INFO );
  284.         else
  285.             pDatabase->GetQueryDefInfo(index, *pQueryInfo,
  286.                                 AFX_DAO_ALL_INFO );
  287.  
  288.     }
  289.     CATCH (CDaoException, e)
  290.     {
  291.         // construct a meaningful message if requested
  292.         if (bReportErrors)
  293.         {
  294.             CString strMessage = _T("Couldn't get information on QueryDef--Exception: ");
  295.             strMessage += e->m_pErrorInfo->m_strDescription;
  296.  
  297.             AfxMessageBox(strMessage);
  298.         }
  299.  
  300.         // indicate failure
  301.         bSuccess = FALSE;
  302.     }
  303.     AND_CATCH (CMemoryException, e)
  304.     {
  305.         // output status if requested
  306.         if (bReportErrors)
  307.             AfxMessageBox(_T("Failed to get info on QueryDef--Memory exception thrown."));
  308.  
  309.         // indicate failure
  310.         bSuccess = FALSE;
  311.     }
  312.     END_CATCH
  313.  
  314.     // return status
  315.     return bSuccess;
  316. }
  317.  
  318. // wrap query deletion with exception handlers
  319. // check for duplicate query name in query collection
  320. // of tabledef object
  321. // IN: pDatabase--pointer to database whose querydef collection we will access
  322. // IN: strQueryName--the name of the querydef we want to delete
  323. // RETURN: TRUE if the querydef was deleted, FALSE otherwise
  324. BOOL deleteQuery(CDaoDatabase *pDatabase, CString strQueryName)
  325. {
  326.     // if the database is non-existent, then the answer is obvious
  327.     if (pDatabase == NULL)
  328.         return FALSE;
  329.  
  330.     // initialize success indicator
  331.     BOOL bSuccess = TRUE;
  332.  
  333.     // MFC exception handler macros used
  334.     TRY
  335.     {
  336.         pDatabase->DeleteQueryDef(strQueryName);
  337.     }
  338.     CATCH (CDaoException, e)
  339.     {
  340.         CString strMessage = _T("Couldn't delete the query--Exception: ");
  341.         strMessage += e->m_pErrorInfo->m_strDescription;
  342.  
  343.         AfxMessageBox(strMessage);
  344.  
  345.         // indicate failure
  346.         bSuccess = FALSE;
  347.     }
  348.     AND_CATCH (CMemoryException, e)
  349.     {
  350.         AfxMessageBox(_T("Failed to delete the query--Memory exception thrown."));
  351.  
  352.         // indicate failure
  353.         bSuccess = FALSE;
  354.     }
  355.     END_CATCH
  356.  
  357.     return bSuccess;
  358. }
  359.  
  360. // create a querydef and set its properties, append it to
  361. // the collection
  362. // IN: pDatabase--pointer to database whose querydef collection we will access
  363. // IN/OUT: ppQueryDef--pointer to pointer of querydef we will process
  364. // IN: pQI--pointer to information needed to create the querydef
  365. // RETURN: TRUE if all processing succeeds, FALSE otherwise
  366. BOOL createSetAndSaveNewQueryDef (CDaoDatabase * pDatabase,
  367.                                     CDaoQueryDef **ppQueryDef,
  368.                                     CDaoQueryDefInfo *pQI)
  369. {
  370.     // if no database, then done immediately
  371.     if (pDatabase == NULL)
  372.         return FALSE;
  373.  
  374.     // initialize failure indicator
  375.     BOOL bFailure = TRUE;
  376.  
  377.     // call our create function and continue if it succeeds
  378.     if (createNewQueryDef(pDatabase, ppQueryDef, pQI->m_strName))
  379.     {
  380.         // can only specify some properties on create, so specify the rest now
  381.         if (modifyQueryDef (pDatabase, *ppQueryDef, pQI))
  382.         {
  383.             // now store the query def permanently
  384.             if (appendQueryDef(pDatabase, *ppQueryDef))
  385.             {
  386.                 // if you get here, you've succeeded,
  387.                 bFailure = FALSE;
  388.             }
  389.         }
  390.     }
  391.  
  392.     // return TRUE if everything succeeds
  393.     return (!bFailure);
  394. }
  395.  
  396. // set the properties of the query def after it has been appended
  397. //
  398. // IN: pDatabase--pointer to database whose querydef collection we will access
  399. // IN: pQueryDef--pointer to  querydef we will modify
  400. // IN: pQI--pointer to information needed to modifiy the querydef
  401. // RETURN: TRUE is modification succeeds, FALSE otherwise
  402. BOOL modifyQueryDef (CDaoDatabase * pDatabase,
  403.                      CDaoQueryDef *pQueryDef,
  404.                      CDaoQueryDefInfo *pQI)
  405. {
  406.     // if no database, then done immediately
  407.     if (pDatabase == NULL)
  408.         return FALSE;
  409.  
  410.     // initialize success indicator
  411.     BOOL bSuccess = TRUE;
  412.  
  413.     // MFC exception handler macros used
  414.     TRY
  415.     {
  416.         pQueryDef->SetSQL(pQI->m_strSQL);
  417.         pQueryDef->SetName(pQI->m_strName);
  418.     }
  419.     CATCH (CDaoException, e)
  420.     {
  421.         CString strMessage = _T("Couldn't modify the query--Exception: ");
  422.         strMessage += e->m_pErrorInfo->m_strDescription;
  423.  
  424.         AfxMessageBox(strMessage);
  425.  
  426.         // indicate failure
  427.         bSuccess = FALSE;
  428.     }
  429.     AND_CATCH (CMemoryException, e)
  430.     {
  431.         AfxMessageBox(_T("Failed to modify the query--Memory exception thrown."));
  432.  
  433.         // indicate failure
  434.         bSuccess = FALSE;
  435.     }
  436.     END_CATCH
  437.  
  438.     return (bSuccess);
  439. }
  440.