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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // FiltCont.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "customer";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. //===============================================================
  10. //  Function:
  11. //          FiltCont();
  12. //
  13. //  Description:
  14. //          This example shows how to use filters to limit the
  15. //          result set of a table.
  16. //
  17. //          This example shows the use of a canContinue node. A
  18. //          Continue node is used to stop evaluating when a certain
  19. //          condition is false for the first time.
  20. //
  21. //          This filter will limit the result set to those
  22. //          customers living in Hawaii, you are listed in the
  23. //          table before the customer with ID 1624.
  24. //
  25. //          Picture of this filter expression:
  26. //
  27. //                                AND
  28. //                              /     \
  29. //                             /       \
  30. //                            /         \
  31. //                           /           \
  32. //                        EQ           Continue
  33. //                      /    \            |
  34. //                     /      \           |
  35. //                    /        \          |
  36. //                 Field 2    szConst    NE
  37. //              (STATE/PROV)   (HI)     /  \
  38. //                                     /    \
  39. //                                    /      \
  40. //                                Field1     fConst
  41. //                             (CUSTOMER NO)(1624.0)
  42. //
  43. //==============================================================
  44. void
  45. FiltCont (void)
  46. {
  47.     hDBIDb          hDb;            // Handle to the database.
  48.     hDBICur         hCur;           // Handle to the table.
  49.     pBYTE           pcanExpr;       // Structure containing
  50.                                     //   filter info.
  51.     hDBIFilter      hFilter;        // Filter handle.
  52.     UINT16          uSizeNodes;     // Size of the nodes in the
  53.                                     //   tree.
  54.     UINT16          uSizeCanExpr;   // Size of the header
  55.                                     //   information.
  56.     UINT16          uSizeLiterals;  // Size of the literals.
  57.     UINT16          uTotalSize;     // Total size of the filter
  58.                                     //   expression.
  59.     UINT32          uNumRecs  = 10; // Number of records to
  60.                                     //   display.
  61.     CANExpr         canExp;         // Contains the header
  62.                                     //   information.
  63.  
  64.     UINT16          Nodes[] =       // Nodes of the filter tree.
  65.             {
  66.                 // Offset 0. Node 1.
  67.                 nodeBINARY,     // canBinary.nodeClass
  68.                 canAND,         // canBinary.canOp
  69.                 8,              // canBinary.iOperand1 - node 2
  70.                 34,             // canBinary.iOperand2 - node 5
  71.                                 //   Offsets in the Nodes array
  72.  
  73.                 // Offset 8. Node 2.
  74.                 nodeBINARY,     // canBinary.nodeClass
  75.                 canEQ ,         // canBinary.canOp
  76.                 16,             // canBinary.iOperand1 - node 3
  77.                 24,             // canBinary.iOperand2 - node 4
  78.                                 //   Offsets in the Nodes array
  79.  
  80.                 // Offset 16. Node 3.
  81.                 nodeFIELD,      // canField.nodeClass
  82.                 canFIELD,       // canField.canOp
  83.                 5,              // canField.iFieldNum
  84.                 11,             // canField.iNameOffset: szField2
  85.                                 //   is the literal at offset
  86.                                 //   strlen(szField1) + 1
  87.  
  88.                 // Offset 24. Node 4.
  89.                 nodeCONST,      // canConst.nodeClass
  90.                 canCONST,       // canConst.canOp
  91.                 fldZSTRING,     // canConst.iType
  92.                 3,              // canConst.iSize
  93.                 31,             // canConst.iOffset: fconst is
  94.                                 //   the literal at offset
  95.                                 //   strlen(szField1) + 1 +
  96.                                 //   sizeof(fConst) +
  97.                                 //   strlen(szField2) + 1
  98.  
  99.                 // Offset 34. Node 5.
  100.                 nodeUNARY,      // canBinary.nodeClass
  101.                 canCONTINUE,    // canBinary.canOp
  102.                 40,             // canBinary.iOperand1 - node 6
  103.                                 //   Offsets in the Nodes array
  104.  
  105.                 // Offset 40. Node 6.
  106.                 nodeBINARY,     // canBinary.nodeClass
  107.                 canNE ,         // canBinary.canOp
  108.                 48,             // canBinary.iOperand1 - node 7
  109.                 56,             // canBinary.iOperand2 - node 8
  110.                                 //   Offsets in the Nodes array
  111.  
  112.                 // Offset 48. Node 7.
  113.                 nodeFIELD,      // canField.nodeClass
  114.                 canFIELD,       // canField.canOp
  115.                 1,              // canField.iFieldNum
  116.                 0,              // canField.iNameOffset:
  117.                                 //   szField1 is the literal at
  118.                                 //   offset 0.
  119.  
  120.                 // Offset 56. Node 1.
  121.                 nodeCONST,      // canConst.nodeClass
  122.                 canCONST,       // canConst.canOp
  123.                 fldFLOAT,       // canConst.iType
  124.                 8,              // canConst.iSize
  125.                 23,             // canConst.iOffset: fconst is
  126.                                 //   the literal at offset
  127.                                 //   strlen(szField1) + 1 +
  128.                                 //   strlen(szField2) + 1
  129.             };
  130.  
  131.     // Name of the field for the third node of the tree.
  132.     CHAR            szField1[] = "CUSTOMER NO";
  133.     // Name of the field for the third node of the tree.
  134.     CHAR            szField2[] = "STATE/PROV";
  135.     // Value of the constant for the second node of the tree.
  136.     FLOAT           fConst     = 1624.0;
  137.     // Value of the constant for the second node of the tree.
  138.     // Field #7
  139.     CHAR            szConst[]  = "HI";
  140.  
  141.     DBIResult       rslt; // Return value from IDAPI functions.
  142.  
  143.     Screen("*** Continue Filter Example ***\r\n");
  144.  
  145.     BREAK_IN_DEBUGGER();
  146.  
  147.     Screen("    Initializing IDAPI...");
  148.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  149.     {                                        
  150.         Screen("\r\n*** End of Example ***");
  151.         return;
  152.     }
  153.  
  154.     Screen("    Setting the database directory...");
  155.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  156.     ChkRslt(rslt, "SetDirectory");
  157.  
  158.     Screen("    Open the %s table...", szTblName);
  159.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName,
  160.                         (pCHAR) szTblType,
  161.                         NULL, NULL, 0, dbiREADWRITE,
  162.                         dbiOPENSHARED, xltFIELD, FALSE, NULL,
  163.                         &hCur);
  164.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  165.     {
  166.         CloseDbAndExit(&hDb);
  167.         Screen("\r\n*** End of Example ***");
  168.         return;
  169.     }
  170.  
  171.     // Go to the beginning of the table
  172.     rslt = DbiSetToBegin(hCur);
  173.     ChkRslt(rslt, "SetToBegin");
  174.  
  175.     Screen("\r\n    Display the %s table...", szTblName);
  176.     DisplayTable(hCur, uNumRecs);
  177.  
  178.     // Size of the nodes.
  179.     uSizeNodes      = sizeof(Nodes);
  180.     // Size of the literals.
  181.     uSizeLiterals   = strlen(szField1) + 1 + sizeof(fConst) +
  182.                       strlen(szField2) + 1 + strlen(szConst)
  183.                       + 1;
  184.     // Size of the header information.
  185.     uSizeCanExpr    = sizeof(CANExpr);
  186.     // Total size of the filter.
  187.     uTotalSize      = uSizeCanExpr + uSizeNodes + uSizeLiterals;
  188.     // Initialize the header information
  189.     canExp.iVer = 1; // Version.
  190.     canExp.iTotalSize = uTotalSize; // Total size of the filter.
  191.     canExp.iNodes = 8; // Number of nodes.
  192.     canExp.iNodeStart = uSizeCanExpr; // The offset in the
  193.                                       //   buffer where the
  194.                                       //   expression nodes
  195.                                       //   start.
  196.     // The offset in the buffer where the literals start.
  197.     canExp.iLiteralStart = uSizeCanExpr + uSizeNodes;
  198.     
  199.     // Allocate space for the filter expression. 
  200.     pcanExpr = (pBYTE)malloc(uTotalSize * sizeof(BYTE));
  201.     if (pcanExpr == NULL)
  202.     {
  203.         Screen("    Could not allocate memory...");
  204.         DbiCloseCursor(&hCur);
  205.         CloseDbAndExit(&hDb);
  206.         Screen("\r\n*** End of Example ***");
  207.         return;
  208.     }
  209.     
  210.     // Initialize the filter expression.
  211.     memmove(pcanExpr, &canExp, uSizeCanExpr);
  212.     memmove(&pcanExpr[uSizeCanExpr], Nodes, uSizeNodes);
  213.  
  214.     memmove(&pcanExpr[uSizeCanExpr + uSizeNodes],
  215.             szField1, strlen(szField1) + 1); // First litteral
  216.  
  217.     memmove(&pcanExpr[(uSizeCanExpr + uSizeNodes +
  218.                       strlen(szField1) + 1)],
  219.             szField2, strlen(szField2) + 1); // First litteral
  220.  
  221.  
  222.     memmove(&pcanExpr[(uSizeCanExpr + uSizeNodes +
  223.                       strlen(szField1) + 1 +
  224.                       strlen(szField2) + 1)],
  225.             &fConst, sizeof(fConst)); // Second litteral
  226.  
  227.     memmove(&pcanExpr[(uSizeCanExpr + uSizeNodes +
  228.                       strlen(szField1) + 1 +
  229.                       strlen(szField2) + 1 + sizeof(fConst))],
  230.             szConst, strlen(szConst) + 1); // First litteral
  231.  
  232.     rslt = DbiSetToBegin(hCur);
  233.     ChkRslt(rslt, "SetToBegin");
  234.  
  235.     Screen("\r\n    Add a filter to the %s table which will"
  236.            " limit the records\r\n        in the result set"
  237.            " to those whose %s field is equal to '%s', until\r\n"
  238.            "        the first record where the %s field is equal to %.1lf...",
  239.            szTblName, szField2, szConst, szField1, fConst);
  240.     rslt = DbiAddFilter(hCur, 0L, 1, FALSE, (pCANExpr)pcanExpr,
  241.                         NULL, &hFilter);
  242.     if (ChkRslt(rslt, "AddFilter") != DBIERR_NONE)
  243.     {
  244.         rslt = DbiCloseCursor(&hCur);
  245.         ChkRslt(rslt, "CloseCursor");
  246.         free(pcanExpr);
  247.         CloseDbAndExit(&hDb);
  248.         Screen("\r\n*** End of Example ***");
  249.         return;
  250.     }
  251.  
  252.     // Activate the filter.
  253.     Screen("    Activate the filter on the %s table...",
  254.            szTblName);
  255.     rslt = DbiActivateFilter(hCur, hFilter);
  256.     ChkRslt(rslt, "ActivateFilter");
  257.  
  258.     rslt = DbiSetToBegin(hCur);
  259.     ChkRslt(rslt, "SetToBegin");
  260.  
  261.     Screen("\r\n    Display the %s table with the filter"
  262.            " set...", szTblName);
  263.     DisplayTable(hCur, uNumRecs);
  264.  
  265.     Screen("\r\n    Deactivate the filter...");
  266.     rslt = DbiDeactivateFilter(hCur, hFilter);
  267.     ChkRslt(rslt, "DeactivateFilter");
  268.  
  269.     Screen("\r\n    Drop the filter...");
  270.     rslt = DbiDropFilter(hCur, hFilter);
  271.     ChkRslt(rslt, "DropFilter");
  272.  
  273.     rslt = DbiCloseCursor(&hCur);
  274.     ChkRslt(rslt, "CloseCursor");
  275.  
  276.     free(pcanExpr);
  277.  
  278.     Screen("    Close the database and exit IDAPI...");
  279.     CloseDbAndExit(&hDb);
  280.  
  281.     Screen("\r\n*** End of Example ***");
  282. }
  283.