Sun Dec 05, 2021 11:16 pm
Login Register Lost Password? Contact Us


Returning a Filter

Questions around writing code and queries

Thu Apr 18, 2019 5:00 pm Change Time Zone

Hi guys,

Im implementing a Roxie query and several filters should be applied to the query according to the options passed by the user (i.e. if a given parameter is not passed, the filter shouldnt be applied).
I was able to implement, but Im wondering a better way to do that and would like to use functions that return the filter.
Example:

Code: Select all
EXPORT myRoxieQuery () := FUNCTION
  Parms := STORED($.iUserInfo);
  myDataSet := DATASET('somefile', THOR);
  filter01 := FilterFunc01(iParams.name);
  filter02 := FilterFunc02(iParams.age);
  RETURN myDataSet(filter01, filter02);
END;


I would like to know if there is someway to implement something like this. The code would be much better (at least it would be cleaner).

Thanks in advance,

- Artur Baruchi
abaruchi
 
Posts: 19
Joined: Thu Apr 18, 2019 4:50 pm

Fri Apr 19, 2019 2:13 pm Change Time Zone

Artur ,
abaruchi wrote:Hi guys,

Im implementing a Roxie query and several filters should be applied to the query according to the options passed by the user (i.e. if a given parameter is not passed, the filter shouldnt be applied).
I was able to implement, but Im wondering a better way to do that and would like to use functions that return the filter.
Example:

Code: Select all
EXPORT myRoxieQuery () := FUNCTION
  Parms := STORED($.iUserInfo);
  myDataSet := DATASET('somefile', THOR);
  filter01 := FilterFunc01(iParams.name);
  filter02 := FilterFunc02(iParams.age);
  RETURN myDataSet(filter01, filter02);
END;


I would like to know if there is someway to implement something like this. The code would be much better (at least it would be cleaner).
You can do it like this:
Code: Select all
EXPORT myRoxieQuery () := FUNCTION
  Parms := STORED($.iUserInfo);
  myDataSet := DATASET('somefile', THOR);
  filter01 := IF(iParams.name='', TRUE,FilterFunc01(iParams.name));
  filter02 := IF(iParams.age=0, TRUE,FilterFunc02(iParams.age);
  RETURN myDataSet(filter01, filter02);
END;
So if no value is passed for either filter condition it just defaults to TRUE.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1606
Joined: Wed Oct 26, 2011 7:40 pm

Mon Apr 22, 2019 1:05 pm Change Time Zone

Hello Richard,
Thanks for your reply. I tried to apply the approach you shown, but when setting TRUE to a string or to a unsigned, an error occur, cause it should be boolean.

Code: Select all
Incompatible types: expected Integer, given Boolean,


I tried to use something like this:

Code: Select all
EXPORT myRoxieQuery () := FUNCTION
  Parms := STORED($.iUserInfo);
  myDataSet := DATASET('somefile', THOR);
  filter01 := IF(iParams.name='', name <> '', name=iParams.name);
  filter02 := IF(iParams.age=0, age > 0, age=iParams.age);
  RETURN myDataSet(filter01, filter02);
END


Don't know if it is the best way to do it, since I'm applying some filter (i.e. if a typo is made, and someone has a negative age, it should be returned).

Thanks,

- Artur Baruchi
abaruchi
 
Posts: 19
Joined: Thu Apr 18, 2019 4:50 pm

Tue Apr 23, 2019 5:44 pm Change Time Zone

Artur,
I tried to apply the approach you shown, but when setting TRUE to a string or to a unsigned, an error occur, cause it should be boolean.
Yes. Because a filter must always be a Boolean expression, I assumed that your FilterFunc01 and FilterFunc02 functions returned Boolean results. Replacing those functions with Boolean expressions is the appropriate thing to do, but you still need to use TRUE as your filter expression if no parameter is passed, like this:
Code: Select all
EXPORT myRoxieQuery () := FUNCTION
  Parms := STORED($.iUserInfo);
  myDataSet := DATASET('somefile', THOR);
  filter01 := IF(iParams.name='', TRUE, name=iParams.name);
            //all recs if no name passed, otherwise filter for passed name
  filter02 := IF(iParams.age=0, TRUE, age=iParams.age);
            //all recs if no age passed, otherwise filter for passed age
  RETURN myDataSet(filter01, filter02);
END

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1606
Joined: Wed Oct 26, 2011 7:40 pm

Thu Apr 25, 2019 12:47 pm Change Time Zone

Hi,

Not sure If you've seen my post on 'Multi-dimensional, Multi-variable' searches.

https://hpccsystems.com/bb/viewtopic.php?f=41&t=6183

As I understand your post it seems to address your problem?

Yours
Allan
Allan
 
Posts: 442
Joined: Sat Oct 01, 2011 7:26 pm


Return to Programming

Who is online

Users browsing this forum: Bing [Bot] and 1 guest