mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-25 07:17:57 +00:00
#1189 - ExpressionList.isNull and SoftDelete
This commit is contained in:
1 parent
e75670f207
commit
0dbba7a211
7 files changed
+47
-25
No files matched your search
@@ -507,21 +507,22 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
|||||||
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
|
public String getAssocIsEmpty(SpiExpressionRequest request, String path) {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
SpiQuery<?> query = request.getQueryRequest().getQuery();
|
SpiQuery<?> query = request.getQueryRequest().getQuery();
|
||||||
if (manyToMany) {
|
if (manyToMany) {
|
||||||
sb.append(query.isAsDraft() ? intersectionDraftTable : intersectionPublishTable);
|
sb.append(query.isAsDraft() ? intersectionDraftTable : intersectionPublishTable);
|
||||||
} else {
|
} else {
|
||||||
sb.append(targetDescriptor.getBaseTable(query.getTemporalMode()));
|
sb.append(targetDescriptor.getBaseTable(query.getTemporalMode()));
|
||||||
}
|
}
|
||||||
sb.append(" where ");
|
sb.append(" x where ");
|
||||||
for (int i = 0; i < exportedProperties.length; i++) {
|
for (int i = 0; i < exportedProperties.length; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
sb.append(" and ");
|
sb.append(" and ");
|
||||||
}
|
}
|
||||||
exportedProperties[i].appendWhere(sb, path);
|
exportedProperties[i].appendWhere(sb, "x.", path);
|
||||||
|
}
|
||||||
|
if (targetDescriptor.isSoftDelete()) {
|
||||||
|
sb.append(" and ").append(targetDescriptor.getSoftDeletePredicate("x"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ public class ExportedProperty {
|
|||||||
/**
|
/**
|
||||||
* Append a logical where for the foreign db column to logical property name,
|
* Append a logical where for the foreign db column to logical property name,
|
||||||
*/
|
*/
|
||||||
public void appendWhere(StringBuilder sb, String path) {
|
public void appendWhere(StringBuilder sb, String alias, String path) {
|
||||||
sb.append(foreignDbColumn).append(" = ");
|
sb.append(alias).append(foreignDbColumn).append(" = ");
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
sb.append(path).append(".");
|
sb.append(path).append(".");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class UpdateQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
updateQuery.update();
|
updateQuery.update();
|
||||||
|
|
||||||
assertThat(updateQuery.getGeneratedSql()).contains("update o_customer set status=? where not exists (select 1 from contact where customer_id = id) and id > ?");
|
assertThat(updateQuery.getGeneratedSql()).contains("update o_customer set status=? where not exists (select 1 from contact x where x.customer_id = id) and id > ?");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
.query();
|
.query();
|
||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where not exists (select 1 from contact where customer_id = t0.id");
|
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where not exists (select 1 from contact x where x.customer_id = t0.id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -37,7 +37,7 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
.query();
|
.query();
|
||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where exists (select 1 from contact where customer_id = t0.id");
|
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where exists (select 1 from contact x where x.customer_id = t0.id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -51,7 +51,7 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
.query();
|
.query();
|
||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where not exists (select 1 from contact_note where contact_id = t0.id");
|
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where not exists (select 1 from contact_note x where x.contact_id = t0.id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -65,7 +65,7 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
.query();
|
.query();
|
||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where exists (select 1 from contact_note where contact_id = t0.id");
|
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where exists (select 1 from contact_note x where x.contact_id = t0.id");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
if (isPostgres()) {
|
if (isPostgres()) {
|
||||||
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note where contact_id = u1.id)");
|
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note where contact_id = u1.id)");
|
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,10 +114,10 @@ public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
|||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
if (isPostgres()) {
|
if (isPostgres()) {
|
||||||
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note where contact_id = u1.id)");
|
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note where contact_id = u1.id)");
|
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public class TestManyWhereJoinM2M extends BaseTestCase {
|
|||||||
|
|
||||||
List<MUser> usersWithNoRoles = query.findList();
|
List<MUser> usersWithNoRoles = query.findList();
|
||||||
|
|
||||||
assertThat(sqlOf(query, 2)).contains("select t0.userid, t0.user_name, t0.user_type_id from muser t0 where not exists (select 1 from mrole_muser where muser_userid = t0.userid)");
|
assertThat(sqlOf(query, 2)).contains("select t0.userid, t0.user_name, t0.user_type_id from muser t0 where not exists (select 1 from mrole_muser x where x.muser_userid = t0.userid)");
|
||||||
assertThat(usersWithNoRoles).isNotEmpty();
|
assertThat(usersWithNoRoles).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ public class TestManyWhereJoinM2M extends BaseTestCase {
|
|||||||
|
|
||||||
List<MUser> usersWithRoles = query.findList();
|
List<MUser> usersWithRoles = query.findList();
|
||||||
|
|
||||||
assertThat(sqlOf(query, 1)).contains("select t0.userid, t0.user_name from muser t0 where exists (select 1 from mrole_muser where muser_userid = t0.userid)");
|
assertThat(sqlOf(query, 1)).contains("select t0.userid, t0.user_name from muser t0 where exists (select 1 from mrole_muser x where x.muser_userid = t0.userid)");
|
||||||
assertThat(usersWithRoles).isNotEmpty();
|
assertThat(usersWithRoles).isNotEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Order> query = Ebean.find(Order.class).where().isNotNull("details").query();
|
Query<Order> query = Ebean.find(Order.class).where().isNotNull("details").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail where order_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail x where x.order_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -40,7 +40,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Order> query = Ebean.find(Order.class).where().isNotEmpty("details").query();
|
Query<Order> query = Ebean.find(Order.class).where().isNotEmpty("details").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail where order_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail x where x.order_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -50,7 +50,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Order> query = Ebean.find(Order.class).where().isNull("details").query();
|
Query<Order> query = Ebean.find(Order.class).where().isNull("details").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail where order_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail x where x.order_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -60,7 +60,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Order> query = Ebean.find(Order.class).where().isEmpty("details").query();
|
Query<Order> query = Ebean.find(Order.class).where().isEmpty("details").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail where order_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail x where x.order_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -70,7 +70,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Role> query = Ebean.find(Role.class).where().isEmpty("permissions").query();
|
Query<Role> query = Ebean.find(Role.class).where().isEmpty("permissions").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission x where x.mt_role_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -80,7 +80,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Role> query = Ebean.find(Role.class).where().isNull("permissions").query();
|
Query<Role> query = Ebean.find(Role.class).where().isNull("permissions").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission x where x.mt_role_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -90,7 +90,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Role> query = Ebean.find(Role.class).where().isNotEmpty("permissions").query();
|
Query<Role> query = Ebean.find(Role.class).where().isNotEmpty("permissions").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission x where x.mt_role_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -100,7 +100,7 @@ public class TestQueryIsNull extends BaseTestCase {
|
|||||||
Query<Role> query = Ebean.find(Role.class).where().isNotNull("permissions").query();
|
Query<Role> query = Ebean.find(Role.class).where().isNotNull("permissions").query();
|
||||||
query.findList();
|
query.findList();
|
||||||
|
|
||||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission x where x.mt_role_id = t0.id)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package org.tests.softdelete;
|
|||||||
|
|
||||||
import io.ebean.BaseTestCase;
|
import io.ebean.BaseTestCase;
|
||||||
import io.ebean.Ebean;
|
import io.ebean.Ebean;
|
||||||
|
import io.ebean.Query;
|
||||||
import io.ebean.Transaction;
|
import io.ebean.Transaction;
|
||||||
import io.ebean.annotation.PersistBatch;
|
import io.ebean.annotation.PersistBatch;
|
||||||
import org.tests.model.softdelete.ESoftDelMid;
|
import org.tests.model.softdelete.ESoftDelMid;
|
||||||
@@ -12,6 +13,8 @@ import org.junit.Test;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class TestSoftDeleteTop extends BaseTestCase {
|
public class TestSoftDeleteTop extends BaseTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,4 +92,22 @@ public class TestSoftDeleteTop extends BaseTestCase {
|
|||||||
Ebean.deletePermanent(top1);
|
Ebean.deletePermanent(top1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWhereNull() {
|
||||||
|
|
||||||
|
ESoftDelTop top1 = new ESoftDelTop("top1");
|
||||||
|
top1.addMids("mid1");
|
||||||
|
top1.addMids("mid2");
|
||||||
|
|
||||||
|
Ebean.save(top1);
|
||||||
|
|
||||||
|
Query<ESoftDelTop> query = Ebean.find(ESoftDelTop.class)
|
||||||
|
.where().isEmpty("mids")
|
||||||
|
.query();
|
||||||
|
|
||||||
|
query.findList();
|
||||||
|
|
||||||
|
assertThat(sqlOf(query)).contains("where not exists (select 1 from esoft_del_mid x where x.top_id = t0.id and x.deleted =)");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user