Skip to content

Commit 99e5142

Browse files
committed
Polishing.
Introduce prefix operator to express queries as Conditions.not(…) as alternative to myCondition.not() (suffix operator) for easier readability. Reformat code, update copyright years. See #1653 Original pull request: #1659
1 parent 97b4154 commit 99e5142

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public static Condition nest(Condition condition) {
5757
return new NestedCondition(condition);
5858
}
5959

60+
/**
61+
* Creates a NOT {@link Condition} that reverses the condition.
62+
*
63+
* @param condition the condition to {@code NOT}.
64+
* @return a NOT {@link Condition}.
65+
* @since 3.1.6
66+
*/
67+
public static Condition not(Condition condition) {
68+
return new Not(condition);
69+
}
70+
6071
/**
6172
* Creates a {@code IS NULL} condition.
6273
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 the original author or authors.
2+
* Copyright 2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
* Renderer for {@link Not}. Uses a {@link RenderTarget} to call back for render results.
2626
*
2727
* @author Jens Schauder
28-
* @since 3.2
28+
* @since 3.1.6
2929
*/
3030
class NotConditionVisitor extends TypedSubtreeVisitor<NestedCondition> {
3131

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ void shouldRenderFullOuterJoin() {
149149

150150
Select select = Select.builder().select(employee.column("id"), department.column("name")) //
151151
.from(employee) //
152-
.join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id")).equals(department.column("id")) //
152+
.join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id"))
153+
.equals(department.column("id")) //
153154
.build();
154155

155156
assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee "
@@ -253,11 +254,9 @@ void shouldRenderNestedJoins() {
253254
Table merchantCustomers = Table.create("merchants_customers");
254255
Table customerDetails = Table.create("customer_details");
255256

256-
Select innerSelect = Select.builder()
257-
.select(customerDetails.column("cd_user_id"))
258-
.from(customerDetails).join(merchantCustomers)
259-
.on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id")))
260-
.build();
257+
Select innerSelect = Select.builder().select(customerDetails.column("cd_user_id")).from(customerDetails)
258+
.join(merchantCustomers)
259+
.on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id"))).build();
261260

262261
InlineQuery innerTable = InlineQuery.create(innerSelect, "inner");
263262

@@ -285,8 +284,7 @@ void shouldRenderJoinWithTwoInlineQueries() {
285284

286285
Select innerSelectOne = Select.builder()
287286
.select(employee.column("id").as("empId"), employee.column("department_Id"), employee.column("name"))
288-
.from(employee)
289-
.build();
287+
.from(employee).build();
290288
Select innerSelectTwo = Select.builder().select(department.column("id"), department.column("name")).from(department)
291289
.build();
292290

@@ -631,21 +629,27 @@ void rendersAliasedExpression() {
631629
.build();
632630

633631
String rendered = SqlRenderer.toString(select);
634-
assertThat(rendered)
635-
.isEqualTo("SELECT table.name AS alias FROM table");
632+
assertThat(rendered).isEqualTo("SELECT table.name AS alias FROM table");
636633
}
637634

638635
@Test // GH-1653
639-
void notOfNested(){
636+
void notOfNested() {
640637

641638
Table table = SQL.table("atable");
642639

643-
Select select = StatementBuilder.select(table.asterisk()).from(table). where(
644-
Conditions.nest(table.column("id").isEqualTo(Expressions.just("1"))
645-
.and(table.column("id").isEqualTo(Expressions.just("2")))).not()).build();
640+
Select select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.nest(
641+
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))
642+
.not()).build();
646643
String sql = SqlRenderer.toString(select);
647644

648645
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
646+
647+
select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.not(Conditions.nest(
648+
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))))
649+
.build();
650+
sql = SqlRenderer.toString(select);
651+
652+
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
649653
}
650654

651655
/**
@@ -749,8 +753,7 @@ void renderAnalyticFunctionWithOutArgument() {
749753

750754
String rendered = SqlRenderer.toString(select);
751755

752-
assertThat(rendered).isEqualTo(
753-
"SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
756+
assertThat(rendered).isEqualTo("SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
754757
}
755758
}
756759
}

0 commit comments

Comments
 (0)