org.springframework.data.repository.query.Parameter Java Examples
The following examples show how to use
org.springframework.data.repository.query.Parameter.
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: StringBasedNeo4jQuery.java From sdn-rx with Apache License 2.0 | 6 votes |
Map<String, Object> bindParameters(Neo4jParameterAccessor parameterAccessor) { final Parameters<?, ?> formalParameters = parameterAccessor.getParameters(); Map<String, Object> resolvedParameters = new HashMap<>(); // Values from the parameter accessor can only get converted after evaluation for (Map.Entry<String, Object> evaluatedParam : spelEvaluator.evaluate(parameterAccessor.getValues()).entrySet()) { resolvedParameters.put(evaluatedParam.getKey(), super.convertParameter(evaluatedParam.getValue())); } formalParameters.stream() .filter(Parameter::isBindable) .forEach(parameter -> { int parameterIndex = parameter.getIndex(); Object parameterValue = super.convertParameter(parameterAccessor.getBindableValue(parameterIndex)); // Add the parameter under its name when possible parameter.getName() .ifPresent(parameterName -> resolvedParameters.put(parameterName, parameterValue)); // Always add under its index. resolvedParameters.put(Integer.toString(parameterIndex), parameterValue); }); return resolvedParameters; }
Example #2
Source File: SimpleDbQueryMethod.java From spring-data-simpledb with MIT License | 6 votes |
/** * @return whether or not the query method contains a {@link Pageable} parameter in its signature. */ public boolean isPagedQuery() { boolean isPaged = false; final Iterator<Parameter> it = getParameters().iterator(); while(it.hasNext()) { final Parameter param = it.next(); if(Pageable.class.isAssignableFrom(param.getType())) { isPaged = true; break; } } return isPaged; }
Example #3
Source File: SimpleDbQueryMethod.java From spring-data-simpledb with MIT License | 6 votes |
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 #4
Source File: QueryUtils.java From spring-data-simpledb with MIT License | 6 votes |
static String buildQuery(final String rawQuery, Parameters parameters, Object... parameterValues) { String replacedRawQuery = rawQuery; for (Iterator<Parameter> iterator = parameters.iterator(); iterator .hasNext();) { Parameter eachParameter = iterator.next(); if (Pageable.class.isAssignableFrom(eachParameter.getType()) || Sort.class.isAssignableFrom(eachParameter.getType())) { continue; } replacedRawQuery = replaceOneParameterInQuery(replacedRawQuery, eachParameter, parameterValues[eachParameter.getIndex()]); } return replacedRawQuery.trim(); }
Example #5
Source File: IgniteRepositoryQuery.java From ignite with Apache License 2.0 | 6 votes |
private <T extends Parameter> int getDynamicQueryConfigurationIndex(QueryMethod method) { Iterator<T> it = (Iterator<T>)method.getParameters().iterator(); int i = 0; boolean found = false; int index = -1; while (it.hasNext()) { T parameter = it.next(); if (DynamicQueryConfig.class.isAssignableFrom(parameter.getType())) { if (found) { throw new IllegalStateException("Invalid '" + method.getName() + "' repository method signature. " + "Only ONE DynamicQueryConfig parameter is allowed"); } found = true; index = i; } i++; } return index; }
Example #6
Source File: IgniteRepositoryQuery.java From ignite with Apache License 2.0 | 6 votes |
private <T extends Parameter> int getDynamicQueryConfigurationIndex(QueryMethod method) { Iterator<T> it = (Iterator<T>)method.getParameters().iterator(); int i = 0; boolean found = false; int index = -1; while (it.hasNext()) { T parameter = it.next(); if (DynamicQueryConfig.class.isAssignableFrom(parameter.getType())) { if (found) { throw new IllegalStateException("Invalid '" + method.getName() + "' repository method signature. " + "Only ONE DynamicQueryConfig parameter is allowed"); } found = true; index = i; } i++; } return index; }
Example #7
Source File: FreemarkerTemplateQuery.java From spring-data-jpa-extra with Apache License 2.0 | 6 votes |
private Map<String, Object> getParams(Object[] values) { JpaParameters parameters = getQueryMethod().getParameters(); //gen model Map<String, Object> params = new HashMap<String, Object>(); for (int i = 0; i < parameters.getNumberOfParameters(); i++) { Object value = values[i]; Parameter parameter = parameters.getParameter(i); if (value != null && canBindParameter(parameter)) { if (!QueryBuilder.isValidValue(value)) { continue; } Class<?> clz = value.getClass(); if (clz.isPrimitive() || String.class.isAssignableFrom(clz) || Number.class.isAssignableFrom(clz) || clz.isArray() || Collection.class.isAssignableFrom(clz) || clz.isEnum()) { params.put(parameter.getName().orElse(null), value); } else { params = QueryBuilder.toParams(value); } } } return params; }
Example #8
Source File: StringQueryParameterBinder.java From spring-data-ebean with Apache License 2.0 | 6 votes |
/** * 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 #9
Source File: SqlSpannerQuery.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
private List<String> getParamTags() { List<String> tags = new ArrayList<>(); Set<String> seen = new HashSet<>(); Parameters<?, ?> parameters = getQueryMethod().getParameters(); for (int i = 0; i < parameters.getNumberOfParameters(); i++) { Parameter param = parameters.getParameter(i); if (isPageableOrSort(param.getType())) { continue; } Optional<String> paramName = param.getName(); if (!paramName.isPresent()) { throw new SpannerDataException( "Query method has a parameter without a valid name: " + getQueryMethod().getName()); } String name = paramName.get(); if (seen.contains(name)) { throw new SpannerDataException( "More than one param has the same name: " + name); } seen.add(name); tags.add(name); } return tags; }
Example #10
Source File: ReactiveStringBasedNeo4jQuery.java From sdn-rx with Apache License 2.0 | 6 votes |
Map<String, Object> bindParameters(Neo4jParameterAccessor parameterAccessor) { final Parameters<?, ?> formalParameters = parameterAccessor.getParameters(); Map<String, Object> resolvedParameters = new HashMap<>(); // Values from the parameter accessor can only get converted after evaluation for (Map.Entry<String, Object> evaluatedParam : spelEvaluator.evaluate(parameterAccessor.getValues()).entrySet()) { resolvedParameters.put(evaluatedParam.getKey(), super.convertParameter(evaluatedParam.getValue())); } formalParameters.stream() .filter(Parameter::isBindable) .forEach(parameter -> { int parameterIndex = parameter.getIndex(); Object parameterValue = super.convertParameter(parameterAccessor.getBindableValue(parameterIndex)); // Add the parameter under its name when possible parameter.getName() .ifPresent(parameterName -> resolvedParameters.put(parameterName, parameterValue)); // Always add under its index. resolvedParameters.put(Integer.toString(parameterIndex), parameterValue); }); return resolvedParameters; }
Example #11
Source File: GqlDatastoreQuery.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
private void setOriginalParamTags() { this.originalParamTags = new ArrayList<>(); Set<String> seen = new HashSet<>(); Parameters parameters = getQueryMethod().getParameters(); for (int i = 0; i < parameters.getNumberOfParameters(); i++) { Parameter param = parameters.getParameter(i); if (Pageable.class.isAssignableFrom(param.getType()) || Sort.class.isAssignableFrom(param.getType())) { continue; } Optional<String> paramName = param.getName(); if (!paramName.isPresent()) { throw new DatastoreDataException( "Query method has a parameter without a valid name: " + getQueryMethod().getName()); } String name = paramName.get(); if (seen.contains(name)) { throw new DatastoreDataException( "More than one param has the same name: " + name); } seen.add(name); this.originalParamTags.add(name); } }
Example #12
Source File: GqlDatastoreQueryTests.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
private Parameters buildParameters(Object[] params, String[] paramNames) { Parameters parameters = mock(Parameters.class); Mockito.<Parameters>when(this.queryMethod.getParameters()) .thenReturn(parameters); when(parameters.getNumberOfParameters()).thenReturn(paramNames.length); when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> { int index = invocation.getArgument(0); Parameter param = mock(Parameter.class); when(param.getName()) .thenReturn(paramNames[index] == null ? Optional.empty() : Optional.of(paramNames[index])); Mockito.<Class>when(param.getType()).thenReturn(params[index].getClass()); return param; }); return parameters; }
Example #13
Source File: SpelExpressionStringQueryParameterBinder.java From spring-data-ebean with Apache License 2.0 | 6 votes |
@Override protected boolean canBindParameter(Parameter parameter) { List<StringQuery.ParameterBinding> parameterBindings = query.getParameterBindings(); // if no parameter bindings are present, we simply rely on the check in super. if (parameterBindings.isEmpty()) { return super.canBindParameter(parameter); } // otherwise determine whether there are any non expression parameters left to be bound. int expressionParameterCount = 0; for (StringQuery.ParameterBinding binding : parameterBindings) { if (binding.isExpression()) { expressionParameterCount++; } } boolean allParametersAreUsedInExpressions = parameterBindings.size() - expressionParameterCount == 0; // if all parameters are used in expressions, then we can skip their bindings now, since they'll get bound later. return !allParametersAreUsedInExpressions && super.canBindParameter(parameter); }
Example #14
Source File: QueryUtils.java From spring-data-simpledb with MIT License | 5 votes |
/** * Supported types: primitives & core java types (Date, primitive arrays, * primitive wrappers) */ public static void validateBindParametersTypes(Parameters parameters) { final Iterator<Parameter> it = parameters.iterator(); while (it.hasNext()) { final Parameter param = it.next(); final Class<?> paramType = param.getType(); if (!(param.isSpecialParameter() || SupportedCoreTypes .isSupported(paramType))) { throw (new IllegalArgumentException("Type " + paramType + " not supported as an annotated query parameter!")); } } }
Example #15
Source File: QueryUtilsArrayTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes" }) private Parameters getMockParameters(String[] placeHolders, Class[] clazzes) { Parameters mockParameters = Mockito.mock(Parameters.class); List<Parameter> parameters = new ArrayList<Parameter>(placeHolders.length); for(int idx = 0; idx < placeHolders.length; ++idx) { parameters.add(getMockParameter(placeHolders[idx], idx, clazzes[idx])); } Mockito.when(mockParameters.iterator()).thenReturn(parameters.iterator()); Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(parameters.size()); return mockParameters; }
Example #16
Source File: QueryUtilsArrayTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Parameter getMockParameter(String placeHolder, Integer idx, Class clazz) { Parameter mockParameter = Mockito.mock(Parameter.class); Mockito.when(mockParameter.getPlaceholder()).thenReturn(placeHolder); Mockito.when(mockParameter.isNamedParameter()).thenReturn(Boolean.TRUE); Mockito.when(mockParameter.getIndex()).thenReturn(idx); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.isSpecialParameter()).thenReturn(TYPES.contains(clazz)); return mockParameter; }
Example #17
Source File: QueryUtilsIndexByQueryTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes" }) private Parameters getMockParameters(String[] placeHolders, Class[] clazzes) { Parameters mockParameters = Mockito.mock(Parameters.class); List<Parameter> parameters = new ArrayList<Parameter>(placeHolders.length); for(int idx = 0; idx < placeHolders.length; ++idx) { parameters.add(getMockParameter(placeHolders[idx], idx, clazzes[idx])); } Mockito.when(mockParameters.iterator()).thenReturn(parameters.iterator()); Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(parameters.size()); return mockParameters; }
Example #18
Source File: QueryUtilsIndexByQueryTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Parameter getMockParameter(String placeHolder, Integer idx, Class clazz) { Parameter mockParameter = Mockito.mock(Parameter.class); Mockito.when(mockParameter.getPlaceholder()).thenReturn(placeHolder); Mockito.when(mockParameter.isNamedParameter()).thenReturn(Boolean.FALSE); Mockito.when(mockParameter.getIndex()).thenReturn(idx); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.isSpecialParameter()).thenReturn(TYPES.contains(clazz)); return mockParameter; }
Example #19
Source File: SimpleDbIndexByQueryMethodBindedTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes" }) private Parameters getMockParameters(String[] placeHolders, Class[] clazzes) { Parameters mockParameters = Mockito.mock(Parameters.class); List<Parameter> parameters = new ArrayList<Parameter>(placeHolders.length); for(int idx = 0; idx < placeHolders.length; ++idx) { parameters.add(getMockParameter(placeHolders[idx], idx, clazzes[idx])); } Mockito.when(mockParameters.iterator()).thenReturn(parameters.iterator()); Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(parameters.size()); return mockParameters; }
Example #20
Source File: SimpleDbIndexByQueryMethodBindedTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Parameter getMockParameter(String placeHolder, Integer idx, Class clazz) { Parameter mockParameter = Mockito.mock(Parameter.class); Mockito.when(mockParameter.getPlaceholder()).thenReturn(placeHolder); Mockito.when(mockParameter.isNamedParameter()).thenReturn(Boolean.FALSE); Mockito.when(mockParameter.getIndex()).thenReturn(idx); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.isSpecialParameter()).thenReturn(TYPES.contains(clazz)); return mockParameter; }
Example #21
Source File: QueryUtilsNamedQueryTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes" }) private Parameters getMockParameters(String[] placeHolders, Class[] clazzes) { Parameters mockParameters = Mockito.mock(Parameters.class); List<Parameter> parameters = new ArrayList<Parameter>(placeHolders.length); for(int idx = 0; idx < placeHolders.length; ++idx) { parameters.add(getMockParameter(placeHolders[idx], idx, clazzes[idx])); } Mockito.when(mockParameters.iterator()).thenReturn(parameters.iterator()); Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(parameters.size()); return mockParameters; }
Example #22
Source File: QueryUtilsNamedQueryTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Parameter getMockParameter(String placeHolder, Integer idx, Class clazz) { Parameter mockParameter = Mockito.mock(Parameter.class); Mockito.when(mockParameter.getPlaceholder()).thenReturn(placeHolder); Mockito.when(mockParameter.isNamedParameter()).thenReturn(Boolean.TRUE); Mockito.when(mockParameter.getIndex()).thenReturn(idx); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.isSpecialParameter()).thenReturn(TYPES.contains(clazz)); return mockParameter; }
Example #23
Source File: SimpleDbNamedQueryMethodBindedTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes" }) private Parameters getMockParameters(String[] placeHolders, Class[] clazzes) { Parameters mockParameters = Mockito.mock(Parameters.class); List<Parameter> parameters = new ArrayList<Parameter>(placeHolders.length); for(int idx = 0; idx < placeHolders.length; ++idx) { parameters.add(getMockParameter(placeHolders[idx], idx, clazzes[idx])); } Mockito.when(mockParameters.iterator()).thenReturn(parameters.iterator()); Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(parameters.size()); return mockParameters; }
Example #24
Source File: SimpleDbNamedQueryMethodBindedTest.java From spring-data-simpledb with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private Parameter getMockParameter(String placeHolder, Integer idx, Class clazz) { Parameter mockParameter = Mockito.mock(Parameter.class); Mockito.when(mockParameter.getPlaceholder()).thenReturn(placeHolder); Mockito.when(mockParameter.isNamedParameter()).thenReturn(Boolean.TRUE); Mockito.when(mockParameter.getIndex()).thenReturn(idx); Mockito.when(mockParameter.getType()).thenReturn(clazz); Mockito.when(mockParameter.isSpecialParameter()).thenReturn(TYPES.contains(clazz)); return mockParameter; }
Example #25
Source File: QueryUtils.java From spring-data-simpledb with MIT License | 5 votes |
public static boolean hasNamedParameter(SimpleDbQueryMethod queryMethod) { for (Parameter param : queryMethod.getParameters()) { if (param.isNamedParameter()) { return Boolean.TRUE; } } return Boolean.FALSE; }
Example #26
Source File: HazelcastPartTreeQuery.java From spring-data-hazelcast with Apache License 2.0 | 5 votes |
/** * <p> * Determine if the arguments to the method need reordered. * </P> * <p> * For searches such as {@code findBySomethingNotNull} there may be more parts than parameters needed to be bound to * them. * </P> * * @param partTree Query parts * @param bindableParameters Parameters expected */ @SuppressWarnings("unchecked") private void prepareRearrange(final PartTree partTree, final Parameters<?, ?> bindableParameters) { this.isRearrangeRequired = false; if (partTree == null || bindableParameters == null) { return; } List<String> queryParams = new ArrayList<>(); List<String> methodParams = new ArrayList<>(); for (Part part : partTree.getParts()) { queryParams.add(part.getProperty().getSegment()); } Iterator<Parameter> bindableParameterIterator = (Iterator<Parameter>) bindableParameters.iterator(); while (bindableParameterIterator.hasNext()) { Parameter parameter = bindableParameterIterator.next(); parameter.getName().ifPresent(methodParams::add); } this.rearrangeIndex = new int[queryParams.size()]; String[] paramsExpected = queryParams.toArray(new String[0]); String[] paramsProvided = methodParams.toArray(new String[0]); for (int i = 0; i < this.rearrangeIndex.length; i++) { this.rearrangeIndex[i] = i; for (int j = 0; j < paramsProvided.length; j++) { if (paramsProvided[j] != null && paramsProvided[j].equals(paramsExpected[i])) { this.rearrangeIndex[i] = j; this.isRearrangeRequired = true; } } } }
Example #27
Source File: DatastoreQueryLookupStrategyTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Test public void resolveSqlQueryTest() { String queryName = "fakeNamedQueryName"; String query = "fake query"; when(this.queryMethod.getNamedQueryName()).thenReturn(queryName); Query queryAnnotation = mock(Query.class); when(this.queryMethod.getQueryAnnotation()).thenReturn(queryAnnotation); NamedQueries namedQueries = mock(NamedQueries.class); Parameters parameters = mock(Parameters.class); Mockito.<Parameters>when(this.queryMethod.getParameters()) .thenReturn(parameters); when(parameters.getNumberOfParameters()).thenReturn(1); when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> { Parameter param = mock(Parameter.class); when(param.getName()).thenReturn(Optional.of("tag")); Mockito.<Class>when(param.getType()).thenReturn(Object.class); return param; }); when(namedQueries.hasQuery(eq(queryName))).thenReturn(true); when(namedQueries.getQuery(eq(queryName))).thenReturn(query); this.datastoreQueryLookupStrategy.resolveQuery(null, null, null, namedQueries); verify(this.datastoreQueryLookupStrategy, times(1)).createGqlDatastoreQuery( eq(Object.class), same(this.queryMethod), eq(query)); }
Example #28
Source File: ReactiveProcessorUtils.java From mongodb-aggregate-query-support with Apache License 2.0 | 5 votes |
private List<Object> getParameterValues(Method method, MongoParameterAccessor mongoParameterAccessor, ConvertingParameterAccessor convertingParameterAccessor ) { List<Object> retval = new ArrayList<>(); int numArgs = method.getParameterCount(); for (int i = 0; i < numArgs; i++) { Parameter param = ((MongoParametersParameterAccessor) mongoParameterAccessor).getParameters().getParameter(i); if (param.isBindable()) { retval.add(convertingParameterAccessor.getBindableValue(i)); } } return retval; }
Example #29
Source File: ProcessorUtils.java From mongodb-aggregate-query-support with Apache License 2.0 | 5 votes |
@SuppressWarnings("WeakerAccess") public List<Object> getParameterValues(Method method, MongoParameterAccessor mongoParameterAccessor, ConvertingParameterAccessor convertingParameterAccessor) { List<Object> retval = new ArrayList<>(); int numArgs = method.getParameterCount(); for (int i = 0; i < numArgs; i++) { Parameter param = ((MongoParametersParameterAccessor) mongoParameterAccessor).getParameters().getParameter(i); if (param.isBindable()) { retval.add(convertingParameterAccessor.getBindableValue(i)); } } return retval; }
Example #30
Source File: SqlSpannerQuery.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private Statement buildStatementFromQueryAndTags(QueryTagValue queryTagValue) { Map<String, java.lang.reflect.Parameter> paramMetadataMap = new HashMap<>(); for (java.lang.reflect.Parameter param : getQueryMethod().getMethod().getParameters()) { Param annotation = param.getAnnotation(Param.class); paramMetadataMap.put(annotation == null ? param.getName() : annotation.value(), param); } return SpannerStatementQueryExecutor.buildStatementFromSqlWithArgs( queryTagValue.sql, queryTagValue.tags, this.paramStructConvertFunc, this.spannerTemplate.getSpannerEntityProcessor().getWriteConverter(), queryTagValue.params.toArray(), paramMetadataMap); }