diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f3da4db9..1ef040d6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ worked to make these changes as minimal as possible. **Potentially Breaking Changes:** +- If you have created any custom implementations of `SortSpecification`, you will need to update those + implementations due to a new rendering strategy for ORDER BY phrases. The old methods `isDescending` and `orderByName` + are removed in favor of a new method `renderForOrderBy` - If you have implemented any custom functions, you will likely need to make changes. The supplied base classes now hold an instance of `BasicColumn` rather than `BindableColumn`. This change was made to make the functions more useful in variety of circumstances. If you follow the patterns shown on the diff --git a/src/main/java/org/mybatis/dynamic/sql/SortSpecification.java b/src/main/java/org/mybatis/dynamic/sql/SortSpecification.java index 48aee58c4..0fd0da18f 100644 --- a/src/main/java/org/mybatis/dynamic/sql/SortSpecification.java +++ b/src/main/java/org/mybatis/dynamic/sql/SortSpecification.java @@ -15,10 +15,8 @@ */ package org.mybatis.dynamic.sql; -import org.mybatis.dynamic.sql.exception.DynamicSqlException; import org.mybatis.dynamic.sql.render.RenderingContext; import org.mybatis.dynamic.sql.util.FragmentAndParameters; -import org.mybatis.dynamic.sql.util.Messages; /** * Defines attributes of columns that are necessary for rendering an order by expression. @@ -35,37 +33,12 @@ public interface SortSpecification { SortSpecification descending(); /** - * Return the phrase that should be written into a rendered order by clause. This should - * NOT include the "DESC" word for descending sort specifications. + * Return a fragment rendered for use in an ORDER BY clause. The fragment should include "DESC" if a + * descending order is desired. * - * @return the order by phrase - * @deprecated Please replace this method by overriding the more general "renderForOrderBy" method. Target for - * removal in release 2.1 + * @param renderingContext the current rendering context + * @return a rendered fragment and parameters if applicable + * @since 2.0.0 */ - @Deprecated(since = "2.0", forRemoval = true) - default String orderByName() { - throw new DynamicSqlException(Messages.getString("ERROR.44")); //$NON-NLS-1$ - } - - /** - * Return true if the sort order is descending. - * - * @return true if the SortSpecification should render as descending - * @deprecated Please replace this method by overriding the more general "renderForOrderBy" method. Target for - * removal in release 2.1 - */ - @Deprecated(since = "2.0", forRemoval = true) - default boolean isDescending() { - throw new DynamicSqlException(Messages.getString("ERROR.44")); //$NON-NLS-1$ - } - - // the default implementation ensures compatibility with prior releases. When the - // deprecated methods are removed, this function can become purely abstract. - default FragmentAndParameters renderForOrderBy(RenderingContext renderingContext) { - String phrase = orderByName(); - if (isDescending()) { - phrase = phrase + " DESC"; //$NON-NLS-1$ - } - return FragmentAndParameters.fromFragment(phrase); - } + FragmentAndParameters renderForOrderBy(RenderingContext renderingContext); } diff --git a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties index c7e90f7e5..c25eb02c5 100644 --- a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties +++ b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties @@ -60,6 +60,4 @@ ERROR.40=Case expressions must have at least one "when" clause ERROR.41=You cannot call "then" in a Kotlin case expression more than once ERROR.42=You cannot call `else` in a Kotlin case expression more than once ERROR.43=A Kotlin cast expression must have one, and only one, `as` element -ERROR.44=You must either implement the "renderForOrderBy" method or both "orderByName" and "isDescending" methods in a \ - sort specification INTERNAL.ERROR=Internal Error {0} diff --git a/src/test/java/org/mybatis/dynamic/sql/DeprecatedSortMethodsTest.java b/src/test/java/org/mybatis/dynamic/sql/DeprecatedSortMethodsTest.java deleted file mode 100644 index 520783155..000000000 --- a/src/test/java/org/mybatis/dynamic/sql/DeprecatedSortMethodsTest.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2016-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mybatis.dynamic.sql; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; - -import java.util.List; - -import org.junit.jupiter.api.Test; -import org.mybatis.dynamic.sql.common.OrderByModel; -import org.mybatis.dynamic.sql.common.OrderByRenderer; -import org.mybatis.dynamic.sql.configuration.StatementConfiguration; -import org.mybatis.dynamic.sql.exception.DynamicSqlException; -import org.mybatis.dynamic.sql.render.RenderingContext; -import org.mybatis.dynamic.sql.render.RenderingStrategies; -import org.mybatis.dynamic.sql.render.TableAliasCalculator; -import org.mybatis.dynamic.sql.util.FragmentAndParameters; -import org.mybatis.dynamic.sql.util.Messages; - -class DeprecatedSortMethodsTest { - - @Test - void bothMethodsExist() { - SortSpecification ss = new SortSpecification() { - @Override - public SortSpecification descending() { - return this; - } - - @Override - public String orderByName() { - return "id"; - } - - @Override - public boolean isDescending() { - return false; - } - }; - - OrderByModel model = OrderByModel.of(List.of(ss)); - - RenderingContext renderingContext = RenderingContext - .withRenderingStrategy(RenderingStrategies.MYBATIS3) - .withTableAliasCalculator(TableAliasCalculator.empty()) - .withStatementConfiguration(new StatementConfiguration()) - .build(); - OrderByRenderer renderer = new OrderByRenderer(renderingContext); - FragmentAndParameters fp = renderer.render(model); - assertThat(fp.fragment()).isEqualTo("order by id"); - } - - @Test - void bothMethodsExistDescending() { - SortSpecification ss = new SortSpecification() { - @Override - public SortSpecification descending() { - return this; - } - - @Override - public String orderByName() { - return "id"; - } - - @Override - public boolean isDescending() { - return true; - } - }; - - OrderByModel model = OrderByModel.of(List.of(ss)); - - RenderingContext renderingContext = RenderingContext - .withRenderingStrategy(RenderingStrategies.MYBATIS3) - .withTableAliasCalculator(TableAliasCalculator.empty()) - .withStatementConfiguration(new StatementConfiguration()) - .build(); - OrderByRenderer renderer = new OrderByRenderer(renderingContext); - FragmentAndParameters fp = renderer.render(model); - assertThat(fp.fragment()).isEqualTo("order by id DESC"); - } - - @Test - void orderByNameMethodMissing() { - SortSpecification ss = new SortSpecification() { - @Override - public SortSpecification descending() { - return this; - } - - @Override - public boolean isDescending() { - return true; - } - }; - - OrderByModel model = OrderByModel.of(List.of(ss)); - - RenderingContext renderingContext = RenderingContext - .withRenderingStrategy(RenderingStrategies.MYBATIS3) - .withTableAliasCalculator(TableAliasCalculator.empty()) - .withStatementConfiguration(new StatementConfiguration()) - .build(); - OrderByRenderer renderer = new OrderByRenderer(renderingContext); - assertThatExceptionOfType(DynamicSqlException.class) - .isThrownBy(() -> renderer.render(model)) - .withMessage(Messages.getString("ERROR.44")); - } - - @Test - void isDescendingMethodMissing() { - SortSpecification ss = new SortSpecification() { - @Override - public SortSpecification descending() { - return this; - } - - @Override - public String orderByName() { - return "id"; - } - }; - - OrderByModel model = OrderByModel.of(List.of(ss)); - - RenderingContext renderingContext = RenderingContext - .withRenderingStrategy(RenderingStrategies.MYBATIS3) - .withTableAliasCalculator(TableAliasCalculator.empty()) - .withStatementConfiguration(new StatementConfiguration()) - .build(); - OrderByRenderer renderer = new OrderByRenderer(renderingContext); - assertThatExceptionOfType(DynamicSqlException.class) - .isThrownBy(() -> renderer.render(model)) - .withMessage(Messages.getString("ERROR.44")); - } - - @Test - void bothMethodsMissing() { - SortSpecification ss = new SortSpecification() { - @Override - public SortSpecification descending() { - return this; - } - }; - - OrderByModel model = OrderByModel.of(List.of(ss)); - - RenderingContext renderingContext = RenderingContext - .withRenderingStrategy(RenderingStrategies.MYBATIS3) - .withTableAliasCalculator(TableAliasCalculator.empty()) - .withStatementConfiguration(new StatementConfiguration()) - .build(); - OrderByRenderer renderer = new OrderByRenderer(renderingContext); - assertThatExceptionOfType(DynamicSqlException.class) - .isThrownBy(() -> renderer.render(model)) - .withMessage(Messages.getString("ERROR.44")); - } -}