Merge pull request #3308 from Incanus3/fix/querybean_or_with_exists

fix TQRootBean.exists() when used with .or()
This commit is contained in:
Rob Bygrave
2024-01-19 08:29:29 +13:00
committed by GitHub
4 changed files with 57 additions and 5 deletions
@@ -745,7 +745,7 @@ public abstract class TQRootBean<T, R> {
* Add EXISTS sub-query predicate.
*/
public R exists(Query<?> subQuery) {
query.where().exists(subQuery);
peekExprList().exists(subQuery);
return root;
}
@@ -753,7 +753,7 @@ public abstract class TQRootBean<T, R> {
* Add NOT EXISTS sub-query predicate.
*/
public R notExists(Query<?> subQuery) {
query.where().notExists(subQuery);
peekExprList().notExists(subQuery);
return root;
}
@@ -764,7 +764,7 @@ public abstract class TQRootBean<T, R> {
* @param bindValues Optional bind values if the SubQuery uses {@code ? } bind values.
*/
public final R exists(String sqlSubQuery, Object... bindValues) {
query().where().exists(sqlSubQuery, bindValues);
peekExprList().exists(sqlSubQuery, bindValues);
return root;
}
@@ -775,7 +775,7 @@ public abstract class TQRootBean<T, R> {
* @param bindValues Optional bind values if the SubQuery uses {@code ? } bind values.
*/
public final R notExists(String sqlSubQuery, Object... bindValues) {
query().where().notExists(sqlSubQuery, bindValues);
peekExprList().notExists(sqlSubQuery, bindValues);
return root;
}
@@ -1,8 +1,9 @@
package org.example.domain;
import io.ebean.annotation.DbArray;
import jakarta.persistence.*;
import org.example.domain.finder.ContactFinder;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.ZonedDateTime;
@@ -17,6 +18,11 @@ import java.util.Map;
@Table(name = "be_contact")
public class Contact extends BaseModel {
/**
* Convenience Finder for 'active record' style.
*/
public static final ContactFinder find = new ContactFinder();
@DbArray
List<@Size(max=20) String> phoneNumbers = new ArrayList<>();
@@ -0,0 +1,18 @@
package org.example.domain.finder;
import io.ebean.Finder;
import org.example.domain.Contact;
import org.example.domain.query.QContact;
/**
*/
public class ContactFinder extends Finder<Long, Contact> {
public ContactFinder() {
super(Contact.class);
}
public QContact typed() {
return new QContact();
}
}
@@ -1,6 +1,8 @@
package org.querytest;
import org.example.domain.Contact;
import org.example.domain.Customer;
import org.example.domain.query.QCustomer;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
@@ -9,6 +11,8 @@ import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class QCustomerAndOrTest {
@Test
@@ -55,6 +59,30 @@ public class QCustomerAndOrTest {
}
@Test
public void testOrWithExists() {
QCustomer query = Customer.find.typed()
.alias("_cust")
.or()
.name.eq("Superman")
.exists(Contact.find.typed()
.alias("contact")
.firstName.eq("Superman")
.raw("contact.customer_id = _cust.id")
.query()
)
.endOr()
.select(QCustomer.alias().id);
query.findList();
assertThat(query.getGeneratedSql()).isEqualTo(
"select _cust.id from be_customer _cust where (" +
"_cust.name = ? or exists (select 1 from be_contact contact where " +
"contact.first_name = ? and contact.customer_id = _cust.id))"
);
}
private Date fiveDaysAgo() {
LocalDateTime fiveDaysAgo = LocalDate.now().atStartOfDay().minusDays(5);
return new Date(fiveDaysAgo.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());