home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / SOFTDEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  10.3 KB  |  294 lines

  1. // BDE32 3.x - (C) Copyright 1996 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. static const DFLOAT fCust_ID = 1000.0;  // Customer ID for the record which is
  9.                                         // added to the table
  10.  
  11.  
  12. static DBIResult AddRecord(hDBICur hCur, DFLOAT fID, pCHAR szName,
  13.                            pCHAR szStreet, pCHAR szCity, pCHAR szState_prov,
  14.                            pCHAR szZIP_PST_CD, pCHAR szCountry, pCHAR szPhone,
  15.                            UINT16 uDay, UINT16 uMonth, INT16 iYear);
  16.  
  17. static DBIResult FindRecord(hDBICur *phCur, DFLOAT fCust_ID);
  18.  
  19. //=====================================================================
  20. //  Function:
  21. //          SoftDel();
  22. //
  23. //  Description:
  24. //          This example shows how the state of the soft-delete flag
  25. //          affects which records are visible in a table. When the
  26. //          soft-delete property is OFF, deleted records are not
  27. //          displayed. When the soft-delete property is ON, deleted
  28. //          records are displayed.  Records are not actually deleted
  29. //          from the table until after the table has been packed.
  30. //=====================================================================
  31. void
  32. SoftDel (void)
  33. {
  34.     hDBIDb      hDb = 0;            // Handle to the database
  35.     hDBICur     hCur = 0;           // Handle to the table
  36.     DBIResult   rslt;               // Return value from IDAPI functions
  37.     BOOL        bSoftDelete;        // Soft-delete enabled?
  38.     UINT16      uLength;            // Space required to store property
  39.  
  40.     Screen("*** Soft Delete Example ***\r\n");
  41.  
  42.     BREAK_IN_DEBUGGER();
  43.  
  44.     Screen("    Initializing IDAPI...");
  45.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  46.     {
  47.         Screen("\r\n*** End of Example ***");
  48.         return;
  49.     }
  50.  
  51.     Screen("    Setting the database directory...");
  52.     rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
  53.     ChkRslt(rslt, "SetDirectory");
  54.  
  55.     Screen("    Open the %s table...", szTblName);
  56.     rslt = DbiOpenTable(hDb, (pCHAR)szTblName, (pCHAR)szTblType, "CUST.MDX",
  57.                         "CUST_NO", 0, dbiREADWRITE, dbiOPENEXCL, xltFIELD,
  58.                         FALSE, NULL, &hCur);
  59.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  60.     {
  61.         CloseDbAndExit(&hDb);
  62.         Screen("\r\n*** End of Example ***");
  63.         return;
  64.     }
  65.  
  66.     Screen("    Switch to the CUST_NO index...");
  67.     rslt = DbiSwitchToIndex(&hCur, "CUST.MDX", "CUST_NO", NULL, FALSE);
  68.     ChkRslt(rslt, "SwitchToIndex");
  69.  
  70.     Screen("    Add a record to the table...");
  71.     if (AddRecord(hCur, fCust_ID, "SC Pro Divers, Ltd.", "71 Beach Hill",
  72.                   "Santa Cruz", "CA", "95060", "U.S.A.", "408-111-2222",
  73.                   3, 22, 1996) != 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
  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("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.