mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-21 03:23:07 +00:00
Merge pull request #3776 from ebean-orm/feature/PString_eqIfNotBlank
Add PString eqIfNotBlank() helper query expression
This commit is contained in:
@@ -26,6 +26,35 @@ public final class PString<R> extends PBaseComparable<R, String> {
|
||||
super(name, root, prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is equal to if the value is non-null and not blank, otherwise no expression is added to the query.
|
||||
* <p>
|
||||
* This is like {@code eqIfPresent()} but additionally treats an empty or whitespace-only value as
|
||||
* "not present". When present, the value is trimmed before being used in the equal to expression.
|
||||
* <p>
|
||||
* This is a helper for building queries in fluid style where a String predicate is effectively
|
||||
* optional, avoiding a separate {@code if} block and {@code trimToNull()} style helper.
|
||||
*
|
||||
* <pre>{@code
|
||||
* List<Customer> customers = new QCustomer()
|
||||
* .name.eqIfNotBlank(nameFilter)
|
||||
* .status.eqIfPresent(statusFilter)
|
||||
* .findList();
|
||||
* }</pre>
|
||||
*
|
||||
* @param value the value which can be null or blank
|
||||
* @return the root query bean instance
|
||||
*/
|
||||
public R eqIfNotBlank(@Nullable String value) {
|
||||
if (value != null) {
|
||||
String trimmed = value.trim();
|
||||
if (!trimmed.isEmpty()) {
|
||||
expr().eq(_name, trimmed);
|
||||
}
|
||||
}
|
||||
return _root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Case insensitive is equal to.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.querytest;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.example.domain.Customer;
|
||||
import org.example.domain.query.QCustomer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.example.domain.query.QCustomer.Alias.name;
|
||||
|
||||
class QStringEqIfNotBlankTest {
|
||||
|
||||
@Test
|
||||
void present_addsEqPredicate() {
|
||||
var q = new QCustomer()
|
||||
.select(name)
|
||||
.name.eqIfNotBlank("rob")
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void null_skipsPredicate() {
|
||||
var q = new QCustomer()
|
||||
.select(name)
|
||||
.name.isNotNull()
|
||||
.name.eqIfNotBlank(null)
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).doesNotContain("t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankWhitespace_skipsPredicate() {
|
||||
var q = new QCustomer()
|
||||
.select(name)
|
||||
.name.isNotNull()
|
||||
.name.eqIfNotBlank(" ")
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).doesNotContain("t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void empty_skipsPredicate() {
|
||||
var q = new QCustomer()
|
||||
.select(name)
|
||||
.name.isNotNull()
|
||||
.name.eqIfNotBlank("")
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).doesNotContain("t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void present_trimsBoundValue() {
|
||||
Database db = DB.getDefault();
|
||||
String unique = "eqIfNotBlank-" + System.nanoTime();
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.name = unique;
|
||||
db.save(customer);
|
||||
|
||||
Customer found = new QCustomer(db)
|
||||
.name.eqIfNotBlank(" " + unique + " ")
|
||||
.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.name).isEqualTo(unique);
|
||||
|
||||
db.delete(customer);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ class QueryAlsoIfTest {
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.also */ t0.id, t0.name from be_customer t0 where t0.name is not null and t0.email is not null");
|
||||
assertThat(q.getGeneratedSql()).contains("from be_customer t0 where t0.name is not null and t0.email is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -61,7 +61,7 @@ class QueryAlsoIfTest {
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.applyIfPresent */ t0.id, t0.name from be_customer t0 where t0.name is not null and t0.status = ?");
|
||||
assertThat(q.getGeneratedSql()).contains("from be_customer t0 where t0.name is not null and t0.status = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,7 +74,7 @@ class QueryAlsoIfTest {
|
||||
.query();
|
||||
|
||||
q.findList();
|
||||
assertThat(q.getGeneratedSql()).isEqualTo("select /* QueryAlsoIfTest.notApplyIfPresent */ t0.id, t0.name from be_customer t0 where t0.name is not null");
|
||||
assertThat(q.getGeneratedSql()).contains("from be_customer t0 where t0.name is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user