Friday, July 16, 2004

Beware of how the FILTER() works

Some older applications use the FILTER ( ) function in Visual FoxPro to identify what the current filter on a file is.
 
Be warned that what this function returns isn't always what a filter was originally set to.
 
Here are some examples:
 
USE HOME(2) + "TASTRADE\DATA\CUSTOMER"
SET FILTER TO region<>"NC"
 
? FILTER ( )
 
This Returns region#"NC"
 
But it gets better than this:
SET FILTER TO (company_name!="Alfred" OR region<>"NC") AND country = "United States"
 
Returns (COMPANY_NAME#"Alfred".OR.REGION#"NC").AND.COUNTRY="United States"
 
For most cases, this won't hurt anyone's code but it's a real pain when you're trying to translate the FILTER() statement to match an original filter. For example, in one application, we have a table that looks like this:
 
cDesc C(30)
cFilter M
 
The cDesc field contains a friendly name like "Important Customers" or "Customers who I need to call" and the filter contains a valid FoxPro expression.
 
The problem is unless you store the variable of the Desc somewhere else, you'll never be able to properly identify what the description is without major work. What kind of work? Well, here's a sample of some code:
 
SELECT * FROM FILTERS INTO ARRAY laFilters
lcRealFilter = UPPER(ALLTRIM(FILTER() ))

FOR lnI=1 TO ALEN(laFilters,1)

lcValue=UPPER(ALLTRIM(lafilters(lnI,2)))

IF lcValue==lcFilter
    lnValue=lnI
** Match found
EXIT
ELSE

ENDIF

ENDFOR

(Note: no - this code isn't optimized!)

But then I also have to check all of these individual conditions:

lcValue=STRTRAN(lcValue,' OR ','.OR.')
lcValue=STRTRAN(lcValue,' $ ','$')
lcValue=STRTRAN(lcValue,['],["])
lcValue=STRTRAN(lcValue,' AND ','.AND.')
lcValue = STRTRAN(lcValue,"<>","#")
lcValue = STRTRAN(lcValue,"!=","#")
lcValue = STRTRAN(lcValue,"!",".NOT.")

It's not pretty but it works. Are there any others? Let me know.

No comments: