OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME

OOFTST 18 - wildcard searches

This sample tests the wildcard searches.


#include "oofile.h"	// the general oofile library
#include "ooftst01.h"	// the declarations for the database classes we will be using in this test.
  
int main()
{
	cout << "OOFILE Validation Suite - Test 18\n"
		 << "Test of wildcard searching\n";
	  
	TEST_CONNECT    theDB;
	dbPeople     People;  

This is a bit of complicated filename logic to support different backends with this one test program 

#ifdef TESTING_CTREE
	theDB.useSeparateFiles();  
	#ifdef _Macintosh
		const char* kExistsName =  ":ooftst01:People.dat";
		const char* kDatabaseName = ":ooftst01:";
	#else
		const char* kExistsName =   "People.dat"
		const char* kDatabaseName = "";
	#endif	  
#else
	#ifdef TESTING_DBASE
		#ifdef _Macintosh
			const char* kExistsName =  ":ooftst01:People.dbf";
			const char* kDatabaseName = ":ooftst01:";
		#else
			const char* kExistsName =   "People.dbf"
			const char* kDatabaseName = "";
		#endif	  
	#else
		#ifdef _Macintosh
			const char* kDatabaseName = ":ooftst01:ooftst01.db";
		#else
			const char* kDatabaseName = "ooftst01.db";
		#endif	
		const char* kExistsName = kDatabaseName;
	#endif
#endif
  

Once we know what to call our files, we try and open them. If they do not exist, we create a new one and put in new test data.

	if (dbConnect::fileExists(kExistsName))
		theDB.openConnection(kDatabaseName);
	else {
		theDB.newConnection(kDatabaseName);
		People.AddTestData();
	}  

Now we create a dbView so we don't have to print out the whole database.

	dbView justNames(People);
	justNames << People.LastName << People.OtherNames;

We now set the sort order so that the records will be sorted by last names.

	People.setSortOrder(People.LastName);  

Now we do our main tests.

The first one tests for what's known as the 'pathological case of *. Becasue * stands for none or more letters, if we search for *, this means we are searching for everything.

Testing: wildcard search pathological *

	People.search(People.LastName=="*");
	cout << endl << "Testing pathological case '*' - should be all records" << endl 
		 << justNames << endl;

This test should give a more ordinary use of *, as part of a string. This search looks for records whose lastName has anything that starts with T, ends with r and has any number of characters in between (including none).

Testing: wildcard search * embedded in string

	cout << "Retrieving T*r: " << People[People.LastName=="T*r"].LastName << endl;  

Now we test the other main wildcard, that of ?. This replaces exactly one letter, but you can specify more than one (as in this example. For example, a search of c?t would return with strings of cut cot, cat or even c8t, but wouldn't return with curt or ct.

Testing: wildcard search ? embedded in string.

	People.search(People.LastName=="D??t");
	cout << "Listing two D??t records: " << endl << justNames << endl;  

This test uses ? just like the one above except that the word can start with any character ending with "mith".

Testing: wildcard search leading ?

	People.search(People.LastName=="?mith");
	cout << "Listing leading '?' with ?mith: " << endl << justNames << endl;  

Similar to the one above, but using * at the end. this means, the string must start with "De" but can have any ending (or no ending at all).

Testing: wildcard search trailing *

	People.search(People.LastName=="De*");
	cout << "Listing trailing single '*' with De*: " << endl << justNames << endl;  

Wildcard searches can be inverted too. If we decide we want all things that don't contain the string found in the search above, we put the != operator in front.

Testing: wildcard search inversion of trailing * using !=

	People.search(People.LastName!="De*");
	cout << "Testing not-equals wildcard with !De*: " << endl << justNames << endl;  

If we want to find any string that start with a certain substring, we can use the startsWith() function as below.

Testing: function startsWith() combined with wildcard search.

	People.search(People.LastName.startsWith("D*n"));
	cout << "Listing startsWith D*n: " << endl << justNames << endl;  

We'll now test the startsWith() function again, but with a different sunstring. The startsWith() function will give the same result as taking a given substring and adding a * to the end, so the string given in this example has an unnecessary * on the end.

Testing: function startsWith() with extra trailing *

	People.search(People.LastName.startsWith("D*n*"));
	cout << "Listing startsWith D*n*: " << endl << justNames << endl;  
	cout << "Test Completed" << endl;
	  
	return EXIT_SUCCESS;
}  

 

Feature index

(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001