Skip to content

Commit bb20c66

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 c73523a commit bb20c66

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
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: 18 additions & 14 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

@@ -621,16 +619,23 @@ void rendersFullyQualifiedNamesInOrderBy() {
621619
}
622620

623621
@Test // GH-1653
624-
void notOfNested(){
622+
void notOfNested() {
625623

626624
Table table = SQL.table("atable");
627625

628-
Select select = StatementBuilder.select(table.asterisk()).from(table). where(
629-
Conditions.nest(table.column("id").isEqualTo(Expressions.just("1"))
630-
.and(table.column("id").isEqualTo(Expressions.just("2")))).not()).build();
626+
Select select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.nest(
627+
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))
628+
.not()).build();
631629
String sql = SqlRenderer.toString(select);
632630

633631
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
632+
633+
select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.not(Conditions.nest(
634+
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))))
635+
.build();
636+
sql = SqlRenderer.toString(select);
637+
638+
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
634639
}
635640

636641
/**
@@ -734,8 +739,7 @@ void renderAnalyticFunctionWithOutArgument() {
734739

735740
String rendered = SqlRenderer.toString(select);
736741

737-
assertThat(rendered).isEqualTo(
738-
"SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
742+
assertThat(rendered).isEqualTo("SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
739743
}
740744
}
741745
}

0 commit comments

Comments
 (0)