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 / index.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  213 lines

  1. // index.cpp : MFC DAO Index specific functions
  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. //
  14. // contains index specific functions:
  15. //BOOL IsExistentIndex(CDaoTableDef *pTableDef, CString strIndexName);
  16. //BOOL createNewIndex(CDaoTableDef *pTableDef, CDaoIndexInfo *pIndexInfo);
  17. //BOOL getIndexInfo(CDaoTableDef *pTableDef, CDaoIndexInfo *pIndexInfo,
  18. //                int IndexIndex, BOOL bReportErrors = TRUE);
  19. //BOOL deleteIndex(CDaoTableDef *pTableDef, CString strIndexName);
  20.  
  21. #include "stdafx.h"
  22. #include "index.h"
  23.  
  24. // check for duplicate name in collection
  25. // of tabledef object
  26. // IN: pTableDef--pointer to tabledef object whose index collection we access
  27. // IN: strIndexName--name of index to check for existence
  28. // RETURN: TRUE if the index exists in the collection, FALSE otherwise
  29. BOOL IsExistentIndex(CDaoTableDef *pTableDef, CString strIndexName)
  30. {
  31.     // if the tabledef is non-existent, then the answer is obvious
  32.     if (pTableDef == NULL)
  33.         return FALSE;
  34.  
  35.     // initialize status indicator
  36.     BOOL bDuplicateIndexName = TRUE;
  37.  
  38.     // see if there is a Index by this name already--duplicate
  39.     // named Indexs are not accepted
  40.     CDaoIndexInfo IndexInfo;    // only needed for the call
  41.  
  42.     // MFC exception handler macros used
  43.     TRY
  44.     {
  45.         // this call will throw an exception if there is no
  46.         // Index by the specified name--test for duplication
  47.         pTableDef->GetIndexInfo(strIndexName, IndexInfo);
  48.     }
  49.     CATCH (CDaoException, e)
  50.     {
  51.         // if this is an 'Item not found' exception, we are
  52.         // cleared to create the Index -- else this is
  53.         // a duplicate Index name and we got another exception
  54.         // which is irrelevant for our purposes
  55.         if (e->m_pErrorInfo->m_lErrorCode == 3265)
  56.             bDuplicateIndexName = FALSE;
  57.     }
  58.     AND_CATCH (CMemoryException, e)
  59.     {
  60.         // do nothing
  61.         ;
  62.     }
  63.     END_CATCH
  64.  
  65.     return bDuplicateIndexName;
  66. }
  67.  
  68. // wraps the CreateIndex DAO call in an exception handler
  69. // IN: pTableDef--pointer to tabledef object whose index collection we access
  70. // IN: pIndexInfo--information on index to be created
  71. // RETURN: TRUE if new index created, FALSE otherwise
  72. BOOL createNewIndex(CDaoTableDef *pTableDef, CDaoIndexInfo *pIndexInfo)
  73. {
  74.     // if the tabledef is non-existent, then the answer is obvious
  75.     if (pTableDef == NULL)
  76.         return FALSE;
  77.  
  78.     // check for existing Index with this name just to be safe
  79.     if (IsExistentIndex(pTableDef, pIndexInfo->m_strName))
  80.     {
  81.         AfxMessageBox(_T("A Index by that name already exists."));
  82.  
  83.         // return FALSE since can't create duplicate Index
  84.         return FALSE;
  85.     }
  86.  
  87.     // initialize failure indicators
  88.     BOOL bCreateFailed = FALSE;
  89.  
  90.     // create a Index with the specified properties
  91.     // it is automatically appended to Index collection
  92.     // of the tabledef
  93.     TRY
  94.     {
  95.         pTableDef->CreateIndex(*pIndexInfo);
  96.     }
  97.     CATCH (CDaoException, e)
  98.     {
  99.         // construct a meaningful message
  100.         CString message = _T("Couldn't create Index--Exception: ");
  101.         message += e->m_pErrorInfo->m_strDescription;
  102.  
  103.         AfxMessageBox(message);
  104.  
  105.         // indicate failure
  106.         bCreateFailed = TRUE;
  107.     }
  108.     AND_CATCH (CMemoryException, e)
  109.     {
  110.         AfxMessageBox(_T("Failed to create Index--Memory exception thrown."));
  111.  
  112.         // indicate failure
  113.         bCreateFailed = TRUE;
  114.     }
  115.     END_CATCH
  116.  
  117.     // return TRUE if creation succeeds
  118.     return (!bCreateFailed);
  119. }
  120.  
  121. // fill a Indexinfo struct--handle exceptions
  122. // IN: pTableDef--pointer to tabledef object whose index collection we access
  123. // OUT: pIndexInfo--information on index that is specifieed
  124. // IN: IndexIndex--the index into the index collection for the item we want
  125. // IN: bReportErrors--if TRUE, report any errors that occur, else silent
  126. //     TRUE by default
  127. // RETURN: TRUE if information obtained, FALSE otherwise
  128. BOOL getIndexInfo(CDaoTableDef *pTableDef, CDaoIndexInfo *pIndexInfo,
  129.                   int IndexIndex, BOOL bReportErrors /*= TRUE*/)
  130. {
  131.     // if the tabledef is non-existent, then the answer is obvious
  132.     if (pTableDef == NULL)
  133.         return FALSE;
  134.  
  135.     // initialize success indicator
  136.     BOOL bSuccess = TRUE;
  137.  
  138.     TRY
  139.     {
  140.         // try to get info on the Index
  141.         pTableDef->GetIndexInfo(IndexIndex, *pIndexInfo, AFX_DAO_ALL_INFO );
  142.     }
  143.     CATCH (CDaoException, e)
  144.     {
  145.         // construct a meaningful message if requested
  146.         if (bReportErrors)
  147.         {
  148.             CString strMessage = _T("Couldn't get information on Index--Exception: ");
  149.             strMessage += e->m_pErrorInfo->m_strDescription;
  150.  
  151.             AfxMessageBox(strMessage);
  152.         }
  153.  
  154.         // indicate failure
  155.         bSuccess = FALSE;
  156.     }
  157.     AND_CATCH (CMemoryException, e)
  158.     {
  159.         // output status if requested
  160.         if (bReportErrors)
  161.             AfxMessageBox(_T("Failed to get info on Index--Memory exception thrown."));
  162.  
  163.         // indicate failure
  164.         bSuccess = FALSE;
  165.     }
  166.     END_CATCH
  167.  
  168.     // return status
  169.     return bSuccess;
  170. }
  171.  
  172. // wrap Index deletion with exception handlers
  173. // IN: pTableDef--pointer to tabledef object whose index collection we access
  174. // IN: strIndexName--name of index to delete
  175. // RETURN: TRUE if deletion suceeded, FALSE otherwise
  176. BOOL deleteIndex(CDaoTableDef *pTableDef, CString strIndexName)
  177. {
  178.     // if the tabledef is non-existent, then the answer is obvious
  179.     if (pTableDef == NULL)
  180.         return FALSE;
  181.  
  182.     // initialize success indicator
  183.     BOOL bSuccess = TRUE;
  184.  
  185.     // MFC exception handler macros used
  186.     TRY
  187.     {
  188.         // this call will throw an exception if there is no
  189.         // Index by the specified name--test for duplication
  190.         pTableDef->DeleteIndex(strIndexName);
  191.     }
  192.     CATCH (CDaoException, e)
  193.     {
  194.         CString strMessage = _T("Couldn't delete the Index--Exception: ");
  195.         strMessage += e->m_pErrorInfo->m_strDescription;
  196.  
  197.         AfxMessageBox(strMessage);
  198.  
  199.         // indicate failure
  200.         bSuccess = FALSE;
  201.     }
  202.     AND_CATCH (CMemoryException, e)
  203.     {
  204.         AfxMessageBox(_T("Failed to delete the Index--Memory exception thrown."));
  205.  
  206.         // indicate failure
  207.         bSuccess = FALSE;
  208.     }
  209.     END_CATCH
  210.  
  211.     return bSuccess;
  212. }
  213.