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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // softdel.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "cust";
  7. static const char szTblType[] = szDBASE;
  8.  
  9. static DBIResult AddRecord(hDBICur hCur, DFLOAT fID, pCHAR szName,
  10.                            pCHAR szStreet, pCHAR szCity, pCHAR szState_prov,
  11.                            pCHAR szZIP_PST_CD, pCHAR szCountry, pCHAR szPhone,
  12.                            UINT16 uDay, UINT16 uMonth, INT16 iYear);
  13.  
  14. static DBIResult FindRecord(hDBICur *phCur, DFLOAT fCust_ID);
  15.  
  16. //=====================================================================
  17. //  Function:
  18. //          SoftDel();
  19. //
  20. //  Description:
  21. //          This example shows how the state of the soft-delete flag
  22. //          affects which records are visible in a table. When the
  23. //          soft-delete property is OFF, deleted records are not
  24. //          displayed. When the soft-delete property is ON, deleted
  25. //          records are displayed.  Records are not actually deleted
  26. //          from the table until after the table has been packed.
  27. //=====================================================================
  28. void
  29. SoftDel (void)
  30. {
  31.     hDBIDb      hDb;                // Handle to the database
  32.     hDBICur     hCur;               // Handle to the table
  33.     DBIResult   rslt;               // Return value from IDAPI functions
  34.     BOOL        bSoftDelete;        // Soft-delete enabled?
  35.     UINT16      uLength;            // Space required to store property
  36.     DFLOAT      fCust_ID = 1000.0;  // Customer ID for the record which is
  37.                                     //   added to the table
  38.  
  39.     Screen("*** Soft Delete Example ***\r\n");
  40.  
  41.     BREAK_IN_DEBUGGER();
  42.  
  43.     Screen("    Initializing IDAPI...");
  44.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  45.     {                                        
  46.         Screen("\r\n*** End of Example ***");
  47.         return;
  48.     }
  49.  
  50.     Screen("    Setting the database directory...");
  51.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  52.     ChkRslt(rslt, "SetDirectory");
  53.  
  54.     Screen("    Open the %s table...", szTblName);
  55.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  56.                         "CUST.MDX", "CUST_NO", 0, dbiREADWRITE,
  57.                         dbiOPENEXCL, xltFIELD, FALSE, NULL, &hCur);
  58.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  59.     {
  60.         CloseDbAndExit(&hDb);
  61.         Screen("\r\n*** End of Example ***");
  62.         return;
  63.     }           
  64.  
  65.     Screen("    Switch to the CUST_NO index...");
  66.     rslt = DbiSwitchToIndex(&hCur, "CUST.MDX", "CUST_NO", NULL, FALSE);
  67.     ChkRslt(rslt, "SwitchToIndex");
  68.  
  69.     Screen("    Add a record to the table...");
  70.     if (AddRecord(hCur, fCust_ID, "SC Pro Divers, Ltd.", "71 Beach Hill",
  71.                   "Santa Cruz", "CA", "95060", "U.S.A.", "408-111-2222",
  72.                   3, 22, 1994)
  73.         != DBIERR_NONE)
  74.     {
  75.         CloseDbAndExit(&hDb);
  76.         Screen("\r\n*** End of Example ***");
  77.         return;
  78.     }
  79.  
  80.     Screen("    Delete the record which we have just added to the"
  81.            " table...");
  82.     rslt = DbiSetToBegin(hCur);
  83.     ChkRslt(rslt, "SetToBegin");
  84.  
  85.     // First need to find the record which we have added to the table.
  86.     if (FindRecord(&hCur, fCust_ID) == DBIERR_NONE)
  87.     {
  88.         // Need to move the current record pointer in the cursor to the
  89.         //   found record.
  90.         rslt = DbiGetNextRecord(hCur, dbiNOLOCK, NULL, NULL);
  91.         ChkRslt(rslt, "GetNextRecord");
  92.  
  93.         rslt = DbiDeleteRecord(hCur, NULL);
  94.         ChkRslt(rslt, "DeleteRecord");
  95.     }
  96.  
  97.     // Determine the state of soft-delete property.
  98.     rslt = DbiGetProp(hCur, curSOFTDELETEON, &bSoftDelete, sizeof(BOOL),
  99.                       &uLength);
  100.     ChkRslt(rslt, "GetProp");
  101.  
  102.     Screen("\r\n    Display the table with SoftDelete %s",
  103.            bSoftDelete ? "ON" : "OFF");
  104.  
  105.     rslt = DbiSetToBegin(hCur);
  106.     ChkRslt(rslt, "SetToBegin");
  107.     DisplayTable(hCur, 10);
  108.  
  109.     // Toggle the Soft-delete property.
  110.     bSoftDelete = !bSoftDelete;
  111.     rslt = DbiSetProp(hCur, curSOFTDELETEON, (UINT32) bSoftDelete);
  112.     ChkRslt(rslt, "SetProp");
  113.  
  114.     rslt = DbiGetProp(hCur, curSOFTDELETEON, &bSoftDelete, sizeof(BOOL),
  115.                       &uLength);
  116.     ChkRslt(rslt, "GetProp");
  117.  
  118.     Screen("\r\n    Display the table with SoftDelete %s",
  119.            bSoftDelete ? "ON" : "OFF");
  120.  
  121.     rslt = DbiSetToBegin(hCur);
  122.     ChkRslt(rslt, "SetToBegin");
  123.     DisplayTable(hCur, 10);
  124.  
  125.     // Pack the table. Note that packing is required in order to remove
  126.     //   deleted records from a dBASE table.
  127.     Screen("\r\n    Pack the table - physically delete the record"
  128.            " from the %s table...", szTblName);
  129.  
  130.     rslt = DbiPackTable(hDb, hCur, NULL, NULL, TRUE);
  131.     ChkRslt(rslt, "PackTable");
  132.  
  133.     Screen("\r\n    Display the table with SoftDelete %s after"
  134.            " packing...", bSoftDelete ? "ON" : "OFF");
  135.  
  136.     rslt = DbiSetToBegin(hCur);
  137.     ChkRslt(rslt, "SetToBegin");
  138.     DisplayTable(hCur, 10);
  139.  
  140.     Screen("\r\n    Close the %s table...", szTblName);
  141.     rslt = DbiCloseCursor(&hCur);
  142.     ChkRslt(rslt, "CloseCursor");
  143.  
  144.     Screen("    Close the database and exit IDAPI...");
  145.     CloseDbAndExit(&hDb);
  146.  
  147.     Screen("\r\n*** End of Example ***");
  148. }
  149.  
  150. //=====================================================================
  151. //  Code:   AddRecord(hCur, fID, szName, szStreet, szCity, szState_prov,
  152. //                    szZIP_PST_CD, szCountry, szPhone, uMonth, uDay,
  153. //                    INT16 iYear);
  154. //
  155. //  Input:  hCur            - Cursor to the table
  156. //          ID              - Customer ID
  157. //          szName          - Name of the company
  158. //          szStreet        - Street address
  159. //          szCity          - City
  160. //          szState_prov    - State/Province
  161. //          szZIP_PST_CD    - Zip or Postal code
  162. //          szCountry       - Country
  163. //          szPhone         - Phone number
  164. //          uMonth          - Month
  165. //          uDay            - Day
  166. //          Year            - Year
  167. //
  168. //  Return: Result of adding the record to the table
  169. //
  170. //  Description:
  171. //          This function will add a record to the given table.
  172. //=====================================================================
  173. DBIResult
  174. AddRecord (hDBICur hCur, DFLOAT fID, pCHAR szName, pCHAR szStreet,
  175.            pCHAR szCity, pCHAR szState_prov, pCHAR szZIP_PST_CD,
  176.            pCHAR szCountry, pCHAR szPhone, UINT16 uMonth, UINT16 uDay,
  177.            INT16 iYear)
  178. {
  179.     DBIResult   rslt;       // Value returned from IDAPI functions
  180.     CURProps    TblProps;   // Table properties
  181.     pBYTE       pRecBuf;    // Record buffer
  182.     DBIDATE     Date;       // Structure to contain the date
  183.  
  184.     // Allocate memory for the record buffer.
  185.     rslt = DbiGetCursorProps(hCur, &TblProps);
  186.     ChkRslt(rslt, "GetCursorProps");
  187.  
  188.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  189.     if (pRecBuf == NULL)
  190.     {
  191.         Screen("    Error - Out of memory");
  192.         return DBIERR_NOMEMORY;
  193.     }
  194.  
  195.     rslt = DbiInitRecord(hCur, pRecBuf);
  196.     ChkRslt(rslt, "InitRec");
  197.  
  198.     // Put the ID Number (fID) into the record buffer.
  199.     rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) &fID);
  200.     ChkRslt(rslt, "PutField");
  201.  
  202.     // Put the Name (szName) into the record buffer.
  203.     rslt = DbiPutField(hCur, 2, pRecBuf, (pBYTE) szName);
  204.     ChkRslt(rslt, "PutField");
  205.  
  206.     // Put the Street (szStreet) into the record buffer.
  207.     rslt = DbiPutField(hCur, 3, pRecBuf, (pBYTE) szStreet);
  208.     ChkRslt(rslt, "PutField");
  209.  
  210.     // Put the City (szCity) into the record buffer.
  211.     rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) szCity);
  212.     ChkRslt(rslt, "PutField");
  213.  
  214.     // Put the State (szState_prov) into the record buffer.
  215.     rslt = DbiPutField(hCur, 5, pRecBuf, (pBYTE) szState_prov);
  216.     ChkRslt(rslt, "PutField");
  217.  
  218.     // Put the Zip Code (szZIP_PST_CD) into the record buffer.
  219.     rslt = DbiPutField(hCur, 6, pRecBuf, (pBYTE) szZIP_PST_CD);
  220.     ChkRslt(rslt, "PutField");
  221.  
  222.     // Put the Country (szCountry) into the record buffer.
  223.     rslt = DbiPutField(hCur, 7, pRecBuf, (pBYTE) szCountry);
  224.     ChkRslt(rslt, "PutField");
  225.  
  226.     // Put the Phone number (szPhone) into the record buffer.
  227.     rslt = DbiPutField(hCur, 8, pRecBuf, (pBYTE) szPhone);
  228.     ChkRslt(rslt, "PutField");
  229.  
  230.     // Encode the date into the format which Paradox understands.
  231.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  232.     ChkRslt(rslt, "DateEncode");
  233.  
  234.     rslt = DbiPutField(hCur, 9, pRecBuf, (pBYTE)&Date);
  235.     ChkRslt(rslt, "PutField");
  236.  
  237.     Screen("    Insert the record into the table...");
  238.     rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
  239.     ChkRslt(rslt, "InsertRecord");
  240.  
  241.     free(pRecBuf);
  242.  
  243.     return rslt;
  244. }
  245.  
  246. //=====================================================================
  247. //  Code:   FindRecord(phCur, fCust_ID);
  248. //
  249. //  Input:  phCur       - pointer to the cursor handle
  250. //          fCust_ID    - Customer ID
  251. //
  252. //  Return: Result of finding the record
  253. //
  254. //  Description:
  255. //          This function will search the given table for the indicated
  256. //          record.
  257. //=====================================================================
  258. DBIResult
  259. FindRecord (hDBICur *phCur, DFLOAT fCust_ID)
  260. {
  261.     DBIResult   rslt;       // Return value from IDAPI functions
  262.     pBYTE       pRecBuf;    // Record buffer to contain the searched-for
  263.                             //   value
  264.     CURProps    TblProps;   // Properties of the table - used to determine
  265.                             //   the size of the record buffer
  266.  
  267.     // Create the key buffer.
  268.     rslt = DbiGetCursorProps(*phCur,&TblProps);
  269.     ChkRslt(rslt, "GetCursorProps");
  270.  
  271.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize);
  272.     if (pRecBuf == NULL)
  273.     {
  274.         Screen("        Error - Out of memory!");
  275.         return DBIERR_NOMEMORY;
  276.     }
  277.  
  278.     // Place the value which we are searching for in the record buffer.
  279.     //   In this case, we are searching for the Customer ID, which is the
  280.     //   first field of the table.
  281.     rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) &fCust_ID);
  282.     ChkRslt(rslt, "PutField");
  283.  
  284.     // The cust table has a primary index on the customer number.
  285.     //   Therefore we do not need to create an index to search on the
  286.     //   customer number.
  287.     rslt = DbiSetToKey(*phCur, keySEARCHEQ, FALSE, 0, 0, pRecBuf);
  288.     ChkRslt(rslt, "SetToKey");
  289.  
  290.     free(pRecBuf);
  291.  
  292.     return rslt;
  293. }
  294.