org.springframework.data.simpledb.query.executions.SingleResultExecution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-simpledb Show documentation
Show all versions of spring-data-simpledb Show documentation
Provides a POJO centric model as per Spring Data interfaces to interact with Amazon SimpleDB, a non-relational datastore
package org.springframework.data.simpledb.query.executions;
import org.springframework.data.simpledb.core.SimpleDbOperations;
import org.springframework.data.simpledb.query.QueryUtils;
import org.springframework.data.simpledb.query.SimpleDbQueryMethod;
import org.springframework.data.simpledb.query.SimpleDbQueryRunner;
import org.springframework.data.simpledb.reflection.ReflectionUtils;
import org.springframework.util.Assert;
public class SingleResultExecution extends AbstractSimpleDbQueryExecution {
/**
* The following single result types can be requested:
*
* - SINGLE_FIELD_RESULT - Any Core Type or Primitive Field
as returned type for query:
*
*
* {@code SELECT field FROM entity}
*
*
* COUNT_RESULT - Boxed Long or Primitive long Field as returned type for query:
*
*
* {@code SELECT count(*) FROM entity}
*
*
* ENTITY_RESULT - Entity as returned type for query:
*
*
* {@code SELECT * FROM entity where itemName()="1"}
*
*
*
*/
public enum SingleResultType {
SINGLE_FIELD_RESULT, COUNT_RESULT, ENTITY_RESULT;
}
public SingleResultExecution(SimpleDbOperations simpleDbOperations) {
super(simpleDbOperations);
}
@Override
protected Object doExecute(SimpleDbQueryMethod queryMethod, SimpleDbQueryRunner queryRunner) {
SingleResultType resultType = detectResultType(queryMethod);
switch(resultType) {
case COUNT_RESULT: {
Class> methodReturnedType = queryMethod.getReturnedObjectType();
boolean isLongClass = Long.class.isAssignableFrom(methodReturnedType);
boolean islongClass = long.class.isAssignableFrom(methodReturnedType);
Assert.isTrue(isLongClass || islongClass,
"Method declared in repository should return type long or Long");
return queryRunner.executeCount();
}
case SINGLE_FIELD_RESULT: {
String attributeName = queryRunner.getSingleQueryFieldName();
Object returnedEntity = queryRunner.executeSingleResultQuery();
return ReflectionUtils.callGetter(returnedEntity, attributeName);
}
case ENTITY_RESULT: {
return queryRunner.executeSingleResultQuery();
}
default:
throw new IllegalArgumentException("Unrecognized Single result type");
}
}
private SingleResultType detectResultType(SimpleDbQueryMethod method) {
String query = method.getAnnotatedQuery();
if(QueryUtils.isCountQuery(query)) {
return SingleResultType.COUNT_RESULT;
} else if(method.isQueryForEntity()) {
return SingleResultType.ENTITY_RESULT;
} else if(QueryUtils.getQueryPartialFieldNames(query).size() == 1) {
return SingleResultType.SINGLE_FIELD_RESULT;
} else {
throw new IllegalArgumentException("Wrong return type for query: " + query);
}
}
}