|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + * http://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 lombok.RequiredArgsConstructor; |
| 19 | + |
| 20 | +import java.util.OptionalLong; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import org.springframework.data.relational.core.sql.Select; |
| 24 | +import org.springframework.data.relational.core.sql.render.SelectRenderContext; |
| 25 | + |
| 26 | +/** |
| 27 | + * Base class for {@link Dialect} implementations. |
| 28 | + * |
| 29 | + * @author Mark Paluch |
| 30 | + * @since 1.1 |
| 31 | + */ |
| 32 | +public abstract class AbstractDialect implements Dialect { |
| 33 | + |
| 34 | + /* |
| 35 | + * (non-Javadoc) |
| 36 | + * @see org.springframework.data.relational.core.dialect.Dialect#getSelectContext() |
| 37 | + */ |
| 38 | + @Override |
| 39 | + public SelectRenderContext getSelectContext() { |
| 40 | + |
| 41 | + Function<Select, ? extends CharSequence> afterOrderBy = getAfterOrderBy(); |
| 42 | + |
| 43 | + return new DialectSelectRenderContext(afterOrderBy); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns a {@link Function afterOrderBy Function}. Typically used for pagination. |
| 48 | + * |
| 49 | + * @return the {@link Function} called on {@code afterOrderBy}. |
| 50 | + */ |
| 51 | + protected Function<Select, CharSequence> getAfterOrderBy() { |
| 52 | + |
| 53 | + Function<Select, ? extends CharSequence> afterOrderBy; |
| 54 | + |
| 55 | + LimitClause limit = limit(); |
| 56 | + |
| 57 | + switch (limit.getClausePosition()) { |
| 58 | + |
| 59 | + case AFTER_ORDER_BY: |
| 60 | + afterOrderBy = new AfterOrderByLimitRenderFunction(limit); |
| 61 | + break; |
| 62 | + |
| 63 | + default: |
| 64 | + throw new UnsupportedOperationException(String.format("Clause position %s not supported!", limit)); |
| 65 | + } |
| 66 | + |
| 67 | + return afterOrderBy.andThen(PrependWithLeadingWhitespace.INSTANCE); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@link SelectRenderContext} derived from {@link Dialect} specifics. |
| 72 | + */ |
| 73 | + class DialectSelectRenderContext implements SelectRenderContext { |
| 74 | + |
| 75 | + private final Function<Select, ? extends CharSequence> afterOrderBy; |
| 76 | + |
| 77 | + DialectSelectRenderContext(Function<Select, ? extends CharSequence> afterOrderBy) { |
| 78 | + this.afterOrderBy = afterOrderBy; |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + * (non-Javadoc) |
| 83 | + * @see org.springframework.data.relational.core.sql.render.SelectRenderContext#afterOrderBy(boolean) |
| 84 | + */ |
| 85 | + @Override |
| 86 | + public Function<Select, ? extends CharSequence> afterOrderBy(boolean hasOrderBy) { |
| 87 | + return afterOrderBy; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * After {@code ORDER BY} function rendering the {@link LimitClause}. |
| 93 | + */ |
| 94 | + @RequiredArgsConstructor |
| 95 | + static class AfterOrderByLimitRenderFunction implements Function<Select, CharSequence> { |
| 96 | + |
| 97 | + private final LimitClause clause; |
| 98 | + |
| 99 | + /* |
| 100 | + * (non-Javadoc) |
| 101 | + * @see java.util.function.Function#apply(java.lang.Object) |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public CharSequence apply(Select select) { |
| 105 | + |
| 106 | + OptionalLong limit = select.getLimit(); |
| 107 | + OptionalLong offset = select.getOffset(); |
| 108 | + |
| 109 | + if (limit.isPresent() && offset.isPresent()) { |
| 110 | + return clause.getLimitOffset(limit.getAsLong(), offset.getAsLong()); |
| 111 | + } |
| 112 | + |
| 113 | + if (limit.isPresent()) { |
| 114 | + return clause.getLimit(limit.getAsLong()); |
| 115 | + } |
| 116 | + |
| 117 | + if (offset.isPresent()) { |
| 118 | + return clause.getOffset(offset.getAsLong()); |
| 119 | + } |
| 120 | + |
| 121 | + return ""; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Prepends a non-empty rendering result with a leading whitespace, |
| 127 | + */ |
| 128 | + @RequiredArgsConstructor |
| 129 | + enum PrependWithLeadingWhitespace implements Function<CharSequence, CharSequence> { |
| 130 | + |
| 131 | + INSTANCE; |
| 132 | + |
| 133 | + /* |
| 134 | + * (non-Javadoc) |
| 135 | + * @see java.util.function.Function#apply(java.lang.Object) |
| 136 | + */ |
| 137 | + @Override |
| 138 | + public CharSequence apply(CharSequence charSequence) { |
| 139 | + |
| 140 | + if (charSequence.length() == 0) { |
| 141 | + return charSequence; |
| 142 | + } |
| 143 | + |
| 144 | + return " " + charSequence; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments