Skip to content

Commit

Permalink
Adds logic to wrap string values in quotes in query (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Sundberg <[email protected]>
  • Loading branch information
seansund authored Jun 6, 2024
1 parent a14330f commit c5b56e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ResultValue;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public enum Operation {
EQUALS("="),
IN("IN", inValue -> (inValue instanceof List<?>) ? inValue : Collections.singletonList(inValue));
IN("IN", QueryValueFormatters::formatListValue);

private final String value;
private final Function<Object, Object> valueFormatter;
private final Function<Object, String> valueFormatter;

Operation(String value) {
this(value, in -> in);
this(value, QueryValueFormatters::formatSimpleValue);
}

Operation(String value, Function<Object, Object> valueFormatter) {
Operation(String value, Function<Object, String> valueFormatter) {
this.value = value;
this.valueFormatter = valueFormatter;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ibm.openpages.support.util.query;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

public abstract class QueryValueFormatters {
public static String formatListValue(Object inValue) {
final Collection<?> value = (inValue instanceof Collection<?>) ? (Collection<?>) inValue : Collections.singletonList(inValue);

final String valueList = value
.stream()
.map(QueryValueFormatters::formatSimpleValue)
.collect(Collectors.joining(","));

return "[" + valueList + "]";
}

public static String formatSimpleValue(Object current) {
return (current instanceof Number) ? current.toString() : "'" + current.toString() + "'"
}
}

0 comments on commit c5b56e6

Please sign in to comment.