mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
88 lines
3.6 KiB
Java
88 lines
3.6 KiB
Java
package org.tests.query;
|
|
|
|
import io.ebean.BaseTestCase;
|
|
import io.ebean.DB;
|
|
import io.ebean.Query;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.tests.model.basic.Customer;
|
|
import org.tests.model.basic.ResetBasicData;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
public class TestImplicitJoinOnParentRelationship extends BaseTestCase {
|
|
|
|
@Test
|
|
public void test() {
|
|
|
|
ResetBasicData.reset();
|
|
|
|
Query<Customer> query = DB.find(Customer.class)
|
|
.select("id, name")
|
|
.where().eq("orders.details.product.name", "Desk")
|
|
.query();
|
|
|
|
query.findList();
|
|
|
|
if (isPostgres()) {
|
|
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 join o_product u3 on u3.id = u2.product_id where u3.name = ?";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
|
|
} else {
|
|
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 join o_product u3 on u3.id = u2.product_id where u3.name = ?";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
}
|
|
|
|
// select distinct t0.id c0, t0.name c1
|
|
// from o_customer t0
|
|
// join o_order u1 on u1.kcustomer_id = t0.id
|
|
// join o_order_detail u2 on u2.order_id = u1.id
|
|
// join o_product u3 on u3.id = u2.product_id
|
|
// where u3.name = ?
|
|
|
|
}
|
|
|
|
|
|
@Test
|
|
public void testWithDisjunction() {
|
|
|
|
ResetBasicData.reset();
|
|
|
|
Query<Customer> query = DB.find(Customer.class)
|
|
.select("id, name")
|
|
.where().disjunction().eq("orders.details.product.name", "Desk").eq("id", 4).endJunction()
|
|
.query();
|
|
|
|
query.findList();
|
|
|
|
if (isPostgres()) {
|
|
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null left join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
} else {
|
|
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null left join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void testWithOr() {
|
|
|
|
ResetBasicData.reset();
|
|
|
|
Query<Customer> query = DB.find(Customer.class)
|
|
.select("id, name")
|
|
.where().or().eq("orders.details.product.name", "Desk").eq("id", 4).endJunction()
|
|
.query();
|
|
|
|
query.findList();
|
|
|
|
if (isPostgres()) {
|
|
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null left join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
|
|
} else {
|
|
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id and u1.order_date is not null left join o_order_detail u2 on u2.order_id = u1.id and u2.id > 0 left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
|
|
assertThat(sqlOf(query, 1)).contains(expectedSql);
|
|
}
|
|
}
|
|
}
|