Refactor BeanPropertyAssocOne findByParentIds use of raw expression

This commit is contained in:
Robin Bygrave
2016-07-12 16:11:51 +12:00
parent d8dfd66af3
commit 6b54b6ef46
3 changed files with 34 additions and 45 deletions
@@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.List;
/**
* Abstract base for properties mapped to an associated bean, list, set or map.
@@ -41,6 +42,11 @@ public abstract class BeanPropertyAssoc<T> extends BeanProperty {
String targetIdProperty;
/**
* Derived list of exported property and matching foreignKey
*/
protected ExportedProperty[] exportedProperties;
/**
* Persist settings.
*/
@@ -385,4 +391,18 @@ public abstract class BeanPropertyAssoc<T> extends BeanProperty {
+ " Perhaps an error in a @JoinColumn";
throw new PersistenceException(msg);
}
protected void bindWhereParentId(List<Object> bindValues, Object parentId) {
if (exportedProperties.length == 1) {
bindValues.add(parentId);
} else {
EntityBean parent = (EntityBean) parentId;
for (int i = 0; i < exportedProperties.length; i++) {
Object embVal = exportedProperties[i].getValue(parent);
bindValues.add(embVal);
}
}
}
}
@@ -85,11 +85,6 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
private BeanProperty mapKeyProperty;
/**
* Derived list of exported property and matching foreignKey
*/
private ExportedProperty[] exportedProperties;
private String exportedPropertyBindProto = "?";
/**
@@ -695,20 +690,6 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
}
}
private void bindWhereParentId(List<Object> bindValues, Object parentId) {
if (exportedProperties.length == 1) {
bindValues.add(parentId);
} else {
EntityBean parent = (EntityBean) parentId;
for (int i = 0; i < exportedProperties.length; i++) {
Object embVal = exportedProperties[i].getValue(parent);
bindValues.add(embVal);
}
}
}
public void addSelectExported(DbSqlContext ctx, String tableAlias) {
String alias = manyToMany ? "int_" : tableAlias;
@@ -50,8 +50,6 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
*/
protected ImportedId importedId;
private ExportedProperty[] exportedProperties;
private String deleteByParentIdSql;
private String deleteByParentIdInSql;
@@ -240,47 +238,37 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
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();
.where()
.raw(rawWhere, bindValues.toArray())
.query();
bindWhereParendId(q, parentId);
return server.findIds(q, t);
}
private List<Object> findIdsByParentIdList(List<Object> parentIdist, Transaction t) {
private List<Object> findIdsByParentIdList(List<Object> parentIdList, Transaction t) {
String rawWhere = deriveWhereParentIdSql(true);
String inClause = targetIdBinder.getIdInValueExpr(parentIdist.size());
String inClause = targetIdBinder.getIdInValueExpr(parentIdList.size());
String expr = rawWhere + inClause;
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 = (Query<?>) server.find(getPropertyType())
.where().raw(expr);
for (int i = 0; i < parentIdist.size(); i++) {
bindWhereParendId(q, parentIdist.get(i));
}
.where().raw(expr, bindValues.toArray());
return server.findIds(q, t);
}
private void bindWhereParendId(Query<?> q, Object parentId) {
if (exportedProperties.length == 1) {
q.setParameter(1, parentId);
} else {
int pos = 1;
EntityBean parent = (EntityBean) parentId;
for (int i = 0; i < exportedProperties.length; i++) {
Object embVal = exportedProperties[i].getValue(parent);
q.setParameter(pos++, embVal);
}
}
}
void addFkey() {
if (importedId != null) {
importedId.addFkeys(name);