I am trying to create a containsAll custom SQL-function for PostgreSQL but I have difficulties with passing the value to the SQL.
The part of the output in the SQL is:
(kba1_0.labels @> bytea ‘\xaced0005737200116a6176612e7574696c2e48617368536574ba44859596b8b7340300007870770c000000103f400000000000017400066c6162656c3178’).
So argument(0) seems to be fine and the issue is about how argument(1) is treated. It converts the set into this binary which I don’t want.
My code looks like the following:
public class ContainsAll extends StandardSQLFunction {
public static final String NAME = "__containsAll";
public ContainsAll() {
super(NAME, false, StandardBasicTypes.BOOLEAN);
}
@Override
public void render(
SqlAppender sqlAppender, List<? extends SqlAstNode> arguments, ReturnableType<?> returnType,
SqlAstTranslator<?> translator
) {
sqlAppender.append('(');
arguments.get(0).accept(translator);
sqlAppender.append(" @> ");
arguments.get(1).accept(translator); // How to do this correctly here?
sqlAppender.append(")");
}
}
Here is how i call the function (value is the 'Set") please don’t wonder about the weird method signature:
public static <T extends BaseEntity, A extends Serializable> Specification<T> containsAll2(
SingularAttribute<? super T, ? extends Collection> attribute, Collection<A> value
) {
return (root, query, builder) -> {
return builder.isTrue(builder.function(ContainsAll.NAME,
Boolean.class,
root.get(attribute),
builder.literal(value)
));
};
}