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

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