Search in sources :

Example 56 with PartTree

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

the class MongoQueryCreatorUnitTests method ignoreCaseShouldEscapeSource.

// DATAMONGO-1232
@Test
void ignoreCaseShouldEscapeSource() {
    PartTree tree = new PartTree("findByUsernameIgnoreCase", User.class);
    ConvertingParameterAccessor accessor = getAccessor(converter, "con.flux+");
    Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
    assertThat(query).isEqualTo(query(where("username").regex("^\\Qcon.flux+\\E$", "i")));
}
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 57 with PartTree

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

the class MongoQueryCreatorUnitTests method shouldCreateNearSphereQueryForSphericalPropertyHavingDistanceWithDefaultMetric.

// DATAMONGO-1110
@Test
void shouldCreateNearSphereQueryForSphericalPropertyHavingDistanceWithDefaultMetric() {
    Point point = new Point(1.0, 1.0);
    Distance distance = new Distance(1.0);
    PartTree tree = new PartTree("findByAddress2dSphere_GeoNear", User.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, point, distance), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(where("address2dSphere.geo").nearSphere(point).maxDistance(1.0)));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Point(org.springframework.data.geo.Point) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) PartTree(org.springframework.data.repository.query.parser.PartTree) Distance(org.springframework.data.geo.Distance) Test(org.junit.jupiter.api.Test)

Example 58 with PartTree

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

the class MongoQueryCreatorUnitTests method createsOrQueryCorrectly.

// DATAMONGO-413
@Test
void createsOrQueryCorrectly() {
    PartTree tree = new PartTree("findByFirstNameOrAge", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Dave", 42), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(new Criteria().orOperator(where("firstName").is("Dave"), where("age").is(42))));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Criteria(org.springframework.data.mongodb.core.query.Criteria) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 59 with PartTree

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

the class MongoQueryCreatorUnitTests method createsQueryWithFindByStartingWithIgnoreCaseCorrectly.

// DATAMONGO-770
@Test
void createsQueryWithFindByStartingWithIgnoreCaseCorrectly() {
    PartTree tree = new PartTree("findByFirstNameStartingWithIgnoreCase", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "dave"), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(where("firstName").regex("^dave", "i")));
}
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 60 with PartTree

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

the class JdbcQueryCreator method validate.

/**
 * Validate parameters for the derived query. Specifically checking that the query method defines scalar parameters
 * and collection parameters where required and that invalid parameter declarations are rejected.
 *
 * @param tree the tree structure defining the predicate of the query.
 * @param parameters parameters for the predicate.
 */
static void validate(PartTree tree, Parameters<?, ?> parameters, MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context) {
    RelationalQueryCreator.validate(tree, parameters);
    for (PartTree.OrPart parts : tree) {
        for (Part part : parts) {
            PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = context.getPersistentPropertyPath(part.getProperty());
            PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(context, propertyPath);
            for (PersistentPropertyPathExtension pathToValidate = path; path.getLength() > 0; path = path.getParentPath()) {
                validateProperty(pathToValidate);
            }
        }
    }
}
Also used : Part(org.springframework.data.repository.query.parser.Part) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) PartTree(org.springframework.data.repository.query.parser.PartTree)

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