Java Code Examples for org.springframework.data.repository.query.parser.Part#getType()

The following examples show how to use org.springframework.data.repository.query.parser.Part#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartValidator.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
void validatePart(Part part) {

		validateIgnoreCase(part);
		switch (part.getType()) {
			case AFTER:
			case BEFORE:
				validateTemporalProperty(part);
				break;
			case IS_EMPTY:
			case IS_NOT_EMPTY:
				validateCollectionProperty(part);
				break;
			case NEAR:
			case WITHIN:
				validatePointProperty(part);
				break;
		}
	}
 
Example 2
Source File: CosmosQueryCreator.java    From spring-data-cosmosdb with MIT License 6 votes vote down vote up
@Override // Note (panli): side effect here, this method will change the iterator status of parameters.
protected Criteria create(Part part, Iterator<Object> parameters) {
    final Part.Type type = part.getType();
    final String subject = getSubject(part);
    final List<Object> values = new ArrayList<>();

    if (CriteriaType.isPartTypeUnSupported(type)) {
        throw new UnsupportedOperationException("Unsupported keyword: " + type);
    }

    for (int i = 0; i < part.getNumberOfArguments(); i++) {
        Assert.isTrue(parameters.hasNext(), "should not reach the end of iterator");
        values.add(parameters.next());
    }

    return Criteria.getInstance(CriteriaType.toCriteriaType(type), subject, values);
}
 
Example 3
Source File: ReactiveCosmosQueryCreator.java    From spring-data-cosmosdb with MIT License 6 votes vote down vote up
@Override // Note (panli): side effect here, this method will change the iterator status of parameters.
protected Criteria create(Part part, Iterator<Object> parameters) {
    final Part.Type type = part.getType();
    final String subject = getSubject(part);
    final List<Object> values = new ArrayList<>();

    if (CriteriaType.isPartTypeUnSupported(type)) {
        throw new UnsupportedOperationException("Unsupported keyword: " + type);
    }

    for (int i = 0; i < part.getNumberOfArguments(); i++) {
        Assert.isTrue(parameters.hasNext(), "should not reach the end of iterator");
        values.add(parameters.next());
    }

    return Criteria.getInstance(CriteriaType.toCriteriaType(type), subject, values);
}
 
Example 4
Source File: ParameterMetadataProvider.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new {@link ParameterMetadata} for the given type and name.
 *
 * @param <T>
 * @param part      must not be {@literal null}.
 * @param type      parameter type, must not be {@literal null}.
 * @param parameter
 * @return
 */
private <T> ParameterMetadata<T> next(Part part, Class<T> type, Parameter parameter) {
    Assert.notNull(type, "Type must not be null!");

    ParameterMetadata<T> value = new ParameterMetadata<T>(type, parameter.getName().get(), part.getType(),
            bindableParameterValues == null ? ParameterMetadata.PLACEHOLDER : bindableParameterValues.next());
    expressions.add(value);

    return value;
}
 
