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 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 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 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); } } }