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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Filter.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "cust";
  7. static const char szTblType[] = szDBASE;
  8.  
  9. //=====================================================================
  10. //  Function:
  11. //          Filter();
  12. //
  13. //  Description:
  14. //          This example shows how to use filters to limit the result
  15. //          set of a table. Filters perform a function similar to that
  16. //          of ranges, but more powerful operations are supported.
  17. //=====================================================================
  18. void
  19. Filter (void)
  20. {
  21.     hDBIDb          hDb;            // Handle to the database.
  22.     hDBICur         hCur;           // Handle to the table.
  23.     pBYTE           pcanExpr;       // Structure containing filter info.
  24.     hDBIFilter      hFilter;        // Filter handle.
  25.     UINT16          uSizeNodes;     // Size of the nodes in the tree.
  26.     UINT16          uSizeCanExpr;   // Size of the header information.
  27.     UINT16          uSizeLiterals;  // Size of the literals.
  28.     UINT16          uTotalSize;     // Total size of the filter expression.
  29.     UINT32          uNumRecs  = 10; // Number of records to display.
  30.     CANExpr         canExp;         // Contains the header information.
  31.     UINT16          Nodes[] =       // Nodes of the filter tree.
  32.             {
  33.                 // Offset 0
  34.                 nodeBINARY,     // canBinary.nodeClass
  35.                 canGT,          // canBinary.canOp
  36.                 8,              // canBinary.iOperand1
  37.                 16,             // canBinary.iOperand2
  38.                                 //   Offsets in the Nodes array
  39.  
  40.                 // Offset 8
  41.                 nodeFIELD,      // canField.nodeClass
  42.                 canFIELD,       // canField.canOp
  43.                 1,              // canField.iFieldNum
  44.                 0,              // canField.iNameOffset: szField is the
  45.                                 //   literal at offset 0
  46.  
  47.                 // Offset 16
  48.                 nodeCONST,      // canConst.nodeClass
  49.                 canCONST,       // canConst.canOp
  50.                 fldFLOAT,       // canConst.iType
  51.                 8,              // canConst.iSize
  52.                 8               // canConst.iOffset: fconst is the
  53.                                 //   literal at offset strlen(szField) + 1
  54.             };
  55.  
  56.     CHAR            szField[] = "CUST_NO";  // Name of the field for the
  57.                                             //   third node of the tree.
  58.     DFLOAT          fConst    = 1500.0;     // Value of the constant for
  59.                                             //   the second node of the tree.
  60.     DBIResult       rslt;                   // Return value from IDAPI
  61.                                             //   functions.
  62.  
  63.     Screen("*** Filter Example ***\r\n");
  64.  
  65.     BREAK_IN_DEBUGGER();
  66.  
  67.     Screen("    Initializing IDAPI...");
  68.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  69.     {                                        
  70.         Screen("\r\n*** End of Example ***");
  71.         return;
  72.     }
  73.  
  74.     Screen("    Setting the database directory...");
  75.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  76.     ChkRslt(rslt, "SetDirectory");
  77.  
  78.     Screen("    Open the %s table...", szTblName);
  79.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  80.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  81.                         xltFIELD, FALSE, NULL, &hCur);
  82.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  83.     {
  84.         CloseDbAndExit(&hDb);
  85.         Screen("\r\n*** End of Example ***");
  86.         return;
  87.     }
  88.  
  89.     // Go to the beginning of the table
  90.     rslt = DbiSetToBegin(hCur);
  91.     ChkRslt(rslt, "SetToBegin");
  92.  
  93.     Screen("\r\n    Display the %s table...", szTblName);
  94.     DisplayTable(hCur, uNumRecs);
  95.  
  96.     uSizeNodes      = sizeof(Nodes); // Size of the nodes.
  97.     uSizeLiterals  = strlen(szField) + 1 + sizeof(fConst); // Size of the
  98.                                                            //  literals.
  99.     uSizeCanExpr    = sizeof(CANExpr); // Size of the header information.
  100.     uTotalSize      = uSizeCanExpr + uSizeNodes + uSizeLiterals; // Total size
  101.                                                                // of the filter.
  102.  
  103.     // Initialize the header information
  104.     canExp.iVer = 1; // Version.
  105.     canExp.iTotalSize = uTotalSize; // Total size of the filter.
  106.     canExp.iNodes = 3; // Number of nodes.
  107.     canExp.iNodeStart = uSizeCanExpr; // The offset in the buffer where the
  108.                                       //   expression nodes start.
  109.     canExp.iLiteralStart = uSizeCanExpr + uSizeNodes; // The offset in the
  110.                                                       //   buffer where the
  111.                                                       //   literals start.
  112.     
  113.     // Allocate space for the filter expression. 
  114.     pcanExpr = (pBYTE)malloc(uTotalSize * sizeof(BYTE));
  115.     if (pcanExpr == NULL)
  116.     {
  117.         Screen("    Could not allocate memory...");
  118.         DbiCloseCursor(&hCur);
  119.         CloseDbAndExit(&hDb);
  120.         Screen("\r\n*** End of Example ***");
  121.         return;
  122.     }
  123.     
  124.     // Initialize the filter expression.
  125.     memmove(pcanExpr, &canExp, uSizeCanExpr);
  126.     memmove(&pcanExpr[uSizeCanExpr], Nodes, uSizeNodes);
  127.  
  128.     memmove(&pcanExpr[uSizeCanExpr + uSizeNodes],
  129.             szField, strlen(szField) + 1); // First litteral
  130.     memmove(&pcanExpr[uSizeCanExpr + uSizeNodes + strlen(szField) + 1],
  131.             &fConst, sizeof(fConst)); // Second litteral
  132.  
  133.     rslt = DbiSetToBegin(hCur);
  134.     ChkRslt(rslt, "SetToBegin");
  135.  
  136.     Screen("\r\n    Add a filter to the %s table which will limit"
  137.            " the records\r\n        which are displayed to those whose"
  138.            " %s field is greater than %.1lf...", szTblName, szField, fConst);
  139.     rslt = DbiAddFilter(hCur, 0L, 1, FALSE, (pCANExpr)pcanExpr, NULL,
  140.                         &hFilter);
  141.     if (ChkRslt(rslt, "AddFilter") != DBIERR_NONE)
  142.     {
  143.         rslt = DbiCloseCursor(&hCur);
  144.         ChkRslt(rslt, "CloseCursor");
  145.         free(pcanExpr);
  146.         CloseDbAndExit(&hDb);
  147.         Screen("\r\n*** End of Example ***");
  148.         return;
  149.     }
  150.  
  151.     // Activate the filter.
  152.     Screen("    Activate the filter on the %s table...", szTblName);
  153.     rslt = DbiActivateFilter(hCur, hFilter);
  154.     ChkRslt(rslt, "ActivateFilter");
  155.  
  156.     rslt = DbiSetToBegin(hCur);
  157.     ChkRslt(rslt, "SetToBegin");
  158.  
  159.     Screen("\r\n    Display the %s table with the filter set...", szTblName);
  160.     DisplayTable(hCur, uNumRecs);
  161.  
  162.     Screen("\r\n    Deactivate the filter...");
  163.     rslt = DbiDeactivateFilter(hCur, hFilter);
  164.     ChkRslt(rslt, "DeactivateFilter");
  165.  
  166.     Screen("\r\n    Drop the filter...");
  167.     rslt = DbiDropFilter(hCur, hFilter);
  168.     ChkRslt(rslt, "DropFilter");
  169.  
  170.     rslt = DbiCloseCursor(&hCur);
  171.     ChkRslt(rslt, "CloseCursor");
  172.  
  173.     free(pcanExpr);
  174.  
  175.     Screen("    Close the database and exit IDAPI...");
  176.     CloseDbAndExit(&hDb);
  177.  
  178.     Screen("\r\n*** End of Example ***");
  179. }
  180.