Skip to content

Commit 97b4154

Browse files
schaudermp911de
authored andcommitted
Properly render NOT condition.
Closes #1653 Original pull request: #1659
1 parent 0035312 commit 97b4154

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ConditionVisitor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@
1515
*/
1616
package org.springframework.data.relational.core.sql.render;
1717

18-
import org.springframework.data.relational.core.sql.AndCondition;
19-
import org.springframework.data.relational.core.sql.Between;
20-
import org.springframework.data.relational.core.sql.Comparison;
21-
import org.springframework.data.relational.core.sql.Condition;
22-
import org.springframework.data.relational.core.sql.ConstantCondition;
23-
import org.springframework.data.relational.core.sql.In;
24-
import org.springframework.data.relational.core.sql.IsNull;
25-
import org.springframework.data.relational.core.sql.Like;
26-
import org.springframework.data.relational.core.sql.NestedCondition;
27-
import org.springframework.data.relational.core.sql.OrCondition;
18+
import org.springframework.data.relational.core.sql.*;
2819
import org.springframework.lang.Nullable;
2920

3021
/**
@@ -103,6 +94,10 @@ private DelegatingVisitor getDelegation(Condition segment) {
10394
return new ConstantConditionVisitor(context, builder::append);
10495
}
10596

97+
if (segment instanceof Not) {
98+
return new NotConditionVisitor(context, builder::append);
99+
}
100+
106101
return null;
107102
}
108103

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2019-2023 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.sql.render;
17+
18+
import org.springframework.data.relational.core.sql.Condition;
19+
import org.springframework.data.relational.core.sql.NestedCondition;
20+
import org.springframework.data.relational.core.sql.Not;
21+
import org.springframework.data.relational.core.sql.Visitable;
22+
import org.springframework.lang.Nullable;
23+
24+
/**
25+
* Renderer for {@link Not}. Uses a {@link RenderTarget} to call back for render results.
26+
*
27+
* @author Jens Schauder
28+
* @since 3.2
29+
*/
30+
class NotConditionVisitor extends TypedSubtreeVisitor<NestedCondition> {
31+
32+
private final RenderContext context;
33+
private final RenderTarget target;
34+
35+
private @Nullable ConditionVisitor conditionVisitor;
36+
37+
NotConditionVisitor(RenderContext context, RenderTarget target) {
38+
39+
this.context = context;
40+
this.target = target;
41+
}
42+
43+
@Override
44+
Delegation enterNested(Visitable segment) {
45+
46+
DelegatingVisitor visitor = getDelegation(segment);
47+
48+
return visitor != null ? Delegation.delegateTo(visitor) : Delegation.retain();
49+
}
50+
51+
@Nullable
52+
private DelegatingVisitor getDelegation(Visitable segment) {
53+
54+
if (segment instanceof Condition) {
55+
return conditionVisitor = new ConditionVisitor(context);
56+
}
57+
58+
return null;
59+
}
60+
61+
@Override
62+
Delegation leaveNested(Visitable segment) {
63+
64+
if (conditionVisitor != null) {
65+
66+
target.onRendered("NOT (" + conditionVisitor.getRenderedPart() + ")");
67+
conditionVisitor = null;
68+
}
69+
70+
return super.leaveNested(segment);
71+
}
72+
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,19 @@ void rendersAliasedExpression() {
635635
.isEqualTo("SELECT table.name AS alias FROM table");
636636
}
637637

638+
@Test // GH-1653
639+
void notOfNested(){
640+
641+
Table table = SQL.table("atable");
642+
643+
Select select = StatementBuilder.select(table.asterisk()).from(table). where(
644+
Conditions.nest(table.column("id").isEqualTo(Expressions.just("1"))
645+
.and(table.column("id").isEqualTo(Expressions.just("2")))).not()).build();
646+
String sql = SqlRenderer.toString(select);
647+
648+
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
649+
}
650+
638651
/**
639652
* Tests the rendering of analytic functions.
640653
*/

0 commit comments

Comments
 (0)