Skip to content

Commit

Permalink
fix: fixed lower case for "IN" operator in queryFilters [DHIS2-11770] (
Browse files Browse the repository at this point in the history
…dhis2#8724)

* fix: fixed lower case for "IN" operator in queryFilters

* fix: quote only string values
  • Loading branch information
gnespolino authored Sep 15, 2021
1 parent 390a503 commit 5bfbec8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
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 )
{
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

0 comments on commit 5bfbec8

Please sign in to comment.