From 66466e875670a503917c5754a16e602db09c7d94 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 20 Nov 2024 15:08:11 +0100 Subject: [PATCH] Fixed rendering of NOT condition. Closes #1945 --- .../core/sql/render/NotConditionVisitor.java | 5 ++--- .../sql/render/SelectRendererUnitTests.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NotConditionVisitor.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NotConditionVisitor.java index dc4e7163d08..77627ffad24 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NotConditionVisitor.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NotConditionVisitor.java @@ -16,7 +16,6 @@ package org.springframework.data.relational.core.sql.render; import org.springframework.data.relational.core.sql.Condition; -import org.springframework.data.relational.core.sql.NestedCondition; import org.springframework.data.relational.core.sql.Not; import org.springframework.data.relational.core.sql.Visitable; import org.springframework.lang.Nullable; @@ -27,7 +26,7 @@ * @author Jens Schauder * @since 3.1.6 */ -class NotConditionVisitor extends TypedSubtreeVisitor { +class NotConditionVisitor extends TypedSubtreeVisitor { private final RenderContext context; private final RenderTarget target; @@ -63,7 +62,7 @@ Delegation leaveNested(Visitable segment) { if (conditionVisitor != null) { - target.onRendered("NOT (" + conditionVisitor.getRenderedPart() + ")"); + target.onRendered("NOT " + conditionVisitor.getRenderedPart()); conditionVisitor = null; } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java index 4bc4d0c7979..2ec941640ab 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java @@ -678,6 +678,26 @@ void notOfNested() { assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)"); } + @Test // GH-1945 + void notOfTrue() { + + Select selectFalse = Select.builder().select(Expressions.just("*")).from("test_table") + .where(Conditions.just("true").not()).build(); + String renderSelectFalse = SqlRenderer.create().render(selectFalse); + + assertThat(renderSelectFalse).isEqualTo("SELECT * FROM test_table WHERE NOT true"); + } + + @Test // GH-1945 + void notOfNestedTrue() { + + Select selectFalseNested = Select.builder().select(Expressions.just("*")).from("test_table") + .where(Conditions.nest(Conditions.just("true")).not()).build(); + String renderSelectFalseNested = SqlRenderer.create().render(selectFalseNested); + + assertThat(renderSelectFalseNested).isEqualTo("SELECT * FROM test_table WHERE NOT (true)"); + } + @Test // GH-1651 void asteriskOfAliasedTableUsesAlias() {