|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.relational.repository.query; |
| 17 | + |
| 18 | +import org.springframework.data.relational.core.query.Criteria; |
| 19 | +import org.springframework.data.relational.core.sql.Expression; |
| 20 | +import org.springframework.data.repository.query.parser.Part; |
| 21 | +import org.springframework.util.Assert; |
| 22 | + |
| 23 | +/** |
| 24 | + * Simple factory to contain logic to create {@link Criteria}s from {@link Part}s. |
| 25 | + * |
| 26 | + * @author Roman Chigvintsev |
| 27 | + */ |
| 28 | +class CriteriaFactory { |
| 29 | + |
| 30 | + private final ParameterMetadataProvider parameterMetadataProvider; |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates new instance of this class with the given {@link ParameterMetadataProvider}. |
| 34 | + * |
| 35 | + * @param parameterMetadataProvider parameter metadata provider (must not be {@literal null}) |
| 36 | + */ |
| 37 | + public CriteriaFactory(ParameterMetadataProvider parameterMetadataProvider) { |
| 38 | + Assert.notNull(parameterMetadataProvider, "Parameter metadata provider must not be null!"); |
| 39 | + this.parameterMetadataProvider = parameterMetadataProvider; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates {@link Criteria} for the given {@link Part}. |
| 44 | + * |
| 45 | + * @param part method name part (must not be {@literal null}) |
| 46 | + * @return {@link Criteria} instance |
| 47 | + * @throws IllegalArgumentException if part type is not supported |
| 48 | + */ |
| 49 | + public Criteria createCriteria(Part part) { |
| 50 | + Part.Type type = part.getType(); |
| 51 | + |
| 52 | + String propertyName = part.getProperty().getSegment(); |
| 53 | + Class<?> propertyType = part.getProperty().getType(); |
| 54 | + |
| 55 | + Criteria.CriteriaStep criteriaStep = Criteria.where(propertyName); |
| 56 | + |
| 57 | + if (type == Part.Type.IS_NULL || type == Part.Type.IS_NOT_NULL) { |
| 58 | + return part.getType() == Part.Type.IS_NULL ? criteriaStep.isNull() : criteriaStep.isNotNull(); |
| 59 | + } |
| 60 | + |
| 61 | + if (type == Part.Type.TRUE || type == Part.Type.FALSE) { |
| 62 | + return part.getType() == Part.Type.TRUE ? criteriaStep.isTrue() : criteriaStep.isFalse(); |
| 63 | + } |
| 64 | + |
| 65 | + switch (type) { |
| 66 | + case BETWEEN: { |
| 67 | + ParameterMetadata geParamMetadata = parameterMetadataProvider.next(part); |
| 68 | + ParameterMetadata leParamMetadata = parameterMetadataProvider.next(part); |
| 69 | + return criteriaStep.greaterThanOrEquals(geParamMetadata.getValue()).and(propertyName) |
| 70 | + .lessThanOrEquals(leParamMetadata.getValue()); |
| 71 | + } |
| 72 | + case AFTER: |
| 73 | + case GREATER_THAN: { |
| 74 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 75 | + return criteriaStep.greaterThan(paramMetadata.getValue()); |
| 76 | + } |
| 77 | + case GREATER_THAN_EQUAL: { |
| 78 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 79 | + return criteriaStep.greaterThanOrEquals(paramMetadata.getValue()); |
| 80 | + } |
| 81 | + case BEFORE: |
| 82 | + case LESS_THAN: { |
| 83 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 84 | + return criteriaStep.lessThan(paramMetadata.getValue()); |
| 85 | + } |
| 86 | + case LESS_THAN_EQUAL: { |
| 87 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 88 | + return criteriaStep.lessThanOrEquals(paramMetadata.getValue()); |
| 89 | + } |
| 90 | + case IN: |
| 91 | + case NOT_IN: { |
| 92 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 93 | + Criteria criteria = part.getType() == Part.Type.IN ? criteriaStep.in(paramMetadata.getValue()) |
| 94 | + : criteriaStep.notIn(paramMetadata.getValue()); |
| 95 | + return criteria.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, part.getProperty().getType())); |
| 96 | + } |
| 97 | + case STARTING_WITH: |
| 98 | + case ENDING_WITH: |
| 99 | + case CONTAINING: |
| 100 | + case NOT_CONTAINING: |
| 101 | + case LIKE: |
| 102 | + case NOT_LIKE: { |
| 103 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 104 | + Criteria criteria = part.getType() == Part.Type.NOT_LIKE || part.getType() == Part.Type.NOT_CONTAINING |
| 105 | + ? criteriaStep.notLike(paramMetadata.getValue()) |
| 106 | + : criteriaStep.like(paramMetadata.getValue()); |
| 107 | + return criteria |
| 108 | + .ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType())); |
| 109 | + } |
| 110 | + case SIMPLE_PROPERTY: { |
| 111 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 112 | + if (paramMetadata.getValue() == null) { |
| 113 | + return criteriaStep.isNull(); |
| 114 | + } |
| 115 | + return criteriaStep.is(paramMetadata.getValue()) |
| 116 | + .ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType())); |
| 117 | + } |
| 118 | + case NEGATING_SIMPLE_PROPERTY: { |
| 119 | + ParameterMetadata paramMetadata = parameterMetadataProvider.next(part); |
| 120 | + return criteriaStep.not(paramMetadata.getValue()) |
| 121 | + .ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType())); |
| 122 | + } |
| 123 | + default: |
| 124 | + throw new IllegalArgumentException("Unsupported keyword " + type); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Checks whether comparison should be done in case-insensitive way. |
| 130 | + * |
| 131 | + * @param part method name part (must not be {@literal null}) |
| 132 | + * @return {@literal true} if comparison should be done in case-insensitive way |
| 133 | + */ |
| 134 | + private boolean shouldIgnoreCase(Part part) { |
| 135 | + return part.shouldIgnoreCase() == Part.IgnoreCaseType.ALWAYS |
| 136 | + || part.shouldIgnoreCase() == Part.IgnoreCaseType.WHEN_POSSIBLE; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Checks whether "upper-case" conversion can be applied to the given {@link Expression}s in case the underlying |
| 141 | + * {@link Part} requires ignoring case. |
| 142 | + * |
| 143 | + * @param part method name part (must not be {@literal null}) |
| 144 | + * @param expressionTypes types of the given expressions (must not be {@literal null} or empty) |
| 145 | + * @throws IllegalStateException if {@link Part} requires ignoring case but "upper-case" conversion cannot be applied |
| 146 | + * to at least one of the given {@link Expression}s |
| 147 | + */ |
| 148 | + private boolean checkCanUpperCase(Part part, Class<?>... expressionTypes) { |
| 149 | + Assert.notEmpty(expressionTypes, "Expression types must not be null or empty"); |
| 150 | + boolean strict = part.shouldIgnoreCase() == Part.IgnoreCaseType.ALWAYS; |
| 151 | + for (Class<?> expressionType : expressionTypes) { |
| 152 | + if (!canUpperCase(expressionType)) { |
| 153 | + if (strict) { |
| 154 | + throw new IllegalStateException("Unable to ignore case of " + expressionType.getName() |
| 155 | + + " type, the property '" + part.getProperty().getSegment() + "' must reference a string"); |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + private boolean canUpperCase(Class<?> expressionType) { |
| 164 | + return expressionType == String.class; |
| 165 | + } |
| 166 | +} |
0 commit comments