org.springframework.data.repository.query.SpelQueryContext Java Examples

The following examples show how to use org.springframework.data.repository.query.SpelQueryContext. 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: RepositoryQueryTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void spelQueryContextShouldBeConfiguredCorrectly() {

	SpelQueryContext spelQueryContext = StringBasedNeo4jQuery.SPEL_QUERY_CONTEXT;

	String template;
	String query;
	SpelQueryContext.SpelExtractor spelExtractor;

	template = "MATCH (user:User) WHERE user.name = :#{#searchUser.name} and user.middleName = ?#{#searchUser.middleName} RETURN user";

	spelExtractor = spelQueryContext.parse(template);
	query = spelExtractor.getQueryString();

	assertThat(query)
		.isEqualTo(
			"MATCH (user:User) WHERE user.name = $__SpEL__0 and user.middleName = $__SpEL__1 RETURN user");

	template = "MATCH (user:User) WHERE user.name=?#{[0]} and user.name=:#{[0]} RETURN user";
	spelExtractor = spelQueryContext.parse(template);
	query = spelExtractor.getQueryString();

	assertThat(query)
		.isEqualTo("MATCH (user:User) WHERE user.name=$__SpEL__0 and user.name=$__SpEL__1 RETURN user");
}
 
Example #2
Source File: GqlDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private void setEvaluatingSpelQueryContext() {
	Set<String> originalTags = new HashSet<>(GqlDatastoreQuery.this.originalParamTags);

	GqlDatastoreQuery.this.evaluatingSpelQueryContext = SpelQueryContext.EvaluatingSpelQueryContext
			.of((counter, spelExpression) -> {
				String newTag;
				do {
					counter++;
					newTag = "@SpELtag" + counter;
				}
				while (originalTags.contains(newTag));
				originalTags.add(newTag);
				return newTag;
			}, (prefix, newTag) -> newTag)
			.withEvaluationContextProvider(GqlDatastoreQuery.this.evaluationContextProvider);
}
 
Example #3
Source File: StringQuery.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
private static SpelExtractor createSpelExtractor(String queryWithSpel,
		boolean parametersShouldBeAccessedByIndex, int greatestParameterIndex) {

	/*
	 * If parameters need to be bound by index, we bind the synthetic expression
	 * parameters starting from position of the greatest discovered index
	 * parameter in order to not mix-up with the actual parameter indices.
	 */
	int expressionParameterIndex = parametersShouldBeAccessedByIndex
			? greatestParameterIndex : 0;

	BiFunction<Integer, String, String> indexToParameterName = parametersShouldBeAccessedByIndex
			? (index, expression) -> String
					.valueOf(index + expressionParameterIndex + 1)
			: (index, expression) -> EXPRESSION_PARAMETER_PREFIX + (index + 1);

	String fixedPrefix = parametersShouldBeAccessedByIndex ? "?" : ":";

	BiFunction<String, String, String> parameterNameToReplacement = (prefix,
			name) -> fixedPrefix + name;

	return SpelQueryContext.of(indexToParameterName, parameterNameToReplacement)
			.parse(queryWithSpel);
}
 
Example #4
Source File: StringQuery.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** */
private static SpelExtractor createSpelExtractor(String queryWithSpel,
    boolean parametersShouldBeAccessedByIndex,
    int greatestParameterIndex) {

    /*
     * If parameters need to be bound by index, we bind the synthetic expression parameters starting from
     * position of the greatest discovered index parameter in order to
     * not mix-up with the actual parameter indices.
     */
    int expressionParameterIndex = parametersShouldBeAccessedByIndex ? greatestParameterIndex : 0;

    BiFunction<Integer, String, String> indexToParameterName = parametersShouldBeAccessedByIndex
        ? (index, expression) -> String.valueOf(
        index + expressionParameterIndex + 1)
        : (index, expression) ->
        EXPRESSION_PARAMETER_PREFIX + (index
            + 1);

    String fixedPrefix = parametersShouldBeAccessedByIndex ? "?" : ":";

    BiFunction<String, String, String> parameterNameToReplacement = (prefix, name) -> fixedPrefix + name;

    return SpelQueryContext.of(indexToParameterName, parameterNameToReplacement).parse(queryWithSpel);
}