Skip to content

Commit 63dd114

Browse files
committed
Properly render NOT.
Closes #1653
1 parent af1027d commit 63dd114

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-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

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

0 commit comments

Comments
 (0)