Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mp911de committed Nov 14, 2023
1 parent 97b4154 commit 99e5142
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public static Condition nest(Condition 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,7 +25,7 @@
* 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> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ void shouldRenderFullOuterJoin() {

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 "
Expand Down Expand Up @@ -253,11 +254,9 @@ void shouldRenderNestedJoins() {
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");

Expand Down Expand Up @@ -285,8 +284,7 @@ void shouldRenderJoinWithTwoInlineQueries() {

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();

Expand Down Expand Up @@ -631,21 +629,27 @@ void rendersAliasedExpression() {
.build();

String rendered = SqlRenderer.toString(select);
assertThat(rendered)
.isEqualTo("SELECT table.name AS alias FROM table");
assertThat(rendered).isEqualTo("SELECT table.name AS alias FROM table");
}

@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)");
}

/**
Expand Down Expand Up @@ -749,8 +753,7 @@ void renderAnalyticFunctionWithOutArgument() {

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");
}
}
}

0 comments on commit 99e5142

Please sign in to comment.