org.springframework.data.repository.query.parser.Part Java Examples
The following examples show how to use
org.springframework.data.repository.query.parser.Part.
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 |
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: ReactiveCosmosQueryCreator.java From spring-data-cosmosdb with MIT License | 6 votes |
@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: CosmosQueryCreator.java From spring-data-cosmosdb with MIT License | 6 votes |
@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: EbeanQueryCreator.java From spring-data-ebean with Apache License 2.0 | 5 votes |
/** * Creates a new {@link ExpressionBuilder} for the given {@link Part} and {@link ExpressionList}. * * @param part must not be {@literal null}. * @param root must not be {@literal null}. */ public ExpressionBuilder(Part part, ExpressionList<?> root) { Assert.notNull(part, "Part must not be null!"); Assert.notNull(root, "ExpressionList must not be null!"); this.part = part; this.root = root; }
Example #5
Source File: CypherQueryCreator.java From sdn-rx with Apache License 2.0 | 5 votes |
/** * Checks whether or not to ignore the case for some operations. {@link PartTreeNeo4jQuery} will already have validated * which properties can be made case insensitive given a certain keyword. * * @param part query part to get checked if case should get ignored * @return should the case get ignored */ boolean ignoreCase(Part part) { switch (part.shouldIgnoreCase()) { case ALWAYS: return true; case WHEN_POSSIBLE: return canIgnoreCase(part); case NEVER: return false; default: throw new IllegalArgumentException("Unsupported option for ignoring case: " + part.shouldIgnoreCase()); } }
Example #6
Source File: AbstractDynamoDBQueryCreator.java From spring-data-dynamodb with Apache License 2.0 | 5 votes |
@Override protected DynamoDBQueryCriteria<T, ID> create(Part part, Iterator<Object> iterator) { DynamoDBQueryCriteria<T, ID> criteria = entityMetadata.isRangeKeyAware() ? new DynamoDBEntityWithHashAndRangeKeyCriteria<T, ID>( (DynamoDBIdIsHashAndRangeKeyEntityInformation<T, ID>) entityMetadata) : new DynamoDBEntityWithHashKeyOnlyCriteria<T, ID>(entityMetadata); return addCriteria(criteria, part, iterator); }
Example #7
Source File: PartValidator.java From sdn-rx with Apache License 2.0 | 5 votes |
private void validateIgnoreCase(Part part) { Assert.isTrue(part.shouldIgnoreCase() != Part.IgnoreCaseType.ALWAYS || canIgnoreCase(part), () -> String.format( "Can not derive query for '%s': Only the case of String based properties can be ignored within the following keywords: %s", queryMethod, formatTypes(TYPES_SUPPORTING_CASE_INSENSITIVITY))); }
Example #8
Source File: PartValidator.java From sdn-rx with Apache License 2.0 | 5 votes |
private void validateTemporalProperty(Part part) { Assert.isTrue(COMPARABLE_TEMPORAL_TYPES.contains(part.getProperty().getLeafType()), () -> String .format( "Can not derive query for '%s': The keywords %s work only with properties with one of the following types: %s", queryMethod, formatTypes(Collections.singletonList(part.getType())), COMPARABLE_TEMPORAL_TYPES)); }
Example #9
Source File: PartTreeConverter.java From spring-data-simpledb with MIT License | 5 votes |
/** * Convert a {@link PartTree} into a where query alike to the one present in the * {@link Query}'s where property. */ public static String toIndexedQuery(final PartTree tree) { final StringBuilder result = new StringBuilder(); final Iterator<OrPart> orIt = tree.iterator(); while(orIt.hasNext()) { final OrPart orPart = orIt.next(); final Iterator<Part> partIt = orPart.iterator(); while(partIt.hasNext()) { final Part part = partIt.next(); result.append(" " + part.getProperty().getSegment() + " "); result.append(convertOperator(part.getType())); if(partIt.hasNext()) { result.append(" AND "); } } if(orIt.hasNext()) { result.append(" OR "); } } return StringUtil.removeExtraSpaces(result.toString()); }
Example #10
Source File: PartValidator.java From sdn-rx with Apache License 2.0 | 5 votes |
private void validatePointProperty(Part part) { Assert.isTrue(ClassTypeInformation.from(Point.class) .isAssignableFrom(part.getProperty().getLeafProperty().getTypeInformation()), () -> String .format("Can not derive query for '%s': %s works only with spatial properties", queryMethod, part.getType())); }
Example #11
Source File: DerivedQueryCreator.java From spring-data with Apache License 2.0 | 5 votes |
/** * Wrapps property expression in order to lower case. Only properties of type String or Iterable<String> are lowered * * @param part * @param property * @return */ private String ignorePropertyCase(final Part part, final String property) { if (!shouldIgnoreCase(part)) { return property; } if (!part.getProperty().getLeafProperty().isCollection()) { return "LOWER(" + property + ")"; } return format("(FOR i IN TO_ARRAY(%s) RETURN LOWER(i))", property); }
Example #12
Source File: PartValidator.java From sdn-rx with Apache License 2.0 | 5 votes |
private void validateCollectionProperty(Part part) { Assert.isTrue(part.getProperty().getLeafProperty().isCollection(), () -> String .format("Can not derive query for '%s': The keywords %s work only with collection properties", queryMethod, formatTypes(Collections.singletonList(part.getType())))); }
Example #13
Source File: DerivedQueryCreator.java From spring-data with Apache License 2.0 | 5 votes |
/** * Ensures that the same geo fields are used in geospatial parts of non-nested properties are the same in case * geospatial return type is expected * * @param part */ private void checkUniqueLocation(final Part part) { isUnique = isUnique == null ? true : isUnique; isUnique = (uniqueLocation == null || uniqueLocation.equals(ignorePropertyCase(part))) ? isUnique : false; if (!geoFields.isEmpty()) { Assert.isTrue(isUnique, "Different location fields are used - Distance is ambiguous"); } uniqueLocation = ignorePropertyCase(part); }
Example #14
Source File: HazelcastQueryCreator.java From spring-data-hazelcast with Apache License 2.0 | 5 votes |
private boolean ifIgnoreCase(Part part) { switch (part.shouldIgnoreCase()) { case ALWAYS: Assert.state(canUpperCase(part.getProperty()), String.format("Unable to ignore case of %s types, the property '%s' must reference a String", part.getProperty().getType().getName(), part.getProperty().getSegment())); return true; case WHEN_POSSIBLE: return canUpperCase(part.getProperty()); case NEVER: default: return false; } }
Example #15
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 #16
Source File: SolrQueryCreator.java From dubbox with Apache License 2.0 | 5 votes |
@Override protected Query and(Part part, Query base, Iterator<Object> iterator) { if (base == null) { return create(part, iterator); } PersistentPropertyPath<SolrPersistentProperty> path = context.getPersistentPropertyPath(part.getProperty()); return base.addCriteria(from(part.getType(), new Criteria(path.toDotPath(SolrPersistentProperty.PropertyToFieldNameConverter.INSTANCE)), iterator)); }
Example #17
Source File: CosmosQueryCreator.java From spring-data-cosmosdb with MIT License | 5 votes |
private String getSubject(@NonNull Part part) { String subject = mappingContext.getPersistentPropertyPath(part.getProperty()).toDotPath(); final Class<?> domainType = part.getProperty().getOwningType().getType(); @SuppressWarnings("unchecked") final CosmosEntityInformation information = new CosmosEntityInformation(domainType); if (information.getIdField().getName().equals(subject)) { subject = Constants.ID_PROPERTY_NAME; } return subject; }
Example #18
Source File: ReactiveCosmosQueryCreator.java From spring-data-cosmosdb with MIT License | 5 votes |
private String getSubject(@NonNull Part part) { String subject = mappingContext.getPersistentPropertyPath(part.getProperty()).toDotPath(); final Class<?> domainType = part.getProperty().getOwningType().getType(); @SuppressWarnings("unchecked") final CosmosEntityInformation information = new CosmosEntityInformation(domainType); if (information.getIdField().getName().equals(subject)) { subject = Constants.ID_PROPERTY_NAME; } return subject; }
Example #19
Source File: PartTreeDatastoreQuery.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private void applySelectWithFilter(Object[] parameters, Builder builder) { Iterator it = Arrays.asList(parameters).iterator(); Filter[] filters = this.filterParts.stream().map((part) -> { //build properties chain for nested properties //if the property is not nested, the list would contain only one property List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part); String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName) .collect(Collectors.joining(".")); if (part.getType() == Part.Type.IS_NULL) { return PropertyFilter.isNull(fieldName); } BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType()); if (filterFactory == null) { throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType()); } if (!it.hasNext()) { throw new DatastoreDataException( "Too few parameters are provided for query method: " + getQueryMethod().getName()); } Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next()); return filterFactory.apply(fieldName, convertedValue); }).toArray(Filter[]::new); builder.setFilter( (filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]); }
Example #20
Source File: PartTreeDatastoreQuery.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private List<DatastorePersistentProperty> getPropertiesChain(Part part) { Iterable<PropertyPath> iterable = () -> part.getProperty().iterator(); return StreamSupport.stream(iterable.spliterator(), false) .map(propertyPath -> this.datastoreMappingContext .getPersistentEntity(propertyPath.getOwningType()) .getPersistentProperty(propertyPath.getSegment()) ).collect(Collectors.toList()); }
Example #21
Source File: PartTreeFirestoreQuery.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private StructuredQuery.Builder createBuilderWithFilter(Object[] parameters) { StructuredQuery.Builder builder = StructuredQuery.newBuilder(); Iterator it = Arrays.asList(parameters).iterator(); StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder(); compositeFilter.setOp(StructuredQuery.CompositeFilter.Operator.AND); this.tree.getParts().forEach(part -> { StructuredQuery.FieldReference fieldReference = StructuredQuery.FieldReference.newBuilder() .setFieldPath(buildName(part)).build(); StructuredQuery.Filter.Builder filter = StructuredQuery.Filter.newBuilder(); if (part.getType() == Part.Type.IS_NULL) { filter.getUnaryFilterBuilder().setField(fieldReference) .setOp(StructuredQuery.UnaryFilter.Operator.IS_NULL); } else { if (!it.hasNext()) { throw new FirestoreDataException( "Too few parameters are provided for query method: " + getQueryMethod().getName()); } Object value = it.next(); filter.getFieldFilterBuilder().setField(fieldReference) .setOp(getOperator(part, value)) .setValue(this.classMapper.toFirestoreValue(value)); } compositeFilter.addFilters(filter.build()); }); builder.setWhere(StructuredQuery.Filter.newBuilder().setCompositeFilter(compositeFilter.build())); return builder; }
Example #22
Source File: PartTreeFirestoreQuery.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private String buildName(Part part) { Iterable<PropertyPath> iterable = () -> part.getProperty().iterator(); return StreamSupport .stream(iterable.spliterator(), false) .map(propertyPath -> { FirestorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(propertyPath.getOwningType()); return persistentEntity.getPersistentProperty(propertyPath.getSegment()).getFieldName(); }) .collect(Collectors.joining(".")); }
Example #23
Source File: ParameterMetadataProvider.java From spring-data-ebean with Apache License 2.0 | 5 votes |
/** * 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 #24
Source File: CypherQueryCreator.java From sdn-rx with Apache License 2.0 | 5 votes |
@Override protected Condition and(Part part, Condition base, Iterator<Object> actualParameters) { if (base == null) { return create(part, actualParameters); } return base.and(createImpl(part, actualParameters)); }
Example #25
Source File: VaultQueryCreator.java From spring-vault with Apache License 2.0 | 4 votes |
private static boolean isIgnoreCase(Part part) { return part.shouldIgnoreCase() != IgnoreCaseType.NEVER; }
Example #26
Source File: MybatisPartTreeMapperBuilder.java From spring-data-mybatis with Apache License 2.0 | 4 votes |
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 #27
Source File: EbeanQueryCreator.java From spring-data-ebean with Apache License 2.0 | 4 votes |
/** * Builds a Ebean {@link Expression} from the underlying {@link Part}. * * @return */ public Expression build() { PropertyPath property = part.getProperty(); Part.Type type = part.getType(); switch (type) { case BETWEEN: ParameterMetadataProvider.ParameterMetadata<Comparable> first = provider.next(part); ParameterMetadataProvider.ParameterMetadata<Comparable> second = provider.next(part); return Expr.between(property.toDotPath(), first.getParameterValue(), second.getParameterValue()); case AFTER: case GREATER_THAN: return Expr.gt(property.toDotPath(), provider.next(part).getParameterValue()); case GREATER_THAN_EQUAL: return Expr.ge(property.toDotPath(), provider.next(part).getParameterValue()); case BEFORE: case LESS_THAN: return Expr.lt(property.toDotPath(), provider.next(part).getParameterValue()); case LESS_THAN_EQUAL: return Expr.le(property.toDotPath(), provider.next(part).getParameterValue()); case IS_NULL: return Expr.isNull(property.toDotPath()); case IS_NOT_NULL: return Expr.isNotNull(property.toDotPath()); case NOT_IN: ParameterMetadataProvider.ParameterMetadata<? extends Collection> pmNotIn = provider.next(part, Collection.class); return Expr.not(Expr.in(property.toDotPath(), ParameterMetadataProvider.ParameterMetadata.toCollection(pmNotIn.getParameterValue()))); case IN: ParameterMetadataProvider.ParameterMetadata<? extends Collection> pmIn = provider.next(part, Collection.class); return Expr.in(property.toDotPath(), ParameterMetadataProvider.ParameterMetadata.toCollection(pmIn.getParameterValue())); case STARTING_WITH: return Expr.startsWith(property.toDotPath(), (String) provider.next(part).getParameterValue()); case ENDING_WITH: return Expr.endsWith(property.toDotPath(), (String) provider.next(part).getParameterValue()); case CONTAINING: return Expr.contains(property.toDotPath(), (String) provider.next(part).getParameterValue()); case NOT_CONTAINING: return Expr.not(Expr.contains(property.toDotPath(), (String) provider.next(part).getParameterValue())); case LIKE: return Expr.like(property.toDotPath(), (String) provider.next(part).getParameterValue()); case NOT_LIKE: return Expr.not(Expr.like(property.toDotPath(), (String) provider.next(part).getParameterValue())); case TRUE: return Expr.eq(property.toDotPath(), true); case FALSE: return Expr.eq(property.toDotPath(), false); case SIMPLE_PROPERTY: ParameterMetadataProvider.ParameterMetadata<Object> pmEquals = provider.next(part); return pmEquals.isIsNullParameter() ? Expr.isNull(property.toDotPath()) : Expr.eq(property.toDotPath(), pmEquals.getParameterValue()); case NEGATING_SIMPLE_PROPERTY: ParameterMetadataProvider.ParameterMetadata<Object> pmNot = provider.next(part); return pmNot.isIsNullParameter() ? Expr.isNull(property.toDotPath()) : Expr.ne(property.toDotPath(), pmNot.getParameterValue()); // case IS_EMPTY: // return Expr.isEmpty(property.toDotPath()); // case IS_NOT_EMPTY: // return Expr.isNotEmpty(property.toDotPath()); default: throw new IllegalArgumentException("Unsupported keyword " + type); } }
Example #28
Source File: VaultQueryCreator.java From spring-vault with Apache License 2.0 | 4 votes |
@Override protected VaultQuery create(Part part, Iterator<Object> parameters) { return new VaultQuery(createPredicate(part, parameters)); }
Example #29
Source File: VaultQueryCreator.java From spring-vault with Apache License 2.0 | 4 votes |
@Override protected VaultQuery and(Part part, VaultQuery base, Iterator<Object> parameters) { return base.and(createPredicate(part, parameters)); }
Example #30
Source File: SpelQueryCreator.java From spring-data-keyvalue with Apache License 2.0 | 4 votes |
@Override protected String and(Part part, String base, Iterator<Object> iterator) { return ""; }