Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TQRootBean.exists() when used with .or() #3308

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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