Browse Source

Polishing.

Reformat code. Add Kotlin test to verify Criteria usage.

See #2226
Original pull request: #2227
main
Mark Paluch 5 days ago
parent
commit
28c8d8f5f9
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 12
      spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Criteria.java
  2. 4
      spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Update.java
  3. 34
      spring-data-relational/src/test/java/org/springframework/data/relational/core/query/CriteriaUnitTests.java
  4. 11
      spring-data-relational/src/test/java/org/springframework/data/relational/core/query/UpdateUnitTests.java
  5. 45
      spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaKtUnitTests.kt
  6. 2
      src/main/antora/modules/ROOT/nav.adoc
  7. 1
      src/main/antora/modules/ROOT/pages/jdbc/property-paths.adoc
  8. 1
      src/main/antora/modules/ROOT/pages/r2dbc/property-paths.adoc

12
spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Criteria.java

@ -166,7 +166,7 @@ public class Criteria implements CriteriaDefinition { @@ -166,7 +166,7 @@ public class Criteria implements CriteriaDefinition {
* @return a new {@link CriteriaStep} object to complete the first {@link Criteria}.
* @since 4.1
*/
public static <T, P> CriteriaStep where(TypedPropertyPath<T,P> property) {
public static <T, P> CriteriaStep where(TypedPropertyPath<T, P> property) {
return where(property.toDotPath());
}
@ -198,8 +198,8 @@ public class Criteria implements CriteriaDefinition { @@ -198,8 +198,8 @@ public class Criteria implements CriteriaDefinition {
* @since 4.1
*/
@CheckReturnValue
public <T,P> CriteriaStep and(TypedPropertyPath<T,P> property) {
return and(TypedPropertyPath.of(property).toDotPath());
public <T, P> CriteriaStep and(TypedPropertyPath<T, P> property) {
return and(property.toDotPath());
}
/**
@ -260,8 +260,8 @@ public class Criteria implements CriteriaDefinition { @@ -260,8 +260,8 @@ public class Criteria implements CriteriaDefinition {
* @since 4.1
*/
@CheckReturnValue
public <T,P> CriteriaStep or(TypedPropertyPath<T,P> property) {
return or(TypedPropertyPath.of(property).toDotPath());
public <T, P> CriteriaStep or(TypedPropertyPath<T, P> property) {
return or(property.toDotPath());
}
/**
@ -524,11 +524,9 @@ public class Criteria implements CriteriaDefinition { @@ -524,11 +524,9 @@ public class Criteria implements CriteriaDefinition {
}
SqlIdentifier column = criteria.getColumn();
Assert.state(column != null, "Column must not be null");
Comparator comparator = criteria.getComparator();
Assert.state(comparator != null, "Comparator must not be null");
stringBuilder.append(column.toSql(IdentifierProcessing.NONE)).append(' ').append(comparator.getComparator());

4
spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Update.java

@ -75,7 +75,7 @@ public class Update { @@ -75,7 +75,7 @@ public class Update {
* @return new instance of {@link Update}.
* @since 4.1
*/
public static <T,P> Update update(TypedPropertyPath<T,P> property, @Nullable Object value) {
public static <T, P> Update update(TypedPropertyPath<T, P> property, @Nullable Object value) {
return update(property.toDotPath(), value);
}
@ -103,7 +103,7 @@ public class Update { @@ -103,7 +103,7 @@ public class Update {
* @since 4.1
*/
@CheckReturnValue
public <T,P> Update set(TypedPropertyPath<T,P> property, @Nullable Object value) {
public <T, P> Update set(TypedPropertyPath<T, P> property, @Nullable Object value) {
return set(property.toDotPath(), value);
}

34
spring-data-relational/src/test/java/org/springframework/data/relational/core/query/CriteriaUnitTests.java

@ -30,6 +30,7 @@ import org.springframework.data.relational.core.sql.SqlIdentifier; @@ -30,6 +30,7 @@ import org.springframework.data.relational.core.sql.SqlIdentifier;
* @author Mark Paluch
* @author Jens Schauder
* @author Roman Chigvintsev
* @author Christoph Strobl
*/
class CriteriaUnitTests {
@ -46,6 +47,11 @@ class CriteriaUnitTests { @@ -46,6 +47,11 @@ class CriteriaUnitTests {
assertThat(criteria).hasToString("(foo IS NOT NULL AND foo IS NULL)");
}
@Test // GH-2226
void whereWithTypedPropertyPathIsEqualToStringColumnName() {
assertThat(where(Person::getName).is("o")).isEqualTo(where("name").is("o"));
}
@Test // DATAJDBC-513
void fromCriteriaOptimized() {
@ -95,6 +101,13 @@ class CriteriaUnitTests { @@ -95,6 +101,13 @@ class CriteriaUnitTests {
assertThat(criteria.getValue()).isEqualTo("bar");
}
@Test // GH-2226
void andChainedCriteriaWithTypedPropertyPathIsEqualToStringColumnName() {
assertThat(where(Person::getName).is("o").and(Person::getAge).isNotNull())
.isEqualTo(where("name").is("o").and("age").isNotNull());
}
@Test // DATAJDBC-513
void andGroupedCriteria() {
@ -131,6 +144,13 @@ class CriteriaUnitTests { @@ -131,6 +144,13 @@ class CriteriaUnitTests {
assertThat(criteria.getValue()).isEqualTo("bar");
}
@Test // GH-2226
void orChainedCriteriaWithTypedPropertyPathIsEqualToStringColumnName() {
assertThat(where(Person::getName).is("o").or(Person::getAge).isNotNull())
.isEqualTo(where("name").is("o").or("age").isNotNull());
}
@Test // DATAJDBC-513
void orGroupedCriteria() {
@ -323,4 +343,18 @@ class CriteriaUnitTests { @@ -323,4 +343,18 @@ class CriteriaUnitTests {
}
}
}
static class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return this.age;
}
}
}

11
spring-data-relational/src/test/java/org/springframework/data/relational/core/query/UpdateUnitTests.java

@ -15,10 +15,10 @@ @@ -15,10 +15,10 @@
*/
package org.springframework.data.relational.core.query;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link Update}.
*
@ -28,13 +28,12 @@ import static org.assertj.core.api.Assertions.*; @@ -28,13 +28,12 @@ import static org.assertj.core.api.Assertions.*;
public class UpdateUnitTests {
@Test // DATAJDBC-513
public void shouldRenderUpdateToString() {
void shouldRenderUpdateToString() {
assertThat(Update.update("foo", "baz").set("bar", 42)).hasToString("SET foo = 'baz', bar = 42");
}
@Test // GH-2226
public void shouldRenderUpdateWithTypedPropertyPathToString() {
void shouldRenderUpdateWithTypedPropertyPathToString() {
assertThat(Update.update(Person::getFirstName, "baz").set("bar", 42)).hasToString("SET firstName = 'baz', bar = 42");
}
@ -42,7 +41,7 @@ public class UpdateUnitTests { @@ -42,7 +41,7 @@ public class UpdateUnitTests {
private String firstName;
private String lastName;
public String getFirstName() {
String getFirstName() {
return firstName;
}

45
spring-data-relational/src/test/kotlin/org/springframework/data/relational/core/query/CriteriaKtUnitTests.kt

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2026-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.query
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.data.core.div
/**
* Kotlin unit tests for [Criteria].
*
* @author Mark Paluch
*/
class CriteriaKtUnitTests {
@Test
fun shouldAcceptKProperty() {
assertThat(
Criteria.where(Person::lastName).isEqual("foo")
).isEqualTo(Criteria.where("lastName").isEqual("foo"))
}
@Test
fun shouldAcceptKPropertyPath() {
assertThat(
Criteria.where(Person::parent / Person::lastName).isEqual("foo")
).isEqualTo(Criteria.where("parent.lastName").isEqual("foo"))
}
data class Person(val firstName: String, val lastName: String, val parent: Person)
}

2
src/main/antora/modules/ROOT/nav.adoc

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
** xref:jdbc/entity-persistence.adoc[]
** xref:jdbc/sequences.adoc[]
** xref:jdbc/mapping.adoc[]
** xref:jdbc/property-paths.adoc[]
** xref:jdbc/query-methods.adoc[]
** xref:jdbc/mybatis.adoc[]
** xref:jdbc/events.adoc[]
@ -39,6 +40,7 @@ @@ -39,6 +40,7 @@
** xref:r2dbc/entity-persistence.adoc[]
** xref:r2dbc/sequences.adoc[]
** xref:r2dbc/mapping.adoc[]
** xref:r2dbc/property-paths.adoc[]
** xref:r2dbc/repositories.adoc[]
** xref:r2dbc/query-methods.adoc[]
** xref:r2dbc/entity-callbacks.adoc[]

1
src/main/antora/modules/ROOT/pages/jdbc/property-paths.adoc

@ -0,0 +1 @@ @@ -0,0 +1 @@
include::{commons}@data-commons::page$property-paths.adoc[]

1
src/main/antora/modules/ROOT/pages/r2dbc/property-paths.adoc

@ -0,0 +1 @@ @@ -0,0 +1 @@
include::{commons}@data-commons::page$property-paths.adoc[]
Loading…
Cancel
Save