Skip to content

Commit 5b5a7cd

Browse files
danielecanterischauder
authored andcommitted
Handle ConstantCondition in ConditionVisitor.
Original pull request #978 Closes #907
1 parent 04fa01b commit 5b5a7cd

File tree

5 files changed

+102
-14
lines changed

5 files changed

+102
-14
lines changed

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

+1-14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Mark Paluch
2828
* @author Jens Schauder
2929
* @author Meng Zuozhu
30+
* @author Daniele Canteri
3031
* @since 1.1
3132
* @see SQL
3233
* @see Expressions
@@ -303,20 +304,6 @@ public static In notIn(Column column, Select subselect) {
303304
return notIn(column, new SubselectExpression(subselect));
304305
}
305306

306-
static class ConstantCondition extends AbstractSegment implements Condition {
307-
308-
private final String condition;
309-
310-
ConstantCondition(String condition) {
311-
this.condition = condition;
312-
}
313-
314-
@Override
315-
public String toString() {
316-
return condition;
317-
}
318-
}
319-
320307
// Utility constructor.
321308
private Conditions() {}
322309
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2019-2021 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;
17+
18+
/**
19+
* {@link Condition} representing fixed sql statement
20+
*
21+
* @author Daniele Canteri
22+
* @since 2.3
23+
*/
24+
public class ConstantCondition extends AbstractSegment implements Condition {
25+
26+
private final String condition;
27+
28+
ConstantCondition(String condition) {
29+
this.condition = condition;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return condition;
35+
}
36+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.data.relational.core.sql.Between;
2020
import org.springframework.data.relational.core.sql.Comparison;
2121
import org.springframework.data.relational.core.sql.Condition;
22+
import org.springframework.data.relational.core.sql.ConstantCondition;
2223
import org.springframework.data.relational.core.sql.In;
2324
import org.springframework.data.relational.core.sql.IsNull;
2425
import org.springframework.data.relational.core.sql.Like;
@@ -32,6 +33,7 @@
3233
*
3334
* @author Mark Paluch
3435
* @author Jens Schauder
36+
* @author Daniele Canteri
3537
* @since 1.1
3638
* @see AndCondition
3739
* @see OrCondition
@@ -101,6 +103,10 @@ private DelegatingVisitor getDelegation(Condition segment) {
101103
return new NestedConditionVisitor(context, builder::append);
102104
}
103105

106+
if (segment instanceof ConstantCondition) {
107+
return new ConstantConditionVisitor(context, builder::append);
108+
}
109+
104110
return null;
105111
}
106112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019-2021 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.ConstantCondition;
19+
20+
/**
21+
* Renderer for {@link ConstantCondition}. Uses a {@link RenderTarget} to call back for render results.
22+
*
23+
* @author Daniele Canteri
24+
* @since 2.3
25+
*/
26+
class ConstantConditionVisitor extends TypedSingleConditionRenderSupport<ConstantCondition> {
27+
28+
private final RenderTarget target;
29+
30+
ConstantConditionVisitor(RenderContext context, RenderTarget target) {
31+
super(context);
32+
this.target = target;
33+
}
34+
35+
/**
36+
*
37+
* (non-Javadoc)
38+
* @see org.springframework.data.relational.core.sql.render.TypedSubtreeVisitor#leaveMatched(org.springframework.data.relational.core.sql.Visitable)
39+
*/
40+
@Override
41+
Delegation leaveMatched(ConstantCondition segment) {
42+
43+
target.onRendered(segment.toString());
44+
45+
return super.leaveMatched(segment);
46+
}
47+
48+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Unit tests for rendered {@link org.springframework.data.relational.core.sql.Conditions}.
3030
*
3131
* @author Mark Paluch
32+
* @author Daniele Canteri
3233
*/
3334
public class ConditionRendererUnitTests {
3435

@@ -230,4 +231,14 @@ public void shouldRenderNotIn() {
230231

231232
assertThat(sql).endsWith("WHERE my_table.left NOT IN (my_table.right)");
232233
}
234+
235+
@Test // DATAJDBC-907
236+
public void shouldRenderJust() {
237+
238+
String sql = SqlRenderer.toString(StatementBuilder.select(left).from(table)
239+
.where(Conditions.just("sql"))
240+
.build());
241+
242+
assertThat(sql).endsWith("WHERE sql");
243+
}
233244
}

0 commit comments

Comments
 (0)