Java Code Examples for org.springframework.data.repository.query.Parameter#isNamedParameter()

The following examples show how to use org.springframework.data.repository.query.Parameter#isNamedParameter() . 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: StringQueryParameterBinder.java    From spring-data-ebean with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the {@link LikeParameterBinding} to be applied before binding a parameter value to the query.
 *
 * @param ebeanQuery must not be {@literal null}.
 * @param position
 * @param parameter  must not be {@literal null}.
 * @return the {@link ParameterBinding} for the given parameters or {@literal null} if none available.
 */
private ParameterBinding getBindingFor(Object ebeanQuery, int position, Parameter parameter) {

    Assert.notNull(ebeanQuery, "EbeanQueryWrapper must not be null!");
    Assert.notNull(parameter, "Parameter must not be null!");

    if (parameter.isNamedParameter()) {
        return query.getBindingFor(parameter.getName().orElseThrow(() -> new IllegalArgumentException("Parameter needs to be named!")));
    }

    try {
        return query.getBindingFor(position);

    } catch (IllegalArgumentException ex) {

        // We should actually reject parameters unavailable, but as EclipseLink doesn't implement ….getParameter(int) for
        // native queries correctly we need to fall back to an indexed parameter
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=427892

        return new ParameterBinding(position);
    }
}
 
Example 2
Source File: SimpleDbQueryMethod.java    From spring-data-simpledb with MIT License 6 votes vote down vote up
private void assertParameterNamesInAnnotatedQuery(String annotatedQuery) {

		if(!StringUtils.hasText(annotatedQuery)) {
			return;
		}

		for(Parameter parameter : getParameters()) {

			if(!parameter.isNamedParameter()) {
				continue;
			}

			if(!annotatedQuery.contains(String.format(":%s", parameter.getName()))) {
				throw new IllegalStateException(String.format(
						"Using named parameters for method %s but parameter '%s' not found in annotated query '%s'!",
						method, parameter.getName(), annotatedQuery));
			}
		}
	}
 
Example 3
Source File: QueryUtils.java    From spring-data-simpledb with MIT License 5 votes vote down vote up
public static boolean hasNamedParameter(SimpleDbQueryMethod queryMethod) {
	for (Parameter param : queryMethod.getParameters()) {
		if (param.isNamedParameter()) {
			return Boolean.TRUE;
		}
	}
	return Boolean.FALSE;
}
 
Example 4
Source File: QueryUtils.java    From spring-data-simpledb with MIT License 4 votes vote down vote up
static String replaceOneParameterInQuery(String rawQuery,
			Parameter parameter, Object parameterValue) {
		final String bindEndCharacter = "\\b";

		String namedParameter;
		if (parameter.isNamedParameter()) {
			namedParameter = parameter.getPlaceholder() + bindEndCharacter;
		} else {
			namedParameter = "\\?";
		}

		Assert.isTrue(rawQuery.matches("(.*)(" + namedParameter + ")(.*)"));
//		String rawQuery = aRawQuery;

//		if (isInOperation(rawQuery, namedParameter)) {
//			String replacedValue = createInOperatorStatement(parameterValue);
//			rawQuery = rawQuery.replaceFirst(namedParameter, replacedValue);
//		} else {
//			// handle LIKE queries & quoting together
//			String encodedValue = SimpleDBAttributeConverter.encode(parameterValue);
//			StringBuilder namedParamBuilder = new StringBuilder();
//			StringBuilder paramValueBuilder = new StringBuilder();
//			Pattern likeOperatorPattern = Pattern.compile("(%?)" + namedParameter + "(%?)");
//			Matcher m = likeOperatorPattern.matcher(rawQuery);
//			if (m.find()) {
//				paramValueBuilder.append(SINGLE_QUOTE);
//				if (!m.group(1).isEmpty()) {
//					namedParamBuilder.append("%");
//					paramValueBuilder.append("%");
//				}
//				namedParamBuilder.append(namedParameter);
//				paramValueBuilder.append(encodedValue);
//				if (!m.group(2).isEmpty()) {
//					namedParamBuilder.append("%");
//					paramValueBuilder.append("%");
//				}
//				paramValueBuilder.append(SINGLE_QUOTE);
//				rawQuery = rawQuery.replaceFirst(namedParamBuilder.toString(), paramValueBuilder.toString());
//			}
//		}
		return replaceOneParameterInQuery(rawQuery, namedParameter, parameterValue);
	}