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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Callback.C
  4. #include "snipit.h"
  5.  
  6. static const DBIPATH szTblName      = "contacts";
  7. static const DBIPATH szRstrctTblName= "TempRest";
  8. static const char szTblType[]       = szPARADOX;
  9. pCHAR  CallBackData;
  10.  
  11. #define NUMOFFIELDS 3
  12.  
  13. static DBIResult getFieldDescs(hDBICur hCur, UINT16 *uNumFields,
  14.                                FLDDesc **fldDesc);
  15. static DBIResult changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDescSrc,
  16.                                   FLDDesc *fldDescDest, CROpType *ecrFldOp);
  17. CBRType DBIFN _export CallBackFunc(CBType ecbType, UINT32 iClientData,
  18.                                    pVOID pCbInfo);
  19.                                   
  20. //=====================================================================
  21. //  Function:
  22. //          TBRestructureCallBack();
  23. //
  24. //  Description:
  25. //          This example shows how to register and use a callback for a table
  26. //          restructure.  The restructure will simply drop fields.
  27. //
  28. //          The following steps are followed:
  29. //              Getting the existing field descriptors
  30. //              Modifying the field descriptors to the new table structure
  31. //              Initializing the CRTblDesc (create table descriptor)
  32. //              Registering the callback
  33. //              Restructuring the table
  34. //              Unregistering the callback
  35. //=====================================================================
  36. void
  37. TBRestructureCallBack (void)
  38. {
  39.     hDBIDb      hDb;                        // Handle to the database
  40.     hDBICur     hCur;                       // Handle to the table
  41.     CRTblDesc   crTblDesc;                  // Table descriptor
  42.     UINT16      uNumFields;                 // Number of fields
  43.     UINT16      uNumOfRecs = 5;             // No. of records to display
  44.     FLDDesc     *fldDescSrc;                // Array of field descriptors
  45.     FLDDesc     fldDescDest[NUMOFFIELDS];   // Restructure field descriptors
  46.     CROpType    ecrFldOp[NUMOFFIELDS];      // Restructure operations
  47.     RESTCbDesc  CbInfo;                     // Variable which is used within
  48.                                             //   the callback
  49.     DBIResult   rslt;                       // Return value from IDAPI
  50.                                             //   functions
  51.     DBIPATH     szKeyViol = "KEYVIOL";      // Name for the key violation table
  52.  
  53.     Screen("*** Restructure CallBack Example ***\r\n");
  54.  
  55.     BREAK_IN_DEBUGGER();
  56.  
  57.     Screen("    Initializing IDAPI...");
  58.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  59.     {
  60.         Screen("*** End of Example ***");
  61.         return;
  62.     }
  63.  
  64.     Screen("    Setting the database directory... ");
  65.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  66.     ChkRslt(rslt, "SetDirectory");
  67.  
  68.     Screen("    Open the %s table....", szTblName);
  69.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  70.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  71.                         xltFIELD, FALSE, NULL, &hCur);
  72.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  73.     {
  74.         CloseDbAndExit(&hDb);
  75.         Screen("\r\n*** End of Example ***");
  76.         return;
  77.     }
  78.  
  79.     rslt = DbiSetToBegin(hCur);
  80.     ChkRslt(rslt, "SetToBegin");
  81.  
  82.     Screen("    Display the %s table...", szTblName);
  83.     DisplayTable(hCur, uNumOfRecs);
  84.  
  85.     // Get the field descriptors for the existing table.
  86.     if (getFieldDescs(hCur, &uNumFields, &fldDescSrc) != DBIERR_NONE)
  87.     {
  88.         rslt = DbiCloseCursor(&hCur);
  89.         ChkRslt(rslt, "CloseCursor");
  90.         CloseDbAndExit(&hDb);
  91.         Screen("\r\n*** End of Example ***");
  92.         return;
  93.     }
  94.  
  95.     Screen("\r\n    Close the %s table...", szTblName);
  96.     DbiCloseCursor(&hCur);
  97.  
  98.     // Allocate enough space for the data passed to the callback function.
  99.     CallBackData = (pCHAR)malloc(100 * sizeof(CHAR));
  100.     if (CallBackData == NULL)
  101.     {
  102.         CloseDbAndExit(&hDb);
  103.         Screen("\r\n*** End of Example ***");
  104.         return;
  105.     }
  106.  
  107.     // Register the callback.
  108.     rslt = DbiRegisterCallBack(NULL, cbRESTRUCTURE, (UINT32) CallBackData,
  109.                                sizeof(RESTCbDesc), &CbInfo, CallBackFunc);
  110.     if(rslt!=DBIERR_NONE)
  111.     {
  112.         free(CallBackData);
  113.         CloseDbAndExit(&hDb);
  114.         Screen("\r\n*** End of Example ***");
  115.         return;
  116.     }                                  
  117.     
  118.     Screen("    Initialize the table descriptor...");
  119.     memset(&crTblDesc, 0, sizeof(CRTblDesc));
  120.     strcpy(crTblDesc.szTblName, szTblName);
  121.     strcpy(crTblDesc.szTblType, szTblType);
  122.     crTblDesc.bPack         = TRUE;
  123.     changeFieldDescs(&uNumFields, fldDescSrc, fldDescDest, ecrFldOp);
  124.     crTblDesc.iFldCount     = uNumFields;
  125.     crTblDesc.pecrFldOp     = ecrFldOp;
  126.     crTblDesc.pfldDesc      = fldDescDest;
  127.  
  128.     Screen("    Restructure the %s table to the %s table...",
  129.            szTblName, szRstrctTblName);
  130.     rslt = DbiDoRestructure(hDb, 1, &crTblDesc, (pCHAR) szRstrctTblName,
  131.                             szKeyViol, NULL, FALSE);
  132.     if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE)
  133.     {
  134.         free(CallBackData);
  135.         free(fldDescSrc);
  136.         DbiRegisterCallBack(hCur, cbRESTRUCTURE, NULL, 0, NULL, NULL);
  137.         CloseDbAndExit(&hDb);
  138.         Screen("\r\n*** End of Example ***");
  139.         return;
  140.     }
  141.  
  142.     // Unregister the callback by passing in NULL for the function.
  143.     rslt = DbiRegisterCallBack(hCur, cbRESTRUCTURE, NULL, 0, NULL, NULL);
  144.     if (ChkRslt(rslt, "RegisterCallBack") != DBIERR_NONE)
  145.     {
  146.         free(CallBackData);
  147.         free(fldDescSrc);
  148.         rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName,
  149.                               (pCHAR) szTblType);
  150.         ChkRslt(rslt, "DeleteTable");
  151.         CloseDbAndExit(&hDb);
  152.         Screen("\r\n*** End of Example ***");
  153.         return;
  154.     }
  155.     
  156.     Screen("    Open the %s table...", szRstrctTblName);
  157.     rslt = DbiOpenTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType,
  158.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  159.                         xltFIELD, FALSE, NULL, &hCur);
  160.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  161.     {
  162.         free(CallBackData);
  163.         free(fldDescSrc);
  164.         rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName,
  165.                               (pCHAR) szTblType);
  166.         ChkRslt(rslt, "DeleteTable");
  167.         CloseDbAndExit(&hDb);
  168.         Screen("\r\n*** End of Example ***");
  169.         return;
  170.     }
  171.  
  172.     rslt = DbiSetToBegin(hCur);
  173.     ChkRslt(rslt, "SetToBegin");
  174.  
  175.     Screen("    Display the %s table...", szRstrctTblName);
  176.     DisplayTable(hCur, uNumOfRecs);
  177.  
  178.     Screen("\r\n    Close the %s table...", szRstrctTblName);
  179.     rslt = DbiCloseCursor(&hCur);
  180.     ChkRslt(rslt, "CloseCursor");
  181.  
  182.     Screen("    Delete the %s restructured table...", szRstrctTblName);
  183.     rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
  184.     ChkRslt(rslt, "DeleteTable");
  185.  
  186.     free(fldDescSrc);
  187.     free(CallBackData);
  188.  
  189.     Screen("    Close the database and exit IDAPI...");
  190.     CloseDbAndExit(&hDb);
  191.  
  192.     Screen("\r\n*** End of Example ***");
  193. }
  194.  
  195. //===============================================================
  196. //  Function:
  197. //          getFieldDescs(hCur, *uNumFields, **fldDescSrc)
  198. //
  199. //  Input:  hCur            - Handle to the cursor
  200. //          uNumFields      - Number of fields in the field descriptor
  201. //                            (Returned)
  202. //          fldDescSrc      - Existing field descriptor (Input)
  203. //
  204. //  Return: IDAPI return code
  205. //
  206. //  Description:
  207. //          This function gets the field descriptor for an existing cursor
  208. //================================================================
  209. DBIResult
  210. getFieldDescs (hDBICur hCur, UINT16 *uNumFields, FLDDesc **fldDesc)
  211. {
  212.     DBIResult rslt;
  213.     CURProps curProps;
  214.  
  215.     // Change the translation mode to xltNONE to get the physical 
  216.     //   field descriptors.
  217.     rslt = DbiSetProp(hCur, curXLTMODE, xltNONE);
  218.     ChkRslt(rslt, "SetProp");
  219.  
  220.     rslt = DbiGetCursorProps(hCur, &curProps);
  221.     if (ChkRslt(rslt, "GetCursorProps") != DBIERR_NONE)
  222.     {
  223.         return rslt;
  224.     }
  225.  
  226.     *uNumFields = curProps.iFields;
  227.  
  228.     *fldDesc = (FLDDesc *) malloc(curProps.iFields * sizeof(FLDDesc));
  229.     if (fldDesc == NULL)
  230.     {
  231.         return DBIERR_NOMEMORY;
  232.     }
  233.  
  234.     rslt = DbiGetFieldDescs(hCur, *fldDesc);
  235.     if (ChkRslt(rslt, "GetFIeldDescs") != DBIERR_NONE)
  236.     {
  237.         return rslt;
  238.     }
  239.  
  240.     // Change the translation mode back to xltFIELD.
  241.     rslt = DbiSetProp(hCur, curXLTMODE, xltFIELD);
  242.     ChkRslt(rslt, "SetProp");
  243.  
  244.     return DBIERR_NONE;
  245. }
  246.  
  247. //===============================================================
  248. //  Function:
  249. //          changeFieldDescs(uNumFields, FldDescSrc,
  250. //                           fldDescDest, ecrFldOp);
  251. //
  252. //  Input:  uNumFields    - Number of fields in the existing field descriptor
  253. //                          Also returns the number of fields in the new
  254. //                          field descriptor (Input and Returned)
  255. //          fldDescSrc    - Existing field descriptor (Input)
  256. //          fldDescDest   - New field descriptor to use (Returned)
  257. //          ecrFldOp      - Array of operations on the fields in
  258. //                          fldDescDest (Returned)
  259. //
  260. //  Return: IDAPI return code
  261. //
  262. //  Description:
  263. //          This function sets up the field structure for the new table.
  264. //================================================================
  265. DBIResult
  266. changeFieldDescs (UINT16 *uNumFields, FLDDesc *fldDescSrc,
  267.                   FLDDesc *fldDescDest, CROpType *ecrFldOp)
  268. {
  269.     UINT16      i;
  270.  
  271.     for (i=0; i < NUMOFFIELDS; i++)
  272.     {
  273.         fldDescDest[i] = fldDescSrc[i];
  274.         ecrFldOp[i] = crNOOP;
  275.     }
  276.  
  277.     *uNumFields = NUMOFFIELDS;
  278.  
  279.     return DBIERR_NONE;
  280. }
  281.  
  282. //======================================================================
  283. //  Name:   CallBackFunc(ecbType, iClientData, pCbInfo)
  284. //
  285. //  Input:  ecbType     - Callback type
  286. //          iClientData - Pointer to client information that is passed into
  287. //                        the callback function
  288. //          pCbInfo     - The callback structure that holds the information
  289. //                        about the current state
  290. //
  291. //  Return: The action that should be taken
  292. //
  293. //  Description:
  294. //          This function will be called from the BDE during the processing
  295. //          of the restructure.
  296. //======================================================================
  297. CBRType DBIFN
  298. CallBackFunc (CBType ecbType, UINT32 iClientData, pVOID pCbInfo)
  299. {
  300.     RESTCbDesc      *eCBRestDesc;   // Variable to contain passed-in
  301.                                     //   information
  302.     pCHAR            pszMsg;        // Information to be displayed.
  303.                                                            
  304.     // Set to stop an unused variable warning.
  305.     iClientData = iClientData;
  306.  
  307.     switch (ecbType)
  308.     {
  309.         // In case this is a restructure progress callback, display the
  310.         //   information.
  311.         case cbRESTRUCTURE:
  312.  
  313.             eCBRestDesc = (RESTCbDesc far *)pCbInfo;
  314.  
  315.             // Restructuring the old field.
  316.             if(eCBRestDesc->eRestrObjType == restrOLDFLD)
  317.             {
  318.                 pszMsg = (pCHAR)malloc(DBIMAXMSGLEN * sizeof(CHAR) + 1);
  319.                 if (!pszMsg)
  320.                 {
  321.                     return cbrUSEDEF;
  322.                 }
  323.                 strcpy(pszMsg, eCBRestDesc->uObjDesc.fldDesc.szName);
  324.                 Screen("\r\n### CallBack information:  Deleting the %s "
  325.                        "field...", pszMsg);
  326.  
  327.                 free(pszMsg);
  328.             }
  329.             else
  330.             {
  331.                 // Restructuring the new table's index.
  332.                 if(eCBRestDesc->eRestrObjType == restrNEWINDEX)
  333.                 {
  334.                     Screen("### CallBack information: Recreating the "
  335.                            "Primary index...\r\n");
  336.                 }
  337.  
  338.                 // Not one we are expecting.
  339.                 else
  340.                 {
  341.                     Screen("### In the callback function");
  342.                 }
  343.             }
  344.             break;
  345.  
  346.         default:
  347.             Screen("### In the callback function");
  348.     }
  349.  
  350.     return cbrUSEDEF;
  351. }
  352.