Skip to content

Commit

Permalink
Merge pull request #3308 from Incanus3/fix/querybean_or_with_exists
Browse files Browse the repository at this point in the history
fix TQRootBean.exists() when used with .or()
  • Loading branch information
rbygrave authored Jan 18, 2024
2 parents 09d5163 + 0046d98 commit 2a62d41
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,15 +745,15 @@ public R withLock(Query.LockType lockType, Query.LockWait lockWait) {
* Add EXISTS sub-query predicate.
*/
public R exists(Query<?> subQuery) {
query.where().exists(subQuery);
peekExprList().exists(subQuery);
return root;
}

/**
* Add NOT EXISTS sub-query predicate.
*/
public R notExists(Query<?> subQuery) {
query.where().notExists(subQuery);
peekExprList().notExists(subQuery);
return root;
}

Expand All @@ -764,7 +764,7 @@ public R notExists(Query<?> subQuery) {
* @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;
}

Expand All @@ -775,7 +775,7 @@ public final R exists(String sqlSubQuery, Object... bindValues) {
* @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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +18,11 @@
@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<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +11,8 @@
import java.util.Date;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class QCustomerAndOrTest {

@Test
Expand Down Expand Up @@ -55,6 +59,30 @@ public void testQuery() {

}

@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());
Expand Down

0 comments on commit 2a62d41

Please sign in to comment.