Filters

Chapter Updated 06/24/25




The objective of this chapter is to provide information regarding functionality for setting up table filters.


The xFilter class is used to establish a data filter on a given table (DBF file).

  1. A filter can be established to limit selected records in a query.
  2. Similar to a WHERE clause in SQL.
  3. The filter logic uses the expression module for processing.
  4. Any of the expression routines that return a logical value can be used to establish a filter condition.
  5. See chapter four or the xb_ex_expression sample program for more information on valid expressions.


Methods for xbFilter

MethodDescriptionParms
xbFilter( xbDbf *dbf )ConstructorPointer to main xbase structure.
~xbFilter()Destructor
xbInt16 Set( xbString &sFilterExpression )
xbInt16 Set( xbString &sFilterExpression, xbIx *ix, void *vpTag )
Set filter sFilterExpression - Filter Expression (AGE > 27). xbIx - Pointer to optional index file if using an index. vpTag - Pointer to optional index tag if using an index.
xbInt16 SetIxTag( xbIx *ix, void *vpTag )
xbInt16 SetIxTag( const xbString &sTagName )
Set index on a given filter. xbIx - Pointer to index file. vpTag - Pointer to index tag. sTagName - Tag Name.
xbInt16 GetFirstRecord( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the first qualified record (not indexed).
xbInt16 GetNextRecord ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the next qualified record (not indexed).
xbInt16 GetPrevRecord ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the previous qualified record (not indexed).
xbInt16 GetLastRecord ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the last qualified record (not indexed).
void SetLimit( xbInt32 ulLimit ) Set the max unmber of records limit.
xbInt32 GetLimit() const Get the current MaxLimit setting.
void ResetQryCnt() Rest the limit counter.
xbInt32 GetQryCnt() const Return the current query count.
xbInt16 Find( const xbString &sKey, xbInt16 iOpt = XB_ACTIVE_RECS )
xbInt16 Find( const xbDate &dtKey, xbInt16 iOpt = XB_ACTIVE_RECS )
xbInt16 Find( const xbDouble &dKey, xbInt16 iOpt = XB_ACTIVE_RECS )
Find a filtered record for a given index key. sKey - String Key.
dtKey - Date Key.
dKey - Numeric (xbDouble) Key. iOpt - is one of XB_ACTIVE_RECS, XB_ALL_RECS, XB_DELETED_RECS
xbInt16 GetFirstRecordIx( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the first qualified record for specified index key.
xbInt16 GetNextRecordIx ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the first qualified record for specified index key.
xbInt16 GetPrevRecordIx ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the first qualified record for specified index key.
xbInt16 GetLastRecordIx ( xbInt16 iOpt = XB_ACTIVE_RECS ) Get the first qualified record for specified index key.


Example program demonstrating xbFilter class.

/* xb_ex_filter.cpp XBase64 Software Library Copyright (c) 1997,2003,2014,2020,2022,2023 Gary A Kunkel The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net */ // This program demonstrates usage of the xbFilter class // It uses the DBF table and NDX index file that were created by the xb_test_filter program. #include "xbase.h" using namespace xb; int main( int argCnt, char **av ) { int iRc = 0; xbXBase x; x.SetDataDirectory( PROJECT_DATA_DIR ); x.EnableMsgLogging(); x.SetLogSize( 1000000L ); #ifdef XB_DBF4_SUPPORT xbDbf *myTable = new xbDbf4( &x ); #else xbDbf *myTable = new xbDbf3( &x ); #endif if(( iRc = myTable->Open( "TestFilt.dbf" )) != XB_NO_ERROR ){ std::cout << "Error opening TestFilt.dbf" << std::endl; std::cout << "Table TestFile.dbf created in program xb_test_filter.cpp" << std::endl; x.DisplayError( iRc ); return 1; } #ifdef XB_BLOCKREAD_SUPPORT myTable->EnableBlockReadProcessing(); // speed things up #endif xbFilter myFilter( myTable ); myFilter.Set( "ZFLD = 'Z'" ); std::cout << "\nLoop through table forwards"; iRc = myFilter.GetFirstRecord(); while( iRc == XB_NO_ERROR ){ myTable->DumpRecord( myTable->GetCurRecNo(), 1, 0, ',' ); iRc = myFilter.GetNextRecord(); } std::cout << "\nLoop through table backwards"; iRc = myFilter.GetLastRecord(); while( iRc == XB_NO_ERROR ){ myTable->DumpRecord( myTable->GetCurRecNo(), 1, 0, ',' ); iRc = myFilter.GetPrevRecord(); } // Index example #ifdef XB_NDX_SUPPORT if(( iRc = myTable->OpenIndex( "NDX", "TestFilt.NDX")) != XB_NO_ERROR ){ std::cout << "Error opening TestFilt.NDX" << std::endl; x.DisplayError( iRc ); return 1; } xbFilter myFilterIx( myTable ); myFilterIx.Set( "ZFLD = 'Z'", "TestFilt" ); std::cout << "\nLoop through table forwards by index"; iRc = myFilterIx.GetFirstRecordIx(); while( iRc == XB_NO_ERROR ){ myTable->DumpRecord( myTable->GetCurRecNo(), 1, 0, ',' ); iRc = myFilterIx.GetNextRecordIx(); } std::cout << "\nLoop through table backwards by index"; iRc = myFilterIx.GetLastRecordIx(); while( iRc == XB_NO_ERROR ){ myTable->DumpRecord( myTable->GetCurRecNo(), 1, 0, ',' ); iRc = myFilterIx.GetPrevRecordIx(); } #endif // XB_NDX_SUPPORT x.CloseAllTables(); return 0; }