mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#678 - Remove SqlQuery.findSet() and SqlQuery.findMap() ... migrate to use SlqQuery.findList()
This commit is contained in:
@@ -1211,42 +1211,6 @@ public interface EbeanServer {
|
||||
*/
|
||||
void findEachWhile(SqlQuery query, QueryEachWhileConsumer<SqlRow> consumer, Transaction transaction);
|
||||
|
||||
/**
|
||||
* Execute the sql query returning a set of MapBean.
|
||||
* <p>
|
||||
* Generally you are able to use {@link SqlQuery#findSet()} rather than
|
||||
* explicitly calling this method. You could use this method if you wish to
|
||||
* explicitly control the transaction used for the query.
|
||||
* </p>
|
||||
*
|
||||
* @param query
|
||||
* the query to execute.
|
||||
* @param transaction
|
||||
* the transaction to use (can be null).
|
||||
* @return the set of fetched MapBean.
|
||||
*
|
||||
* @see SqlQuery#findSet()
|
||||
*/
|
||||
Set<SqlRow> findSet(SqlQuery query, Transaction transaction);
|
||||
|
||||
/**
|
||||
* Execute the sql query returning a map of MapBean.
|
||||
* <p>
|
||||
* Generally you are able to use {@link SqlQuery#findMap()} rather than
|
||||
* explicitly calling this method. You could use this method if you wish to
|
||||
* explicitly control the transaction used for the query.
|
||||
* </p>
|
||||
*
|
||||
* @param query
|
||||
* the query to execute.
|
||||
* @param transaction
|
||||
* the transaction to use (can be null).
|
||||
* @return the set of fetched MapBean.
|
||||
*
|
||||
* @see SqlQuery#findMap()
|
||||
*/
|
||||
Map<?, SqlRow> findMap(SqlQuery query, Transaction transaction);
|
||||
|
||||
/**
|
||||
* Execute the sql query returning a single MapBean or null.
|
||||
* <p>
|
||||
|
||||
@@ -51,16 +51,6 @@ public interface SqlQuery extends Serializable {
|
||||
*/
|
||||
List<SqlRow> findList();
|
||||
|
||||
/**
|
||||
* Execute the query returning a set.
|
||||
*/
|
||||
Set<SqlRow> findSet();
|
||||
|
||||
/**
|
||||
* Execute the query returning a map.
|
||||
*/
|
||||
Map<?, SqlRow> findMap();
|
||||
|
||||
/**
|
||||
* Execute the SqlQuery iterating a row at a time.
|
||||
* <p>
|
||||
@@ -135,11 +125,6 @@ public interface SqlQuery extends Serializable {
|
||||
*/
|
||||
SqlQuery setMaxRows(int maxRows);
|
||||
|
||||
/**
|
||||
* Set the column to use to determine the keys for a Map.
|
||||
*/
|
||||
SqlQuery setMapKey(String mapKey);
|
||||
|
||||
/**
|
||||
* Set a timeout on this query.
|
||||
* <p>
|
||||
|
||||
@@ -35,11 +35,6 @@ public interface SpiSqlQuery extends SqlQuery {
|
||||
*/
|
||||
int getMaxRows();
|
||||
|
||||
/**
|
||||
* Return the key property for maps.
|
||||
*/
|
||||
String getMapKey();
|
||||
|
||||
/**
|
||||
* Return the query timeout.
|
||||
*/
|
||||
|
||||
@@ -1441,31 +1441,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
}
|
||||
}
|
||||
|
||||
public Set<SqlRow> findSet(SqlQuery query, Transaction t) {
|
||||
|
||||
RelationalQueryRequest request = new RelationalQueryRequest(this, relationalQueryEngine, query, t);
|
||||
|
||||
try {
|
||||
request.initTransIfRequired();
|
||||
return request.findSet();
|
||||
|
||||
} finally {
|
||||
request.endTransIfRequired();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<?, SqlRow> findMap(SqlQuery query, Transaction t) {
|
||||
|
||||
RelationalQueryRequest request = new RelationalQueryRequest(this, relationalQueryEngine, query, t);
|
||||
try {
|
||||
request.initTransIfRequired();
|
||||
return request.findMap();
|
||||
|
||||
} finally {
|
||||
request.endTransIfRequired();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the bean by either performing an insert or update.
|
||||
*/
|
||||
@@ -1480,7 +1455,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
persister.save(checkEntityBean(bean), t);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void markAsDirty(Object bean) {
|
||||
if (!(bean instanceof EntityBean)) {
|
||||
|
||||
@@ -5,12 +5,14 @@ import com.avaje.ebean.QueryEachConsumer;
|
||||
import com.avaje.ebean.QueryEachWhileConsumer;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface RelationalQueryEngine {
|
||||
|
||||
/**
|
||||
* Find a list of beans using relational query.
|
||||
*/
|
||||
Object findMany(RelationalQueryRequest request);
|
||||
List<SqlRow> findList(RelationalQueryRequest request);
|
||||
|
||||
/**
|
||||
* Find each query using relational query.
|
||||
|
||||
@@ -26,8 +26,6 @@ import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Wraps the objects involved in executing a SqlQuery.
|
||||
@@ -107,22 +105,9 @@ public final class RelationalQueryRequest {
|
||||
queryEngine.findEach(this, consumer);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<SqlRow> findList() {
|
||||
queryType = SpiQuery.Type.LIST;
|
||||
return (List<SqlRow>) queryEngine.findMany(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<SqlRow> findSet() {
|
||||
queryType = SpiQuery.Type.SET;
|
||||
return (Set<SqlRow>) queryEngine.findMany(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<?, SqlRow> findMap() {
|
||||
queryType = SpiQuery.Type.MAP;
|
||||
return (Map<?, SqlRow>) queryEngine.findMany(this);
|
||||
return queryEngine.findList(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+6
-10
@@ -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);
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.QueryEachConsumer;
|
||||
import com.avaje.ebean.QueryEachWhileConsumer;
|
||||
@@ -17,6 +9,11 @@ import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import com.avaje.ebeaninternal.api.SpiSqlQuery;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Default implementation of SQuery - SQL Query.
|
||||
*/
|
||||
@@ -47,11 +44,6 @@ public class DefaultRelationalQuery implements SpiSqlQuery {
|
||||
|
||||
private int bufferFetchSizeHint;
|
||||
|
||||
/**
|
||||
* The property used to get the key value for a Map.
|
||||
*/
|
||||
private String mapKey;
|
||||
|
||||
/**
|
||||
* Bind parameters when using the query language.
|
||||
*/
|
||||
@@ -84,14 +76,6 @@ public class DefaultRelationalQuery implements SpiSqlQuery {
|
||||
return server.findList(this, null);
|
||||
}
|
||||
|
||||
public Set<SqlRow> findSet() {
|
||||
return server.findSet(this, null);
|
||||
}
|
||||
|
||||
public Map<?, SqlRow> findMap() {
|
||||
return server.findMap(this, null);
|
||||
}
|
||||
|
||||
public SqlRow findUnique() {
|
||||
return server.findUnique(this, null);
|
||||
}
|
||||
@@ -152,15 +136,6 @@ public class DefaultRelationalQuery implements SpiSqlQuery {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMapKey() {
|
||||
return mapKey;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setMapKey(String mapKey) {
|
||||
this.mapKey = mapKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user