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
This commit is contained in:
Mark Paluch
2023-11-14 08:39:09 +01:00
parent c73523a8b6
commit bb20c66505
3 changed files with 31 additions and 16 deletions

View File

@@ -57,6 +57,17 @@ public abstract class Conditions {
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.
*

View File

@@ -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");
* 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.
*
* @author Jens Schauder
* @since 3.2
* @since 3.1.6
*/
class NotConditionVisitor extends TypedSubtreeVisitor<NestedCondition> {

View File

@@ -149,7 +149,8 @@ class SelectRendererUnitTests {
Select select = Select.builder().select(employee.column("id"), department.column("name")) //
.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();
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 customerDetails = Table.create("customer_details");
Select innerSelect = Select.builder()
.select(customerDetails.column("cd_user_id"))
.from(customerDetails).join(merchantCustomers)
.on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id")))
.build();
Select innerSelect = Select.builder().select(customerDetails.column("cd_user_id")).from(customerDetails)
.join(merchantCustomers)
.on(merchantCustomers.column("mc_user_id").isEqualTo(customerDetails.column("cd_user_id"))).build();
InlineQuery innerTable = InlineQuery.create(innerSelect, "inner");
@@ -285,8 +284,7 @@ class SelectRendererUnitTests {
Select innerSelectOne = Select.builder()
.select(employee.column("id").as("empId"), employee.column("department_Id"), employee.column("name"))
.from(employee)
.build();
.from(employee).build();
Select innerSelectTwo = Select.builder().select(department.column("id"), department.column("name")).from(department)
.build();
@@ -621,16 +619,23 @@ class SelectRendererUnitTests {
}
@Test // GH-1653
void notOfNested(){
void notOfNested() {
Table table = SQL.table("atable");
Select select = StatementBuilder.select(table.asterisk()).from(table). where(
Conditions.nest(table.column("id").isEqualTo(Expressions.just("1"))
.and(table.column("id").isEqualTo(Expressions.just("2")))).not()).build();
Select select = StatementBuilder.select(table.asterisk()).from(table).where(Conditions.nest(
table.column("id").isEqualTo(Expressions.just("1")).and(table.column("id").isEqualTo(Expressions.just("2"))))
.not()).build();
String sql = SqlRenderer.toString(select);
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);
assertThat(rendered).isEqualTo(
"SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
assertThat(rendered).isEqualTo("SELECT ROW_NUMBER() OVER(PARTITION BY employee.department) FROM employee");
}
}
}