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 / tabledef.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  10KB  |  352 lines

  1. // tabledef.cpp : MFC DAO TableDef 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. // contains:
  14. //
  15. //BOOL IsExistentTable(CDaoDatabase *pDatabase, CString strTableName);
  16. //BOOL createNewTableDef(CDaoDatabase * pDatabase,
  17. //                                  CDaoTableDef **ppTableDef,
  18. //                                  CString strTableName);
  19. //BOOL appendTableDef(CDaoDatabase *pDatabase, CDaoTableDef *pTableDef);
  20. //BOOL openTableDef(CDaoDatabase * pDatabase, CDaoTableDef **ppTableDef,
  21. //                CString strTableName);
  22. //BOOL getTableInfo(CDaoDatabase *pDatabase, CDaoTableDefInfo *pTableInfo,
  23. //                int tableIndex, BOOL bReportErrors);
  24. //void deleteTable(CDaoDatabase *pDatabase, CString strTableName);
  25.  
  26.  
  27. #include "stdafx.h"
  28. #include "tabledef.h"
  29.  
  30. // check for duplicate table name in tabledef collection
  31. // of database object
  32. // IN: pDatabase--pointer to database object whose tabledef collection we will
  33. //     access
  34. // IN: strTableName--name of the table whose existence we want to check
  35. // RETURN: TRUE if the table already exists, FALSE otherwise
  36. BOOL IsExistentTable(CDaoDatabase *pDatabase, CString strTableName)
  37. {
  38.     // if the database is non-existent, then the answer is obvious
  39.     if (pDatabase == NULL)
  40.         return FALSE;
  41.  
  42.     // initialize status indicator
  43.     BOOL bDuplicateTableName = TRUE;
  44.  
  45.     // see if there is a table by this name already--duplicate
  46.     // named tables are not accepted
  47.     CDaoTableDefInfo tableInfo; // only needed for the call
  48.  
  49.     // MFC exception handler macros used
  50.     TRY
  51.     {
  52.         // this call will throw an exception if there is no
  53.         // table by the specified name--test for duplication
  54.         pDatabase->GetTableDefInfo(strTableName, tableInfo);
  55.     }
  56.     CATCH (CDaoException, e)
  57.     {
  58.         // if this is an 'Item not found' exception, we are
  59.         // cleared to create the table -- else this is
  60.         // a duplicate tablename and we got another exception
  61.         // which is irrelevant for our purposes
  62.         if (e->m_pErrorInfo->m_lErrorCode == 3265)
  63.             bDuplicateTableName = FALSE;
  64.     }
  65.     AND_CATCH (CMemoryException, e)
  66.     {
  67.         // do nothing
  68.         ;
  69.     }
  70.     END_CATCH
  71.  
  72.     return bDuplicateTableName;
  73. }
  74.  
  75. // wraps the CreateTableDef call with an exception handler and duplicate
  76. // name check
  77. // IN: pDatabase--pointer to database object whose tabledef collection we will
  78. //     access
  79. // OUT: ppTableDef--pointer to pointer to tabledef we are creating
  80. // IN: strTableName--name of the table we want to create
  81. // RETURN: TRUE if creation succeeds, FALSE otherwise
  82. BOOL createNewTableDef(CDaoDatabase * pDatabase,
  83.                                     CDaoTableDef **ppTableDef,
  84.                                     CString strTableName)
  85. {
  86.     // if the database is non-existent, then the answer is obvious
  87.     if (pDatabase == NULL)
  88.         return FALSE;
  89.  
  90.     // check for existing table with this name just to be safe
  91.     if (IsExistentTable(pDatabase, strTableName))
  92.     {
  93.         AfxMessageBox(_T("A table by that name already exists."));
  94.  
  95.         // return FALSE since can't create duplicate table
  96.         return FALSE;
  97.     }
  98.  
  99.     // initialize creation failure indicator
  100.     BOOL bCreateFailed = FALSE;
  101.  
  102.     // construct tabledef
  103.     *ppTableDef = new CDaoTableDef(pDatabase);
  104.  
  105.     // failed to allocate so exit
  106.     if ((*ppTableDef) == NULL)
  107.         return FALSE;
  108.  
  109.     // no duplication, so create the tabledef if possible
  110.     TRY
  111.     {
  112.         (*ppTableDef)->Create(strTableName);
  113.     }
  114.     CATCH (CDaoException, e)
  115.     {
  116.         // construct a meaningful message
  117.         CString strMessage = _T("Couldn't create tabledef--Exception: ");
  118.         strMessage += e->m_pErrorInfo->m_strDescription;
  119.  
  120.         AfxMessageBox(strMessage);
  121.  
  122.         // indicate failure
  123.         bCreateFailed = TRUE;
  124.  
  125.         // delete the tabledef on failure
  126.         if (*ppTableDef != NULL)
  127.         {
  128.             delete *ppTableDef;
  129.             *ppTableDef = NULL;
  130.         }
  131.     }
  132.     AND_CATCH (CMemoryException, e)
  133.     {
  134.         // output status
  135.         AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown."));
  136.  
  137.         // indicate failure
  138.         bCreateFailed = TRUE;
  139.  
  140.         // delete the tabledef on failure
  141.         if (*ppTableDef != NULL)
  142.         {
  143.             delete *ppTableDef;
  144.             *ppTableDef = NULL;
  145.         }
  146.     }
  147.     END_CATCH
  148.  
  149.     // return TRUE if creation succeeds
  150.     return (!bCreateFailed);
  151. }
  152.  
  153. // wraps the OpenTableDef call with an exception handler
  154. // IN: pDatabase--pointer to database object whose tabledef collection we will
  155. //     access
  156. // OUT: ppTableDef--pointer to pointer to tabledef we are opening
  157. // IN: strTableName--name of the table we want to open
  158. // RETURN: TRUE if open succeeds, FALSE otherwise
  159. BOOL openTableDef(CDaoDatabase * pDatabase, CDaoTableDef **ppTableDef,
  160.                   CString strTableName)
  161. {
  162.     // if the database is non-existent, then the answer is obvious
  163.     if (pDatabase == NULL)
  164.         return FALSE;
  165.  
  166.     // initialize creation failure indicator
  167.     BOOL bOpenFailed = FALSE;
  168.  
  169.     // construct tabledef
  170.     *ppTableDef = new CDaoTableDef(pDatabase);
  171.  
  172.     // failed to allocate so exit
  173.     if ((*ppTableDef) == NULL)
  174.         return FALSE;
  175.  
  176.     // open the tabledef if possible
  177.     TRY
  178.     {
  179.         (*ppTableDef)->Open(strTableName);
  180.     }
  181.     CATCH (CDaoException, e)
  182.     {
  183.         // construct a meaningful message
  184.         CString strMessage = _T("Couldn't open tabledef--Exception: ");
  185.         strMessage += e->m_pErrorInfo->m_strDescription;
  186.  
  187.         AfxMessageBox(strMessage);
  188.  
  189.         // indicate failure
  190.         bOpenFailed = TRUE;
  191.  
  192.         // delete the tabledef on failure
  193.         if (*ppTableDef != NULL)
  194.         {
  195.             delete *ppTableDef;
  196.             *ppTableDef = NULL;
  197.         }
  198.     }
  199.     AND_CATCH (CMemoryException, e)
  200.     {
  201.         // output status
  202.         AfxMessageBox(_T("Failed to open tabledef--Memory exception thrown."));
  203.  
  204.         // indicate failure
  205.         bOpenFailed = TRUE;
  206.  
  207.         // delete the tabledef on failure
  208.         if (*ppTableDef != NULL)
  209.         {
  210.             delete *ppTableDef;
  211.             *ppTableDef = NULL;
  212.         }
  213.     }
  214.     END_CATCH
  215.  
  216.     // return TRUE if open succeeds
  217.     return (!bOpenFailed);
  218. }
  219.  
  220. // append the tabledef to the database collection
  221. // IN: pDatabase--pointer to database object whose tabledef collection we will
  222. //     access
  223. // IN: pTableDef--pointer tabledef we are appending to the collection
  224. // RETURN: TRUE if append succeeds, FALSE otherwise
  225. BOOL appendTableDef(CDaoDatabase *pDatabase, CDaoTableDef *pTableDef)
  226. {
  227.     // if the database is non-existent, then the answer is obvious
  228.     if (pDatabase == NULL)
  229.         return FALSE;
  230.  
  231.     // initialize success indicator
  232.     BOOL bSuccess = TRUE;
  233.  
  234.     // append the tabledef to the collection
  235.     TRY
  236.     {
  237.         pTableDef->Append();
  238.     }
  239.     CATCH (CDaoException, e)
  240.     {
  241.         // construct informative message
  242.         CString strMessage = _T("Couldn't append TableDef--Exception: ");
  243.         strMessage += e->m_pErrorInfo->m_strDescription;
  244.  
  245.         // output status
  246.         AfxMessageBox(strMessage);
  247.  
  248.         // failure
  249.         bSuccess = FALSE;
  250.     }
  251.     AND_CATCH (CMemoryException, e)
  252.     {
  253.         // output status
  254.         AfxMessageBox(_T("Failed to append tabledef--Memory exception thrown."));
  255.  
  256.         // failure
  257.         bSuccess = FALSE;
  258.     }
  259.     END_CATCH
  260.  
  261.     // return status
  262.     return bSuccess;
  263. }
  264.  
  265. // fill a tableinfo struct--handle exceptions
  266. // IN: pDatabase--pointer to database object whose tabledef collection we will
  267. //     access
  268. // OUT: pTableInfo--pointer to structure to hold the tabledef info
  269. // IN: tableIndex--index into the collection indicating which item we want
  270. // IN: bReportErrors--TRUE by default, if TRUE, report all errors, else silent
  271. // RETURN: TRUE if information obtained, FALSE otherwise
  272. BOOL getTableInfo(CDaoDatabase *pDatabase, CDaoTableDefInfo *pTableInfo,
  273.                   int tableIndex, BOOL bReportErrors /*= TRUE*/)
  274. {
  275.     // if the database is non-existent, then the answer is obvious
  276.     if (pDatabase == NULL)
  277.         return FALSE;
  278.  
  279.     // initialize success indicator
  280.     BOOL bSuccess = TRUE;
  281.  
  282.     TRY
  283.     {
  284.         // try to get info on the table
  285.         pDatabase->GetTableDefInfo(tableIndex, *pTableInfo, AFX_DAO_ALL_INFO );
  286.     }
  287.     CATCH (CDaoException, e)
  288.     {
  289.         // construct a meaningful message if request
  290.         if (bReportErrors)
  291.         {
  292.             CString strMessage = _T("Couldn't get information on table--Exception: ");
  293.             strMessage += e->m_pErrorInfo->m_strDescription;
  294.  
  295.             AfxMessageBox(strMessage);
  296.         }
  297.  
  298.         // indicate failure
  299.         bSuccess = FALSE;
  300.     }
  301.     AND_CATCH (CMemoryException, e)
  302.     {
  303.         // output status if requested
  304.         if (bReportErrors)
  305.             AfxMessageBox(_T("Failed to get info on table--Memory exception thrown."));
  306.  
  307.         // indicate failure
  308.         bSuccess = FALSE;
  309.     }
  310.     END_CATCH
  311.  
  312.     // return status
  313.     return bSuccess;
  314. }
  315.  
  316. // detete the specified table from the collection of tables
  317. // IN: pDatabase--pointer to database object whose tabledef collection we will
  318. //     access
  319. // IN: strTableName--name of the table we want to delete
  320. void deleteTable(CDaoDatabase *pDatabase, CString strTableName)
  321. {
  322.     // if the database is non-existent, then the answer is obvious
  323.     if (pDatabase == NULL)
  324.         return;
  325.  
  326.     // see if there is a table by this name -- if not, can't proceed
  327.     if (!IsExistentTable(pDatabase, strTableName))
  328.         return;
  329.  
  330.     // MFC exception handler macros used
  331.     TRY
  332.     {
  333.         pDatabase->DeleteTableDef(strTableName);
  334.     }
  335.     CATCH (CDaoException, e)
  336.     {
  337.         // construct a meaningful message if request
  338.         CString strMessage = _T("Couldn't delete the table--Exception: ");
  339.         strMessage += e->m_pErrorInfo->m_strDescription;
  340.  
  341.         AfxMessageBox(strMessage);
  342.     }
  343.     AND_CATCH (CMemoryException, e)
  344.     {
  345.         // output status if requested
  346.         AfxMessageBox(_T("Failed to delete the table--Memory exception thrown."));
  347.     }
  348.     END_CATCH
  349.  
  350.     return;
  351. }
  352.