mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor BeanPropertyAssocMany findByParentIds use of raw expression
This commit is contained in:
@@ -299,23 +299,25 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
/**
|
||||
* Find the Id's of detail beans given a parent Id or list of parent Id's.
|
||||
*/
|
||||
public List<Object> findIdsByParentId(Object parentId, List<Object> parentIdist, Transaction t, ArrayList<Object> excludeDetailIds) {
|
||||
public List<Object> findIdsByParentId(Object parentId, List<Object> parentIdList, Transaction t, ArrayList<Object> excludeDetailIds) {
|
||||
if (parentId != null) {
|
||||
return findIdsByParentId(parentId, t, excludeDetailIds);
|
||||
} else {
|
||||
return findIdsByParentIdList(parentIdist, t, excludeDetailIds);
|
||||
return findIdsByParentIdList(parentIdList, t, excludeDetailIds);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Object> findIdsByParentId(Object parentId, Transaction t, ArrayList<Object> excludeDetailIds) {
|
||||
|
||||
String rawWhere = deriveWhereParentIdSql(false, "");
|
||||
List<Object> bindValues = new ArrayList<Object>();
|
||||
bindWhereParentId(bindValues, parentId);
|
||||
|
||||
EbeanServer server = getBeanDescriptor().getEbeanServer();
|
||||
Query<?> q = server.find(getPropertyType())
|
||||
.where().raw(rawWhere).query();
|
||||
|
||||
bindWhereParendId(1, q, parentId);
|
||||
.where()
|
||||
.raw(rawWhere, bindValues.toArray())
|
||||
.query();
|
||||
|
||||
if (excludeDetailIds != null && !excludeDetailIds.isEmpty()) {
|
||||
Expression idIn = q.getExpressionFactory().idIn(excludeDetailIds);
|
||||
@@ -371,21 +373,24 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
query.where().raw(expr, bindValues.toArray());
|
||||
}
|
||||
|
||||
private List<Object> findIdsByParentIdList(List<Object> parentIdist, Transaction t, ArrayList<Object> excludeDetailIds) {
|
||||
private List<Object> findIdsByParentIdList(List<Object> parentIdList, Transaction t, ArrayList<Object> excludeDetailIds) {
|
||||
|
||||
String rawWhere = deriveWhereParentIdSql(true, "");
|
||||
String inClause = buildInClauseBinding(parentIdist.size(), exportedPropertyBindProto);
|
||||
String inClause = buildInClauseBinding(parentIdList.size(), exportedPropertyBindProto);
|
||||
|
||||
String expr = rawWhere + inClause;
|
||||
|
||||
EbeanServer server = getBeanDescriptor().getEbeanServer();
|
||||
Query<?> q = server.find(getPropertyType()).where().raw(expr).query();
|
||||
|
||||
int pos = 1;
|
||||
for (int i = 0; i < parentIdist.size(); i++) {
|
||||
pos = bindWhereParendId(pos, q, parentIdist.get(i));
|
||||
List<Object> bindValues = new ArrayList<Object>();
|
||||
for (int i = 0; i < parentIdList.size(); i++) {
|
||||
bindWhereParentId(bindValues, parentIdList.get(i));
|
||||
}
|
||||
|
||||
EbeanServer server = getBeanDescriptor().getEbeanServer();
|
||||
Query<?> q = server.find(getPropertyType())
|
||||
.where()
|
||||
.raw(expr, bindValues.toArray())
|
||||
.query();
|
||||
|
||||
if (excludeDetailIds != null && !excludeDetailIds.isEmpty()) {
|
||||
Expression idIn = q.getExpressionFactory().idIn(excludeDetailIds);
|
||||
q.where().not(idIn);
|
||||
@@ -690,20 +695,18 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private int bindWhereParendId(int pos, Query<?> q, Object parentId) {
|
||||
private void bindWhereParentId(List<Object> bindValues, Object parentId) {
|
||||
|
||||
if (exportedProperties.length == 1) {
|
||||
q.setParameter(pos++, parentId);
|
||||
bindValues.add(parentId);
|
||||
|
||||
} else {
|
||||
|
||||
EntityBean parent = (EntityBean) parentId;
|
||||
for (int i = 0; i < exportedProperties.length; i++) {
|
||||
Object embVal = exportedProperties[i].getValue(parent);
|
||||
q.setParameter(pos++, embVal);
|
||||
bindValues.add(embVal);
|
||||
}
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void addSelectExported(DbSqlContext ctx, String tableAlias) {
|
||||
|
||||
@@ -5,11 +5,17 @@ import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class BeanPropertyAssocManyTest extends BaseTestCase {
|
||||
|
||||
@@ -40,4 +46,21 @@ public class BeanPropertyAssocManyTest extends BaseTestCase {
|
||||
assertTrue(ref.isReference());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findIdsByParentId() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Object> customerIds = new ArrayList<Object>();
|
||||
customerIds.add(1L);
|
||||
customerIds.add(2L);
|
||||
|
||||
List<Object> contactIdsForOne = contacts().findIdsByParentId(1L, null, null, null);
|
||||
|
||||
List<Object> contactIdsForMultiple = contacts().findIdsByParentId(null, customerIds, null, null);
|
||||
|
||||
assertThat(contactIdsForOne).isNotEmpty();
|
||||
assertThat(contactIdsForMultiple).isNotEmpty();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.grammer;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -36,6 +37,16 @@ public class EqlParserTest {
|
||||
assertThat(query.getGeneratedSql()).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test @Ignore
|
||||
public void where_namedParam() throws Exception {
|
||||
|
||||
Query<Customer> query = parse("where name eq :name");
|
||||
query.setParameter("name", "Rob");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_or1() throws Exception {
|
||||
|
||||
|
||||
+4
-2
@@ -75,7 +75,8 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
|
||||
assertEquals(0, Ebean.find(User.class).findList().size());
|
||||
}
|
||||
|
||||
@Test public void testFindByParentIdList() {
|
||||
@Test
|
||||
public void testFindByParentIdList() {
|
||||
|
||||
if (isMsSqlServer()) return;
|
||||
|
||||
@@ -92,7 +93,8 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
|
||||
ids.add(1L);
|
||||
ids.add(2L);
|
||||
|
||||
beanProperty.findIdsByParentId(null, ids, null, null);
|
||||
beanProperty.findIdsByParentId(null, ids, null, null);
|
||||
beanProperty.findIdsByParentId(1L, null, null, null);
|
||||
}
|
||||
|
||||
@Entity @Table(name = "em_user") public static class User {
|
||||
|
||||
@@ -15,6 +15,8 @@ import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestManyLazyLoadingQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -55,6 +57,7 @@ public class TestManyLazyLoadingQuery extends BaseTestCase {
|
||||
beanProperty.addWhereParentIdIn(query0, parentIds, false);
|
||||
|
||||
query0.findList();
|
||||
assertThat(query0.getGeneratedSql()).contains(" from o_order_detail t0 where (t0.order_id) in (");
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
|
||||
Reference in New Issue
Block a user