home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / TBRSTRCT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  9.7 KB  |  284 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // tbrstrct.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[]       = "contacts";
  7. static const char szRstrctTblName[] = "rstrct";
  8. static const char szTblType[]       = szPARADOX;
  9.  
  10. #define NUMOFFIELDS 4
  11.  
  12. static DBIResult getFieldDescs(hDBICur hCur, UINT16 *uNumFields,
  13.                                FLDDesc **fldDesc);
  14. static DBIResult changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
  15.                                   FLDDesc *fldDescriptor, CROpType *ecrFldOp);
  16.  
  17. //=====================================================================
  18. //  Function:
  19. //          TBRestructure();
  20. //
  21. //  Description:
  22. //          This example shows how to change the characteristics of a
  23. //          table, adding, dropping and changing fields.
  24. //
  25. //          The following steps are followed:
  26. //              Getting the existing field descriptors
  27. //              Modifying the field descriptors to the new table structure
  28. //              Initializing the CRTblDesc (create table descriptor)
  29. //              Restructuring the table
  30. //=====================================================================
  31. void
  32. TBRestructure (void)
  33. {
  34.     hDBIDb      hDb;            // Handle to the database
  35.     hDBICur     hCur;           // Handle to the table
  36.     CRTblDesc   crTblDesc;      // Table descriptor
  37.     UINT16      uNumFields;     // Number of fields
  38.     UINT16      uNumOfRecs = 5; // Nunmber of records to display
  39.     FLDDesc     *fldDesc;       // Pointer to the field descriptors
  40.     FLDDesc     fldDescriptor[NUMOFFIELDS]; // Field descriptor
  41.     CROpType    ecrFldOp[NUMOFFIELDS];      // Field operations
  42.     DBIResult   rslt;           // Return value from IDAPI functions
  43.     DBIPATH     szKeyViol = "KEYVIOL"; // Name for the key violation table
  44.  
  45.     Screen("*** Restructure Example ***\r\n");
  46.  
  47.     BREAK_IN_DEBUGGER();
  48.  
  49.     Screen("    Initializing IDAPI...");
  50.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  51.     {                                        
  52.         Screen("*** End of Example ***");
  53.         return;
  54.     }
  55.  
  56.     Screen("    Setting the database directory... ");
  57.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  58.     ChkRslt(rslt, "SetDirectory");
  59.  
  60.     Screen("    Open the %s table....", szTblName);
  61.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  62.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  63.                         xltFIELD, FALSE, NULL, &hCur);
  64.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  65.     {
  66.         CloseDbAndExit(&hDb);
  67.         Screen("\r\n*** End of Example ***");
  68.         return;
  69.     }
  70.  
  71.     rslt = DbiSetToBegin(hCur);
  72.     ChkRslt(rslt, "SetToBegin");
  73.  
  74.     Screen("    Display the first %u records of the %s table...",
  75.            uNumOfRecs, szTblName);
  76.     DisplayTable(hCur, uNumOfRecs);
  77.  
  78.     // Get the field descriptors for the existing table.
  79.     if (getFieldDescs(hCur, &uNumFields, &fldDesc) != DBIERR_NONE)
  80.     {
  81.         rslt = DbiCloseCursor(&hCur);
  82.         ChkRslt(rslt, "CloseCursor");
  83.         CloseDbAndExit(&hDb);
  84.         Screen("\r\n*** End of Example ***");
  85.         return;
  86.     }
  87.  
  88.     Screen("\r\n    Close the %s table...", szTblName);
  89.     DbiCloseCursor(&hCur);
  90.  
  91.     Screen("    Initialize the table descriptor...");
  92.     memset(&crTblDesc, 0, sizeof(CRTblDesc));
  93.     strcpy(crTblDesc.szTblName, szTblName);
  94.     strcpy(crTblDesc.szTblType, szTblType);
  95.     crTblDesc.bPack         = TRUE;
  96.     changeFieldDescs(&uNumFields, fldDesc, fldDescriptor, ecrFldOp);
  97.     crTblDesc.iFldCount     = uNumFields;
  98.     crTblDesc.pecrFldOp     = ecrFldOp;
  99.     crTblDesc.pfldDesc      = fldDescriptor;
  100.  
  101.     Screen("    Restructure the %s table to the %s table...",
  102.            szTblName, szRstrctTblName);
  103.     rslt = DbiDoRestructure(hDb, 1, &crTblDesc, (pCHAR) szRstrctTblName,
  104.                             szKeyViol, NULL, FALSE);
  105.     if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE)
  106.     {
  107.         free(fldDesc);
  108.         CloseDbAndExit(&hDb);
  109.         Screen("\r\n*** End of Example ***");
  110.         return;
  111.     }
  112.  
  113.     Screen("    Open the %s table...", szRstrctTblName);
  114.     rslt = DbiOpenTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType,
  115.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  116.                         xltFIELD, FALSE, NULL, &hCur);
  117.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  118.     {
  119.         free(fldDesc);
  120.         rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
  121.         ChkRslt(rslt, "DeleteTable");
  122.         CloseDbAndExit(&hDb);
  123.         Screen("\r\n*** End of Example ***");
  124.         return;
  125.     }
  126.  
  127.     rslt = DbiSetToBegin(hCur);
  128.     ChkRslt(rslt, "SetToBegin");
  129.  
  130.     Screen("    Display the first %u records of the %s table...",
  131.            uNumOfRecs, szRstrctTblName);
  132.     DisplayTable(hCur, uNumOfRecs);
  133.  
  134.     Screen("\r\n    Close the %s table...", szRstrctTblName);
  135.     rslt = DbiCloseCursor(&hCur);
  136.     ChkRslt(rslt, "CloseCursor");
  137.  
  138.     Screen("    Delete the %s table...", szRstrctTblName);
  139.     rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
  140.     ChkRslt(rslt, "DeleteTable");
  141.  
  142.     free(fldDesc);
  143.  
  144.     Screen("    Close the database and exit IDAPI...");
  145.     CloseDbAndExit(&hDb);
  146.  
  147.     Screen("\r\n*** End of Example ***");
  148. }
  149.  
  150. //===============================================================
  151. //  Function:
  152. //          getFieldDescs(hCur, *uNumFields, **fldDesc)
  153. //
  154. //  Input:  hCur            - Handle to the cursor
  155. //          uNumFields      - Number of fields in the field descriptor
  156. //                            (Returned)
  157. //          fldDesc         - Existing field descriptor (Input)
  158. //
  159. //  Return: IDAPI return code
  160. //
  161. //  Description:
  162. //          This function gets the field descriptor for an existing cursor.
  163. //================================================================
  164. DBIResult
  165. getFieldDescs (hDBICur hCur, UINT16 *uNumFields, FLDDesc **fldDesc)
  166. {
  167.     DBIResult   rslt;       // Return value from IDAPI functions
  168.     CURProps    curProps;   // Properties of the table
  169.  
  170.     // Change the translation mode to xltNONE to get the physical 
  171.     //   field descriptors.
  172.     rslt = DbiSetProp(hCur, curXLTMODE, xltNONE);
  173.     ChkRslt(rslt, "SetProp");
  174.  
  175.     rslt = DbiGetCursorProps(hCur, &curProps);
  176.     if (ChkRslt(rslt, "GetCursorProps") != DBIERR_NONE)
  177.     {
  178.         return rslt;
  179.     }
  180.  
  181.     *uNumFields = curProps.iFields;
  182.  
  183.     *fldDesc = (FLDDesc *) malloc(curProps.iFields * sizeof(FLDDesc));
  184.     if (fldDesc == NULL)
  185.     {
  186.         return DBIERR_NOMEMORY;
  187.     }
  188.  
  189.     rslt = DbiGetFieldDescs(hCur, *fldDesc);
  190.     if (ChkRslt(rslt, "GetFIeldDescs") != DBIERR_NONE)
  191.     {
  192.         return rslt;
  193.     }
  194.  
  195.     // Change the translation mode back to xltFIELD.
  196.     rslt = DbiSetProp(hCur, curXLTMODE, xltFIELD);
  197.     ChkRslt(rslt, "SetProp");
  198.  
  199.     return DBIERR_NONE;
  200. }
  201.  
  202. //===============================================================
  203. //  Function:
  204. //          changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
  205. //                           FLDDesc *fldDescriptor, CROpType *ecrFldOp);
  206. //
  207. //  Input:  uNumFields      - Number of fields in the existing field
  208. //                            descriptor. Also returns the number of
  209. //                            fields in the new field descriptor
  210. //                            (Input and Returned)
  211. //          fldDesc         - Existing field descriptor (Input)
  212. //          fldDescriptor   - New field descriptor to use (Returned)
  213. //          ecrFldOp        - Array of operations on the fields in
  214. //                            fldDescriptor (Returned)
  215. //
  216. //  Return: IDAPI return code
  217. //
  218. //  Description:
  219. //          This function sets up the field structure for the new table.
  220. //================================================================
  221. DBIResult
  222. changeFieldDescs (UINT16 *uNumFields, FLDDesc *fldDesc,
  223.                   FLDDesc *fldDescriptor, CROpType *ecrFldOp)
  224. {
  225.     UINT16      i;      // Loop Counter
  226.     UINT16      uField; // Number of the field
  227.  
  228.     uField = 0;
  229.     
  230.     for (i=0; i < *uNumFields; i++)
  231.     {
  232.         // Keep the "Last Name" field as is.
  233.         if (! strcmp(fldDesc[i].szName, "Last Name"))
  234.         {
  235.             fldDescriptor[uField] = fldDesc[i];
  236.             ecrFldOp[uField] = crNOOP;
  237.             uField++;
  238.         }
  239.  
  240.         // Change the "First Name" field.
  241.         if (! strcmp(fldDesc[i].szName, "First Name"))
  242.         {
  243.             fldDescriptor[uField] = fldDesc[i];
  244.             strcpy(fldDescriptor[uField].szName, "First");
  245.             ecrFldOp[uField] = crMODIFY;
  246.             uField++;
  247.         }
  248.  
  249.         // Move the "Phone" Field.
  250.         if (! strcmp(fldDesc[i].szName, "Phone"))
  251.         {
  252.             fldDescriptor[uField] = fldDesc[i];
  253.             ecrFldOp[uField] = crCOPY;
  254.             uField++;
  255.         }
  256.     }
  257.  
  258.     // Make certain that there is enough space in the field descriptor
  259.     //   for a new field.
  260.     if (uField < *uNumFields)
  261.     {
  262.         // Add the "Age" field.
  263.         fldDescriptor[uField].iFldNum        = 0;
  264.         strcpy(fldDescriptor[uField].szName, "Age");
  265.         fldDescriptor[uField].iFldType       = fldPDXSHORT;
  266.         fldDescriptor[uField].iSubType       = 0;
  267.         fldDescriptor[uField].iUnits1        = 0;
  268.         fldDescriptor[uField].iUnits2        = 0;
  269.         fldDescriptor[uField].iOffset        = 0;
  270.         fldDescriptor[uField].iLen           = 0;
  271.         fldDescriptor[uField].iNullOffset    = 0;
  272.         fldDescriptor[uField].efldvVchk      = fldvNOCHECKS;
  273.         fldDescriptor[uField].efldrRights    = fldrREADWRITE;
  274.  
  275.         ecrFldOp[uField] = crADD;
  276.  
  277.         uField++;
  278.     }
  279.     
  280.     *uNumFields = uField;
  281.  
  282.     return DBIERR_NONE;
  283. }
  284.