|
| 1 | +/* |
| 2 | + * Copyright 2022 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.core.dialect; |
| 17 | + |
| 18 | +import org.springframework.data.domain.Sort; |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents how the {@link Sort.NullHandling} option of an {@code ORDER BY} sort expression is to be evaluated. |
| 22 | + * |
| 23 | + * @author Chirag Tailor |
| 24 | + */ |
| 25 | +public interface OrderByNullHandling { |
| 26 | + /** |
| 27 | + * An {@link OrderByNullHandling} that can be used for databases conforming to the SQL standard which uses |
| 28 | + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to make null values appear before |
| 29 | + * or after non-null values in the result set. |
| 30 | + */ |
| 31 | + OrderByNullHandling SQL_STANDARD = new SqlStandardOrderByNullHandling(); |
| 32 | + |
| 33 | + /** |
| 34 | + * An {@link OrderByNullHandling} that can be used for databases that do not support the SQL standard usage of |
| 35 | + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to control where null values appear |
| 36 | + * respective to non-null values in the result set. |
| 37 | + */ |
| 38 | + OrderByNullHandling NONE = nullHandling -> ""; |
| 39 | + |
| 40 | + /** |
| 41 | + * Converts a {@link Sort.NullHandling} option to the appropriate SQL text to be included an {@code ORDER BY} sort |
| 42 | + * expression. |
| 43 | + */ |
| 44 | + String evaluate(Sort.NullHandling nullHandling); |
| 45 | + |
| 46 | + /** |
| 47 | + * An {@link OrderByNullHandling} implementation for databases conforming to the SQL standard which uses |
| 48 | + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to make null values appear before |
| 49 | + * or after non-null values in the result set. |
| 50 | + * |
| 51 | + * @author Chirag Tailor |
| 52 | + */ |
| 53 | + class SqlStandardOrderByNullHandling implements OrderByNullHandling { |
| 54 | + |
| 55 | + private static final String NULLS_FIRST = "NULLS FIRST"; |
| 56 | + private static final String NULLS_LAST = "NULLS LAST"; |
| 57 | + private static final String UNSPECIFIED = ""; |
| 58 | + |
| 59 | + @Override |
| 60 | + public String evaluate(Sort.NullHandling nullHandling) { |
| 61 | + |
| 62 | + switch (nullHandling) { |
| 63 | + case NULLS_FIRST: return NULLS_FIRST; |
| 64 | + case NULLS_LAST: return NULLS_LAST; |
| 65 | + case NATIVE: return UNSPECIFIED; |
| 66 | + default: |
| 67 | + throw new UnsupportedOperationException("Sort.NullHandling " + nullHandling + " not supported"); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments