All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.simpledb.query.executions.SingleResultExecution Maven / Gradle / Ivy

Go to download

Provides a POJO centric model as per Spring Data interfaces to interact with Amazon SimpleDB, a non-relational datastore

There is a newer version: 1.0.1
Show newest version
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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy