Operators { private static void writeFieldInArrayCheckCondition( final QueryStatement query, final QueryWriterResolver resolver, final FieldPath contextFieldPath, final Object queryParameter ) throws QueryBuildException { if (contextFieldPath == null) { throw new QueryBuildException("Operator \"$in\" not allowed outside of field context"); } if (!(queryParameter instanceof List)) { throw new QueryBuildException("\"$in\" operator must be applied only to JSON array"); } List paramAsList = (List) queryParameter; Writer writer = query.getBodyWriter(); try { if (paramAsList.size() == 0) { writer.write("(FALSE)"); return; } writer.write(String.format("((%s)in(", contextFieldPath.toSQL())); for (int i = paramAsList.size(); i > 0; --i) { writer.write(String.format("to_json(?)::jsonb%s", (i == 1) ? "" : ",")); } writer.write("))"); query.pushParameterSetter((statement, index) -> { for (Object obj : paramAsList) { statement.setObject(index++, obj); } return index; }); } catch (IOException e) { throw new QueryBuildException("Query search conditions write failed because of exception", e); } } private  Operators(); static void addAll(final PostgresQueryWriterResolver resolver);  }
@Test public void writeFieldInArrayCheckConditionTest() throws Exception { final String operatorName = "$in"; FieldPath fieldPath = PostgresFieldPath.fromString(operatorName.replace("$", "")); List<Integer> queryParam = new ArrayList<>(Arrays.asList(1, 10, 100)); StringWriter body = new StringWriter(); QueryStatement queryStatement = mock(QueryStatement.class); when(queryStatement.getBodyWriter()).thenReturn(body); resolver .resolve(operatorName) .write(queryStatement, resolver, fieldPath, queryParam); assertEquals(queryStatement.getBodyWriter().toString().trim(), "((document#>'{in}')in(to_json(?)::jsonb,to_json(?)::jsonb,to_json(?)::jsonb))"); verify(queryStatement).pushParameterSetter(any()); }