Search in sources :

Example 1 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project ignite by apache.

the class IgniteQueryGenerator method generateSql.

/**
 * @param mtd Method.
 * @param metadata Metadata.
 */
@NotNull
public static IgniteQuery generateSql(Method mtd, RepositoryMetadata metadata) {
    PartTree parts = new PartTree(mtd.getName(), metadata.getDomainType());
    StringBuilder sql = new StringBuilder();
    if (parts.isDelete())
        throw new UnsupportedOperationException("DELETE clause is not supported now.");
    else {
        sql.append("SELECT ");
        if (parts.isDistinct())
            throw new UnsupportedOperationException("DISTINCT clause in not supported.");
        if (parts.isCountProjection())
            sql.append("COUNT(1) ");
        else
            sql.append(" * ");
    }
    sql.append("FROM ").append(metadata.getDomainType().getSimpleName());
    if (parts.iterator().hasNext()) {
        sql.append(" WHERE ");
        for (PartTree.OrPart orPart : parts) {
            sql.append("(");
            for (Part part : orPart) {
                handleQueryPart(sql, part);
                sql.append(" AND ");
            }
            sql.delete(sql.length() - 5, sql.length());
            sql.append(") OR ");
        }
        sql.delete(sql.length() - 4, sql.length());
    }
    addSorting(sql, parts.getSort());
    if (parts.isLimiting()) {
        sql.append(" LIMIT ");
        sql.append(parts.getMaxResults().intValue());
    }
    return new IgniteQuery(sql.toString(), parts.isCountProjection(), getOptions(mtd));
}
Also used : Part(org.springframework.data.repository.query.parser.Part) PartTree(org.springframework.data.repository.query.parser.PartTree) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class IndexEnsuringQueryCreationListenerUnitTests method skipsQueryCreationForMethodWithoutPredicate.

// DATAMONGO-1753
@Test
public void skipsQueryCreationForMethodWithoutPredicate() {
    PartTree tree = mock(PartTree.class);
    when(tree.hasPredicate()).thenReturn(false);
    PartTreeMongoQuery query = mock(PartTreeMongoQuery.class, Answers.RETURNS_MOCKS);
    when(query.getTree()).thenReturn(tree);
    listener.onCreation(query);
    verify(provider, times(0)).indexOps(any());
}
Also used : PartTreeMongoQuery(org.springframework.data.mongodb.repository.query.PartTreeMongoQuery) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.Test)

Example 3 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-keyvalue by spring-projects.

the class SpelQueryEngineUnitTests method createQueryForMethodWithArgs.

private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {
    List<Class<?>> types = new ArrayList<>(args.length);
    for (Object arg : args) {
        types.add(arg.getClass());
    }
    Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
    RepositoryMetadata metadata = mock(RepositoryMetadata.class);
    doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);
    PartTree partTree = new PartTree(method.getName(), method.getReturnType());
    SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));
    return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ArrayList(java.util.ArrayList) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) RepositoryMetadata(org.springframework.data.repository.core.RepositoryMetadata) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) PartTree(org.springframework.data.repository.query.parser.PartTree) SpelQueryCreator(org.springframework.data.keyvalue.repository.query.SpelQueryCreator)

Example 4 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method createsTrueClauseCorrectly.

// DATAMONGO-338
@Test
void createsTrueClauseCorrectly() {
    PartTree tree = new PartTree("findByActiveTrue", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter), context);
    Query query = query(where("active").is(true));
    assertThat(creator.createQuery()).isEqualTo(query);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 5 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method createsAndQueryCorrectly.

// DATAMONGO-469
@Test
void createsAndQueryCorrectly() {
    Person person = new Person();
    MongoQueryCreator creator = new MongoQueryCreator(new PartTree("findByFirstNameAndFriend", Person.class), getAccessor(converter, "Oliver", person), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(where("firstName").is("Oliver").and("friend").is(person)));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Person(org.springframework.data.mongodb.core.Person) Test(org.junit.jupiter.api.Test)

Aggregations

PartTree (org.springframework.data.repository.query.parser.PartTree)60 Test (org.junit.jupiter.api.Test)51 Query (org.springframework.data.mongodb.core.query.Query)44 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)8 Point (org.springframework.data.geo.Point)6 Method (java.lang.reflect.Method)4 Distance (org.springframework.data.geo.Distance)4 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)4 QueryMethod (org.springframework.data.repository.query.QueryMethod)3 Part (org.springframework.data.repository.query.parser.Part)3 DefaultRepositoryMetadata (org.springframework.data.repository.core.support.DefaultRepositoryMetadata)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 MongoException (com.mongodb.MongoException)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.Test)1