Skip to content

Handle ConstantCondition in ConditionVisitor. #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author Meng Zuozhu
* @author Daniele Canteri
* @since 1.1
* @see SQL
* @see Expressions
Expand Down Expand Up @@ -303,20 +304,6 @@ public static In notIn(Column column, Select subselect) {
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() {}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -32,6 +33,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Daniele Canteri
* @since 1.1
* @see AndCondition
* @see OrCondition
Expand Down Expand Up @@ -101,6 +103,10 @@ private DelegatingVisitor getDelegation(Condition segment) {
return new NestedConditionVisitor(context, builder::append);
}

if (segment instanceof ConstantCondition) {
return new ConstantConditionVisitor(context, builder::append);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Unit tests for rendered {@link org.springframework.data.relational.core.sql.Conditions}.
*
* @author Mark Paluch
* @author Daniele Canteri
*/
public class ConditionRendererUnitTests {

Expand Down Expand Up @@ -230,4 +231,14 @@ public void shouldRenderNotIn() {

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");
}
}