diff --git a/pom.xml b/pom.xml index 2c64717780..1539b78bae 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-relational-parent - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT pom Spring Data Relational Parent diff --git a/spring-data-jdbc-distribution/pom.xml b/spring-data-jdbc-distribution/pom.xml index 0646c2846d..c55d9ccad0 100644 --- a/spring-data-jdbc-distribution/pom.xml +++ b/spring-data-jdbc-distribution/pom.xml @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT ../pom.xml diff --git a/spring-data-jdbc/pom.xml b/spring-data-jdbc/pom.xml index 11114a795e..f76dc28f46 100644 --- a/spring-data-jdbc/pom.xml +++ b/spring-data-jdbc/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-jdbc - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. @@ -15,7 +15,7 @@ org.springframework.data spring-data-relational-parent - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java index 80fa379a49..a95c9b1933 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 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. @@ -51,6 +51,7 @@ * @author Milan Milanov * @author Myeonghyeon Lee * @author Mikhail Polivakha + * @author Chirag Tailor */ class SqlGenerator { @@ -714,7 +715,7 @@ private OrderByField orderToOrderByField(Sort.Order order) { SqlIdentifier columnName = this.entity.getRequiredPersistentProperty(order.getProperty()).getColumnName(); Column column = Column.create(columnName, this.getTable()); - return OrderByField.from(column, order.getDirection()); + return OrderByField.from(column, order.getDirection()).withNullHandling(order.getNullHandling()); } /** diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java index 4153ac6b3b..fa79aec3bd 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java @@ -257,6 +257,21 @@ void saveAndLoadManyEntitiesWithReferencedEntitySortedAndPaged() { .containsExactly("Star"); } + @Test // GH-821 + @EnabledOnFeature({SUPPORTS_QUOTED_IDS, SUPPORTS_NULL_HANDLING}) + void saveAndLoadManyEntitiesWithReferencedEntitySortedWithNullHandling() { + + template.save(createLegoSet(null)); + template.save(createLegoSet("Star")); + template.save(createLegoSet("Frozen")); + + Iterable reloadedLegoSets = template.findAll(LegoSet.class, Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST))); + + assertThat(reloadedLegoSets) // + .extracting("name") // + .containsExactly("Frozen", "Star", null); + } + @Test // DATAJDBC-112 @EnabledOnFeature(SUPPORTS_QUOTED_IDS) void saveAndLoadManyEntitiesByIdWithReferencedEntity() { diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java index 363d9c9d1d..7ea24dfebe 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 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. @@ -25,7 +25,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.springframework.data.annotation.Id; import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Version; @@ -64,6 +63,7 @@ * @author Milan Milanov * @author Myeonghyeon Lee * @author Mikhail Polivakha + * @author Chirag Tailor */ class SqlGeneratorUnitTests { @@ -245,6 +245,26 @@ void findAllSortedByMultipleFields() { "x_other ASC"); } + @Test // GH-821 + void findAllSortedWithNullHandling_resolvesNullHandlingWhenDialectSupportsIt() { + + SqlGenerator sqlGenerator = createSqlGenerator(DummyEntity.class, PostgresDialect.INSTANCE); + + String sql = sqlGenerator.getFindAll(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST))); + + assertThat(sql).contains("ORDER BY \"dummy_entity\".\"x_name\" ASC NULLS LAST"); + } + + @Test // GH-821 + void findAllSortedWithNullHandling_ignoresNullHandlingWhenDialectDoesNotSupportIt() { + + SqlGenerator sqlGenerator = createSqlGenerator(DummyEntity.class, SqlServerDialect.INSTANCE); + + String sql = sqlGenerator.getFindAll(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST))); + + assertThat(sql).endsWith("ORDER BY dummy_entity.x_name ASC"); + } + @Test // DATAJDBC-101 void findAllPagedByUnpaged() { diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java index 6e3e65a484..d252745034 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 the original author or authors. + * Copyright 2020-2022 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. @@ -29,6 +29,7 @@ * presence or absence of features in tests. * * @author Jens Schauder + * @author Chirag Tailor */ public class TestDatabaseFeatures { @@ -83,6 +84,10 @@ private void supportsMultiDimensionalArrays() { assumeThat(database).isNotIn(Database.H2, Database.Hsql); } + private void supportsNullHandling() { + assumeThat(database).isNotIn(Database.MySql, Database.MariaDb, Database.SqlServer); + } + public void databaseIs(Database database) { assumeThat(this.database).isEqualTo(database); } @@ -115,6 +120,7 @@ public enum Feature { SUPPORTS_ARRAYS(TestDatabaseFeatures::supportsArrays), // SUPPORTS_GENERATED_IDS_IN_REFERENCED_ENTITIES(TestDatabaseFeatures::supportsGeneratedIdsInReferencedEntities), // SUPPORTS_NANOSECOND_PRECISION(TestDatabaseFeatures::supportsNanosecondPrecision), // + SUPPORTS_NULL_HANDLING(TestDatabaseFeatures::supportsNullHandling), IS_POSTGRES(f -> f.databaseIs(Database.PostgreSql)), // IS_HSQL(f -> f.databaseIs(Database.Hsql)); diff --git a/spring-data-relational/pom.xml b/spring-data-relational/pom.xml index a6eb48c891..f862f08876 100644 --- a/spring-data-relational/pom.xml +++ b/spring-data-relational/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-relational - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT Spring Data Relational Spring Data Relational support @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 2.4.0-SNAPSHOT + 2.4.0-821-support-sort-null-handling-SNAPSHOT diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java index 6563df66a3..7043a9f734 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -18,6 +18,7 @@ import java.util.OptionalLong; import java.util.function.Function; +import org.springframework.data.domain.Sort; import org.springframework.data.relational.core.sql.LockMode; import org.springframework.data.relational.core.sql.LockOptions; import org.springframework.data.relational.core.sql.Select; @@ -28,6 +29,7 @@ * * @author Mark Paluch * @author Myeonghyeon Lee + * @author Chirag Tailor * @since 1.1 */ public abstract class AbstractDialect implements Dialect { @@ -42,7 +44,7 @@ public SelectRenderContext getSelectContext() { Function afterFromTable = getAfterFromTable(); Function afterOrderBy = getAfterOrderBy(); - return new DialectSelectRenderContext(afterFromTable, afterOrderBy); + return new DialectSelectRenderContext(afterFromTable, afterOrderBy, orderByNullHandling()); } /** @@ -105,12 +107,14 @@ static class DialectSelectRenderContext implements SelectRenderContext { private final Function afterFromTable; private final Function afterOrderBy; + private final OrderByNullHandling orderByNullHandling; DialectSelectRenderContext(Function afterFromTable, - Function afterOrderBy) { + Function afterOrderBy, OrderByNullHandling orderByNullHandling) { this.afterFromTable = afterFromTable; this.afterOrderBy = afterOrderBy; + this.orderByNullHandling = orderByNullHandling; } /* @@ -130,6 +134,11 @@ static class DialectSelectRenderContext implements SelectRenderContext { public Function afterOrderBy(boolean hasOrderBy) { return afterOrderBy; } + + @Override + public String evaluateOrderByNullHandling(Sort.NullHandling nullHandling) { + return orderByNullHandling.evaluate(nullHandling); + } } /** diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Db2Dialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Db2Dialect.java index 82f527a148..f3860c1fe6 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Db2Dialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Db2Dialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 the original author or authors. + * Copyright 2020-2022 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. @@ -25,6 +25,7 @@ * An SQL dialect for DB2. * * @author Jens Schauder + * @author Chirag Tailor * @since 2.0 */ public class Db2Dialect extends AbstractDialect { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Dialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Dialect.java index 5febb8c52f..eee10f0b6c 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Dialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Dialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -33,6 +33,7 @@ * @author Myeonghyeon Lee * @author Christoph Strobl * @author Mikhail Polivakha + * @author Chirag Tailor * @since 1.1 */ public interface Dialect { @@ -120,4 +121,13 @@ default Set> simpleTypes() { default InsertRenderContext getInsertRenderContext() { return InsertRenderContexts.DEFAULT; } + + /** + * Return the {@link OrderByNullHandling} used by this dialect. + * + * @return the {@link OrderByNullHandling} used by this dialect. + */ + default OrderByNullHandling orderByNullHandling() { + return OrderByNullHandling.SQL_STANDARD; + } } diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/H2Dialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/H2Dialect.java index 7444edde27..ee6d370785 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/H2Dialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/H2Dialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -30,6 +30,7 @@ * @author Mark Paluch * @author Myeonghyeon Lee * @author Christph Strobl + * @author Chirag Tailor * @since 2.0 */ public class H2Dialect extends AbstractDialect { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/HsqlDbDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/HsqlDbDialect.java index cf534de202..96016eb283 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/HsqlDbDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/HsqlDbDialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -20,6 +20,7 @@ * * @author Jens Schauder * @author Myeonghyeon Lee + * @author Chirag Tailor */ public class HsqlDbDialect extends AbstractDialect { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/MySqlDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/MySqlDialect.java index 6032582186..a1a9919293 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/MySqlDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/MySqlDialect.java @@ -169,4 +169,9 @@ public IdentifierProcessing getIdentifierProcessing() { public Collection getConverters() { return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE); } + + @Override + public OrderByNullHandling orderByNullHandling() { + return OrderByNullHandling.NONE; + } } diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OracleDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OracleDialect.java index 90f7d466e7..40fc7142ff 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OracleDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OracleDialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -20,7 +20,6 @@ import org.springframework.data.convert.WritingConverter; import java.util.Collection; -import java.util.Collections; import static java.util.Arrays.*; @@ -28,6 +27,7 @@ * An SQL dialect for Oracle. * * @author Jens Schauder + * @author Chirag Tailor * @since 2.1 */ public class OracleDialect extends AnsiDialect { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OrderByNullHandling.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OrderByNullHandling.java new file mode 100644 index 0000000000..4dcadc8eff --- /dev/null +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OrderByNullHandling.java @@ -0,0 +1,71 @@ +/* + * Copyright 2022 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.springframework.data.relational.core.dialect; + +import org.springframework.data.domain.Sort; + +/** + * Represents how the {@link Sort.NullHandling} option of an {@code ORDER BY} sort expression is to be evaluated. + * + * @author Chirag Tailor + */ +public interface OrderByNullHandling { + /** + * An {@link OrderByNullHandling} that can be used for databases conforming to the SQL standard which uses + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to make null values appear before + * or after non-null values in the result set. + */ + OrderByNullHandling SQL_STANDARD = new SqlStandardOrderByNullHandling(); + + /** + * An {@link OrderByNullHandling} that can be used for databases that do not support the SQL standard usage of + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to control where null values appear + * respective to non-null values in the result set. + */ + OrderByNullHandling NONE = nullHandling -> ""; + + /** + * Converts a {@link Sort.NullHandling} option to the appropriate SQL text to be included an {@code ORDER BY} sort + * expression. + */ + String evaluate(Sort.NullHandling nullHandling); + + /** + * An {@link OrderByNullHandling} implementation for databases conforming to the SQL standard which uses + * {@code NULLS FIRST} and {@code NULLS LAST} in {@code ORDER BY} sort expressions to make null values appear before + * or after non-null values in the result set. + * + * @author Chirag Tailor + */ + class SqlStandardOrderByNullHandling implements OrderByNullHandling { + + private static final String NULLS_FIRST = "NULLS FIRST"; + private static final String NULLS_LAST = "NULLS LAST"; + private static final String UNSPECIFIED = ""; + + @Override + public String evaluate(Sort.NullHandling nullHandling) { + + switch (nullHandling) { + case NULLS_FIRST: return NULLS_FIRST; + case NULLS_LAST: return NULLS_LAST; + case NATIVE: return UNSPECIFIED; + default: + throw new UnsupportedOperationException("Sort.NullHandling " + nullHandling + " not supported"); + } + } + } +} diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/PostgresDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/PostgresDialect.java index 8782bbf3da..815fe53aeb 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/PostgresDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/PostgresDialect.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -39,6 +39,7 @@ * @author Myeonghyeon Lee * @author Jens Schauder * @author Nikita Konev + * @author Chirag Tailor * @since 1.1 */ public class PostgresDialect extends AbstractDialect { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/SqlServerDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/SqlServerDialect.java index 11ae0d3cf3..7f393e05e1 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/SqlServerDialect.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/SqlServerDialect.java @@ -156,4 +156,9 @@ public IdentifierProcessing getIdentifierProcessing() { public InsertRenderContext getInsertRenderContext() { return InsertRenderContexts.MS_SQL_SERVER; } + + @Override + public OrderByNullHandling orderByNullHandling() { + return OrderByNullHandling.NONE; + } } diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitor.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitor.java index a918231dd2..949a234a60 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitor.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -24,6 +24,7 @@ * * @author Mark Paluch * @author Jens Schauder + * @author Chirag Tailor * @since 1.1 */ class OrderByClauseVisitor extends TypedSubtreeVisitor implements PartRenderer { @@ -59,11 +60,15 @@ Delegation enterMatched(OrderByField segment) { @Override Delegation leaveMatched(OrderByField segment) { - OrderByField field = segment; + if (segment.getDirection() != null) { + builder.append(" ") // + .append(segment.getDirection()); + } - if (field.getDirection() != null) { + String nullHandling = context.getSelectRenderContext().evaluateOrderByNullHandling(segment.getNullHandling()); + if (!nullHandling.isEmpty()) { builder.append(" ") // - .append(field.getDirection()); + .append(nullHandling); } return Delegation.leave(); diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/SelectRenderContext.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/SelectRenderContext.java index 74592f3915..12bbaa0d5e 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/SelectRenderContext.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/SelectRenderContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -18,17 +18,20 @@ import java.util.OptionalLong; import java.util.function.Function; +import org.springframework.data.domain.Sort; +import org.springframework.data.relational.core.dialect.OrderByNullHandling; import org.springframework.data.relational.core.sql.LockMode; import org.springframework.data.relational.core.sql.Select; /** * Render context specifically for {@code SELECT} statements. This interface declares rendering hooks that are called - * before/after a specific {@code SELECT} clause part. The rendering content is appended directly after/before an + * before/after/during a specific {@code SELECT} clause part. The rendering content is appended directly after/before an * element without further whitespace processing. Hooks are responsible for adding required surrounding whitespaces. * * @author Mark Paluch * @author Myeonghyeon Lee * @author Jens Schauder + * @author Chirag Tailor * @since 1.1 */ public interface SelectRenderContext { @@ -86,4 +89,14 @@ public interface SelectRenderContext { return lockPrefix; }; } + + /** + * Customization hook: Rendition of the null handling option for an {@code ORDER BY} sort expression. + * + * @param nullHandling the {@link Sort.NullHandling} for the {@code ORDER BY} sort expression. Must not be {@literal null}. + * @return render {@link String} SQL text to be included in an {@code ORDER BY} sort expression. + */ + default String evaluateOrderByNullHandling(Sort.NullHandling nullHandling) { + return OrderByNullHandling.NONE.evaluate(nullHandling); + } } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/PostgresDialectRenderingUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/PostgresDialectRenderingUnitTests.java index c2aca005e3..ddf09cdcfc 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/PostgresDialectRenderingUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/PostgresDialectRenderingUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -20,7 +20,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Sort; +import org.springframework.data.relational.core.sql.Column; import org.springframework.data.relational.core.sql.LockMode; +import org.springframework.data.relational.core.sql.OrderByField; import org.springframework.data.relational.core.sql.Select; import org.springframework.data.relational.core.sql.StatementBuilder; import org.springframework.data.relational.core.sql.Table; @@ -33,6 +36,7 @@ * @author Mark Paluch * @author Jens Schauder * @author Myeonghyeon Lee + * @author Chirag Tailor */ public class PostgresDialectRenderingUnitTests { @@ -147,4 +151,62 @@ public void shouldRenderSelectWithLimitWithLockRead() { assertThat(sql).isEqualTo("SELECT foo.* FROM foo LIMIT 10 FOR SHARE OF foo"); } + + @Test // GH-821 + void shouldRenderSelectOrderByWithNoOptions() { + + Table table = Table.create("foo"); + Select select = StatementBuilder.select(table.asterisk()) + .from(table) + .orderBy(OrderByField.from(Column.create("bar", table))) + .build(); + + String sql = SqlRenderer.create(factory.createRenderContext()).render(select); + + assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar"); + } + + @Test // GH-821 + void shouldRenderSelectOrderByWithDirection() { + + Table table = Table.create("foo"); + Select select = StatementBuilder.select(table.asterisk()) + .from(table) + .orderBy(OrderByField.from(Column.create("bar", table), Sort.Direction.ASC)) + .build(); + + String sql = SqlRenderer.create(factory.createRenderContext()).render(select); + + assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar ASC"); + } + + @Test // GH-821 + void shouldRenderSelectOrderByWithNullHandling() { + + Table table = Table.create("foo"); + Select select = StatementBuilder.select(table.asterisk()) + .from(table) + .orderBy(OrderByField.from(Column.create("bar", table)) + .withNullHandling(Sort.NullHandling.NULLS_FIRST)) + .build(); + + String sql = SqlRenderer.create(factory.createRenderContext()).render(select); + + assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar NULLS FIRST"); + } + + @Test // GH-821 + void shouldRenderSelectOrderByWithDirectionAndNullHandling() { + + Table table = Table.create("foo"); + Select select = StatementBuilder.select(table.asterisk()) + .from(table) + .orderBy(OrderByField.from(Column.create("bar", table), Sort.Direction.DESC) + .withNullHandling(Sort.NullHandling.NULLS_FIRST)) + .build(); + + String sql = SqlRenderer.create(factory.createRenderContext()).render(select); + + assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar DESC NULLS FIRST"); + } } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/SqlServerDialectRenderingUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/SqlServerDialectRenderingUnitTests.java index 2c2f865c09..403faa241f 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/SqlServerDialectRenderingUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/dialect/SqlServerDialectRenderingUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -20,7 +20,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Sort; +import org.springframework.data.relational.core.sql.Column; import org.springframework.data.relational.core.sql.LockMode; +import org.springframework.data.relational.core.sql.OrderByField; import org.springframework.data.relational.core.sql.Select; import org.springframework.data.relational.core.sql.StatementBuilder; import org.springframework.data.relational.core.sql.Table; @@ -33,6 +36,7 @@ * @author Mark Paluch * @author Jens Schauder * @author Myeonghyeon Lee + * @author Chirag Tailor */ public class SqlServerDialectRenderingUnitTests { @@ -192,4 +196,19 @@ public void shouldRenderSelectWithLimitOffsetAndOrderByWithLockRead() { assertThat(sql).isEqualTo("SELECT foo.* FROM foo WITH (HOLDLOCK, ROWLOCK) ORDER BY foo.column_1 OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY"); } + + @Test // GH-821 + void shouldRenderSelectOrderByIgnoringNullHandling() { + + Table table = Table.create("foo"); + Select select = StatementBuilder.select(table.asterisk()) + .from(table) + .orderBy(OrderByField.from(Column.create("bar", table)) + .withNullHandling(Sort.NullHandling.NULLS_FIRST)) + .build(); + + String sql = SqlRenderer.create(factory.createRenderContext()).render(select); + + assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar"); + } }