Example 5
Source File: CypherQueryCreator.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
private Condition createImpl(Part part, Iterator<Object> actualParameters) {

		PersistentPropertyPath<Neo4jPersistentProperty> path = mappingContext
			.getPersistentPropertyPath(part.getProperty());
		Neo4jPersistentProperty persistentProperty = path.getRequiredLeafProperty();

		boolean ignoreCase = ignoreCase(part);
		switch (part.getType()) {
			case AFTER:
			case GREATER_THAN:
				return toCypherProperty(persistentProperty, ignoreCase)
					.gt(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case BEFORE:
			case LESS_THAN:
				return toCypherProperty(persistentProperty, ignoreCase)
					.lt(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case BETWEEN:
				return betweenCondition(persistentProperty, actualParameters, ignoreCase);
			case CONTAINING:
				return toCypherProperty(persistentProperty, ignoreCase)
					.contains(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case ENDING_WITH:
				return toCypherProperty(persistentProperty, ignoreCase)
					.endsWith(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case EXISTS:
				return Predicates.exists(toCypherProperty(persistentProperty));
			case FALSE:
				return toCypherProperty(persistentProperty, ignoreCase).isFalse();
			case GREATER_THAN_EQUAL:
				return toCypherProperty(persistentProperty, ignoreCase)
					.gte(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case IN:
				return toCypherProperty(persistentProperty, ignoreCase)
					.in(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case IS_EMPTY:
				return toCypherProperty(persistentProperty, ignoreCase).isEmpty();
			case IS_NOT_EMPTY:
				return toCypherProperty(persistentProperty, ignoreCase).isEmpty().not();
			case IS_NOT_NULL:
				return toCypherProperty(persistentProperty, ignoreCase).isNotNull();
			case IS_NULL:
				return toCypherProperty(persistentProperty, ignoreCase).isNull();
			case LESS_THAN_EQUAL:
				return toCypherProperty(persistentProperty, ignoreCase)
					.lte(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case LIKE:
				return likeCondition(persistentProperty, nextRequiredParameter(actualParameters).nameOrIndex,
					ignoreCase);
			case NEAR:
				return createNearCondition(persistentProperty, actualParameters);
			case NEGATING_SIMPLE_PROPERTY:
				return toCypherProperty(persistentProperty, ignoreCase)
					.isNotEqualTo(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case NOT_CONTAINING:
				return toCypherProperty(persistentProperty, ignoreCase)
					.contains(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase)).not();
			case NOT_IN:
				return toCypherProperty(persistentProperty, ignoreCase)
					.in(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase))
					.not();
			case NOT_LIKE:
				return likeCondition(persistentProperty, nextRequiredParameter(actualParameters).nameOrIndex,
					ignoreCase).not();
			case SIMPLE_PROPERTY:
				return toCypherProperty(persistentProperty, ignoreCase)
					.isEqualTo(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case STARTING_WITH:
				return toCypherProperty(persistentProperty, ignoreCase)
					.startsWith(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case REGEX:
				return toCypherProperty(persistentProperty, ignoreCase)
					.matches(toCypherParameter(nextRequiredParameter(actualParameters), ignoreCase));
			case TRUE:
				return toCypherProperty(persistentProperty, ignoreCase).isTrue();
			case WITHIN:
				return createWithinCondition(persistentProperty, actualParameters);
			default:
				throw new IllegalArgumentException("Unsupported part type: " + part.getType());
		}
	}
 
Example 6
Source File: MybatisPartTreeMapperBuilder.java    From spring-data-mybatis with Apache License 2.0 4 votes vote down vote up
private String buildQueryCondition() {

		StringBuilder builder = new StringBuilder();

		int c = 0, count = 0;
		MybatisParameters parameters = method.getParameters();
		for (PartTree.OrPart orPart : tree) {
			if (count++ > 0) {
				builder.append(" or ");
			}

			int andCount = 0;
			for (Part part : orPart) {
				if (andCount++ > 0) {
					builder.append(" and ");
				}
				MybatisPersistentProperty property = entity
						.getPersistentProperty(part.getProperty().getSegment());
				if (null == property) {
					throw new MappingException(
							"can not find property: " + part.getProperty().getSegment()
									+ " in entity: " + entity.getName());
				}
				String columnName = queryConditionLeft(property.getColumnName(),
						part.shouldIgnoreCase());

				String operation = calculateOperation(part.getType());
				String[] properties = new String[part.getType().getNumberOfArguments()];
				for (int i = 0; i < properties.length; i++) {
					properties[i] = this.resolveParameterName(c++);
				}

				if (part.getType() == IN || part.getType() == NOT_IN) {
					StringBuilder inBuilder = new StringBuilder();
					Class<?> typeClass = parameters.getParameter(c - properties.length)
							.getType();
					String judgeEmpty = null;
					if (typeClass.isArray()) {
						judgeEmpty = "length == 0";
					}
					else if (Collection.class.isAssignableFrom(typeClass)) {
						judgeEmpty = "isEmpty()";
					}
					inBuilder.append("<choose><when test=\"").append(properties[0])
							.append(" == null");
					if (null != judgeEmpty) {
						inBuilder.append(" || ").append(properties[0]).append('.')
								.append(judgeEmpty);
					}
					inBuilder.append("\">");
					if (part.getType() == IN) {
						inBuilder.append(" 1 = 0 ");
					}
					else {
						inBuilder.append(" 1 = 1");
					}
					inBuilder.append("</when><otherwise>");
					inBuilder.append(columnName + operation + queryConditionRight(
							part.getType(), part.shouldIgnoreCase(), properties));
					inBuilder.append("</otherwise></choose>");
					builder.append(inBuilder);
				}
				else {
					builder.append(columnName).append(operation)
							.append(queryConditionRight(part.getType(),
									part.shouldIgnoreCase(), properties));
				}

			}

		}

		return builder.toString().trim();
	}
 
Example 7
Source File: IgniteQueryGenerator.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Transform part to sql expression
 */
private static void handleQueryPart(StringBuilder sql, Part part) {
    sql.append("(");

    sql.append(part.getProperty());

    switch (part.getType()) {
        case SIMPLE_PROPERTY:
            sql.append("=?");
            break;
        case NEGATING_SIMPLE_PROPERTY:
            sql.append("<>?");
            break;
        case GREATER_THAN:
            sql.append(">?");
            break;
        case GREATER_THAN_EQUAL:
            sql.append(">=?");
            break;
        case LESS_THAN:
            sql.append("<?");
            break;
        case LESS_THAN_EQUAL:
            sql.append("<=?");
            break;
        case IS_NOT_NULL:
            sql.append(" IS NOT NULL");
            break;
        case IS_NULL:
            sql.append(" IS NULL");
            break;
        case BETWEEN:
            sql.append(" BETWEEN ? AND ?");
            break;
        case FALSE:
            sql.append(" = FALSE");
            break;
        case TRUE:
            sql.append(" = TRUE");
            break;
        case CONTAINING:
            sql.append(" LIKE '%' || ? || '%'");
            break;
        case NOT_CONTAINING:
            sql.append(" NOT LIKE '%' || ? || '%'");
            break;
        case LIKE:
            sql.append(" LIKE '%' || ? || '%'");
            break;
        case NOT_LIKE:
            sql.append(" NOT LIKE '%' || ? || '%'");
            break;
        case STARTING_WITH:
            sql.append(" LIKE  ? || '%'");
            break;
        case ENDING_WITH:
            sql.append(" LIKE '%' || ?");
            break;
        case IN:
            sql.append(" IN ?");
            break;
        case NOT_IN:
            sql.append(" NOT IN ?");
            break;
        case REGEX:
            sql.append(" REGEXP ?");
            break;
        case NEAR:
        case AFTER:
        case BEFORE:
        case EXISTS:
        default:
            throw new UnsupportedOperationException(part.getType() + " is not supported!");
    }

    sql.append(")");
}
 
Example 8
Source File: IgniteQueryGenerator.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Transform part to qryStr expression
 */
private static void handleQueryPart(StringBuilder sql, Part part) {
    sql.append("(");

    sql.append(part.getProperty());

    switch (part.getType()) {
        case SIMPLE_PROPERTY:
            sql.append("=?");
            break;
        case NEGATING_SIMPLE_PROPERTY:
            sql.append("<>?");
            break;
        case GREATER_THAN:
            sql.append(">?");
            break;
        case GREATER_THAN_EQUAL:
            sql.append(">=?");
            break;
        case LESS_THAN:
            sql.append("<?");
            break;
        case LESS_THAN_EQUAL:
            sql.append("<=?");
            break;
        case IS_NOT_NULL:
            sql.append(" IS NOT NULL");
            break;
        case IS_NULL:
            sql.append(" IS NULL");
            break;
        case BETWEEN:
            sql.append(" BETWEEN ? AND ?");
            break;
        case FALSE:
            sql.append(" = FALSE");
            break;
        case TRUE:
            sql.append(" = TRUE");
            break;
        //TODO: review this legacy code, LIKE should be -> LIKE ?
        case LIKE:
        case CONTAINING:
            sql.append(" LIKE '%' || ? || '%'");
            break;
        case NOT_CONTAINING:
            //TODO: review this legacy code, NOT_LIKE should be -> NOT LIKE ?
        case NOT_LIKE:
            sql.append(" NOT LIKE '%' || ? || '%'");
            break;
        case STARTING_WITH:
            sql.append(" LIKE  ? || '%'");
            break;
        case ENDING_WITH:
            sql.append(" LIKE '%' || ?");
            break;
        case IN:
            sql.append(" IN ?");
            break;
        case NOT_IN:
            sql.append(" NOT IN ?");
            break;
        case REGEX:
            sql.append(" REGEXP ?");
            break;
        case NEAR:
        case AFTER:
        case BEFORE:
        case EXISTS:
        default:
            throw new UnsupportedOperationException(part.getType() + " is not supported!");
    }

    sql.append(")");
}
 
Example 9
Source File: IgniteQueryGenerator.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Transform part to qryStr expression
 */
private static void handleQueryPart(StringBuilder sql, Part part) {
    sql.append("(");

    sql.append(part.getProperty());

    switch (part.getType()) {
        case SIMPLE_PROPERTY:
            sql.append("=?");
            break;
        case NEGATING_SIMPLE_PROPERTY:
            sql.append("<>?");
            break;
        case GREATER_THAN:
            sql.append(">?");
            break;
        case GREATER_THAN_EQUAL:
            sql.append(">=?");
            break;
        case LESS_THAN:
            sql.append("<?");
            break;
        case LESS_THAN_EQUAL:
            sql.append("<=?");
            break;
        case IS_NOT_NULL:
            sql.append(" IS NOT NULL");
            break;
        case IS_NULL:
            sql.append(" IS NULL");
            break;
        case BETWEEN:
            sql.append(" BETWEEN ? AND ?");
            break;
        case FALSE:
            sql.append(" = FALSE");
            break;
        case TRUE:
            sql.append(" = TRUE");
            break;
        //TODO: review this legacy code, LIKE should be -> LIKE ?
        case LIKE:
        case CONTAINING:
            sql.append(" LIKE '%' || ? || '%'");
            break;
        case NOT_CONTAINING:
            //TODO: review this legacy code, NOT_LIKE should be -> NOT LIKE ?
        case NOT_LIKE:
            sql.append(" NOT LIKE '%' || ? || '%'");
            break;
        case STARTING_WITH:
            sql.append(" LIKE  ? || '%'");
            break;
        case ENDING_WITH:
            sql.append(" LIKE '%' || ?");
            break;
        case IN:
            sql.append(" IN ?");
            break;
        case NOT_IN:
            sql.append(" NOT IN ?");
            break;
        case REGEX:
            sql.append(" REGEXP ?");
            break;
        case NEAR:
        case AFTER:
        case BEFORE:
        case EXISTS:
        default:
            throw new UnsupportedOperationException(part.getType() + " is not supported!");
    }

    sql.append(")");
}
 
Example 10
Source File: HazelcastQueryCreator.java    From spring-data-hazelcast with Apache License 2.0 4 votes vote down vote up
private Predicate<?, ?> from(Part part, Iterator<Comparable<?>> iterator) {
    String property = part.getProperty().toDotPath();
    Type type = part.getType();
    boolean ignoreCase = ifIgnoreCase(part);

    switch (type) {
        case AFTER:
        case GREATER_THAN:
        case GREATER_THAN_EQUAL:
        case BEFORE:
        case LESS_THAN:
        case LESS_THAN_EQUAL:
        case BETWEEN:
            return fromInequalityVariant(type, property, iterator);
        case IS_NULL:
        case IS_NOT_NULL:
            return fromNullVariant(type, property);
        case IN:
        case NOT_IN:
            return fromCollectionVariant(type, property, iterator);
        case CONTAINING:
        case NOT_CONTAINING:
        case STARTING_WITH:
        case ENDING_WITH:
        case LIKE:
        case NOT_LIKE:
            return fromLikeVariant(type, ignoreCase, property, iterator);
        case TRUE:
        case FALSE:
            return fromBooleanVariant(type, property);
        case SIMPLE_PROPERTY:
        case NEGATING_SIMPLE_PROPERTY:
            return fromEqualityVariant(type, ignoreCase, property, iterator);
        case REGEX:
            return Predicates.regex(property, iterator.next().toString());
        case IS_EMPTY:
        case IS_NOT_EMPTY:
            return fromEmptyVariant(type, property);
        /* case EXISTS:*/
        case NEAR:
        case WITHIN:
            return fromGeoVariant(type, property, iterator);

        default:
            throw new InvalidDataAccessApiUsageException(String.format("Unsupported type '%s'", type));
    }
}
 
Example 11
Source File: SpelQueryCreator.java    From spring-data-keyvalue with Apache License 2.0 4 votes vote down vote up
private static boolean requiresInverseLookup(Part part) {
	return part.getType() == Type.IN;
}
 
Example 12
Source File: AbstractDynamoDBQueryCreator.java    From spring-data-dynamodb with Apache License 2.0 4 votes vote down vote up
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part, Iterator<Object> iterator) {
	if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
		throw new UnsupportedOperationException("Case insensitivity not supported");

	Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();
	
	PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
	String leafNodePropertyName = leafNodePropertyPath.toDotPath();
	if (leafNodePropertyName.indexOf(".") != -1)
	{
		int index = leafNodePropertyName.lastIndexOf(".");
		leafNodePropertyName = leafNodePropertyName.substring(index);
	}

	switch (part.getType()) {
	
	case IN:
		Object in = iterator.next();
		Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
				+ leafNodePropertyName + "'");
		boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class);
		boolean isArray = ObjectUtils.isArray(in);
		Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
		Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
		return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType);
	case CONTAINING:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS,
				iterator.next(), leafNodePropertyType);
	case STARTING_WITH:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH,
				iterator.next(), leafNodePropertyType);
	case BETWEEN:
		Object first = iterator.next();
		Object second = iterator.next();
		return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType);
	case AFTER:
	case GREATER_THAN:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(),
				leafNodePropertyType);
	case BEFORE:
	case LESS_THAN:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(),
				leafNodePropertyType);
	case GREATER_THAN_EQUAL:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(),
				leafNodePropertyType);
	case LESS_THAN_EQUAL:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(),
				leafNodePropertyType);
	case IS_NULL:
		return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL);
	case IS_NOT_NULL:
		return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL);
	case TRUE:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE,
				leafNodePropertyType);
	case FALSE:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE,
				leafNodePropertyType);
	case SIMPLE_PROPERTY:
		return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType);
	case NEGATING_SIMPLE_PROPERTY:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(),
				leafNodePropertyType);
	default:
		throw new IllegalArgumentException("Unsupported keyword " + part.getType());
	}

}