Skip to content

Commit

Permalink
feat: include validation for query
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Aug 28, 2024
1 parent 87d5691 commit 18e14e3
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.data.page.Page;
import jakarta.data.repository.Query;
import jakarta.enterprise.inject.spi.CDI;
import org.eclipse.jnosql.communication.semistructured.QueryType;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.query.AbstractRepository;
import org.eclipse.jnosql.mapping.core.query.AnnotationOperation;
Expand Down Expand Up @@ -127,16 +128,23 @@ public Object invoke(Object instance, Method method, Object[] params) throws Thr
var repositoryMetadata = repositoryMetadata(method);
if (repositoryMetadata.metadata().isEmpty()) {
var query = method.getAnnotation(Query.class);
var queryType = QueryType.parse(query.value());
LOGGER.fine("Executing the query " + query.value() + " with the type " + queryType);
Map<String, Object> parameters = RepositoryReflectionUtils.INSTANCE.getParams(method, params);
var prepare = template.prepare(query.value());
parameters.forEach(prepare::bind);
if (prepare.isCount()) {
return prepare.count();
}
Stream<?> entities = prepare.result();
if (method.getReturnType().equals(long.class) || method.getReturnType().equals(Long.class)) {
if(isLong(method)) {
if(queryType.isNotSelect()) {
throw new UnsupportedOperationException("Because of eventual persistence consistency, " +
"it is not possible to execute a query that is not a SELECT query and return a long value, query: " + query.value());
}
return entities.count();
}

return Void.class;
}
return unwrapInvocationTargetException(() -> repository(method).invoke(instance, method, params));
Expand Down Expand Up @@ -270,5 +278,9 @@ private Class<?> getGenericTypeFromParameter(Parameter parameter) {
throw new IllegalArgumentException("Cannot determine generic type from parameter");
}

private static boolean isLong(Method method) {
return method.getReturnType().equals(long.class) || method.getReturnType().equals(Long.class);
}


}

0 comments on commit 18e14e3

Please sign in to comment.