Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed lower case for "IN" operator in queryFilters [DHIS2-11770] #8724

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static org.hisp.dhis.common.QueryOperator.IN;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -63,13 +65,16 @@ public InQueryFilter( String field, QueryFilter queryFilter )
* @param encodedFilter actual "in" parameters
* @return a SQL condition representing this InQueryFilter
*/
public String getSqlFilter( String encodedFilter )
public String getSqlFilter( String encodedFilter, boolean isText )
Copy link
Member

@larshelge larshelge Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a description to explain the initial problem and why this fixes it :-) My understanding is we already quote event analytics query filters but I might be wrong.

{
List<String> filterItems = getFilterItems( encodedFilter );
String condition = "";
if ( hasNonMissingValue( filterItems ) )
{
condition = field + " " + operator.getValue() + streamOfNonMissingValues( filterItems )
.filter( Objects::nonNull )
.map( item -> toLowerIfNecessary( item, isText ) )
.map( item -> quoteIfNecessary( item, isText ) )
.collect( Collectors.joining( ",", " (", ")" ) );
if ( hasMissingValue( filterItems ) )
{
Expand All @@ -87,6 +92,21 @@ public String getSqlFilter( String encodedFilter )
return condition + " ";
}

private String quoteIfNecessary( String item, boolean isText )
{
return isText ? quote( item ) : item;
}

private String toLowerIfNecessary( String item, boolean isText )
{
return isText ? item.toLowerCase() : item;
}

public String renderSqlFilter( boolean isText, Function<String, String> encoder )
{
return this.getSqlFilter( encoder.apply( this.getFilter() ), isText );
}

private boolean hasMissingValue( List<String> filterItems )
{
return anyMatch( filterItems, this::isMissingItem );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ else if ( QueryOperator.IN.equals( operator ) )
return "'" + encodedFilter + "'";
}

private String quote( String filterItem )
protected String quote( String filterItem )
{
return "'" + filterItem + "'";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ else if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.CHILDREN

if ( IN.equals( filter.getOperator() ) )
{
QueryFilter inQueryFilter = new InQueryFilter( field, filter );
sql += "and " + getSqlFilter( inQueryFilter, item );
InQueryFilter inQueryFilter = new InQueryFilter( field, filter );
sql += hlp.whereAnd() + " " + inQueryFilter.renderSqlFilter( item.isText(),
toEncode -> statementBuilder.encode( toEncode, false ) );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,9 @@ else if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.CHILDREN

if ( IN.equals( filter.getOperator() ) )
{
QueryFilter inQueryFilter = new InQueryFilter( field, filter );
sql += hlp.whereAnd() + " " + getSqlFilter( inQueryFilter, item );
InQueryFilter inQueryFilter = new InQueryFilter( field, filter );
sql += hlp.whereAnd() + " " + inQueryFilter.renderSqlFilter( item.isText(),
toEncode -> statementBuilder.encode( toEncode, false ) );
}
else
{
Expand Down