Browse Source
We now support empty IN lists by rendering a condition that evaluates to FALSE using 1 = 0. For NOT IN, we render a condition that evaluates to TRUE using 1 = 1.pull/247/head
8 changed files with 243 additions and 5 deletions
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* Copyright 2020 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; |
||||
|
||||
/** |
||||
* Simple condition that evaluates to SQL {@code FALSE}. |
||||
* |
||||
* @author Mark Paluch |
||||
* @since 2.1 |
||||
*/ |
||||
public class FalseCondition implements Condition { |
||||
|
||||
public static final FalseCondition INSTANCE = new FalseCondition(); |
||||
|
||||
private FalseCondition() {} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see java.lang.Object#toString() |
||||
*/ |
||||
@Override |
||||
public String toString() { |
||||
return "1 = 0"; |
||||
} |
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* Copyright 2020 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; |
||||
|
||||
/** |
||||
* Simple condition that evaluates to SQL {@code TRUE}. |
||||
* |
||||
* @author Mark Paluch |
||||
* @since 2.1 |
||||
*/ |
||||
public class TrueCondition implements Condition { |
||||
|
||||
public static final TrueCondition INSTANCE = new TrueCondition(); |
||||
|
||||
private TrueCondition() {} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see java.lang.Object#toString() |
||||
*/ |
||||
@Override |
||||
public String toString() { |
||||
return "1 = 1"; |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* Copyright 2019-2020 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.FalseCondition; |
||||
import org.springframework.data.relational.core.sql.In; |
||||
import org.springframework.data.relational.core.sql.TrueCondition; |
||||
|
||||
/** |
||||
* Renderer for empty {@link In}. Uses a {@link RenderTarget} to call back for render results. |
||||
* |
||||
* @author Mark Paluch |
||||
* @since 2.1 |
||||
*/ |
||||
class EmptyInVisitor extends TypedSingleConditionRenderSupport<In> { |
||||
|
||||
private final RenderTarget target; |
||||
|
||||
EmptyInVisitor(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(In segment) { |
||||
|
||||
target.onRendered(segment.isNotIn() ? TrueCondition.INSTANCE.toString() : FalseCondition.INSTANCE.toString()); |
||||
|
||||
return super.leaveMatched(segment); |
||||
} |
||||
} |
||||
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright 2020 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; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
/** |
||||
* Unit tests for {@link In}. |
||||
* |
||||
* @author Mark Paluch |
||||
*/ |
||||
public class InTests { |
||||
|
||||
@Test // DATAJDBC-604
|
||||
void shouldRenderToString() { |
||||
|
||||
Table table = Table.create("table"); |
||||
|
||||
assertThat(In.create(table.column("col"), SQL.bindMarker())).hasToString("table.col IN (?)"); |
||||
assertThat(In.create(table.column("col"), SQL.bindMarker()).not()).hasToString("table.col NOT IN (?)"); |
||||
} |
||||
|
||||
@Test // DATAJDBC-604
|
||||
void shouldRenderEmptyExpressionToString() { |
||||
|
||||
Table table = Table.create("table"); |
||||
|
||||
assertThat(In.create(table.column("col"))).hasToString("1 = 0"); |
||||
assertThat(In.create(table.column("col")).not()).hasToString("1 = 1"); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue