Skip to content

Instantly share code, notes, and snippets.

@rayalex
Last active May 23, 2024 12:44
Show Gist options
  • Save rayalex/a450de5674d2ff743a63892c9e93a765 to your computer and use it in GitHub Desktop.
Save rayalex/a450de5674d2ff743a63892c9e93a765 to your computer and use it in GitHub Desktop.
Array Ops support for Postgres ad Hibernate 5
public class PostgreSQLIndexedSearchFunction implements SQLFunction {
@Override
public boolean hasArguments() {
return true;
}
@Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
@Override
public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
return new BooleanType();
}
@Override
public String render(
Type firstArgumentType,
List arguments,
SessionFactoryImplementor factory
) throws QueryException {
if (arguments.size() != 2) {
throw new IllegalArgumentException("Function must be passed 2 arguments");
}
var array = (String) arguments.get(0);
var search = (String) arguments.get(1);
return String.format("%s @> ARRAY[%s]::varchar[]", array, search);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment