Browse Source

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
3.1.x
Mark Paluch 2 years ago
parent
commit
bb20c66505
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 11
      spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Conditions.java
  2. 4
      spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NotConditionVisitor.java
  3. 30
      spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

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

@ -57,6 +57,17 @@ public abstract class Conditions {
return new NestedCondition(condition); return new NestedCondition(condition);
} }
/**
* Creates a NOT {@link Condition} that reverses the condition.
*
* @param condition the condition to {@code NOT}.
* @return a NOT {@link Condition}.
* @since 3.1.6
*/
public static Condition not(Condition condition) {
return new Not(condition);
}
/** /**
* Creates a {@code IS NULL} condition. * Creates a {@code IS NULL} condition.
* *

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2023 the original author or authors. * Copyright 2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import org.springframework.lang.Nullable;
* Renderer for {@link Not}. Uses a {@link RenderTarget} to call back for render results. * Renderer for {@link Not}. Uses a {@link RenderTarget} to call back for render results.
* *
* @author Jens Schauder * @author Jens Schauder
* @since 3.2 * @since 3.1.6
*/ */
class NotConditionVisitor extends TypedSubtreeVisitor<NestedCondition> { class NotConditionVisitor extends TypedSubtreeVisitor<NestedCondition> {

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

@ -149,7 +149,8 @@ class SelectRendererUnitTests {
Select select = Select.builder().select(employee.column("id"), department.column("name")) // Select select = Select.builder().select(employee.column("id"), department.column("name")) //
.from(employee) // .from(employee) //
.join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id")).equals(department.column("id")) // .join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id"))
.equals(department.column("id")) //
.build(); .build();
assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee " assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee "
@ -253,11 +254,9 @@ class SelectRendererUnitTests {
Table merchantCustomers = Table.create("merchants_customers"); Table merchantCustomers = Table.create("merchants_customers");
Table customerDetails = Table.create("customer_details"); Table customerDetails = Table.create("customer_details");
Select innerSelect = Select.builder() Select innerSelect = Select.builder().select(customerDetails.column("cd_user_id")).from(customerDetails)
.select(customerDetails.column("cd_user_id")) .join(merchantCustomers)
.from(customerDetails).join(merchantCustomers) .on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id"))).build();
.on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id")))
.build();
InlineQuery innerTable = InlineQuery.create(innerSelect, "inner"); InlineQuery innerTable = InlineQuery.create(innerSelect, "inner");
@ -285,8 +284,7 @@ class SelectRendererUnitTests {
Select innerSelectOne = Select.builder() Select innerSelectOne = Select.builder()
.select(employee.column("id").as("empId"), employee.column("department_Id"), employee.column("name")) .select(employee.column("id").as("empId"), employee.column("department_Id"), employee.column("name"))
.from(employee) .from(employee).build();
.build();
Select innerSelectTwo = Select.builder().select(department.column("id"), department.column("name")).from(department) Select innerSelectTwo = Select.builder().select(department.column("id"), department.column("name")).from(department)
.build(); .build();
@ -625,12 +623,19 @@ class SelectRendererUnitTests {
Table table = SQL.table("atable"); Table table = SQL.table("atable");
Select select = StatementBuilder.select(table.asterisk()).from(table). where( Select select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.nest(
Conditions.nest(table.column("id").isEqualTo(Expressions.just("1")) table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))
.and(table.column("id").isEqualTo(Expressions.just("2")))).not()).build(); .not()).build();
String sql = SqlRenderer.toString(select); String sql = SqlRenderer.toString(select);
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)"); assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.not(Conditions.nest(
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))))
.build();
sql = SqlRenderer.toString(select);
assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)");
} }
/** /**
@ -734,8 +739,7 @@ class SelectRendererUnitTests {
String rendered = SqlRenderer.toString(select); String rendered = SqlRenderer.toString(select);
assertThat(rendered).isEqualTo( assertThat(rendered).isEqualTo("SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
"SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
} }
} }
} }

Loading…
Cancel
Save