home *** CD-ROM | disk | FTP | other *** search
- // BDE32 3.x - (C) Copyright 1996 by Borland International
-
- // Filter.c
- #include "snipit.h"
-
- static const char szTblName[] = "cust"; // Name of the table
- static const char szTblType[] = szDBASE; // Type of table
- static const char szField[] = "CUST_NO"; // Name of the field for the
- // third node of the tree.
- static const DFLOAT fConst = 1500.0; // Value of the constant for
- // the second node of the tree.
-
- //=====================================================================
- // Function:
- // Filter();
- //
- // Description:
- // This example shows how to use filters to limit the result
- // set of a table. Filters perform a function similar to that
- // of ranges, but more powerful operations are supported.
- //=====================================================================
- void
- Filter (void)
- {
- hDBIDb hDb = 0; // Handle to the database.
- hDBICur hCur = 0; // Handle to the table.
- DBIResult rslt; // Return value from IDAPI functions.
- pBYTE pcanExpr; // Structure containing filter info.
- hDBIFilter hFilter; // Filter handle.
- UINT16 uSizeNodes; // Size of the nodes in the tree.
- UINT16 uSizeCanExpr; // Size of the header information.
- UINT32 uSizeLiterals; // Size of the literals.
- UINT32 uTotalSize; // Total size of the filter expression.
- UINT32 uNumRecs = 10; // Number of records to display.
- CANExpr canExp; // Contains the header information.
- struct {
- CANBinary BinaryNode;
- CANField FieldNode;
- CANConst ConstantNode;
- }
- Nodes = { // Nodes of the filter tree.
- {
- // Offset 0
- nodeBINARY, // canBinary.nodeClass
- canGT, // canBinary.canOp
- sizeof(Nodes.BinaryNode), // canBinary.iOperand1
- sizeof(Nodes.BinaryNode) + sizeof(Nodes.FieldNode),
- // canBinary.iOperand2
- // Offsets in the Nodes array
- },
- {
- // Offset sizeof(Nodes.BinaryNode)
- nodeFIELD, // canField.nodeClass
- canFIELD, // canField.canOp
- 1, // canField.iFieldNum
- 0, // canField.iNameOffset: szField is the
- // literal at offset 0
- },
- {
- // Offset sizeof(Nodes.BinaryNode) + sizeof(Nodes.FieldNode)
- nodeCONST, // canConst.nodeClass
- canCONST, // canConst.canOp
- fldFLOAT, // canConst.iType
- sizeof(fConst), // canConst.iSize
- 8, // canConst.iOffset: fconst is the
- // literal at offset strlen(szField) + 1
- }};
-
- Screen("*** Filter Example ***\r\n");
-
- BREAK_IN_DEBUGGER();
-
- Screen(" Initializing IDAPI...");
-
- if (InitAndConnect(&hDb) != DBIERR_NONE)
- {
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Setting the database directory...");
- rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- Screen(" Open the %s table...", szTblName);
- rslt = DbiOpenTable(hDb, (pCHAR)szTblName, (pCHAR)szTblType, NULL, NULL, 0,
- dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL,
- &hCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Go to the beginning of the table
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen("\r\n Display the %s table...", szTblName);
- DisplayTable(hCur, uNumRecs);
-
- uSizeNodes = sizeof(Nodes); // Size of the nodes.
- uSizeLiterals = (UINT16)(strlen((pCHAR)szField) + 1 + sizeof(fConst));
- // Size of the literals.
- uSizeCanExpr = sizeof(CANExpr); // Size of the header information.
- uTotalSize = (UINT16)(uSizeCanExpr + uSizeNodes + uSizeLiterals);
- // Total size of the filter.
-
- // Initialize the header information
- canExp.iVer = 1; // Version.
- canExp.iTotalSize = (UINT16)uTotalSize; // Total size of the filter.
- canExp.iNodes = 3; // Number of nodes.
- canExp.iNodeStart = uSizeCanExpr; // The offset in the buffer where the
- // expression nodes start.
- canExp.iLiteralStart = (UINT16)(uSizeCanExpr + uSizeNodes);
- // The offset in the buffer where the
- // literals start.
-
- // Allocate contiguous memory space to hold
- // 1) Header information i.e. the CANExpr structure
- // 2) Binary, field and constant nodes i.e. the Nodes structure
- // 3) Literal and constant pool i.e. field names and constant values
- pcanExpr = (pBYTE)malloc(uTotalSize * sizeof(BYTE));
- if (pcanExpr == NULL)
- {
- Screen(" Could not allocate memory...");
- DbiCloseCursor(&hCur);
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Initialize the filter expression by placing header, nodes and
- // pool into pcanexpr
-
- // Move header information into pcanexpr. pcanExpr will now look as follows:
- // **canExp**| | |
- // | CANExpr | All Nodes | Literal, Constant Pool |
- // |----------------------------------------------------|
- // 0 sizeof(uTotalSize)
- memmove(pcanExpr, &canExp, uSizeCanExpr);
-
- // Move node structure into pcanexpr. pcanExpr will now look as follows:
- // |**canExp*|**Node Structure*| |
- // | CANExpr | All Nodes | Literal, Constant Pool |
- // |----------------------------------------------------|
- // 0 sizeof(uTotalSize)
- memmove(&pcanExpr[uSizeCanExpr], &Nodes, uSizeNodes);
-
- // Move the literal into pcanexpr. pcanExpr will now look as follows:
- // |**canExp*|**Node Structure*|***szField |
- // | CANExpr | All Nodes | Literal, Constant Pool |
- // |----------------------------------------------------|
- // 0 sizeof(uTotalSize)
- memmove(&pcanExpr[uSizeCanExpr + uSizeNodes],
- szField, strlen(szField) + 1); // First literal "CUST_NO"
-
- // Move the literal into pcanexpr. pcanExpr will now look as follows:
- // |**canExp*|**Node Structure*|***szField*****fConst***|
- // | CANExpr | All Nodes | Literal, Constant Pool |
- // |----------------------------------------------------|
- // 0 sizeof(uTotalSize)
- memmove(&pcanExpr[uSizeCanExpr + uSizeNodes + strlen(szField) + 1],
- &fConst, sizeof(fConst)); // Second literal 1500.00
-
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen("\r\n Add a filter to the %s table which will limit"
- " the records\r\n which are displayed to those whose"
- " %s field is greater than %.1lf...", szTblName, szField, fConst);
- rslt = DbiAddFilter(hCur, 0L, 1, FALSE, (pCANExpr)pcanExpr, NULL,
- &hFilter);
- if (ChkRslt(rslt, "AddFilter") != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- free(pcanExpr);
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Activate the filter.
- Screen(" Activate the filter on the %s table...", szTblName);
- rslt = DbiActivateFilter(hCur, hFilter);
- ChkRslt(rslt, "ActivateFilter");
-
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen("\r\n Display the %s table with the filter set...", szTblName);
- DisplayTable(hCur, uNumRecs);
-
- Screen("\r\n Deactivate the filter...");
- rslt = DbiDeactivateFilter(hCur, hFilter);
- ChkRslt(rslt, "DeactivateFilter");
-
- Screen("\r\n Drop the filter...");
- rslt = DbiDropFilter(hCur, hFilter);
- ChkRslt(rslt, "DropFilter");
-
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
-
- free(pcanExpr);
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb);
-
- Screen("\r\n*** End of Example ***");
- }
-