#678 - Remove SqlQuery.findSet() and SqlQuery.findMap() ... migrate to use SlqQuery.findList()

This commit is contained in:
Robin Bygrave committed 2016-04-29 08:37:52 +12:00
1 parent ad17da5b36
commit f867cd4c01
10 files changed
+16 -306

No files matched your search

@@ -1,156 +0,0 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.RelationalQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.util.BeanCollectionFactory;
import java.util.Collection;
import java.util.Map;
/**
* Wraps a BeanCollection with helper methods to add beans.
* <p>
* Helps adding the bean to the underlying set list or map.
* </p>
*/
public final class BeanCollectionWrapper {
/**
* Flag set if this builds a Map rather than a Collection.
*/
private final boolean isMap;
/**
* A property name used as key for a Map.
*/
private final String mapKey;
/**
* The actual BeanCollection.
*/
private final BeanCollection<?> beanCollection;
/**
* Collection type of BeanCollection.
*/
private final Collection<Object> collection;
/**
* Map type of BeanCollection.
*/
private final Map<Object, Object> map;
/**
* The associated BeanDescriptor.
*/
private final BeanDescriptor<?> desc;
/**
* The number of rows added.
*/
private int rowCount;
public BeanCollectionWrapper(RelationalQueryRequest request) {
this.desc = null;
SpiQuery.Type queryType = request.getQueryType();
this.mapKey = request.getQuery().getMapKey();
this.isMap = SpiQuery.Type.MAP.equals(queryType);
this.beanCollection = createBeanCollection(queryType);
this.collection = getCollection(isMap);
this.map = getMap(isMap);
}
@SuppressWarnings({"unchecked", "rawtypes"})
private Map<Object, Object> getMap(boolean isMap) {
return isMap ? (Map) beanCollection : null;
}
@SuppressWarnings("unchecked")
private Collection<Object> getCollection(boolean isMap) {
return isMap ? null : (Collection<Object>) beanCollection;
}
/**
* Return the underlying BeanCollection.
*/
public BeanCollection<?> getBeanCollection() {
return beanCollection;
}
/**
* Create a BeanCollection of the correct type.
*/
private BeanCollection<?> createBeanCollection(SpiQuery.Type manyType) {
return BeanCollectionFactory.create(manyType);
}
/**
* Return true if this wraps a Map rather than a set or list.
*/
public boolean isMap() {
return isMap;
}
/**
* Return the number of rows added to this wrapper.
*/
public int size() {
return rowCount;
}
/**
* Add the bean to the collection held in this wrapper.
*/
public void add(EntityBean bean) {
add(bean, beanCollection);
}
/**
* Add the bean to the collection passed.
*
* @param bean the bean to add
* @param collection the collection or map to add the bean to
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public void add(EntityBean bean, Object collection) {
if (bean == null) {
return;
}
rowCount++;
if (isMap) {
Object keyValue;
if (mapKey != null) {
// use the value for the property
keyValue = desc.getValue(bean, mapKey);
} else {
// use the uniqueId for this
keyValue = desc.getId(bean);
}
Map mapColl = (Map) collection;
mapColl.put(keyValue, bean);
} else {
((Collection) collection).add(bean);
}
}
/**
* Specifically add to a Collection.
*/
public void addToCollection(Object bean) {
collection.add(bean);
}
/**
* Specifically add to this as a Map with a known key.
*/
public void addToMap(Object bean, Object key) {
map.put(key, bean);
}
}
@@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Perform native sql fetches.
@@ -73,7 +75,7 @@ public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
}
}
public Object findMany(RelationalQueryRequest request) {
public List<SqlRow> findList(RelationalQueryRequest request) {
long startTime = System.currentTimeMillis();
try {
@@ -85,9 +87,7 @@ public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
int loadRowCount = 0;
BeanCollectionWrapper wrapper = new BeanCollectionWrapper(request);
boolean isMap = wrapper.isMap();
String mapKey = request.getQuery().getMapKey();
List<SqlRow> rows = new ArrayList<SqlRow>();
SpiSqlQuery query = request.getQuery();
SqlQueryListener listener = query.getListener();
@@ -104,11 +104,7 @@ public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
if (listener != null) {
listener.process(bean);
} else {
if (isMap) {
wrapper.addToMap(bean, bean.get(mapKey));
} else {
wrapper.addToCollection(bean);
}
rows.add(bean);
}
loadRowCount++;
if (loadRowCount == maxRows) {
@@ -120,7 +116,7 @@ public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
logSummary(request, startTime);
return wrapper.getBeanCollection();
return rows;
} catch (Exception e) {
throw new PersistenceException(Message.msg("fetch.error", e.getMessage(), request.getSql()), e);