Handle ConstantCondition in ConditionVisitor.

Original pull request #978
Closes #907
This commit is contained in:
Daniele Canteri
2021-05-14 22:42:45 +02:00
committed by Jens Schauder
parent 04fa01b8ee
commit 5b5a7cddae
5 changed files with 102 additions and 14 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.util.Assert;
* @author Mark Paluch
* @author Jens Schauder
* @author Meng Zuozhu
* @author Daniele Canteri
* @since 1.1
* @see SQL
* @see Expressions
@@ -303,20 +304,6 @@ public abstract class Conditions {
return notIn(column, new SubselectExpression(subselect));
}
static class ConstantCondition extends AbstractSegment implements Condition {
private final String condition;
ConstantCondition(String condition) {
this.condition = condition;
}
@Override
public String toString() {
return condition;
}
}
// Utility constructor.
private Conditions() {}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2019-2021 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.sql;
/**
* {@link Condition} representing fixed sql statement
*
* @author Daniele Canteri
* @since 2.3
*/
public class ConstantCondition extends AbstractSegment implements Condition {
private final String condition;
ConstantCondition(String condition) {
this.condition = condition;
}
@Override
public String toString() {
return condition;
}
}

View File

@@ -19,6 +19,7 @@ import org.springframework.data.relational.core.sql.AndCondition;
import org.springframework.data.relational.core.sql.Between;
import org.springframework.data.relational.core.sql.Comparison;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.sql.ConstantCondition;
import org.springframework.data.relational.core.sql.In;
import org.springframework.data.relational.core.sql.IsNull;
import org.springframework.data.relational.core.sql.Like;
@@ -32,6 +33,7 @@ import org.springframework.lang.Nullable;
*
* @author Mark Paluch
* @author Jens Schauder
* @author Daniele Canteri
* @since 1.1
* @see AndCondition
* @see OrCondition
@@ -101,6 +103,10 @@ class ConditionVisitor extends TypedSubtreeVisitor<Condition> implements PartRen
return new NestedConditionVisitor(context, builder::append);
}
if (segment instanceof ConstantCondition) {
return new ConstantConditionVisitor(context, builder::append);
}
return null;
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2019-2021 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.sql.render;
import org.springframework.data.relational.core.sql.ConstantCondition;
/**
* Renderer for {@link ConstantCondition}. Uses a {@link RenderTarget} to call back for render results.
*
* @author Daniele Canteri
* @since 2.3
*/
class ConstantConditionVisitor extends TypedSingleConditionRenderSupport<ConstantCondition> {
private final RenderTarget target;
ConstantConditionVisitor(RenderContext context, RenderTarget target) {
super(context);
this.target = target;
}
/**
*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.render.TypedSubtreeVisitor#leaveMatched(org.springframework.data.relational.core.sql.Visitable)
*/
@Override
Delegation leaveMatched(ConstantCondition segment) {
target.onRendered(segment.toString());
return super.leaveMatched(segment);
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.data.relational.core.sql.Table;
* Unit tests for rendered {@link org.springframework.data.relational.core.sql.Conditions}.
*
* @author Mark Paluch
* @author Daniele Canteri
*/
public class ConditionRendererUnitTests {
@@ -230,4 +231,14 @@ public class ConditionRendererUnitTests {
assertThat(sql).endsWith("WHERE my_table.left NOT IN (my_table.right)");
}
@Test // DATAJDBC-907
public void shouldRenderJust() {
String sql = SqlRenderer.toString(StatementBuilder.select(left).from(table)
.where(Conditions.just("sql"))
.build());
assertThat(sql).endsWith("WHERE sql");
}
}