@Override @SuppressWarnings("unchecked") protected Object doExecute(final AbstractEbeanQuery repositoryQuery, final Object[] values) { ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); Object createQuery = repositoryQuery.createQuery(values); if (createQuery instanceof Query) { Query ormQuery = (Query) createQuery; PagedList pagedList = ormQuery.findPagedList(); return PageableExecutionUtils.getPage(pagedList.getList(), accessor.getPageable(), () -> pagedList.getTotalCount()); } else if (createQuery instanceof SqlQuery) { SqlQuery sqlQuery = (SqlQuery) createQuery; // TODO page return sqlQuery.findList(); } else { throw new InvalidEbeanQueryMethodException("query must be Query or SqlQuery"); } }
@Override protected Query doCreateQuery(Object[] values) { String nativeQuery = getQuery(values); JpaParameters parameters = getQueryMethod().getParameters(); ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); String sortedQueryString = QueryUtils .applySorting(nativeQuery, accessor.getSort(), QueryUtils.detectAlias(nativeQuery)); Query query = bind(createJpaQuery(sortedQueryString), values); if (parameters.hasPageableParameter()) { Pageable pageable = (Pageable) (values[parameters.getPageableIndex()]); if (pageable != null) { query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); } } return query; }
private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args) throws NoSuchMethodException, SecurityException { Class<?>[] argTypes = new Class<?>[args.length]; if (!ObjectUtils.isEmpty(args)) { for (int i = 0; i < args.length; i++) { argTypes[i] = args[i].getClass(); } } Method method = PersonRepository.class.getMethod(methodName, argTypes); doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method); PartTree partTree = new PartTree(method.getName(), method.getReturnType()); SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor( new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args)); KeyValueQuery<SpelExpression> q = creator.createQuery(); q.getCriteria().setEvaluationContext(new StandardEvaluationContext(args)); return q; }
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception { List<Class<?>> types = new ArrayList<>(args.length); for (Object arg : args) { types.add(arg.getClass()); } Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()])); RepositoryMetadata metadata = mock(RepositoryMetadata.class); doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method); PartTree partTree = new PartTree(method.getName(), method.getReturnType()); SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor( new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args)); return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args)); }
private TarantoolQueryCreator createQueryCreatorForMethodWithArgs(Method method, Object[] args) { PartTree partTree = new PartTree(method.getName(), method.getReturnType()); TarantoolQueryCreator creator = new TarantoolQueryCreator(partTree, new ParametersParameterAccessor(new DefaultParameters( method), args)); return creator; }
protected BaseQuery<?> createQuery(ParametersParameterAccessor accessor) { Class<?> entityClass = getQueryMethod().getEntityInformation().getJavaType(); CriteriaQuery<?> query = new SnowdropQueryCreator(entityClass, tree, accessor, mappingContext).createQuery(); query.setTargetFields(TargetFieldsUtils.getTargetFieldsMap(getQueryMethod())); query.setMaxResults(tree.getMaxResults()); query.setDistinct(tree.isDistinct()); return query; }
private String replacePlaceholders(String input, ParametersParameterAccessor accessor) { Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input); String result = input; while (matcher.find()) { String group = matcher.group(); int index = Integer.parseInt(matcher.group(1)); result = result.replace(group, getParameterWithIndex(accessor, index)); } return result; }
private String getParameterWithIndex(ParametersParameterAccessor accessor, int index) { Object parameter = accessor.getBindableValue(index); if (parameter == null) { return "null"; } if (conversionService.canConvert(parameter.getClass(), String.class)) { return conversionService.convert(parameter, String.class); } return parameter.toString(); }
protected Class<?> getProjection(ParametersParameterAccessor accessor) { Optional<Class<?>> dp = accessor.getDynamicProjection(); if (dp.isPresent()) { return dp.get(); } Class<?> returnType = getQueryMethod().getReturnedObjectType(); if (returnType.isInterface()) { return returnType; } // TODO - handle DTO return null; }
@Override public Object execute(Object[] parameters) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); ResultProcessor resultProcessor = getQueryMethod().getResultProcessor().withDynamicProjection(accessor); Object source = executeInternal(accessor); return resultProcessor.processResult(source); }
private GcloudDatastoreQueryCreator createCreator(Class<?> repositoryClass, Method method, Object... values) { QueryMethod queryMethod = new QueryMethod(method, new DefaultRepositoryMetadata(repositoryClass), new SpelAwareProxyProjectionFactory()); return new GcloudDatastoreQueryCreator( new PartTree(method.getName(), queryMethod.getResultProcessor().getReturnedType() .getDomainType()), new ParametersParameterAccessor(queryMethod.getParameters(), values), DatastoreOptions.getDefaultInstance()); }
@Override public Object execute(Object[] parameters) { ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor); return processor.processResult(doExecute(parameters)); }
@Override public Object doCreateQuery(Object[] values) { ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), values); Object query = createEbeanQuery(this.query.getQueryString()); return createBinder(values).bindAndPrepare(query); }
protected EbeanQueryCreator createCreator(ParametersParameterAccessor accessor) { EbeanServer ebeanServer = getEbeanServer(); Query ebeanQuery = ebeanServer.createQuery(domainClass); ExpressionList expressionList = ebeanQuery.where(); ParameterMetadataProvider provider = new ParameterMetadataProvider(accessor); ResultProcessor processor = getQueryMethod().getResultProcessor(); return new EbeanQueryCreator(tree, processor.getReturnedType(), expressionList, provider); }
@Override @SuppressWarnings("unchecked") protected Object doExecute(AbstractEbeanQuery query, Object[] values) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); Pageable pageable = accessor.getPageable(); Object createQuery = query.createQuery(values); List<Object> resultList = null; int pageSize = pageable.getPageSize(); if (createQuery instanceof Query) { Query ormQuery = (Query) createQuery; ormQuery.setMaxRows(pageSize + 1); ormQuery.findList(); } else if (createQuery instanceof SqlQuery) { SqlQuery sqlQuery = (SqlQuery) createQuery; sqlQuery.setMaxRows(pageSize + 1); sqlQuery.findList(); } else { throw new InvalidEbeanQueryMethodException("query must be Query or SqlQuery"); } boolean hasNext = resultList != null && resultList.size() > pageSize; return new SliceImpl<Object>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext); }
/** * Creates a new {@link ParameterBinder}. * * @param parameters must not be {@literal null}. * @param values must not be {@literal null}. */ public ParameterBinder(DefaultParameters parameters, Object[] values) { Assert.notNull(parameters, "Parameters must not be null!"); Assert.notNull(values, "Values must not be null!"); Assert.isTrue(parameters.getNumberOfParameters() == values.length, "Invalid number of parameters given!"); this.parameters = parameters; this.values = values.clone(); this.accessor = new ParametersParameterAccessor(parameters, this.values); }
VaultQuery createQuery(String methodName, String value) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils.findMethod(dummy.class, "someUnrelatedMethod", String.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator( partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value }), mappingContext); return queryCreator.createQuery().getCriteria(); }
VaultQuery createQuery(String methodName, List<String> value) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils .findMethod(dummy.class, "someUnrelatedMethod", List.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator( partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value }), mappingContext); return queryCreator.createQuery().getCriteria(); }
VaultQuery createQuery(String methodName, String value, String anotherValue) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils.findMethod(dummy.class, "someUnrelatedMethod", String.class, String.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator(partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value, anotherValue }), mappingContext); return queryCreator.createQuery().getCriteria(); }
@Override protected Object doExecute(AbstractGqQuery repositoryQuery, Object[] values) { long total = repositoryQuery.getResultCount(values); ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); Pageable pageable = accessor.getPageable(); if (pageable.getOffset() >= total) { return new PageImpl<Object>(Collections.emptyList(), pageable, total); } List<?> content = repositoryQuery.getResultList(values, pageable); return new PageImpl<>(content, pageable, total); }
@Override public Object execute(Object[] parameters) { ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); KeyValueQuery<?> query = prepareQuery(parameters); ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor); return processor.processResult(doExecute(parameters, query)); }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected KeyValueQuery<?> prepareQuery(KeyValueQuery<?> instance, Object[] parameters) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); Object criteria = instance.getCriteria(); if (criteria instanceof SpelCriteria || criteria instanceof SpelExpression) { SpelExpression spelExpression = getSpelExpression(criteria); EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), parameters); criteria = new SpelCriteria(spelExpression, context); } KeyValueQuery<?> query = new KeyValueQuery(criteria); Pageable pageable = accessor.getPageable(); Sort sort = accessor.getSort(); query.setOffset(pageable.toOptional().map(Pageable::getOffset).orElse(-1L)); if (pageable.isPaged()) { query.setRows(pageable.getPageSize()); } else if (instance.getRows() >= 0) { query.setRows(instance.getRows()); } query.setSort(sort.isUnsorted() ? instance.getSort() : sort); return query; }
@Override public Query<T> doCreateQuery(Object[] values) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); DynamoDBQueryCreator<T, ID> queryCreator = createQueryCreator(accessor); return queryCreator.createQuery(); }
@Override public Query<Long> doCreateCountQuery(Object[] values,boolean pageQuery) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); DynamoDBCountQueryCreator<T, ID> queryCreator = createCountQueryCreator(accessor,pageQuery); return queryCreator.createQuery(); }
@Override public Object execute(AbstractDynamoDBQuery<T, ID> dynamoDBQuery, Object[] values) { ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); Pageable pageable = accessor.getPageable(); Query<T> query = dynamoDBQuery.doCreateQueryWithPermissions(values); List<T> results = query.getResultList(); return createPage(results, pageable,dynamoDBQuery,values); }
@Override public Object execute(AbstractDynamoDBQuery<T, ID> dynamoDBQuery, Object[] values) { ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); Pageable pageable = accessor.getPageable(); Query<T> query = dynamoDBQuery.doCreateQueryWithPermissions(values); List<T> results = query.getResultList(); return createSlice(results, pageable); }
public static ParametersParameterAccessor generateParameters(QueryMethod method) { return new ParametersParameterAccessor(method.getParameters(), new Object[method.getParameters().getNumberOfParameters()]); }
protected BaseQuery<?> createQuery(ParametersParameterAccessor parameterAccessor) { String queryString = replacePlaceholders(this.query, parameterAccessor); Class<?> entityClass = getQueryMethod().getEntityInformation().getJavaType(); return new StringQuery<>(entityClass, queryString); }
private Object executeInternal(ParametersParameterAccessor accessor) { BaseQuery<?> query = createQuery(accessor); Class<?> projectionClass = getProjection(accessor); if (projectionClass != null) { ProjectionInformation pi = getQueryMethod().getFactory().getProjectionInformation(projectionClass); query.setProjectionInformation(pi); } Sort sort = query.getSort(); if (sort == null) { sort = accessor.getSort(); } else { sort = sort.and(accessor.getSort()); } query.setSort(sort); if (query.getPageable() == null) { query.setPageable(accessor.getPageable()); } if (isModify(query)) { throw new UnsupportedOperationException("Hibernate Search repository support is read-only!"); } else if (getQueryMethod().isSliceQuery()) { return snowdropOperations.findSlice(query); } else if (getQueryMethod().isPageQuery()) { return snowdropOperations.findPageable(query); } else if (getQueryMethod().isStreamQuery()) { return snowdropOperations.stream(query); } else if (getQueryMethod().isCollectionQuery()) { return snowdropOperations.findAll(query); } else if (isCountProjection(query)) { return snowdropOperations.count(query); } else if (isExistsProjection(query)) { return (snowdropOperations.count(query) > 0); } else if ((projectionClass != null) || getQueryMethod().isQueryForEntity()) { return snowdropOperations.findSingle(query); } else { throw new IllegalArgumentException("Invalid query type."); } }
private Sort getDynamicSort(Object[] values) { return parameters.potentiallySortsDynamically() ? new ParametersParameterAccessor(parameters, values).getSort() : null; }
public SolrParametersParameterAccessor(SolrQueryMethod solrQueryMethod, Object[] values) { this.parameters = solrQueryMethod.getParameters(); this.parametersParameterAccessorDelegate = new ParametersParameterAccessor(this.parameters, values.clone()); }
private Query<?> createQuery(Object[] values, boolean withPageSort) { Query<?> q = QB.create(metadata); ParametersParameterAccessor accessor = new ParametersParameterAccessor( parameters, values); Or or = new Or(); int index = 0; for (GqOrPart node : tree) { And and = new And(); for (GqPart part : node) { PropertyPath path = part.getProperty(); if (path.getOwningType().getType() != metadata.getThisType()) { throw new IllegalArgumentException("PathType:" + path.getOwningType().getType() + " metadata:" + metadata.getThisType()); } String fieldName = path.getSegment(); ColumnMapping field = metadata.findField(fieldName); PairIO<GqParameter> paramInfo = getBindParamIndex(index++, fieldName); Object obj = accessor.getBindableValue(paramInfo.first); if (field != null) { IgnoreIf ignore = paramInfo.second.getIgnoreIf(); if (ignore == null || !QueryUtils.isIgnore(ignore, obj)) { add(and, part, field.field(), obj); } } } or.addCondition(and); } q.addCondition(or); if (withPageSort) { Sort sort = tree.getSort(); if (accessor.getSort() != null) { sort = accessor.getSort(); } Pageable page = accessor.getPageable(); if (page != null && page.getSort() != null) { sort = page.getSort(); } if (sort != null) setSortToSpec(q, sort, metadata); } return q; }
/** * <P> * Handle {@code @Param}. * </P> * <OL> * <LI><B>Without {@code @Param}</B> * <P> * Arguments to the call are assumed to follow the same sequence as cited in the method name. * </P> * <P> * Eg. * * <pre> * findBy<U>One</U>And<U>Two</U>(String <U>one</U>, String <U>two</U>); * </pre> * </P> * </LI> * <LI><B>With {@code @Param}</B> * <P> * Arguments to the call are use the {@code @Param} to match them against the fields. * <P> * Eg. * * <pre> * findBy<U>One</U>And<U>Two</U>(@Param("two") String <U>two</U>, @Param("one") String <U>one</U>); * </pre> * </P> * </LI> * </OL> * * @param parameters Possibly empty * @param partTree Query tree to traverse * @return Paremeters in correct order */ private ParametersParameterAccessor prepareAccessor(final Object[] originalParameters, final PartTree partTree) { if (!this.isRearrangeKnown) { this.prepareRearrange(partTree, this.queryMethod.getParameters().getBindableParameters()); this.isRearrangeKnown = true; } Object[] parameters = originalParameters; if (parameters != null && this.isRearrangeRequired) { parameters = new Object[originalParameters.length]; for (int i = 0; i < parameters.length; i++) { int index = rearrangeIndex[i]; parameters[i] = originalParameters[index]; } } return new ParametersParameterAccessor(this.queryMethod.getParameters(), parameters); }
protected KeyValueQuery<?> prepareQuery(Object[] parameters) { return prepareQuery(createQuery(new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters)), parameters); }
protected DynamoDBQueryCreator<T, ID> createQueryCreator(ParametersParameterAccessor accessor) { return new DynamoDBQueryCreator<T, ID>(tree, accessor, queryMethod.getEntityInformation(), dynamoDBOperations); }
protected DynamoDBCountQueryCreator<T, ID> createCountQueryCreator(ParametersParameterAccessor accessor,boolean pageQuery) { return new DynamoDBCountQueryCreator<T, ID>(tree, accessor, queryMethod.getEntityInformation(), dynamoDBOperations, pageQuery); }
private Object doExecute(MybatisQueryExecution execution, Object[] parameters) { Object result = execution.execute(this, parameters); ParametersParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), parameters); ResultProcessor withDynamicProjection = method.getResultProcessor().withDynamicProjection(accessor); return withDynamicProjection.processResult(result, new TupleConverter(withDynamicProjection.getReturnedType())); }
/** * Creates a new {@link Query} for the given parameter values. * * @param values * @return */ public Object createQuery(Object[] values) { ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); EbeanQueryCreator ebeanQueryCreator = createCreator(accessor); return restrictMaxResultsIfNecessary((Query) invokeBinding(getBinder(values), ebeanQueryCreator.createQuery())); }
/** * Creates a new {@link ParameterMetadataProvider} from the given * {@link ParametersParameterAccessor} with impl for parameter value customizations. * * @param accessor must not be {@literal null}. */ public ParameterMetadataProvider(ParametersParameterAccessor accessor) { this(accessor.iterator(), accessor.getParameters()); }