#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave committed 2016-12-11 23:23:22 +13:00
1 parent 5821dc96b9
commit 971e2dc91b
2134 files changed
+10721 -8652

No files matched your search

@@ -0,0 +1,43 @@
package io.ebeaninternal.server.query;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A base object for query Future objects.
*
* @param <T> the entity bean type
* @author rbygrave
*/
abstract class BaseFuture<T> implements Future<T> {
final FutureTask<T> futureTask;
BaseFuture(FutureTask<T> futureTask) {
this.futureTask = futureTask;
}
public boolean cancel(boolean mayInterruptIfRunning) {
return futureTask.cancel(mayInterruptIfRunning);
}
public T get() throws InterruptedException, ExecutionException {
return futureTask.get();
}
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return futureTask.get(timeout, unit);
}
public boolean isCancelled() {
return futureTask.isCancelled();
}
public boolean isDone() {
return futureTask.isDone();
}
}
@@ -0,0 +1,790 @@
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebean.Version;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.bean.EntityBeanIntercept;
import io.ebean.bean.NodeUsageCollector;
import io.ebean.bean.NodeUsageListener;
import io.ebean.bean.ObjectGraphNode;
import io.ebean.bean.PersistenceContext;
import io.ebean.event.readaudit.ReadEvent;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiQuery.Mode;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.autotune.ProfilingListener;
import io.ebeaninternal.server.core.Message;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanCollectionHelp;
import io.ebeaninternal.server.deploy.BeanCollectionHelpFactory;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.lib.util.StringHelper;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.type.DataReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* An object that represents a SqlSelect statement.
* <p>
* The SqlSelect is based on a tree (Object Graph). The tree is traversed to see
* what parts are included in the tree according to the value of
* find.getInclude();
* </p>
* <p>
* The tree structure is flattened into a SqlSelectChain. The SqlSelectChain is
* the key object used in reading the flat resultSet back into Objects.
* </p>
*/
public class CQuery<T> implements DbReadContext, CancelableQuery {
private static final Logger logger = LoggerFactory.getLogger(CQuery.class);
private static final int GLOBAL_ROW_LIMIT = Integer.valueOf(System.getProperty("ebean.query.globallimit", "1000000"));
/**
* The resultSet rows read.
*/
private int rowCount;
/**
* The number of master EntityBeans loaded.
*/
private int loadedBeanCount;
/**
* Flag set when no more rows are in the resultSet.
*/
private boolean noMoreRows;
/**
* The 'master' bean just loaded.
*/
private EntityBean nextBean;
/**
* Holds the previous loaded bean.
*/
private EntityBean currentBean;
/**
* Caches hasNext for consecutive hasNext() calls.
*/
private boolean hasNextCache;
private final BeanPropertyAssocMany<?> lazyLoadManyProperty;
private Object lazyLoadParentId;
private EntityBean lazyLoadParentBean;
/**
* The 'master' collection being populated.
*/
private final BeanCollection<T> collection;
/**
* The help for the 'master' collection.
*/
private final BeanCollectionHelp<T> help;
/**
* The overall find request wrapper object.
*/
private final OrmQueryRequest<T> request;
private final BeanDescriptor<T> desc;
private final SpiQuery<T> query;
private final boolean disableLazyLoading;
private Map<String, String> currentPathMap;
private String currentPrefix;
/**
* Where clause predicates.
*/
private final CQueryPredicates predicates;
private final boolean rawSql;
/**
* The final sql that is generated.
*/
private final String sql;
/**
* Where clause to show in logs when using an existing query plan.
*/
private final String logWhereSql;
/**
* Set to true if the row number column is included in the sql.
*/
private final boolean rowNumberIncluded;
/**
* Tree that knows how to build the master and detail beans from the
* resultSet.
*/
private final SqlTreeNode rootNode;
/**
* For master detail query.
*/
private final BeanPropertyAssocMany<?> manyProperty;
private final int maxRowsLimit;
private DataReader dataReader;
/**
* The statement used to create the resultSet.
*/
private PreparedStatement pstmt;
private boolean cancelled;
private String bindLog;
private final CQueryPlan queryPlan;
private final Mode queryMode;
private final boolean autoTuneProfiling;
private final ObjectGraphNode objectGraphNode;
private final ProfilingListener profilingListener;
private final WeakReference<NodeUsageListener> profilingListenerRef;
private final Boolean readOnly;
private long startNano;
private long executionTimeMicros;
/**
* Flag set when findIterate is being read audited.
*/
private boolean auditFindIterate;
/**
* A buffer of Ids collected for findIterate auditing.
*/
private List<Object> auditFindIterateIds;
/**
* Create the Sql select based on the request.
*/
@SuppressWarnings("unchecked")
public CQuery(OrmQueryRequest<T> request, CQueryPredicates predicates, CQueryPlan queryPlan) {
this.request = request;
this.queryPlan = queryPlan;
this.query = request.getQuery();
this.queryMode = query.getMode();
this.lazyLoadManyProperty = query.getLazyLoadMany();
this.readOnly = request.isReadOnly();
this.disableLazyLoading = query.isDisableLazyLoading();
this.objectGraphNode = query.getParentNode();
this.profilingListener = query.getProfilingListener();
this.autoTuneProfiling = profilingListener != null;
this.profilingListenerRef = autoTuneProfiling ? new WeakReference<>(profilingListener) : null;
// set the generated sql back to the query
// so its available to the user...
query.setGeneratedSql(queryPlan.getSql());
SqlTree sqlTree = queryPlan.getSqlTree();
this.rootNode = sqlTree.getRootNode();
this.manyProperty = sqlTree.getManyProperty();
this.sql = queryPlan.getSql();
this.rawSql = queryPlan.isRawSql();
this.rowNumberIncluded = queryPlan.isRowNumberIncluded();
this.logWhereSql = queryPlan.getLogWhereSql();
this.desc = request.getBeanDescriptor();
this.predicates = predicates;
this.maxRowsLimit = query.getMaxRows() > 0 ? query.getMaxRows() : GLOBAL_ROW_LIMIT;
this.help = createHelp(request);
this.collection = (help != null ? help.createEmptyNoParent() : null);
}
private BeanCollectionHelp<T> createHelp(OrmQueryRequest<T> request) {
if (request.isFindById()) {
return null;
} else {
SpiQuery.Type manyType = request.getQuery().getType();
if (manyType == null) {
// subQuery compiled for InQueryExpression
return null;
}
return BeanCollectionHelpFactory.create(request);
}
}
@Override
public boolean isDraftQuery() {
return query.isAsDraft();
}
@Override
public boolean isDisableLazyLoading() {
return disableLazyLoading;
}
public Boolean isReadOnly() {
return readOnly;
}
public void propagateState(Object e) {
if (Boolean.TRUE.equals(readOnly)) {
if (e instanceof EntityBean) {
((EntityBean) e)._ebean_getIntercept().setReadOnly(true);
}
}
}
public DataReader getDataReader() {
return dataReader;
}
public Mode getQueryMode() {
return queryMode;
}
public CQueryPredicates getPredicates() {
return predicates;
}
SpiOrmQueryRequest<?> getQueryRequest() {
return request;
}
public void cancel() {
synchronized (this) {
this.cancelled = true;
if (pstmt != null) {
try {
pstmt.cancel();
} catch (SQLException e) {
String msg = "Error cancelling query";
throw new PersistenceException(msg, e);
}
}
}
}
/**
* Prepare bind and execute query with Forward only hints.
*/
boolean prepareBindExecuteQueryForwardOnly(boolean dbPlatformForwardOnlyHint) throws SQLException {
return prepareBindExecuteQueryWithOption(dbPlatformForwardOnlyHint);
}
/**
* Prepare bind and execute the query normally.
*/
boolean prepareBindExecuteQuery() throws SQLException {
return prepareBindExecuteQueryWithOption(false);
}
private boolean prepareBindExecuteQueryWithOption(boolean forwardOnlyHint) throws SQLException {
synchronized (this) {
if (cancelled || query.isCancelled()) {
// cancelled before we started
cancelled = true;
return false;
}
startNano = System.nanoTime();
// prepare
SpiTransaction t = request.getTransaction();
Connection conn = t.getInternalConnection();
if (query.isRawSql()) {
ResultSet suppliedResultSet = query.getRawSql().getResultSet();
if (suppliedResultSet != null) {
// this is a user supplied ResultSet so use that
dataReader = queryPlan.createDataReader(suppliedResultSet);
bindLog = "";
return true;
}
}
if (forwardOnlyHint) {
// Use forward only hints for large resultSet processing (Issue 56, MySql specific)
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
pstmt.setFetchSize(Integer.MIN_VALUE);
} else {
pstmt = conn.prepareStatement(sql);
}
if (query.getTimeout() > 0) {
pstmt.setQueryTimeout(query.getTimeout());
}
if (query.getBufferFetchSizeHint() > 0) {
pstmt.setFetchSize(query.getBufferFetchSizeHint());
}
DataBind dataBind = queryPlan.bindEncryptedProperties(pstmt, conn);
bindLog = predicates.bind(dataBind);
// executeQuery
ResultSet rset = pstmt.executeQuery();
dataReader = queryPlan.createDataReader(rset);
return true;
}
}
/**
* Close the resources.
* <p>
* The JDBC resultSet and statement need to be closed. Its important that this method is called.
* </p>
*/
public void close() {
try {
if (auditFindIterateIds != null && !auditFindIterateIds.isEmpty()) {
auditIterateLogMessage();
}
} catch (Throwable e) {
logger.error("Error logging read audit logs", e);
}
try {
if (dataReader != null) {
dataReader.close();
dataReader = null;
}
} catch (SQLException e) {
logger.error("Error closing dataReader", e);
}
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
logger.error("Error closing preparedStatement", e);
}
}
/**
* Return the persistence context.
*/
public PersistenceContext getPersistenceContext() {
return request.getPersistenceContext();
}
public void setLazyLoadedChildBean(EntityBean bean, Object lazyLoadParentId) {
if (lazyLoadParentId != null) {
if (!lazyLoadParentId.equals(this.lazyLoadParentId)) {
// get the appropriate parent bean from the persistence context
this.lazyLoadParentBean = (EntityBean) lazyLoadManyProperty.getBeanDescriptor().contextGet(getPersistenceContext(), lazyLoadParentId);
this.lazyLoadParentId = lazyLoadParentId;
}
// add the loadedBean to the appropriate collection of lazyLoadParentBean
lazyLoadManyProperty.addBeanToCollectionWithCreate(lazyLoadParentBean, bean, true);
}
}
/**
* Read a row from the result set returning a bean.
* <p>
* If the query includes a many then the first object in the returned array is
* the one/master and the second the many/detail.
* </p>
*/
private boolean readNextBean() throws SQLException {
if (!moveToNextRow()) {
if (currentBean == null) {
return false;
} else {
// the last bean
nextBean = currentBean;
loadedBeanCount++;
return true;
}
}
loadedBeanCount++;
if (manyProperty == null) {
// only single resultSet row required to build object so we are done
// read a single resultSet row into single bean
nextBean = rootNode.load(this, null, null);
return true;
}
if (nextBean == null) {
// very first read
nextBean = rootNode.load(this, null, null);
} else {
// nextBean set to previously read currentBean
nextBean = currentBean;
request.persistenceContextAdd(nextBean);
// check the current row we have just moved to
if (checkForDifferentBean()) {
return true;
}
}
readUntilDifferentBeanStarted();
return true;
}
/**
* Read resultSet rows until we hit the end or get a different bean.
*/
private void readUntilDifferentBeanStarted() throws SQLException {
while (moveToNextRow()) {
if (checkForDifferentBean()) return;
}
}
/**
* Read the currentBean from the row data returning true if the bean
* is different to the nextBean (false if we need to read more rows).
*/
private boolean checkForDifferentBean() throws SQLException {
currentBean = rootNode.load(this, null, null);
return currentBean != nextBean;
}
/**
* Return true if we can move to the next resultSet row.
*/
private boolean moveToNextRow() throws SQLException {
if (!dataReader.next()) {
noMoreRows = true;
return false;
}
rowCount++;
dataReader.resetColumnPosition();
if (rowNumberIncluded) {
// row_number() column used for limit features
dataReader.incrementPos(1);
}
return true;
}
long getQueryExecutionTimeMicros() {
return executionTimeMicros;
}
boolean readBean() throws SQLException {
boolean result = hasNext();
updateExecutionStatistics();
return result;
}
protected EntityBean next() {
if (auditFindIterate) {
auditIterateNextBean();
}
hasNextCache = false;
return nextBean;
}
protected boolean hasNext() throws SQLException {
synchronized (this) {
if (noMoreRows || cancelled || loadedBeanCount >= maxRowsLimit) {
return false;
}
if (hasNextCache) {
return true;
}
hasNextCache = readNextBean();
return hasNextCache;
}
}
/**
* Read version beans and their effective dates.
*/
@SuppressWarnings("unchecked")
List<Version<T>> readVersions() throws SQLException {
List<Version<T>> versionList = new ArrayList<>();
Version version;
while ((version = readNextVersion()) != null) {
versionList.add(version);
}
updateExecutionStatistics();
return versionList;
}
private Version readNextVersion() throws SQLException {
if (moveToNextRow()) {
return rootNode.loadVersion(this);
}
return null;
}
BeanCollection<T> readCollection() throws SQLException {
while (hasNext()) {
EntityBean bean = next();
help.add(collection, bean, false);
}
updateExecutionStatistics();
return collection;
}
void updateExecutionStatistics() {
try {
long exeNano = System.nanoTime() - startNano;
executionTimeMicros = TimeUnit.NANOSECONDS.toMicros(exeNano);
if (autoTuneProfiling) {
profilingListener.collectQueryInfo(objectGraphNode, loadedBeanCount, executionTimeMicros);
}
queryPlan.executionTime(loadedBeanCount, executionTimeMicros, objectGraphNode);
} catch (Exception e) {
logger.error("Error updating execution statistics", e);
}
}
QueryIterator<T> readIterate(int bufferSize, OrmQueryRequest<T> request) {
if (bufferSize > 0) {
return new CQueryIteratorWithBuffer<>(this, request, bufferSize);
} else {
return new CQueryIteratorSimple<>(this, request);
}
}
String getLoadedRowDetail() {
if (manyProperty == null) {
return String.valueOf(rowCount);
} else {
return loadedBeanCount + ":" + rowCount;
}
}
public void register(String path, EntityBeanIntercept ebi) {
path = getPath(path);
request.getGraphContext().register(path, ebi);
}
public void register(String path, BeanCollection<?> bc) {
path = getPath(path);
request.getGraphContext().register(path, bc);
}
/**
* Return true if this is a raw sql query as opposed to Ebean generated sql.
*/
public boolean isRawSql() {
return rawSql;
}
/**
* Return the where predicate for display in the transaction log.
*/
String getLogWhereSql() {
return logWhereSql;
}
/**
* Return the property that is associated with the many. There can only be one
* per SqlSelect. This can be null.
*/
public BeanPropertyAssocMany<?> getManyProperty() {
return manyProperty;
}
public String getBindLog() {
return bindLog;
}
public SpiTransaction getTransaction() {
return request.getTransaction();
}
/**
* Return the short bean name.
*/
String getBeanName() {
return desc.getName();
}
/**
* Return the generated sql.
*/
public String getGeneratedSql() {
return sql;
}
/**
* Create a PersistenceException including interesting information like the bindLog and sql used.
*/
PersistenceException createPersistenceException(SQLException e) {
return createPersistenceException(e, getTransaction(), bindLog, sql);
}
/**
* Create a PersistenceException including interesting information like the bindLog and sql used.
*/
static PersistenceException createPersistenceException(SQLException e, SpiTransaction t, String bindLog, String sql) {
if (t.isLogSummary()) {
// log the error to the transaction log
String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r", "\n"}, "\\n ");
String msg = "ERROR executing query: bindLog[" + bindLog + "] error[" + errMsg + "]";
t.logSummary(msg);
}
// ensure 'rollback' is logged if queryOnly transaction
t.getConnection();
// build a decent error message for the exception
String m = Message.msg("fetch.sqlerror", e.getMessage(), bindLog, sql);
return new PersistenceException(m, e);
}
/**
* Should we create profileNodes for beans created in this query.
* <p>
* This is true for all queries except lazy load bean queries.
* </p>
*/
public boolean isAutoTuneProfiling() {
// need query.isProfiling() because we just take the data
// from the lazy loaded or refreshed beans and put it into the already
// existing beans which are already collecting usage information
return autoTuneProfiling && query.isUsageProfiling();
}
private String getPath(String propertyName) {
if (currentPrefix == null) {
return propertyName;
} else if (propertyName == null) {
return currentPrefix;
}
String path = currentPathMap.get(propertyName);
if (path != null) {
return path;
} else {
return currentPrefix + "." + propertyName;
}
}
public void profileBean(EntityBeanIntercept ebi, String prefix) {
ObjectGraphNode node = request.getGraphContext().getObjectGraphNode(prefix);
ebi.setNodeUsageCollector(new NodeUsageCollector(node, profilingListenerRef));
}
public void setCurrentPrefix(String currentPrefix, Map<String, String> currentPathMap) {
this.currentPrefix = currentPrefix;
this.currentPathMap = currentPathMap;
}
/**
* A find bean query with read auditing so build and log the ReadEvent.
*/
void auditFind(EntityBean bean) {
if (bean != null) {
// only audit when a bean was actually found
desc.readAuditBean(queryPlan.getAuditQueryKey(), bindLog, bean);
}
}
/**
* a find many query with read auditing so build the ReadEvent and log it.
*/
void auditFindMany() {
if (!collection.isEmpty()) {
// get the id values of the underlying collection
List<Object> ids = new ArrayList<>(collection.size());
Collection<T> underlyingBeans = collection.getActualDetails();
for (T underlyingBean : underlyingBeans) {
ids.add(desc.getIdForJson(underlyingBean));
}
ReadEvent futureReadEvent = query.getFutureFetchAudit();
if (futureReadEvent == null) {
// normal query execution
desc.readAuditMany(queryPlan.getAuditQueryKey(), bindLog, ids);
} else {
// this query was executed via findFutureList() and the prepare()
// has already been called so set the details and log
futureReadEvent.setQueryKey(queryPlan.getAuditQueryKey());
futureReadEvent.setBindLog(bindLog);
futureReadEvent.setIds(ids);
desc.readAuditFutureMany(futureReadEvent);
}
}
}
/**
* Indicate that read auditing is occurring on a this findIterate query.
*/
void auditFindIterate() {
auditFindIterate = true;
}
/**
* Send the current buffer of findIterate collected ids to the audit log.
*/
private void auditIterateLogMessage() {
desc.readAuditMany(queryPlan.getAuditQueryKey(), bindLog, auditFindIterateIds);
// create a new list on demand with the next bean/id
auditFindIterateIds = null;
}
/**
* Add the id to the audit id buffer and flush if needed in batches of 100.
*/
private void auditIterateNextBean() {
if (auditFindIterateIds == null) {
auditFindIterateIds = new ArrayList<>(100);
}
auditFindIterateIds.add(desc.getIdForJson(nextBean));
if (auditFindIterateIds.size() >= 100) {
auditIterateLogMessage();
}
}
}
@@ -0,0 +1,655 @@
package io.ebeaninternal.server.query;
import io.ebean.RawSql;
import io.ebean.RawSql.ColumnMapping;
import io.ebean.RawSql.ColumnMapping.Column;
import io.ebean.RawSqlBuilder;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.SqlLimitRequest;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebean.config.dbplatform.SqlLimiter;
import io.ebean.event.readaudit.ReadAuditQueryPlan;
import io.ebean.text.PathProperties;
import io.ebean.util.StringHelper;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.el.ElPropertyValue;
import io.ebeaninternal.server.persist.Binder;
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
import io.ebeaninternal.server.querydefn.OrmQueryLimitRequest;
import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Generates the SQL SELECT statements taking into account the physical
* deployment properties.
*/
class CQueryBuilder {
private final String tableAliasPlaceHolder;
private final String columnAliasPrefix;
private final SqlLimiter sqlLimiter;
private final CQueryBuilderRawSql rawSqlHandler;
private final Binder binder;
private final boolean selectCountWithAlias;
private final CQueryHistorySupport historySupport;
private final CQueryDraftSupport draftSupport;
private final DatabasePlatform dbPlatform;
/**
* Create the SqlGenSelect.
*/
CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
this.dbPlatform = dbPlatform;
this.binder = binder;
this.draftSupport = draftSupport;
this.historySupport = historySupport;
this.tableAliasPlaceHolder = dbPlatform.getTableAliasPlaceHolder();
this.columnAliasPrefix = dbPlatform.getColumnAliasPrefix();
this.sqlLimiter = dbPlatform.getSqlLimiter();
this.rawSqlHandler = new CQueryBuilderRawSql(sqlLimiter, dbPlatform);
this.selectCountWithAlias = dbPlatform.isSelectCountWithAlias();
}
/**
* split the order by claus on the field delimiter and prefix each field with
* the relation name
*/
static String prefixOrderByFields(String name, String orderBy) {
StringBuilder sb = new StringBuilder();
for (String token : orderBy.split(",")) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(name);
sb.append(".");
sb.append(token.trim());
}
return sb.toString();
}
/**
* Build the delete query.
*/
<T> CQueryUpdate buildUpdateQuery(String type, OrmQueryRequest<T> request) {
SpiQuery<T> query = request.getQuery();
String rootTableAlias = query.getAlias();
query.setDelete();
CQueryPredicates predicates = new CQueryPredicates(binder, request);
CQueryPlan queryPlan = request.getQueryPlan();
if (queryPlan != null) {
// skip building the SqlTree and Sql string
predicates.prepare(false);
return new CQueryUpdate(type, request, predicates, queryPlan.getSql());
}
predicates.prepare(true);
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
String sql;
if (type.equals("Delete")) {
sql = buildDeleteSql(request, rootTableAlias, predicates, sqlTree);
} else {
sql = buildUpdateSql(request, rootTableAlias, predicates, sqlTree);
}
// cache the query plan
queryPlan = new CQueryPlan(request, sql, sqlTree, false, false, predicates.getLogWhereSql());
request.putQueryPlan(queryPlan);
return new CQueryUpdate(type, request, predicates, sql);
}
private <T> String buildDeleteSql(OrmQueryRequest<T> request, String rootTableAlias, CQueryPredicates predicates, SqlTree sqlTree) {
if (!sqlTree.isIncludeJoins()) {
// simple - delete from table ...
return aliasStrip(buildSql("delete", request, predicates, sqlTree).getSql());
}
// wrap as - delete from table where id in (select id ...)
String sql = buildSql(null, request, predicates, sqlTree).getSql();
sql = request.getBeanDescriptor().getDeleteByIdInSql() + "in (" + sql + ")";
String alias = (rootTableAlias == null) ? "t0" : rootTableAlias;
sql = aliasReplace(sql, alias);
return sql;
}
private <T> String buildUpdateSql(OrmQueryRequest<T> request, String rootTableAlias, CQueryPredicates predicates, SqlTree sqlTree) {
String updateClause = "update " + request.getBeanDescriptor().getBaseTable() + " set " + predicates.getDbUpdateClause();
if (!sqlTree.isIncludeJoins()) {
// simple - update table set ... where ...
return aliasStrip(buildSqlUpdate(updateClause, request, predicates, sqlTree).getSql());
}
// wrap as - update table set ... where id in (select id ...)
String sql = buildSqlUpdate(null, request, predicates, sqlTree).getSql();
sql = updateClause + " " + request.getBeanDescriptor().getWhereIdInSql() + "in (" + sql + ")";
String alias = (rootTableAlias == null) ? "t0" : rootTableAlias;
sql = aliasReplace(sql, alias);
return sql;
}
/**
* Strip the root table alias.
*/
private String aliasStrip(String sql) {
sql = StringHelper.replaceString(sql, "${RTA}.", "");
return StringHelper.replaceString(sql, " ${RTA}", "");
}
/**
* Replace the root table alias.
*/
private String aliasReplace(String sql, String replaceWith) {
sql = StringHelper.replaceString(sql, "${RTA}.", replaceWith + ".");
return StringHelper.replaceString(sql, "${RTA}", replaceWith);
}
CQueryFetchSingleAttribute buildFetchAttributeQuery(OrmQueryRequest<?> request) {
SpiQuery<?> query = request.getQuery();
query.setSingleAttribute();
CQueryPredicates predicates = new CQueryPredicates(binder, request);
CQueryPlan queryPlan = request.getQueryPlan();
if (queryPlan != null) {
predicates.prepare(false);
return new CQueryFetchSingleAttribute(request, predicates, queryPlan);
}
// use RawSql or generated Sql
predicates.prepare(true);
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
SqlLimitResponse s = buildSql(null, request, predicates, sqlTree);
queryPlan = new CQueryPlan(request, s.getSql(), sqlTree, false, s.isIncludesRowNumberColumn(), predicates.getLogWhereSql());
request.putQueryPlan(queryPlan);
return new CQueryFetchSingleAttribute(request, predicates, queryPlan);
}
/**
* Build the find ids query.
*/
<T> CQueryFetchSingleAttribute buildFetchIdsQuery(OrmQueryRequest<T> request) {
request.getQuery().setSelectId();
return buildFetchAttributeQuery(request);
}
/**
* Return the history support if this query needs it (is a 'as of' type query).
*/
private <T> CQueryHistorySupport getHistorySupport(SpiQuery<T> query) {
return query.getTemporalMode() != SpiQuery.TemporalMode.CURRENT ? historySupport : null;
}
/**
* Return the draft support (or null) for a 'asDraft' query.
*/
private <T> CQueryDraftSupport getDraftSupport(SpiQuery<T> query) {
return query.getTemporalMode() == SpiQuery.TemporalMode.DRAFT ? draftSupport : null;
}
/**
* Build the row count query.
*/
<T> CQueryRowCount buildRowCountQuery(OrmQueryRequest<T> request) {
SpiQuery<T> query = request.getQuery();
// always set the order by to null for row count query
query.setOrder(null);
query.setFirstRow(0);
query.setMaxRows(0);
ManyWhereJoins manyWhereJoins = query.getManyWhereJoins();
if (manyWhereJoins.isFormulaWithJoin()) {
query.select(manyWhereJoins.getFormulaProperties());
} else {
query.setSelectId();
}
CQueryPredicates predicates = new CQueryPredicates(binder, request);
CQueryPlan queryPlan = request.getQueryPlan();
if (queryPlan != null) {
// skip building the SqlTree and Sql string
predicates.prepare(false);
String sql = queryPlan.getSql();
return new CQueryRowCount(request, predicates, sql);
}
predicates.prepare(true);
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
if (SpiQuery.TemporalMode.CURRENT == query.getTemporalMode()) {
sqlTree.addSoftDeletePredicate(query);
}
boolean hasMany = sqlTree.hasMany();
String sqlSelect = "select count(*)";
if (hasMany) {
// need to count distinct id's ...
query.setSqlDistinct(true);
sqlSelect = null;
}
SqlLimitResponse s = buildSql(sqlSelect, request, predicates, sqlTree);
String sql = s.getSql();
if (hasMany || query.isRawSql()) {
sql = "select count(*) from ( " + sql + ")";
if (selectCountWithAlias) {
sql += " as c";
}
}
// cache the query plan
queryPlan = new CQueryPlan(request, sql, sqlTree, false, s.isIncludesRowNumberColumn(), predicates.getLogWhereSql());
request.putQueryPlan(queryPlan);
return new CQueryRowCount(request, predicates, sql);
}
/**
* Return the SQL Select statement as a String. Converts logical property
* names to physical deployment column names.
*/
<T> CQuery<T> buildQuery(OrmQueryRequest<T> request) {
CQueryPredicates predicates = new CQueryPredicates(binder, request);
CQueryPlan queryPlan = request.getQueryPlan();
if (queryPlan != null) {
// Reuse the query plan so skip generating SqlTree and SQL.
// We do prepare and bind the new parameters
predicates.prepare(false);
return new CQuery<>(request, predicates, queryPlan);
}
// RawSql or Generated Sql query
// Prepare the where, having and order by clauses.
// This also parses them from logical property names to
// database columns and determines 'includes'.
// We need to check these 'includes' for extra joins
// that are not included via select
predicates.prepare(true);
// Build the tree structure that represents the query.
SpiQuery<T> query = request.getQuery();
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
if (query.isAsOfQuery()) {
sqlTree.addAsOfTableAlias(query);
} else if (SpiQuery.TemporalMode.CURRENT == query.getTemporalMode()) {
sqlTree.addSoftDeletePredicate(query);
}
SqlLimitResponse res = buildSql(null, request, predicates, sqlTree);
boolean rawSql = request.isRawSql();
if (rawSql) {
queryPlan = new CQueryPlanRawSql(request, res, sqlTree, predicates.getLogWhereSql());
} else {
queryPlan = new CQueryPlan(request, res, sqlTree, false, predicates.getLogWhereSql());
}
BeanDescriptor<T> desc = request.getBeanDescriptor();
if (desc.isReadAuditing()) {
// log the query plan based bean type (i.e. ignoring query disabling for logging the sql/plan)
desc.getReadAuditLogger().queryPlan(new ReadAuditQueryPlan(desc.getFullName(), queryPlan.getAuditQueryKey(), queryPlan.getSql()));
}
// cache the query plan because we can reuse it and also
// gather query performance statistics based on it.
request.putQueryPlan(queryPlan);
return new CQuery<>(request, predicates, queryPlan);
}
/**
* Build the SqlTree.
* <p>
* The SqlTree is immutable after construction and so is safe to use by
* concurrent threads.
* </p>
* <p>
* The predicates is used to add additional joins that come from the where or
* order by clauses that are not already included for the select clause.
* </p>
*/
private SqlTree createSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
if (request.isNativeSql()) {
return createNativeSqlTree(request, predicates);
}
if (request.isRawSql()) {
return createRawSqlSqlTree(request, predicates);
}
return new SqlTreeBuilder(tableAliasPlaceHolder, columnAliasPrefix, request, predicates, historySupport, draftSupport).build();
}
/**
* Create the SqlTree by reading the ResultSetMetaData and mapping table/columns to bean property paths.
*/
private SqlTree createNativeSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates) {
SpiQuery<?> query = request.getQuery();
// parse named parameters returning the final sql to execute
String sql = predicates.parseBindParams(query.getNativeSql());
query.setGeneratedSql(sql);
Connection connection = request.getTransaction().getConnection();
BeanDescriptor<?> desc = request.getBeanDescriptor();
try {
PreparedStatement statement = connection.prepareStatement(sql);
predicates.bind(statement, connection);
List<String> propertyNames = new ArrayList<>();
ResultSet resultSet = statement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols = 1 + metaData.getColumnCount();
for (int i = 1; i < cols; i++) {
String tableName = metaData.getTableName(i).toLowerCase();
String columnName = metaData.getColumnName(i).toLowerCase();
String path = desc.findBeanPath(tableName, columnName);
if (path != null) {
propertyNames.add(path);
} else {
propertyNames.add(RawSqlBuilder.IGNORE_COLUMN);
}
}
RawSql rawSql = RawSqlBuilder.resultSet(resultSet, propertyNames.toArray(new String[propertyNames.size()]));
query.setRawSql(rawSql);
return createRawSqlSqlTree(request, predicates);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private SqlTree createRawSqlSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates) {
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
ColumnMapping columnMapping = request.getQuery().getRawSql().getColumnMapping();
PathProperties pathProps = new PathProperties();
// convert list of columns into (tree like) PathProperties
Iterator<Column> it = columnMapping.getColumns();
while (it.hasNext()) {
RawSql.ColumnMapping.Column column = it.next();
String propertyName = column.getPropertyName();
if (!RawSqlBuilder.IGNORE_COLUMN.equals(propertyName)) {
ElPropertyValue el = descriptor.getElGetValue(propertyName);
if (el == null && propertyName.endsWith("Id")) {
// try default naming convention for foreign key columns
String foreignIdPath = assocOneIdPath(propertyName);
el = descriptor.getElGetValue(foreignIdPath);
if (el != null) {
propertyName = foreignIdPath;
}
}
if (el == null) {
throw new PersistenceException("Property [" + propertyName + "] not found on " + descriptor.getFullName());
} else {
BeanProperty beanProperty = el.getBeanProperty();
if (beanProperty.isId()) {
if (propertyName.contains(".")) {
// For @Id properties we chop off the last part of the path
propertyName = SplitName.parent(propertyName);
}
} else if (beanProperty.isDiscriminator()) {
propertyName = SplitName.parent(propertyName);
} else if (beanProperty instanceof BeanPropertyAssocOne<?>) {
String msg = "Column [" + column.getDbColumn() + "] mapped to complex Property[" + propertyName + "]";
msg += ". It should be mapped to a simple property (probably the Id property). ";
throw new PersistenceException(msg);
}
if (propertyName != null) {
String[] pathProp = SplitName.split(propertyName);
pathProps.addToPath(pathProp[0], pathProp[1]);
}
}
}
}
OrmQueryDetail detail = new OrmQueryDetail();
// transfer PathProperties into OrmQueryDetail
for (PathProperties.Props props : pathProps.getPathProps()) {
detail.fetch(props.getPath(), props.getProperties());
}
// check if @Id property included in RawSql
boolean rawNoId = true;
BeanProperty idProperty = descriptor.getIdProperty();
if (idProperty != null && columnMapping.contains(idProperty.getName())) {
// contains the @Id property for the root level bean
rawNoId = false;
}
// build SqlTree based on OrmQueryDetail of the RawSql
return new SqlTreeBuilder(request, predicates, detail, rawNoId).build();
}
/**
* Return a path for a foreign key property using the default naming convention.
*/
private String assocOneIdPath(String propertyName) {
return propertyName.substring(0, propertyName.length() - 2) + ".id";
}
/**
* Return the SQL response with row limiting (when not an update statement).
*/
private SqlLimitResponse buildSql(String selectClause, OrmQueryRequest<?> request, CQueryPredicates predicates, SqlTree select) {
return buildSql(selectClause, request, predicates, select, false);
}
/**
* Return the SQL response for update statement (stripping table alias for find by id expression).
*/
private SqlLimitResponse buildSqlUpdate(String selectClause, OrmQueryRequest<?> request, CQueryPredicates predicates, SqlTree select) {
return buildSql(selectClause, request, predicates, select, true);
}
private SqlLimitResponse buildSql(String selectClause, OrmQueryRequest<?> request, CQueryPredicates predicates, SqlTree select, boolean stripAlias) {
SpiQuery<?> query = request.getQuery();
if (query.isNativeSql()) {
return new SqlLimitResponse(query.getGeneratedSql(), false);
}
if (query.isRawSql()) {
return rawSqlHandler.buildSql(request, predicates, query.getRawSql().getSql());
}
BeanPropertyAssocMany<?> manyProp = select.getManyProperty();
boolean useSqlLimiter = false;
StringBuilder sb = new StringBuilder(500);
String dbOrderBy = predicates.getDbOrderBy();
if (selectClause != null) {
sb.append(selectClause);
} else {
useSqlLimiter = (query.hasMaxRowsOrFirstRow() && manyProp == null);
if (!useSqlLimiter) {
sb.append("select ");
if (query.isDistinctQuery()) {
sb.append("distinct ");
}
}
sb.append(select.getSelectSql());
if (query.isDistinctQuery() && dbOrderBy != null && !query.isSingleAttribute()) {
// add the orderBy columns to the select clause (due to distinct)
sb.append(", ").append(convertDbOrderByForSelect(dbOrderBy));
}
}
if (selectClause == null || !selectClause.startsWith("update")) {
sb.append(" from ");
sb.append(select.getFromSql());
}
String inheritanceWhere = select.getInheritanceWhereSql();
boolean hasWhere = false;
if (!inheritanceWhere.isEmpty()) {
sb.append(" where");
sb.append(inheritanceWhere);
hasWhere = true;
}
if (query.isAsOfBaseTable() && !historySupport.isStandardsBased()) {
hasWhere = appendWhere(hasWhere, sb);
sb.append(historySupport.getAsOfPredicate(request.getBaseTableAlias()));
}
if (request.isFindById() || query.getId() != null) {
appendWhere(hasWhere, sb);
BeanDescriptor<?> desc = request.getBeanDescriptor();
String idSql = desc.getIdBinderIdSql();
if (idSql.isEmpty()) {
throw new IllegalStateException("Executing FindById query on entity bean " + desc.getName()
+ " that doesn't have an @Id property??");
}
if (stripAlias) {
// strip the table alias for use in update statement
idSql = StringHelper.replaceString(idSql, "t0.", "");
}
sb.append(idSql).append(" ");
hasWhere = true;
}
String dbWhere = predicates.getDbWhere();
if (!isEmpty(dbWhere)) {
if (!hasWhere) {
hasWhere = true;
sb.append(" where ");
} else {
sb.append(" and ");
}
sb.append(dbWhere);
}
String dbFilterMany = predicates.getDbFilterMany();
if (!isEmpty(dbFilterMany)) {
if (!hasWhere) {
hasWhere = true;
sb.append(" where ");
} else {
sb.append("and ");
}
sb.append(dbFilterMany);
}
if (!query.isIncludeSoftDeletes()) {
List<String> softDeletePredicates = query.getSoftDeletePredicates();
if (softDeletePredicates != null) {
if (!hasWhere) {
sb.append(" where ");
} else {
sb.append("and ");
}
for (int i = 0; i < softDeletePredicates.size(); i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(softDeletePredicates.get(i));
}
}
}
String groupBy = select.getGroupBy();
if (groupBy != null) {
sb.append(" group by ").append(groupBy);
}
String dbHaving = predicates.getDbHaving();
if (!isEmpty(dbHaving)) {
sb.append(" having ").append(dbHaving);
}
if (dbOrderBy != null) {
sb.append(" order by ").append(dbOrderBy);
}
if (useSqlLimiter) {
// use LIMIT/OFFSET, ROW_NUMBER() or rownum type SQL query limitation
SqlLimitRequest r = new OrmQueryLimitRequest(sb.toString(), dbOrderBy, query, dbPlatform);
return sqlLimiter.limit(r);
} else {
return new SqlLimitResponse(dbPlatform.completeSql(sb.toString(), query), false);
}
}
/**
* Append where or and based on the hasWhere flag.
*/
private boolean appendWhere(boolean hasWhere, StringBuilder sb) {
if (hasWhere) {
sb.append(" and ");
} else {
sb.append(" where ");
}
return true;
}
/**
* Convert the dbOrderBy clause to be safe for adding to select. This is done when 'distinct' is
* used.
*/
private String convertDbOrderByForSelect(String dbOrderBy) {
// just remove the ASC and DESC keywords
return dbOrderBy.replaceAll("(?i)\\b asc\\b|\\b desc\\b", "");
}
private boolean isEmpty(String s) {
return s == null || s.isEmpty();
}
}
@@ -0,0 +1,148 @@
package io.ebeaninternal.server.query;
import io.ebean.RawSql;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebean.config.dbplatform.SqlLimiter;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.querydefn.OrmQueryLimitRequest;
import io.ebeaninternal.server.util.BindParamsParser;
class CQueryBuilderRawSql {
private final SqlLimiter sqlLimiter;
private final DatabasePlatform dbPlatform;
CQueryBuilderRawSql(SqlLimiter sqlLimiter, DatabasePlatform dbPlatform) {
this.sqlLimiter = sqlLimiter;
this.dbPlatform = dbPlatform;
}
/**
* Build the full SQL Select statement for the request.
*/
SqlLimitResponse buildSql(OrmQueryRequest<?> request, CQueryPredicates predicates, RawSql.Sql rsql) {
if (rsql == null) {
// this is a ResultSet based RawSql query - just use some placeholder for the SQL
return new SqlLimitResponse("--ResultSetBasedRawSql", false);
}
if (!rsql.isParsed()) {
String sql = rsql.getUnparsedSql();
BindParams bindParams = request.getQuery().getBindParams();
if (bindParams != null && bindParams.requiresNamedParamsPrepare()) {
// convert named parameters into positioned parameters
sql = BindParamsParser.parse(bindParams, sql);
}
return new SqlLimitResponse(sql, false);
}
String orderBy = getOrderBy(predicates, rsql);
// build the actual sql String
String sql = buildMainQuery(orderBy, request, predicates, rsql);
SpiQuery<?> query = request.getQuery();
if (query.hasMaxRowsOrFirstRow() && sqlLimiter != null) {
// wrap with a limit offset or ROW_NUMBER() etc
return sqlLimiter.limit(new OrmQueryLimitRequest(sql, orderBy, query, dbPlatform));
} else {
// add back select keyword (it was removed to support sqlQueryLimiter)
String prefix = "select " + (rsql.isDistinct() ? "distinct " : "");
sql = prefix + sql;
return new SqlLimitResponse(sql, false);
}
}
private String buildMainQuery(String orderBy, OrmQueryRequest<?> request, CQueryPredicates predicates, RawSql.Sql sql) {
StringBuilder sb = new StringBuilder();
sb.append(sql.getPreFrom());
sb.append(" ");
String s = sql.getPreWhere();
BindParams bindParams = request.getQuery().getBindParams();
if (bindParams != null && bindParams.requiresNamedParamsPrepare()) {
// convert named parameters into positioned parameters
// Named Parameters only allowed prior to dynamic where
// clause (so not allowed in having etc - use unparsed)
s = BindParamsParser.parse(bindParams, s);
}
sb.append(s);
sb.append(" ");
String dynamicWhere = null;
if (request.getQuery().getId() != null) {
// need to convert this as well. This avoids the
// assumption that id has its proper dbColumn assigned
// which may change if using multiple raw sql statements
// against the same bean.
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
//FIXME: I think this is broken... needs to be logical
// and then parsed for RawSqlSelect...
dynamicWhere = descriptor.getIdBinderIdSql();
}
String dbWhere = predicates.getDbWhere();
if (!isEmpty(dbWhere)) {
if (dynamicWhere == null) {
dynamicWhere = dbWhere;
} else {
dynamicWhere += " and " + dbWhere;
}
}
if (!isEmpty(dynamicWhere)) {
if (sql.isAndWhereExpr()) {
sb.append(" and ");
} else {
sb.append(" where ");
}
sb.append(dynamicWhere);
sb.append(" ");
}
String preHaving = sql.getPreHaving();
if (!isEmpty(preHaving)) {
sb.append(preHaving);
sb.append(" ");
}
String dbHaving = predicates.getDbHaving();
if (!isEmpty(dbHaving)) {
sb.append(" ");
if (sql.isAndHavingExpr()) {
sb.append("and ");
} else {
sb.append("having ");
}
sb.append(dbHaving);
sb.append(" ");
}
if (!isEmpty(orderBy)) {
sb.append(" ").append(sql.getOrderByPrefix()).append(" ").append(orderBy);
}
return sb.toString().trim();
}
private boolean isEmpty(String s) {
return s == null || s.isEmpty();
}
private String getOrderBy(CQueryPredicates predicates, RawSql.Sql sql) {
String orderBy = predicates.getDbOrderBy();
if (orderBy != null) {
return orderBy;
} else {
return sql.getOrderBy();
}
}
}
@@ -0,0 +1,28 @@
package io.ebeaninternal.server.query;
import java.util.Map;
/**
* Support 'asDraft' queries.
*/
class CQueryDraftSupport {
/**
* The mapping of base tables to their matching 'draft' table.
*/
private final Map<String, String> tableMap;
CQueryDraftSupport(Map<String, String> tableMap) {
this.tableMap = tableMap;
}
/**
* Return the draft table associated to the base table.
* <p>
* This returns null for entities that are not draftable and in that case
* the usual base table is used.
*/
String getDraftTable(String table) {
return tableMap.get(table);
}
}
@@ -0,0 +1,502 @@
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebean.ValuePair;
import io.ebean.Version;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.bean.ObjectGraphNode;
import io.ebean.config.ServerConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.DiffHelp;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.lib.util.Str;
import io.ebeaninternal.server.persist.Binder;
import io.ebeaninternal.server.transaction.TransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Handles the Object Relational fetching.
*/
public class CQueryEngine {
private static final Logger logger = LoggerFactory.getLogger(CQueryEngine.class);
private static final int defaultSecondaryQueryBatchSize = 100;
private static final String T0 = "t0";
private final int defaultFetchSizeFindList;
private final int defaultFetchSizeFindEach;
private final boolean forwardOnlyHintOnFindIterate;
private final CQueryBuilder queryBuilder;
private final CQueryHistorySupport historySupport;
public CQueryEngine(ServerConfig serverConfig, DatabasePlatform dbPlatform, Binder binder, Map<String, String> asOfTableMapping, Map<String, String> draftTableMap) {
this.defaultFetchSizeFindEach = serverConfig.getJdbcFetchSizeFindEach();
this.defaultFetchSizeFindList = serverConfig.getJdbcFetchSizeFindList();
this.forwardOnlyHintOnFindIterate = dbPlatform.isForwardOnlyHintOnFindIterate();
this.historySupport = new CQueryHistorySupport(dbPlatform.getHistorySupport(), asOfTableMapping, serverConfig.getAsOfSysPeriod());
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, historySupport, new CQueryDraftSupport(draftTableMap));
}
public <T> CQuery<T> buildQuery(OrmQueryRequest<T> request) {
return queryBuilder.buildQuery(request);
}
public <T> int delete(OrmQueryRequest<T> request) {
CQueryUpdate query = queryBuilder.buildUpdateQuery("Delete", request);
return executeUpdate(request, query);
}
public <T> int update(OrmQueryRequest<T> request) {
CQueryUpdate query = queryBuilder.buildUpdateQuery("Update", request);
return executeUpdate(request, query);
}
private <T> int executeUpdate(OrmQueryRequest<T> request, CQueryUpdate query) {
try {
int rows = query.execute();
if (request.isLogSql()) {
String logSql = query.getGeneratedSql();
if (TransactionManager.SQL_LOGGER.isTraceEnabled()) {
logSql = Str.add(logSql, "; --bind(", query.getBindLog(), ") rows:", String.valueOf(rows));
}
request.logSql(logSql);
}
return rows;
} catch (SQLException e) {
throw CQuery.createPersistenceException(e, request.getTransaction(), query.getBindLog(), query.getGeneratedSql());
}
}
/**
* Build and execute the findSingleAttributeList query.
*/
public <A> List<A> findSingleAttributeList(OrmQueryRequest<?> request) {
CQueryFetchSingleAttribute rcQuery = queryBuilder.buildFetchAttributeQuery(request);
return findAttributeList(request, rcQuery);
}
@SuppressWarnings("unchecked")
private <A> List<A> findAttributeList(OrmQueryRequest<?> request, CQueryFetchSingleAttribute rcQuery) {
try {
List<A> list = (List<A>) rcQuery.findList();
if (request.isLogSql()) {
logGeneratedSql(request, rcQuery.getGeneratedSql(), rcQuery.getBindLog());
}
if (request.isLogSummary()) {
request.getTransaction().logSummary(rcQuery.getSummary());
}
return list;
} catch (SQLException e) {
throw CQuery.createPersistenceException(e, request.getTransaction(), rcQuery.getBindLog(), rcQuery.getGeneratedSql());
}
}
/**
* Build and execute the find Id's query.
*/
public <A> List<A> findIds(OrmQueryRequest<?> request) {
CQueryFetchSingleAttribute rcQuery = queryBuilder.buildFetchIdsQuery(request);
return findAttributeList(request, rcQuery);
}
private <T> void logGeneratedSql(OrmQueryRequest<T> request, String sql, String bindLog) {
String logSql = sql;
if (TransactionManager.SQL_LOGGER.isTraceEnabled()) {
logSql = Str.add(logSql, "; --bind(", bindLog, ")");
}
request.logSql(logSql);
}
/**
* Build and execute the row count query.
*/
public <T> int findRowCount(OrmQueryRequest<T> request) {
CQueryRowCount rcQuery = queryBuilder.buildRowCountQuery(request);
try {
int rowCount = rcQuery.findRowCount();
if (request.isLogSql()) {
logGeneratedSql(request, rcQuery.getGeneratedSql(), rcQuery.getBindLog());
}
if (request.isLogSummary()) {
request.getTransaction().logSummary(rcQuery.getSummary());
}
if (request.getQuery().isFutureFetch()) {
request.getTransaction().end();
}
return rowCount;
} catch (SQLException e) {
throw CQuery.createPersistenceException(e, request.getTransaction(), rcQuery.getBindLog(), rcQuery.getGeneratedSql());
}
}
/**
* Read many beans using an iterator (except you need to close() the iterator
* when you have finished).
*/
public <T> QueryIterator<T> findIterate(OrmQueryRequest<T> request) {
CQuery<T> cquery = queryBuilder.buildQuery(request);
request.setCancelableQuery(cquery);
try {
if (defaultFetchSizeFindEach > 0) {
request.setDefaultFetchBuffer(defaultFetchSizeFindEach);
}
if (!cquery.prepareBindExecuteQueryForwardOnly(forwardOnlyHintOnFindIterate)) {
// query has been cancelled already
logger.trace("Future fetch already cancelled");
return null;
}
if (request.isLogSql()) {
logSql(cquery);
}
// first check batch sizes set on query joins
int iterateBufferSize = request.getSecondaryQueriesMinBatchSize(defaultSecondaryQueryBatchSize);
if (iterateBufferSize < 1) {
// not set on query joins so check if batch size set on query itself
int queryBatch = request.getQuery().getLazyLoadBatchSize();
if (queryBatch > 0) {
iterateBufferSize = queryBatch;
}
}
QueryIterator<T> readIterate = cquery.readIterate(iterateBufferSize, request);
if (request.isLogSummary()) {
logFindManySummary(cquery);
}
if (request.isAuditReads()) {
// indicates we need to audit as the iterator progresses
cquery.auditFindIterate();
}
return readIterate;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
}
}
/**
* Execute the find versions query returning version beans.
*/
public <T> List<Version<T>> findVersions(OrmQueryRequest<T> request) {
SpiQuery<T> query = request.getQuery();
String sysPeriodLower = getSysPeriodLower(query);
if (query.isVersionsBetween() && !historySupport.isStandardsBased()) {
// just add as normal predicates using the lower bound
query.where().gt(sysPeriodLower, query.getVersionStart());
query.where().lt(sysPeriodLower, query.getVersionEnd());
}
// order by id asc, lower sys period desc
query.orderBy().asc(request.getBeanDescriptor().getIdProperty().getName());
query.orderBy().desc(sysPeriodLower);
CQuery<T> cquery = queryBuilder.buildQuery(request);
try {
cquery.prepareBindExecuteQuery();
if (request.isLogSql()) {
logSql(cquery);
}
List<Version<T>> versions = cquery.readVersions();
// just order in memory rather than use NULLS LAST as that
// is not universally supported, not expect huge list here
Collections.sort(versions, OrderVersionDesc.INSTANCE);
deriveVersionDiffs(versions, request);
if (request.isLogSummary()) {
logFindManySummary(cquery);
}
if (request.isAuditReads()) {
cquery.auditFindMany();
}
return versions;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
} finally {
if (cquery != null) {
cquery.close();
}
}
}
private <T> void deriveVersionDiffs(List<Version<T>> versions, OrmQueryRequest<T> request) {
BeanDescriptor<T> descriptor = request.getBeanDescriptor();
if (!versions.isEmpty()) {
Version<T> current = versions.get(0);
if (versions.size() > 1) {
for (int i = 1; i < versions.size(); i++) {
Version<T> next = versions.get(i);
deriveVersionDiff(current, next, descriptor);
current = next;
}
}
// put an empty map into the last one
current.setDiff(new LinkedHashMap<>());
}
}
private <T> void deriveVersionDiff(Version<T> current, Version<T> prior, BeanDescriptor<T> descriptor) {
Map<String, ValuePair> diff = DiffHelp.diff(current.getBean(), prior.getBean(), descriptor);
current.setDiff(diff);
}
/**
* Return the lower sys_period given the table alias of the query or default.
*/
private <T> String getSysPeriodLower(SpiQuery<T> query) {
String rootTableAlias = query.getAlias();
if (rootTableAlias == null) {
rootTableAlias = T0;
}
return historySupport.getSysPeriodLower(rootTableAlias);
}
/**
* Find a list/map/set of beans.
*/
<T> BeanCollection<T> findMany(OrmQueryRequest<T> request) {
SpiQuery<T> query = request.getQuery();
if (!query.isDistinct() && (query.getMaxRows() > 1 || query.getFirstRow() > 0)) {
// deemed to be a be a paging query - check that the order by contains
// the id property to ensure unique row ordering for predicable paging
// but only in case, this is not a distinct query
request.getBeanDescriptor().appendOrderById(query);
}
CQuery<T> cquery = queryBuilder.buildQuery(request);
request.setCancelableQuery(cquery);
try {
if (defaultFetchSizeFindList > 0) {
request.setDefaultFetchBuffer(defaultFetchSizeFindList);
}
if (!cquery.prepareBindExecuteQuery()) {
// query has been cancelled already
logger.trace("Future fetch already cancelled");
return null;
}
if (request.isLogSql()) {
logSql(cquery);
}
BeanCollection<T> beanCollection = cquery.readCollection();
if (request.isLogSummary()) {
logFindManySummary(cquery);
}
if (request.isAuditReads()) {
cquery.auditFindMany();
}
request.executeSecondaryQueries(false);
return beanCollection;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
} finally {
if (cquery != null) {
cquery.close();
}
if (query.isFutureFetch()) {
// end the transaction for futureFindIds
// as it had it's own transaction
logger.debug("Future fetch completed!");
request.getTransaction().end();
}
}
}
/**
* Find and return a single bean using its unique id.
*/
@SuppressWarnings("unchecked")
public <T> T find(OrmQueryRequest<T> request) {
EntityBean bean = null;
CQuery<T> cquery = queryBuilder.buildQuery(request);
try {
cquery.prepareBindExecuteQuery();
if (request.isLogSql()) {
logSql(cquery);
}
if (cquery.readBean()) {
bean = cquery.next();
}
if (request.isLogSummary()) {
logFindBeanSummary(cquery);
}
if (request.isAuditReads()) {
cquery.auditFind(bean);
}
request.executeSecondaryQueries(false);
return (T) bean;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
} finally {
cquery.close();
}
}
/**
* Log the generated SQL to the transaction log.
*/
private void logSql(CQuery<?> query) {
String sql = query.getGeneratedSql();
if (TransactionManager.SQL_LOGGER.isTraceEnabled()) {
sql = Str.add(sql, "; --bind(", query.getBindLog(), ")");
}
query.getTransaction().logSql(sql);
}
/**
* Log the FindById summary to the transaction log.
*/
private void logFindBeanSummary(CQuery<?> q) {
SpiQuery<?> query = q.getQueryRequest().getQuery();
String loadMode = query.getLoadMode();
String loadDesc = query.getLoadDescription();
String lazyLoadProp = query.getLazyLoadProperty();
ObjectGraphNode node = query.getParentNode();
String originKey;
if (node == null || node.getOriginQueryPoint() == null) {
originKey = null;
} else {
originKey = node.getOriginQueryPoint().getKey();
}
StringBuilder msg = new StringBuilder(200);
msg.append("FindBean ");
if (loadMode != null) {
msg.append("mode[").append(loadMode).append("] ");
}
msg.append("type[").append(q.getBeanName()).append("] ");
if (query.isAutoTuned()) {
msg.append("tuned[true] ");
}
if (query.isAsDraft()) {
msg.append(" draft[true] ");
}
if (originKey != null) {
msg.append("origin[").append(originKey).append("] ");
}
if (lazyLoadProp != null) {
msg.append("lazyLoadProp[").append(lazyLoadProp).append("] ");
}
if (loadDesc != null) {
msg.append("load[").append(loadDesc).append("] ");
}
msg.append("exeMicros[").append(q.getQueryExecutionTimeMicros());
msg.append("] rows[").append(q.getLoadedRowDetail());
msg.append("] bind[").append(q.getBindLog()).append("]");
q.getTransaction().logSummary(msg.toString());
}
/**
* Log the FindMany to the transaction log.
*/
private void logFindManySummary(CQuery<?> q) {
SpiQuery<?> query = q.getQueryRequest().getQuery();
String loadMode = query.getLoadMode();
String loadDesc = query.getLoadDescription();
String lazyLoadProp = query.getLazyLoadProperty();
ObjectGraphNode node = query.getParentNode();
String originKey;
if (node == null || node.getOriginQueryPoint() == null) {
originKey = null;
} else {
originKey = node.getOriginQueryPoint().getKey();
}
StringBuilder msg = new StringBuilder(200);
msg.append("FindMany ");
if (loadMode != null) {
msg.append("mode[").append(loadMode).append("] ");
}
msg.append("type[").append(q.getBeanName()).append("] ");
if (query.isAutoTuned()) {
msg.append("tuned[true] ");
}
if (query.isAsDraft()) {
msg.append(" draft[true] ");
}
if (originKey != null) {
msg.append("origin[").append(originKey).append("] ");
}
if (lazyLoadProp != null) {
msg.append("lazyLoadProp[").append(lazyLoadProp).append("] ");
}
if (loadDesc != null) {
msg.append("load[").append(loadDesc).append("] ");
}
msg.append("exeMicros[").append(q.getQueryExecutionTimeMicros());
msg.append("] rows[").append(q.getLoadedRowDetail());
msg.append("] predicates[").append(q.getLogWhereSql());
msg.append("] bind[").append(q.getBindLog()).append("]");
q.getTransaction().logSummary(msg.toString());
}
}
@@ -0,0 +1,172 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.type.RsetDataReader;
import io.ebeaninternal.server.type.ScalarType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Base compiled query request for single attribute queries.
*/
class CQueryFetchSingleAttribute {
private static final Logger logger = LoggerFactory.getLogger(CQueryFetchSingleAttribute.class);
/**
* The overall find request wrapper object.
*/
private final OrmQueryRequest<?> request;
private final BeanDescriptor<?> desc;
private final SpiQuery<?> query;
/**
* Where clause predicates.
*/
private final CQueryPredicates predicates;
/**
* The final sql that is generated.
*/
private final String sql;
private RsetDataReader dataReader;
/**
* The statement used to create the resultSet.
*/
private PreparedStatement pstmt;
private String bindLog;
private int executionTimeMicros;
private int rowCount;
private final ScalarType<Object> scalarType;
/**
* Create the Sql select based on the request.
*/
CQueryFetchSingleAttribute(OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryPlan plan) {
this.request = request;
this.query = request.getQuery();
this.sql = plan.getSql();
this.desc = request.getBeanDescriptor();
this.predicates = predicates;
this.scalarType = plan.getSingleProperty().getScalarType();
query.setGeneratedSql(sql);
}
/**
* Return a summary description of this query.
*/
protected String getSummary() {
StringBuilder sb = new StringBuilder(80);
sb.append("FindAttr exeMicros[").append(executionTimeMicros)
.append("] rows[").append(rowCount)
.append("] type[").append(desc.getName())
.append("] predicates[").append(predicates.getLogWhereSql())
.append("] bind[").append(bindLog).append("]");
return sb.toString();
}
/**
* Execute the query returning the row count.
*/
protected List<Object> findList() throws SQLException {
long startNano = System.nanoTime();
try {
prepareExecute();
List<Object> result = new ArrayList<>();
while (dataReader.next()) {
result.add(scalarType.read(dataReader));
dataReader.resetColumnPosition();
rowCount++;
}
long exeNano = System.nanoTime() - startNano;
executionTimeMicros = (int) exeNano / 1000;
return result;
} finally {
close();
}
}
/**
* Return the bind log.
*/
protected String getBindLog() {
return bindLog;
}
/**
* Return the generated sql.
*/
protected String getGeneratedSql() {
return sql;
}
private void prepareExecute() throws SQLException {
SpiTransaction t = request.getTransaction();
Connection conn = t.getInternalConnection();
pstmt = conn.prepareStatement(sql);
if (query.getBufferFetchSizeHint() > 0) {
pstmt.setFetchSize(query.getBufferFetchSizeHint());
}
if (query.getTimeout() > 0) {
pstmt.setQueryTimeout(query.getTimeout());
}
bindLog = predicates.bind(pstmt, conn);
dataReader = new RsetDataReader(request.getDataTimeZone(), pstmt.executeQuery());
}
/**
* Close the resources.
* <p>
* The jdbc resultSet and statement need to be closed. Its important that
* this method is called.
* </p>
*/
private void close() {
try {
if (dataReader != null) {
dataReader.close();
dataReader = null;
}
} catch (SQLException e) {
logger.error("Error closing DataReader", e);
}
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
logger.error("Error closing PreparedStatement", e);
}
}
}
@@ -0,0 +1,70 @@
package io.ebeaninternal.server.query;
import io.ebean.config.dbplatform.DbHistorySupport;
import java.util.Map;
/**
* Helper to support history functions.
*/
class CQueryHistorySupport {
/**
* The DB specific support.
*/
private final DbHistorySupport dbHistorySupport;
/**
* The mapping of base tables to their matching 'with history' views.
*/
private final Map<String, String> asOfTableMap;
/**
* The sys period column.
*/
private final String sysPeriod;
CQueryHistorySupport(DbHistorySupport dbHistorySupport, Map<String, String> asOfTableMap, String sysPeriod) {
this.dbHistorySupport = dbHistorySupport;
this.asOfTableMap = asOfTableMap;
this.sysPeriod = sysPeriod;
}
/**
* Return true if the underlying history support is standards based.
*/
boolean isStandardsBased() {
return dbHistorySupport.isStandardsBased();
}
/**
* Return the 'as of' history view for the given base table.
*/
String getAsOfView(String table) {
return asOfTableMap.get(table);
}
/**
* Return the lower bound column.
*/
String getSysPeriodLower(String tableAlias) {
return dbHistorySupport.getSysPeriodLower(tableAlias, sysPeriod);
}
/**
* Return the upper bound column.
*/
String getSysPeriodUpper(String tableAlias) {
return dbHistorySupport.getSysPeriodUpper(tableAlias, sysPeriod);
}
/**
* Return the predicate appended to the end of the query.
* <p>
* Note used for Oracle total recall etc with the more standard approach.
*/
String getAsOfPredicate(String tableAlias) {
return dbHistorySupport.getAsOfPredicate(tableAlias, sysPeriod);
}
}
@@ -0,0 +1,46 @@
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebeaninternal.server.core.OrmQueryRequest;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
/**
* QueryIterator that does not require a buffer for secondary queries.
*/
class CQueryIteratorSimple<T> implements QueryIterator<T> {
private final CQuery<T> cquery;
private final OrmQueryRequest<T> request;
CQueryIteratorSimple(CQuery<T> cquery, OrmQueryRequest<T> request) {
this.cquery = cquery;
this.request = request;
}
public boolean hasNext() {
try {
request.flushPersistenceContextOnIterate();
return cquery.hasNext();
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
}
}
@SuppressWarnings("unchecked")
public T next() {
return (T) cquery.next();
}
public void close() {
cquery.updateExecutionStatistics();
cquery.close();
request.endTransIfRequired();
}
public void remove() {
throw new PersistenceException("Remove not allowed");
}
}
@@ -0,0 +1,66 @@
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebeaninternal.server.core.OrmQueryRequest;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* A QueryIterator that uses a buffer to execute secondary queries periodically.
*/
class CQueryIteratorWithBuffer<T> implements QueryIterator<T> {
private final CQuery<T> cquery;
private final int bufferSize;
private final OrmQueryRequest<T> request;
private final ArrayList<T> buffer;
private boolean moreToLoad = true;
CQueryIteratorWithBuffer(CQuery<T> cquery, OrmQueryRequest<T> request, int bufferSize) {
this.cquery = cquery;
this.request = request;
this.bufferSize = bufferSize;
this.buffer = new ArrayList<>(bufferSize);
}
@SuppressWarnings("unchecked")
public boolean hasNext() {
try {
if (buffer.isEmpty() && moreToLoad) {
// load buffer
request.flushPersistenceContextOnIterate();
int i = -1;
while (moreToLoad && ++i < bufferSize) {
if (cquery.hasNext()) {
buffer.add((T) cquery.next());
} else {
moreToLoad = false;
}
}
request.executeSecondaryQueries(true);
}
return !buffer.isEmpty();
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
}
}
public T next() {
return buffer.remove(0);
}
public void close() {
cquery.updateExecutionStatistics();
cquery.close();
request.endTransIfRequired();
}
public void remove() {
throw new PersistenceException("Remove not allowed");
}
}
@@ -0,0 +1,77 @@
package io.ebeaninternal.server.query;
import io.ebean.OrderBy;
import io.ebean.OrderBy.Property;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import io.ebeaninternal.server.deploy.id.IdBinder;
import io.ebeaninternal.server.el.ElPropertyValue;
import java.util.List;
/**
* Creates the order by expression clause.
*/
class CQueryOrderBy {
private final BeanDescriptor<?> desc;
private final SpiQuery<?> query;
/**
* Create the logical order by clause.
*/
public static String parse(BeanDescriptor<?> desc, SpiQuery<?> query) {
return new CQueryOrderBy(desc, query).parseInternal();
}
private CQueryOrderBy(BeanDescriptor<?> desc, SpiQuery<?> query) {
this.desc = desc;
this.query = query;
}
private String parseInternal() {
OrderBy<?> orderBy = query.getOrderBy();
if (orderBy == null) {
return null;
}
StringBuilder sb = new StringBuilder();
List<Property> properties = orderBy.getProperties();
if (properties.isEmpty()) {
// order by clause removed by filterMany()
return null;
}
for (int i = 0; i < properties.size(); i++) {
if (i > 0) {
sb.append(", ");
}
Property p = properties.get(i);
String expression = parseProperty(p);
sb.append(expression);
}
return sb.toString();
}
private String parseProperty(Property p) {
String propName = p.getProperty();
ElPropertyValue el = desc.getElGetValue(propName);
if (el == null) {
return p.toStringFormat();
}
BeanProperty beanProperty = el.getBeanProperty();
if (beanProperty instanceof BeanPropertyAssoc<?>) {
BeanPropertyAssoc<?> ap = (BeanPropertyAssoc<?>) beanProperty;
IdBinder idBinder = ap.getTargetDescriptor().getIdBinder();
return idBinder.getOrderBy(el.getElName(), p.isAscending());
}
return p.toStringFormat();
}
}
@@ -0,0 +1,253 @@
package io.ebeaninternal.server.query;
import io.ebean.bean.ObjectGraphNode;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebeaninternal.api.CQueryPlanKey;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.core.timezone.DataTimeZone;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.query.CQueryPlanStats.Snapshot;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.type.DataReader;
import io.ebeaninternal.server.type.RsetDataReader;
import io.ebeaninternal.server.util.Md5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Represents a query for a given SQL statement.
* <p>
* This can be executed multiple times with different bind parameters.
* </p>
* <p>
* That is, the sql including the where clause, order by clause etc must be
* exactly the same to share the same query plan with the only difference being
* bind values.
* </p>
* <p>
* This is useful in that is common in OLTP type applications that the same
* query will be executed quite a lot just with different bind values. With this
* query plan we can bypass some of the query statement generation (for
* performance) and collect statistics on the number and average execution
* times. This is turn can be used to identify queries that could be looked at
* for performance tuning.
* </p>
*/
public class CQueryPlan {
private static final Logger logger = LoggerFactory.getLogger(CQueryPlan.class);
private final SpiEbeanServer server;
private final boolean autoTuned;
private final CQueryPlanKey planKey;
private final boolean rawSql;
private final boolean rowNumberIncluded;
private final String sql;
private final String logWhereSql;
private final SqlTree sqlTree;
/**
* Encrypted properties required additional binding.
*/
private final BeanProperty[] encryptedProps;
private final CQueryPlanStats stats;
private final Class<?> beanType;
protected final DataTimeZone dataTimeZone;
private final int asOfTableCount;
/**
* Key used to identify the query plan in audit logging.
*/
private volatile String auditQueryHash;
/**
* Create a query plan based on a OrmQueryRequest.
*/
CQueryPlan(OrmQueryRequest<?> request, SqlLimitResponse sqlRes, SqlTree sqlTree, boolean rawSql, String logWhereSql) {
this.server = request.getServer();
this.dataTimeZone = server.getDataTimeZone();
this.beanType = request.getBeanDescriptor().getBeanType();
this.planKey = request.getQueryPlanKey();
this.autoTuned = request.getQuery().isAutoTuned();
this.asOfTableCount = request.getQuery().getAsOfTableCount();
this.sql = sqlRes.getSql();
this.rowNumberIncluded = sqlRes.isIncludesRowNumberColumn();
this.sqlTree = sqlTree;
this.rawSql = rawSql;
this.logWhereSql = logWhereSql;
this.encryptedProps = sqlTree.getEncryptedProps();
this.stats = new CQueryPlanStats(this, server.isCollectQueryOrigins());
}
/**
* Create a query plan for a raw sql query.
*/
CQueryPlan(OrmQueryRequest<?> request, String sql, SqlTree sqlTree,
boolean rawSql, boolean rowNumberIncluded, String logWhereSql) {
this.server = request.getServer();
this.dataTimeZone = server.getDataTimeZone();
this.beanType = request.getBeanDescriptor().getBeanType();
this.planKey = buildPlanKey(sql, rawSql, rowNumberIncluded, logWhereSql);
this.autoTuned = false;
this.asOfTableCount = 0;
this.sql = sql;
this.sqlTree = sqlTree;
this.rawSql = rawSql;
this.rowNumberIncluded = rowNumberIncluded;
this.logWhereSql = logWhereSql;
this.encryptedProps = sqlTree.getEncryptedProps();
this.stats = new CQueryPlanStats(this, server.isCollectQueryOrigins());
}
private CQueryPlanKey buildPlanKey(String sql, boolean rawSql, boolean rowNumberIncluded, String logWhereSql) {
return new RawSqlQueryPlanKey(sql, rawSql, rowNumberIncluded, logWhereSql);
}
public String toString() {
return beanType + " hash:" + planKey;
}
public Class<?> getBeanType() {
return beanType;
}
public DataReader createDataReader(ResultSet rset) {
return new RsetDataReader(dataTimeZone, rset);
}
/**
* Bind keys for encrypted properties if necessary returning the DataBind.
*/
DataBind bindEncryptedProperties(PreparedStatement stmt, Connection conn) throws SQLException {
DataBind dataBind = new DataBind(dataTimeZone, stmt, conn);
if (encryptedProps != null) {
for (BeanProperty encryptedProp : encryptedProps) {
String key = encryptedProp.getEncryptKey().getStringValue();
dataBind.setString(key);
}
}
return dataBind;
}
int getAsOfTableCount() {
return asOfTableCount;
}
boolean isAutoTuned() {
return autoTuned;
}
CQueryPlanKey getPlanKey() {
return planKey;
}
/**
* Return a key used in audit logging to identify the query.
*/
String getAuditQueryKey() {
if (auditQueryHash == null) {
// volatile object assignment (so happy for multithreaded access)
auditQueryHash = calcAuditQueryKey();
}
return auditQueryHash;
}
private String calcAuditQueryKey() {
// rawSql needs to include the MD5 hash of the sql
return rawSql ? planKey.getPartialKey() + "_" + getSqlMd5Hash() : planKey.getPartialKey();
}
/**
* Return the MD5 hash of the underlying sql.
*/
private String getSqlMd5Hash() {
try {
return Md5.hash(sql);
} catch (Exception e) {
logger.error("Failed to MD5 hash the rawSql query", e);
return "error";
}
}
public String getSql() {
return sql;
}
SqlTree getSqlTree() {
return sqlTree;
}
public boolean isRawSql() {
return rawSql;
}
boolean isRowNumberIncluded() {
return rowNumberIncluded;
}
String getLogWhereSql() {
return logWhereSql;
}
/**
* Reset the query statistics.
*/
public void resetStatistics() {
stats.reset();
}
/**
* Register an execution time against this query plan;
*/
void executionTime(long loadedBeanCount, long timeMicros, ObjectGraphNode objectGraphNode) {
stats.add(loadedBeanCount, timeMicros, objectGraphNode);
if (objectGraphNode != null) {
// collect stats based on objectGraphNode for lazy loading reporting
server.collectQueryStats(objectGraphNode, loadedBeanCount, timeMicros);
}
}
public Snapshot getSnapshot(boolean reset) {
return stats.getSnapshot(reset);
}
/**
* Return the current query statistics.
*/
public CQueryPlanStats getQueryStats() {
return stats;
}
/**
* Return the time this query plan was last used.
*/
public long getLastQueryTime() {
return stats.getLastQueryTime();
}
BeanProperty getSingleProperty() {
return sqlTree.getRootNode().getSingleProperty();
}
}
@@ -0,0 +1,68 @@
package io.ebeaninternal.server.query;
import io.ebean.RawSql.ColumnMapping;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.type.DataReader;
import io.ebeaninternal.server.type.RsetDataReaderIndexed;
import java.sql.ResultSet;
import java.util.List;
/**
* RawSql based query plan.
*/
class CQueryPlanRawSql extends CQueryPlan {
private final int[] rsetIndexPositions;
CQueryPlanRawSql(OrmQueryRequest<?> request, SqlLimitResponse sqlRes, SqlTree sqlTree, String logWhereSql) {
super(request, sqlRes, sqlTree, true, logWhereSql);
this.rsetIndexPositions = createIndexPositions(request, sqlTree);
}
@Override
public DataReader createDataReader(ResultSet rset) {
return new RsetDataReaderIndexed(dataTimeZone, rset, rsetIndexPositions, isRowNumberIncluded());
}
private int[] createIndexPositions(OrmQueryRequest<?> request, SqlTree sqlTree) {
List<String> chain = sqlTree.buildRawSqlSelectChain();
ColumnMapping columnMapping = request.getQuery().getRawSql().getColumnMapping();
int[] indexPositions = new int[chain.size()];
// set the resultSet index positions for the property expressions
for (int i = 0; i < chain.size(); i++) {
String logicalPropertyPath = chain.get(i);
int mappedPosition = columnMapping.getIndexPosition(logicalPropertyPath);
if (mappedPosition == -1 && logicalPropertyPath.endsWith(".id")) {
// try a automatically mapped foreign key
mappedPosition = columnMapping.getIndexPosition(foreignKeyPath(logicalPropertyPath));
}
indexPositions[i] = 1 + mappedPosition;
}
// check and handle the case where a discriminator column for
// an associated bean is in the raw SQL but is mapped columnIgnore
for (int i = 0; i < indexPositions.length; i++) {
if (indexPositions[i] == 0) {
if (i < indexPositions.length) {
// expect discriminator column to immediately proceed id column
indexPositions[i] = indexPositions[i + 1] - 1;
}
}
}
return indexPositions;
}
/**
* Return the path for a foreign key column that was automatically mapped.
*/
private String foreignKeyPath(String logicalPropertyPath) {
// trim the .id and replace with Id ... to reverse the auto fk mapping earlier
return logicalPropertyPath.substring(0, logicalPropertyPath.length() - 3) + "Id";
}
}
@@ -0,0 +1,260 @@
package io.ebeaninternal.server.query;
import io.ebean.bean.ObjectGraphNode;
import io.ebean.meta.MetaQueryPlanOriginCount;
import io.ebean.meta.MetaQueryPlanStatistic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
/**
* Statistics for a specific query plan that can accumulate.
*/
public final class CQueryPlanStats {
private final CQueryPlan queryPlan;
private final LongAdder count = new LongAdder();
private final LongAdder totalTime = new LongAdder();
private final LongAdder totalBeans = new LongAdder();
private final LongAccumulator maxTime = new LongAccumulator(Math::max, Long.MIN_VALUE);
private final AtomicLong startTime = new AtomicLong(System.currentTimeMillis());
private long lastQueryTime;
private final ConcurrentHashMap<ObjectGraphNode, LongAdder> origins;
/**
* Construct for a given query plan.
*/
CQueryPlanStats(CQueryPlan queryPlan, boolean collectQueryOrigins) {
this.queryPlan = queryPlan;
this.origins = !collectQueryOrigins ? null : new ConcurrentHashMap<>();
}
/**
* Add a query execution to the statistics.
*/
public void add(long loadedBeanCount, long timeMicros, ObjectGraphNode objectGraphNode) {
count.increment();
totalBeans.add(loadedBeanCount);
totalTime.add(timeMicros);
maxTime.accumulate(timeMicros);
// not safe but should be atomic
lastQueryTime = System.currentTimeMillis();
if (origins != null && objectGraphNode != null) {
// Maintain the origin points this query fires from
// with a simple counter
LongAdder counter = origins.get(objectGraphNode);
if (counter == null) {
// race condition - we can miss counters here but going
// to live with that. Don't want to lock/synchronize etc
counter = new LongAdder();
origins.put(objectGraphNode, counter);
}
counter.increment();
}
}
/**
* Reset the internal statistics counters.
*/
public void reset() {
// Racey but near enough for our purposes as we don't want locks
count.reset();
totalBeans.reset();
totalTime.reset();
maxTime.reset();
startTime.set(System.currentTimeMillis());
if (origins != null) {
for (LongAdder counter : origins.values()) {
counter.reset();
}
}
}
/**
* Return the last time this query was executed.
*/
long getLastQueryTime() {
return lastQueryTime;
}
/**
* Return a Snapshot of the query execution statistics potentially resetting the internal counters.
*/
Snapshot getSnapshot(boolean reset) {
List<MetaQueryPlanOriginCount> origins = getOrigins(reset);
// not guaranteed to be consistent due to time gaps between getting each value out of LongAdders but can live with that
// relative to the cost of making sure count and totalTime etc are all guaranteed to be consistent
if (reset) {
return new Snapshot(queryPlan, count.sumThenReset(), totalTime.sumThenReset(), totalBeans.sumThenReset(), maxTime.getThenReset(), startTime.getAndSet(System.currentTimeMillis()), lastQueryTime, origins);
}
return new Snapshot(queryPlan, count.sum(), totalTime.sum(), totalBeans.sum(), maxTime.get(), startTime.get(), lastQueryTime, origins);
}
/**
* Return the list/snapshot of the origins and their counter value.
*/
private List<MetaQueryPlanOriginCount> getOrigins(boolean reset) {
if (origins == null) {
return Collections.emptyList();
}
List<MetaQueryPlanOriginCount> list = new ArrayList<>();
for (Entry<ObjectGraphNode, LongAdder> entry : origins.entrySet()) {
if (reset) {
list.add(new OriginSnapshot(entry.getKey(), entry.getValue().sumThenReset()));
} else {
list.add(new OriginSnapshot(entry.getKey(), entry.getValue().sum()));
}
}
return list;
}
/**
* Snapshot of the origin ObjectGraphNode and counter value.
*/
private static class OriginSnapshot implements MetaQueryPlanOriginCount {
private final ObjectGraphNode objectGraphNode;
private final long count;
OriginSnapshot(ObjectGraphNode objectGraphNode, long count) {
this.objectGraphNode = objectGraphNode;
this.count = count;
}
public String toString() {
return "node[" + objectGraphNode + "] count[" + count + "]";
}
@Override
public ObjectGraphNode getObjectGraphNode() {
return objectGraphNode;
}
@Override
public long getCount() {
return count;
}
}
/**
* A snapshot of the current statistics for a query plan.
*/
public static class Snapshot implements MetaQueryPlanStatistic {
private final CQueryPlan queryPlan;
private final long count;
private final long totalTime;
private final long totalBeans;
private final long maxTime;
private final long startTime;
private final long lastQueryTime;
private final List<MetaQueryPlanOriginCount> origins;
Snapshot(CQueryPlan queryPlan, long count, long totalTime, long totalBeans, long maxTime, long startTime, long lastQueryTime,
List<MetaQueryPlanOriginCount> origins) {
this.queryPlan = queryPlan;
this.count = count;
this.totalTime = totalTime;
this.totalBeans = totalBeans;
this.maxTime = maxTime;
this.startTime = startTime;
this.lastQueryTime = lastQueryTime;
this.origins = origins;
}
public String toString() {
return queryPlan + " count:" + count + " time:" + totalTime + " maxTime:" + maxTime + " beans:" + totalBeans
+ " start:" + startTime + " lastQuery:" + lastQueryTime + " origins:" + origins;
}
@Override
public Class<?> getBeanType() {
return queryPlan.getBeanType();
}
@Override
public long getExecutionCount() {
return count;
}
@Override
public long getTotalTimeMicros() {
return totalTime;
}
@Override
public long getTotalLoadedBeans() {
return totalBeans;
}
@Override
public long getMaxTimeMicros() {
return maxTime;
}
@Override
public long getCollectionStart() {
return startTime;
}
@Override
public long getLastQueryTime() {
return lastQueryTime;
}
@Override
public boolean isAutoTuned() {
return queryPlan.isAutoTuned();
}
@Override
public String getQueryPlanHash() {
return queryPlan.getPlanKey().toString();
}
@Override
public String getSql() {
return queryPlan.getSql();
}
@Override
public long getAvgTimeMicros() {
return count < 1 ? 0 : totalTime / count;
}
@Override
public long getAvgLoadedBeans() {
return count < 1 ? 0 : totalBeans / count;
}
@Override
public List<MetaQueryPlanOriginCount> getOrigins() {
return origins;
}
}
}
@@ -0,0 +1,488 @@
package io.ebeaninternal.server.query;
import io.ebean.RawSql;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.SpiExpressionList;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DeployParser;
import io.ebeaninternal.server.expression.DefaultExpressionRequest;
import io.ebeaninternal.server.persist.Binder;
import io.ebeaninternal.server.querydefn.OrmQueryProperties;
import io.ebeaninternal.server.querydefn.OrmUpdateProperties;
import io.ebeaninternal.server.type.DataBind;
import io.ebeaninternal.server.util.BindParamsParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Compile Query Predicates.
* <p>
* This includes the where and having expressions which can be made up of
* Strings with named parameters or Expression objects.
* </p>
* <p>
* This builds the appropriate bits of where and having clauses and binds the
* named parameters and expression values into the prepared statement.
* </p>
*/
public class CQueryPredicates {
private static final Logger logger = LoggerFactory.getLogger(CQueryPredicates.class);
private final Binder binder;
private final OrmQueryRequest<?> request;
private final SpiQuery<?> query;
private final Object idValue;
/**
* Named bind parameters.
*/
private final BindParams bindParams;
/**
* Bind values from the where expressions.
*/
private DefaultExpressionRequest filterMany;
/**
* SQL generated from the where expressions.
*/
private String filterManyExprSql;
/**
* Bind values from the where expressions.
*/
private DefaultExpressionRequest where;
/**
* SQL generated from the where expressions.
*/
private String whereExprSql;
/**
* Bind values for having expression.
*/
private DefaultExpressionRequest having;
/**
* SQL generated from the having expression.
*/
private String havingExprSql;
private String dbHaving;
/**
* logicalWhere with property names converted to db columns.
*/
private String dbWhere;
/**
* Filter than can apply to a many fetch join.
*/
private String dbFilterMany;
private String dbOrderBy;
private String dbUpdateClause;
/**
* Includes from where and order by clauses.
*/
private Set<String> predicateIncludes;
private Set<String> orderByIncludes;
CQueryPredicates(Binder binder, OrmQueryRequest<?> request) {
this.binder = binder;
this.request = request;
this.query = request.getQuery();
this.bindParams = query.getBindParams();
this.idValue = query.getId();
}
public String bind(PreparedStatement stmt, Connection connection) throws SQLException {
return bind(binder.dataBind(stmt, connection));
}
public String bind(DataBind dataBind) throws SQLException {
OrmUpdateProperties updateProperties = query.getUpdateProperties();
if (updateProperties != null) {
// bind the update set clause
updateProperties.bind(binder, dataBind);
}
if (query.isVersionsBetween() && binder.isAsOfStandardsBased()) {
// sql2011 based versions between timestamp syntax
Timestamp start = query.getVersionStart();
Timestamp end = query.getVersionEnd();
dataBind.append("between ").append(start).append(" and ").append(end);
binder.bindObject(dataBind, start);
binder.bindObject(dataBind, end);
dataBind.append(", ");
}
CQueryPlan queryPlan = request.getQueryPlan();
if (queryPlan != null) {
int asOfTableCount = queryPlan.getAsOfTableCount();
if (asOfTableCount > 0) {
// bind the asOf value for each table alias as part of the from/join clauses
// there is one effective date predicate per table alias
Timestamp asOf = query.getAsOf();
dataBind.append("asOf ").append(asOf);
for (int i = 0; i < asOfTableCount * binder.getAsOfBindCount(); i++) {
binder.bindObject(dataBind, asOf);
}
dataBind.append(", ");
}
}
if (idValue != null) {
// this is a find by id type query...
request.getBeanDescriptor().bindId(dataBind, idValue);
dataBind.append(idValue);
dataBind.append(", ");
}
if (bindParams != null) {
// bind named and positioned parameters...
binder.bind(bindParams, dataBind, dataBind.log());
}
if (where != null) {
where.bind(dataBind);
}
if (filterMany != null) {
filterMany.bind(dataBind);
}
if (having != null) {
having.bind(dataBind);
}
return dataBind.log().toString();
}
private void buildUpdateClause(boolean buildSql, DeployParser deployParser) {
if (buildSql) {
OrmUpdateProperties updateProperties = query.getUpdateProperties();
if (updateProperties != null) {
dbUpdateClause = updateProperties.buildSetClause(deployParser);
}
}
}
public String parseBindParams(String sql) {
if (bindParams != null && bindParams.requiresNamedParamsPrepare()) {
return BindParamsParser.parse(bindParams, sql);
} else {
return sql;
}
}
/**
* Convert named parameters into an OrderedList.
*/
private void buildBindWhereRawSql(boolean buildSql) {
if (!buildSql && bindParams != null && bindParams.requiresNamedParamsPrepare()) {
if (query.isNativeSql()) {
// convert named params into positioned params
String sql = query.getNativeSql();
BindParamsParser.parse(bindParams, sql);
} else if (query.isRawSql()) {
// RawSql query hit cached query plan. Need to convert
// named parameters into positioned parameters so that
// the named parameters are bound
RawSql.Sql sql = query.getRawSql().getSql();
String s = sql.isParsed() ? sql.getPreWhere() : sql.getUnparsedSql();
BindParamsParser.parse(bindParams, s);
}
}
}
public void prepare(boolean buildSql) {
DeployParser deployParser = request.createDeployParser();
buildUpdateClause(buildSql, deployParser);
buildBindWhereRawSql(buildSql);
SpiExpressionList<?> whereExp = query.getWhereExpressions();
if (whereExp != null) {
this.where = new DefaultExpressionRequest(request, deployParser, binder, whereExp);
if (buildSql) {
whereExprSql = where.buildSql();
}
}
BeanPropertyAssocMany<?> manyProperty = request.getManyProperty();
if (manyProperty != null) {
OrmQueryProperties chunk = query.getDetail().getChunk(manyProperty.getName(), false);
SpiExpressionList<?> filterManyExpr = chunk.getFilterMany();
if (filterManyExpr != null) {
this.filterMany = new DefaultExpressionRequest(request, deployParser, binder, filterManyExpr);
if (buildSql) {
filterManyExprSql = filterMany.buildSql();
}
}
}
// having expression
SpiExpressionList<?> havingExpr = query.getHavingExpressions();
if (havingExpr != null) {
this.having = new DefaultExpressionRequest(request, deployParser, binder, havingExpr);
if (buildSql) {
havingExprSql = having.buildSql();
}
}
if (buildSql) {
parsePropertiesToDbColumns(deployParser);
}
}
/**
* Parse/Convert property names to database columns in the where and order by
* clauses etc.
*/
private void parsePropertiesToDbColumns(DeployParser deployParser) {
// order by is dependent on the manyProperty (if there is one)
String logicalOrderBy = deriveOrderByWithMany(request.getManyProperty());
if (logicalOrderBy != null) {
dbOrderBy = deployParser.parse(logicalOrderBy);
}
// create a copy of the includes required to support the orderBy
orderByIncludes = new HashSet<>(deployParser.getIncludes());
dbWhere = deriveWhere(deployParser);
dbFilterMany = deriveFilterMany(deployParser);
dbHaving = deriveHaving(deployParser);
// all includes including ones for manyWhere clause
predicateIncludes = deployParser.getIncludes();
}
private String deriveFilterMany(DeployParser deployParser) {
if (isEmpty(filterManyExprSql)) {
return null;
} else {
return deployParser.parse(filterManyExprSql);
}
}
private String deriveWhere(DeployParser deployParser) {
return parse(whereExprSql, deployParser);
}
/**
* Replace the table alias place holders.
*/
void parseTableAlias(SqlTreeAlias alias) {
if (dbWhere != null) {
// use the where table alias
dbWhere = alias.parseWhere(dbWhere);
}
if (dbFilterMany != null) {
// use the select table alias
dbFilterMany = alias.parse(dbFilterMany);
}
if (dbHaving != null) {
dbHaving = alias.parseWhere(dbHaving);
}
if (dbOrderBy != null) {
dbOrderBy = alias.parse(dbOrderBy);
}
}
private boolean isEmpty(String s) {
return s == null || s.isEmpty();
}
private String parse(String expr, DeployParser deployParser) {
StringBuilder sb = new StringBuilder();
if (!isEmpty(expr)) {
if (sb.length() > 0) {
sb.append(" and ");
}
sb.append(deployParser.parse(expr));
}
return sb.toString();
}
private String deriveHaving(DeployParser deployParser) {
return parse(havingExprSql, deployParser);
}
private String parseOrderBy() {
return CQueryOrderBy.parse(request.getBeanDescriptor(), query);
}
/**
* There is a many property so we need to make sure the ordering is
* appropriate.
*/
private String deriveOrderByWithMany(BeanPropertyAssocMany<?> manyProp) {
if (manyProp == null) {
return parseOrderBy();
}
String orderBy = parseOrderBy();
BeanDescriptor<?> desc = request.getBeanDescriptor();
String orderById = desc.getDefaultOrderBy();
if (orderBy == null) {
orderBy = orderById;
}
// check for default ordering on the many property...
String manyOrderBy = manyProp.getFetchOrderBy();
if (manyOrderBy != null) {
orderBy = orderBy + ", " + CQueryBuilder.prefixOrderByFields(manyProp.getName(), manyOrderBy);
}
if (request.isFindById()) {
// only one master bean so should be fine...
return orderBy;
}
if (orderBy.startsWith(orderById)) {
return orderBy;
}
// more than one top level row may be returned so
// we need to make sure their is an order by on the
// top level first (to ensure master/detail construction).
int manyPos = orderBy.indexOf(manyProp.getName());
int idPos = orderBy.indexOf(" " + orderById);
if (manyPos == -1) {
// no ordering of the many
if (idPos == -1) {
// append the orderById so that master level objects are ordered
// even if the orderBy is not unique for the master object
return orderBy + ", " + orderById;
}
// orderById is already in the order by clause
return orderBy;
}
if (idPos <= -1 || idPos >= manyPos) {
if (idPos > manyPos) {
// there was an error with the order by...
String msg = "A Query on [" + desc + "] includes a join to a 'many' association [" + manyProp.getName();
msg += "] with an incorrect orderBy [" + orderBy + "]. The id property [" + orderById + "]";
msg += " must come before the many property [" + manyProp.getName() + "] in the orderBy.";
msg += " Ebean has automatically modified the orderBy clause to do this.";
logger.warn(msg);
}
// the id needs to come before the manyPropName
orderBy = orderBy.substring(0, manyPos) + orderById + ", " + orderBy.substring(manyPos);
}
return orderBy;
}
/**
* Return the bind values for the where expression.
*/
public List<Object> getWhereExprBindValues() {
return where.getBindValues();
}
/**
* Return the db update set clause for an UpdateQuery.
*/
String getDbUpdateClause() {
return dbUpdateClause;
}
/**
* Return the db column version of the combined where clause.
*/
String getDbHaving() {
return dbHaving;
}
/**
* Return the db column version of the combined where clause.
*/
String getDbWhere() {
return dbWhere;
}
/**
* Return a db filter for filtering many fetch joins.
*/
String getDbFilterMany() {
return dbFilterMany;
}
/**
* Return the db column version of the order by clause.
*/
String getDbOrderBy() {
return dbOrderBy;
}
/**
* Return the includes required for the where and order by clause.
*/
Set<String> getPredicateIncludes() {
return predicateIncludes;
}
/**
* Return the orderBy includes.
*/
Set<String> getOrderByIncludes() {
return orderByIncludes;
}
String getLogWhereSql() {
if (dbWhere == null && dbFilterMany == null) {
return "";
}
StringBuilder sb = new StringBuilder();
if (dbWhere != null) {
sb.append(dbWhere);
}
if (dbFilterMany != null) {
if (sb.length() > 0) {
sb.append(" and ");
}
sb.append(dbFilterMany);
}
String logPred = sb.toString();
if (logPred.length() > 400) {
logPred = logPred.substring(0, 400) + " ...";
}
return logPred;
}
}
@@ -0,0 +1,140 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Executes the select row count query.
*/
class CQueryRowCount {
/**
* The overall find request wrapper object.
*/
private final OrmQueryRequest<?> request;
private final BeanDescriptor<?> desc;
private final SpiQuery<?> query;
/**
* Where clause predicates.
*/
private final CQueryPredicates predicates;
/**
* The final sql that is generated.
*/
private final String sql;
/**
* The resultSet that is read and converted to objects.
*/
private ResultSet rset;
/**
* The statement used to create the resultSet.
*/
private PreparedStatement pstmt;
private String bindLog;
private int executionTimeMicros;
private int rowCount;
/**
* Create the Sql select based on the request.
*/
CQueryRowCount(OrmQueryRequest<?> request, CQueryPredicates predicates, String sql) {
this.request = request;
this.query = request.getQuery();
this.sql = sql;
query.setGeneratedSql(sql);
this.desc = request.getBeanDescriptor();
this.predicates = predicates;
}
/**
* Return a summary description of this query.
*/
public String getSummary() {
//noinspection StringBufferReplaceableByString
StringBuilder sb = new StringBuilder(80);
sb.append("FindRowCount exeMicros[").append(executionTimeMicros)
.append("] rows[").append(rowCount)
.append("] type[").append(desc.getFullName())
.append("] predicates[").append(predicates.getLogWhereSql())
.append("] bind[").append(bindLog).append("]");
return sb.toString();
}
/**
* Return the bind log.
*/
public String getBindLog() {
return bindLog;
}
/**
* Return the generated sql.
*/
public String getGeneratedSql() {
return sql;
}
/**
* Execute the query returning the row count.
*/
public int findRowCount() throws SQLException {
long startNano = System.nanoTime();
try {
SpiTransaction t = request.getTransaction();
Connection conn = t.getInternalConnection();
pstmt = conn.prepareStatement(sql);
if (query.getTimeout() > 0) {
pstmt.setQueryTimeout(query.getTimeout());
}
bindLog = predicates.bind(pstmt, conn);
rset = pstmt.executeQuery();
if (!rset.next()) {
throw new PersistenceException("Expecting 1 row but got none?");
}
rowCount = rset.getInt(1);
long exeNano = System.nanoTime() - startNano;
executionTimeMicros = (int) exeNano / 1000;
return rowCount;
} finally {
close();
}
}
/**
* Close the resources.
*/
private void close() {
UtilJdbc.close(rset);
UtilJdbc.close(pstmt);
rset = null;
pstmt = null;
}
}
@@ -0,0 +1,123 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Executes the delete query.
*/
class CQueryUpdate {
private final OrmQueryRequest<?> request;
private final BeanDescriptor<?> desc;
private final SpiQuery<?> query;
/**
* Where clause predicates.
*/
private final CQueryPredicates predicates;
/**
* The final sql that is generated.
*/
private final String sql;
private final String type;
/**
* The statement used to create the resultSet.
*/
private PreparedStatement pstmt;
private String bindLog;
private int executionTimeMicros;
private int rowCount;
/**
* Create the Sql select based on the request.
*/
CQueryUpdate(String type, OrmQueryRequest<?> request, CQueryPredicates predicates, String sql) {
this.type = type;
this.request = request;
this.query = request.getQuery();
this.sql = sql;
query.setGeneratedSql(sql);
this.desc = request.getBeanDescriptor();
this.predicates = predicates;
}
/**
* Return a summary description of this query.
*/
public String getSummary() {
StringBuilder sb = new StringBuilder(80);
sb.append(type).append(" exeMicros[").append(executionTimeMicros)
.append("] rows[").append(rowCount)
.append("] type[").append(desc.getName())
.append("] predicates[").append(predicates.getLogWhereSql())
.append("] bind[").append(bindLog).append("]");
return sb.toString();
}
/**
* Return the bind log.
*/
public String getBindLog() {
return bindLog;
}
/**
* Return the generated sql.
*/
public String getGeneratedSql() {
return sql;
}
/**
* Execute the update or delete statement returning the row count.
*/
public int execute() throws SQLException {
long startNano = System.nanoTime();
try {
SpiTransaction t = request.getTransaction();
Connection conn = t.getInternalConnection();
pstmt = conn.prepareStatement(sql);
if (query.getTimeout() > 0) {
pstmt.setQueryTimeout(query.getTimeout());
}
bindLog = predicates.bind(pstmt, conn);
rowCount = pstmt.executeUpdate();
long exeNano = System.nanoTime() - startNano;
executionTimeMicros = (int) exeNano / 1000;
return rowCount;
} finally {
close();
}
}
/**
* Close the resources.
*/
private void close() {
UtilJdbc.close(pstmt);
pstmt = null;
}
}
@@ -0,0 +1,35 @@
package io.ebeaninternal.server.query;
import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
/**
* Base object for making query execution into Callable's.
*
* @param <T> the entity bean type
* @author rbygrave
*/
abstract class CallableQuery<T> {
protected final SpiQuery<T> query;
protected final SpiEbeanServer server;
protected final Transaction transaction;
CallableQuery(SpiEbeanServer server, SpiQuery<T> query, Transaction t) {
this.server = server;
this.query = query;
this.transaction = t;
}
public SpiQuery<T> getQuery() {
return query;
}
public Transaction getTransaction() {
return transaction;
}
}
@@ -0,0 +1,36 @@
package io.ebeaninternal.server.query;
import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import java.util.List;
import java.util.concurrent.Callable;
/**
* Represent the fetch Id's query as a Callable.
*
* @param <T> the entity bean type
*/
public class CallableQueryIds<T> extends CallableQuery<T> implements Callable<List<Object>> {
public CallableQueryIds(SpiEbeanServer server, SpiQuery<T> query, Transaction t) {
super(server, query, t);
}
/**
* Execute the find Id's query returning the list of Id's.
*/
public List<Object> call() throws Exception {
// we have already made a copy of the query
// this way the same query instance is available to the
// QueryFutureIds (as so has access to the List before it is done)
try {
return server.findIdsWithCopy(query, transaction);
} finally {
transaction.end();
}
}
}
@@ -0,0 +1,35 @@
package io.ebeaninternal.server.query;
import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import java.util.List;
import java.util.concurrent.Callable;
/**
* Represent the findList query as a Callable.
*
* @param <T> the entity bean type
*/
public class CallableQueryList<T> extends CallableQuery<T> implements Callable<List<T>> {
public CallableQueryList(SpiEbeanServer server, SpiQuery<T> query, Transaction t) {
super(server, query, t);
}
/**
* Execute the query returning the resulting List.
*/
public List<T> call() throws Exception {
try {
return server.findList(query, transaction);
} finally {
// cleanup the underlying connection
transaction.end();
}
}
}
@@ -0,0 +1,36 @@
package io.ebeaninternal.server.query;
import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import java.util.concurrent.Callable;
/**
* Represent the findRowCount query as a Callable.
*
* @param <T> the entity bean type
*/
public class CallableQueryRowCount<T> extends CallableQuery<T> implements Callable<Integer> {
/**
* Note that the transaction passed in is always a new transaction solely to
* find the row count so it must be cleaned up by this CallableQueryRowCount.
*/
public CallableQueryRowCount(SpiEbeanServer server, SpiQuery<T> query, Transaction t) {
super(server, query, t);
}
/**
* Execute the query returning the row count.
*/
public Integer call() throws Exception {
try {
return server.findRowCountWithCopy(query, transaction);
} finally {
// cleanup the underlying connection
transaction.end();
}
}
}
@@ -0,0 +1,19 @@
package io.ebeaninternal.server.query;
/**
* Defines a cancelable query.
* <p>
* Typically holds a representation of the PreparedStatement to perform the
* actual cancel.
* </p>
*/
public interface CancelableQuery {
/**
* Cancel the query.
* <p>
* For JDBC this translates to calling cancel on the PreparedStatement.
* </p>
*/
void cancel();
}
@@ -0,0 +1,330 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.TableJoinColumn;
import io.ebeaninternal.server.lib.util.StringHelper;
import io.ebeaninternal.server.util.ArrayStack;
import java.util.ArrayList;
import java.util.HashSet;
class DefaultDbSqlContext implements DbSqlContext {
private static final String COMMA = ", ";
private static final String PERIOD = ".";
private static final int STRING_BUILDER_INITIAL_CAPACITY = 140;
private final String tableAliasPlaceHolder;
private final String columnAliasPrefix;
private final ArrayStack<String> tableAliasStack = new ArrayStack<>();
private final ArrayStack<String> joinStack = new ArrayStack<>();
private final ArrayStack<String> prefixStack = new ArrayStack<>();
private boolean useColumnAlias;
private int columnIndex;
private StringBuilder sb = new StringBuilder(STRING_BUILDER_INITIAL_CAPACITY);
/**
* A Set used to make sure formula joins are only added once to a query.
*/
private HashSet<String> formulaJoins;
private HashSet<String> tableJoins;
private final SqlTreeAlias alias;
private String currentPrefix;
private ArrayList<BeanProperty> encryptedProps;
private final CQueryDraftSupport draftSupport;
private final CQueryHistorySupport historySupport;
private final boolean historyQuery;
/**
* Construct for SELECT clause (with column alias settings).
*/
DefaultDbSqlContext(SqlTreeAlias alias, String tableAliasPlaceHolder,
String columnAliasPrefix, boolean alwaysUseColumnAlias, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
this.alias = alias;
this.tableAliasPlaceHolder = tableAliasPlaceHolder;
this.columnAliasPrefix = columnAliasPrefix;
this.useColumnAlias = alwaysUseColumnAlias;
this.draftSupport = draftSupport;
this.historySupport = historySupport;
this.historyQuery = (historySupport != null);
}
@Override
public void startGroupBy() {
this.useColumnAlias = false;
}
public void addEncryptedProp(BeanProperty p) {
if (encryptedProps == null) {
encryptedProps = new ArrayList<>();
}
encryptedProps.add(p);
}
public BeanProperty[] getEncryptedProps() {
if (encryptedProps == null) {
return null;
}
return encryptedProps.toArray(new BeanProperty[encryptedProps.size()]);
}
public String peekJoin() {
return joinStack.peek();
}
public void popJoin() {
joinStack.pop();
}
public void pushJoin(String node) {
joinStack.push(node);
}
public void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2, String inheritance) {
if (tableJoins == null) {
tableJoins = new HashSet<>();
}
String joinKey = table + "-" + a1 + "-" + a2;
if (tableJoins.contains(joinKey)) {
return;
}
tableJoins.add(joinKey);
sb.append(" ").append(type);
boolean addAsOfOnClause = false;
if (draftSupport != null) {
appendTable(table, draftSupport.getDraftTable(table));
} else if (!historyQuery) {
sb.append(" ").append(table).append(" ");
} else {
// check if there is an associated history table and if so
// use the unionAll view - we expect an additional predicate to match
String asOfView = historySupport.getAsOfView(table);
appendTable(table, asOfView);
if (asOfView != null) {
addAsOfOnClause = !historySupport.isStandardsBased();
}
}
sb.append(a2);
sb.append(" on ");
for (int i = 0; i < cols.length; i++) {
TableJoinColumn pair = cols[i];
if (i > 0) {
sb.append(" and ");
}
if (pair.getForeignSqlFormula() != null) {
sb.append(StringHelper.replaceString(pair.getForeignSqlFormula(), tableAliasPlaceHolder, a2));
} else {
sb.append(a2).append(".").append(pair.getForeignDbColumn());
}
sb.append(" = ");
if (pair.getLocalSqlFormula() != null) {
sb.append(StringHelper.replaceString(pair.getLocalSqlFormula(), tableAliasPlaceHolder, a1));
} else {
sb.append(a1).append(".").append(pair.getLocalDbColumn());
}
}
// add on any inheritance where clause
if (inheritance != null && !inheritance.isEmpty()) {
sb.append(" and ").append(a2).append(".").append(inheritance);
}
if (addAsOfOnClause) {
sb.append(" and ").append(historySupport.getAsOfPredicate(a2));
}
sb.append(" ");
}
private void appendTable(String table, String draftTable) {
if (draftTable != null) {
// there is an associated history table and view so use that
sb.append(" ").append(draftTable).append(" ");
} else {
sb.append(" ").append(table).append(" ");
}
}
@Override
public boolean isDraftQuery() {
return draftSupport != null;
}
public String getTableAlias(String prefix) {
return alias.getTableAlias(prefix);
}
public String getTableAliasManyWhere(String prefix) {
return alias.getTableAliasManyWhere(prefix);
}
public void pushSecondaryTableAlias(String alias) {
tableAliasStack.push(alias);
}
public String getRelativePrefix(String propName) {
return currentPrefix == null ? propName : currentPrefix + "." + propName;
}
public void pushTableAlias(String prefix) {
prefixStack.push(currentPrefix);
currentPrefix = prefix;
tableAliasStack.push(getTableAlias(prefix));
}
public void popTableAlias() {
tableAliasStack.pop();
currentPrefix = prefixStack.pop();
}
public DefaultDbSqlContext append(String s) {
sb.append(s);
return this;
}
public DefaultDbSqlContext append(char s) {
sb.append(s);
return this;
}
public void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType) {
// replace ${ta} place holder with the real table alias...
String tableAlias = tableAliasStack.peek();
String converted = StringHelper.replaceString(sqlFormulaJoin, tableAliasPlaceHolder, tableAlias);
if (formulaJoins == null) {
formulaJoins = new HashSet<>();
} else if (formulaJoins.contains(converted)) {
// skip adding a formula join because
// the same join has already been added.
return;
}
// we only want to add this join once
formulaJoins.add(converted);
sb.append(" ");
if (joinType == SqlJoinType.OUTER) {
if ("join".equals(sqlFormulaJoin.substring(0, 4).toLowerCase())) {
// prepend left as we are in the 'many' part
append(" left ");
}
}
sb.append(converted);
sb.append(" ");
}
public void appendFormulaSelect(String sqlFormulaSelect) {
String tableAlias = tableAliasStack.peek();
String converted = StringHelper.replaceString(sqlFormulaSelect, tableAliasPlaceHolder,
tableAlias);
sb.append(COMMA);
sb.append(converted);
appendColumnAlias();
}
@Override
public void appendHistorySysPeriod() {
String tableAlias = tableAliasStack.peek();
sb.append(COMMA);
sb.append(historySupport.getSysPeriodLower(tableAlias));
appendColumnAlias();
sb.append(COMMA);
sb.append(historySupport.getSysPeriodUpper(tableAlias));
appendColumnAlias();
}
private void appendColumnAlias() {
if (useColumnAlias) {
sb.append(" ");
sb.append(columnAliasPrefix);
sb.append(columnIndex);
}
columnIndex++;
}
public void appendColumn(String column) {
appendColumn(tableAliasStack.peek(), column);
}
public void appendColumn(String tableAlias, String column) {
sb.append(COMMA);
if (column.contains("${}")) {
// support DB functions such as lower() etc
// with the use of secondary columns
String x = StringHelper.replaceString(column, "${}", tableAlias);
sb.append(x);
} else {
sb.append(tableAlias);
sb.append(PERIOD);
sb.append(column);
}
appendColumnAlias();
}
public String peekTableAlias() {
return tableAliasStack.peek();
}
public void appendRawColumn(String rawcolumnWithTableAlias) {
sb.append(COMMA);
sb.append(rawcolumnWithTableAlias);
appendColumnAlias();
}
public int length() {
return sb.length();
}
public String getContent() {
String s = sb.toString();
sb = new StringBuilder(STRING_BUILDER_INITIAL_CAPACITY);
return s;
}
public String toString() {
return "DefaultDbSqlContext: " + sb.toString();
}
}
@@ -0,0 +1,150 @@
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebean.Version;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.event.BeanFindController;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.OrmQueryEngine;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.Collection;
import java.util.List;
/**
* Main Finder implementation.
*/
public class DefaultOrmQueryEngine implements OrmQueryEngine {
/**
* Find using predicates
*/
private final CQueryEngine queryEngine;
/**
* Create the Finder.
*/
public DefaultOrmQueryEngine(CQueryEngine queryEngine) {
this.queryEngine = queryEngine;
}
/**
* Flushes the jdbc batch by default unless explicitly turned off on the transaction.
*/
private <T> void flushJdbcBatchOnQuery(OrmQueryRequest<T> request) {
SpiTransaction t = request.getTransaction();
if (t.isBatchFlushOnQuery()) {
// before we perform a query, we need to flush any
// previous persist requests that are queued/batched.
// The query may read data affected by those requests.
t.flushBatch();
}
}
public <T> int delete(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.delete(request);
}
public <T> int update(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.update(request);
}
public <T> int findRowCount(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.findRowCount(request);
}
public <A> List<A> findIds(OrmQueryRequest<?> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.findIds(request);
}
@Override
public <A> List<A> findSingleAttributeList(OrmQueryRequest<?> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.findSingleAttributeList(request);
}
public <T> QueryIterator<T> findIterate(OrmQueryRequest<T> request) {
// LIMITATION: You can not use QueryIterator to load bean cache
flushJdbcBatchOnQuery(request);
return queryEngine.findIterate(request);
}
@Override
public <T> List<Version<T>> findVersions(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
return queryEngine.findVersions(request);
}
public <T> BeanCollection<T> findMany(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
BeanFindController finder = request.getBeanFinder();
BeanCollection<T> result;
if (finder != null && finder.isInterceptFindMany(request)) {
// intercept this request
result = finder.findMany(request);
} else {
result = queryEngine.findMany(request);
}
SpiQuery<T> query = request.getQuery();
if (query.isLoadBeanCache()) {
// load the individual beans into the bean cache
BeanDescriptor<T> descriptor = request.getBeanDescriptor();
Collection<T> c = result.getActualDetails();
for (T bean : c) {
descriptor.cacheBeanPut((EntityBean) bean);
}
}
if (!result.isEmpty() && query.isUseQueryCache()) {
// load the query result into the query cache
request.putToQueryCache(result);
}
return result;
}
/**
* Find a single bean using its unique id.
*/
public <T> T findId(OrmQueryRequest<T> request) {
flushJdbcBatchOnQuery(request);
BeanFindController finder = request.getBeanFinder();
T result;
if (finder != null && finder.isInterceptFind(request)) {
result = finder.find(request);
} else {
result = queryEngine.find(request);
}
if (result != null && request.isUseBeanCache()) {
request.getBeanDescriptor().cacheBeanPut((EntityBean) result);
}
return result;
}
}
@@ -0,0 +1,109 @@
package io.ebeaninternal.server.query;
import io.ebean.SqlRow;
import io.ebeaninternal.server.core.Message;
import io.ebeaninternal.server.core.RelationalQueryEngine;
import io.ebeaninternal.server.core.RelationalQueryRequest;
import io.ebeaninternal.server.persist.Binder;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* Perform native sql fetches.
*/
public class DefaultRelationalQueryEngine implements RelationalQueryEngine {
private final Binder binder;
private final String dbTrueValue;
public DefaultRelationalQueryEngine(Binder binder, String dbTrueValue) {
this.binder = binder;
this.dbTrueValue = dbTrueValue == null ? "true" : dbTrueValue;
}
@Override
public void findEach(RelationalQueryRequest request, Predicate<SqlRow> consumer) {
long startTime = System.currentTimeMillis();
try {
request.executeSql(binder);
while (request.next()) {
if (!consumer.test(readRow(request))) {
break;
}
}
logSummary(request, startTime);
} catch (Exception e) {
throw new PersistenceException(Message.msg("fetch.error", e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
@Override
public void findEach(RelationalQueryRequest request, Consumer<SqlRow> consumer) {
long startTime = System.currentTimeMillis();
try {
request.executeSql(binder);
while (request.next()) {
consumer.accept(readRow(request));
}
logSummary(request, startTime);
} catch (Exception e) {
throw new PersistenceException(Message.msg("fetch.error", e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
public List<SqlRow> findList(RelationalQueryRequest request) {
long startTime = System.currentTimeMillis();
try {
request.executeSql(binder);
List<SqlRow> rows = new ArrayList<>();
while (request.next()) {
rows.add(readRow(request));
}
logSummary(request, startTime);
return rows;
} catch (Exception e) {
throw new PersistenceException(Message.msg("fetch.error", e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
private void logSummary(RelationalQueryRequest request, long startTime) {
if (request.isLogSummary()) {
long exeTime = System.currentTimeMillis() - startTime;
request.getTransaction().logSummary("SqlQuery rows[" + request.getRowCount() + "] time[" + exeTime + "] bind[" + request.getBindLog() + "]");
}
}
/**
* Read the row from the ResultSet and return as a MapBean.
*/
private SqlRow readRow(RelationalQueryRequest request) throws SQLException {
return request.createNewRow(dbTrueValue);
}
}
@@ -0,0 +1,176 @@
package io.ebeaninternal.server.query;
import io.ebean.SqlQuery;
import io.ebean.SqlRow;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* Used to return raw SQL query results.
* <p>
* Refer to {@link SqlQuery} for examples.
* </p>
* <p>
* There are convenience methods such as getInteger(), getBigDecimal() etc. The
* reason for these methods is that the values put into this map often come
* straight from the JDBC resultSet. Depending on the JDBC driver it may put a
* different type into a given property. For example an Integer, BigDecimal,
* Double could all be put into a property depending on the JDBC driver used.
* These convenience methods automatically convert the value as required
* returning the type you expect.
* </p>
*/
public class DefaultSqlRow implements SqlRow {
static final long serialVersionUID = -3120927797041336242L;
private final String dbTrueValue;
/**
* The underlying map of property data.
*/
private final Map<String, Object> map;
/**
* Create with an initialCapacity and loadFactor.
* <p>
* The defaults of these are 16 and 0.75.
* </p>
* <p>
* Note that the Map will rehash the contents when the number of keys in
* this map reaches its threshold (initialCapacity * loadFactor).
* </p>
*/
public DefaultSqlRow(int initialCapacity, float loadFactor, String dbTrueValue) {
this.map = new LinkedHashMap<>(initialCapacity, loadFactor);
this.dbTrueValue = dbTrueValue;
}
public Iterator<String> keys() {
return map.keySet().iterator();
}
/**
* Keys internally always lower cased to take out differences in database dictionaries.
*/
private Object asKey(Object name) {
return ((String) name).toLowerCase();
}
public Object remove(Object name) {
return map.remove(asKey(name));
}
public Object get(Object name) {
return map.get(asKey(name));
}
public Object put(String name, Object value) {
return setInternal(name, value);
}
public Object set(String name, Object value) {
return setInternal(name, value);
}
private Object setInternal(String name, Object newValue) {
return map.put(name.toLowerCase(), newValue);
}
public UUID getUUID(String name) {
return BasicTypeConverter.toUUID(get(name));
}
public Boolean getBoolean(String name) {
return BasicTypeConverter.toBoolean(get(name), dbTrueValue);
}
public Integer getInteger(String name) {
return BasicTypeConverter.toInteger(get(name));
}
public BigDecimal getBigDecimal(String name) {
return BasicTypeConverter.toBigDecimal(get(name));
}
public Long getLong(String name) {
return BasicTypeConverter.toLong(get(name));
}
public Double getDouble(String name) {
return BasicTypeConverter.toDouble(get(name));
}
public Float getFloat(String name) {
return BasicTypeConverter.toFloat(get(name));
}
public String getString(String name) {
return BasicTypeConverter.toString(get(name));
}
public java.util.Date getUtilDate(String name) {
return BasicTypeConverter.toUtilDate(get(name));
}
public Date getDate(String name) {
return BasicTypeConverter.toDate(get(name));
}
public Timestamp getTimestamp(String name) {
return BasicTypeConverter.toTimestamp(get(name));
}
public String toString() {
return map.toString();
}
// ------------------------------------
// Normal map methods...
public void clear() {
map.clear();
}
public boolean containsKey(Object key) {
return map.containsKey(asKey(key));
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Set<Map.Entry<String, Object>> entrySet() {
return map.entrySet();
}
public boolean isEmpty() {
return map.isEmpty();
}
public Set<String> keySet() {
return map.keySet();
}
public void putAll(Map<? extends String, ?> t) {
map.putAll(t);
}
public int size() {
return map.size();
}
public Collection<Object> values() {
return map.values();
}
}
@@ -0,0 +1,125 @@
package io.ebeaninternal.server.query;
import io.ebean.EbeanServer;
import io.ebean.PagedList;
import io.ebeaninternal.api.Monitor;
import io.ebeaninternal.api.SpiQuery;
import javax.persistence.PersistenceException;
import java.util.List;
import java.util.concurrent.Future;
/**
* PagedList implementation based on limit offset types of queries.
*
* @param <T> the entity bean type
*/
public class LimitOffsetPagedList<T> implements PagedList<T> {
private final transient EbeanServer server;
private final SpiQuery<T> query;
private final int firstRow;
private final int maxRows;
private final Monitor monitor = new Monitor();
private int foregroundTotalRowCount = -1;
private Future<Integer> futureRowCount;
private List<T> list;
/**
* Construct with firstRow/maxRows.
*/
public LimitOffsetPagedList(EbeanServer server, SpiQuery<T> query) {
this.server = server;
this.query = query;
this.maxRows = query.getMaxRows();
this.firstRow = query.getFirstRow();
}
public void loadCount() {
getFutureCount();
}
public Future<Integer> getFutureCount() {
synchronized (monitor) {
if (futureRowCount == null) {
futureRowCount = server.findFutureCount(query, null);
}
return futureRowCount;
}
}
public List<T> getList() {
synchronized (monitor) {
if (list == null) {
list = server.findList(query, null);
}
return list;
}
}
@Override
public int getPageIndex() {
if (firstRow == 0) {
return 0;
}
return ((firstRow - 1) / maxRows) + 1;
}
public int getTotalPageCount() {
int rowCount = getTotalCount();
if (rowCount == 0) {
return 0;
} else {
return ((rowCount - 1) / maxRows) + 1;
}
}
public int getTotalCount() {
synchronized (monitor) {
if (futureRowCount != null) {
try {
// background query already initiated so get it with a wait
return futureRowCount.get();
} catch (Exception e) {
throw new PersistenceException(e);
}
}
// already fetched?
if (foregroundTotalRowCount > -1) return foregroundTotalRowCount;
// just using foreground thread
foregroundTotalRowCount = server.findCount(query, null);
return foregroundTotalRowCount;
}
}
public boolean hasNext() {
return (firstRow + maxRows) < getTotalCount();
}
public boolean hasPrev() {
return firstRow > 0;
}
public int getPageSize() {
return maxRows;
}
public String getDisplayXtoYofZ(String to, String of) {
int first = firstRow + 1;
int last = firstRow + getList().size();
int total = getTotalCount();
return first + to + last + of + total;
}
}
@@ -0,0 +1,42 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.CQueryPlanKey;
/**
* QueryPlanKey for native sql queries.
*/
public class NativeSqlQueryPlanKey implements CQueryPlanKey {
private final String sql;
public NativeSqlQueryPlanKey(String sql) {
this.sql = sql;
}
public String toString() {
return getPartialKey();
}
/**
* Return as a partial key. For rawSql hash the sql is part of the key and as such
* needs to be included in order to have a complete key. Typically the MD5 of the sql
* can be used as a short form proxy for the actual sql.
*/
public String getPartialKey() {
return hashCode() + "_n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NativeSqlQueryPlanKey that = (NativeSqlQueryPlanKey) o;
return sql.equals(that.sql);
}
@Override
public int hashCode() {
return sql.hashCode();
}
}
@@ -0,0 +1,29 @@
package io.ebeaninternal.server.query;
import io.ebean.Version;
import java.sql.Timestamp;
import java.util.Comparator;
/**
* Compare Version beans in descending order with nulls last.
*/
class OrderVersionDesc implements Comparator<Version<?>> {
static final OrderVersionDesc INSTANCE = new OrderVersionDesc();
@Override
public int compare(Version<?> o1, Version<?> o2) {
Timestamp v1 = o1.getStart();
if (v1 == null) {
return 1;
}
Timestamp v2 = o2.getStart();
if (v2 == null) {
return -1;
}
return v1.compareTo(v2) * -1;
}
}
@@ -0,0 +1,39 @@
package io.ebeaninternal.server.query;
import io.ebean.FutureIds;
import io.ebean.Query;
import io.ebean.Transaction;
import java.util.List;
import java.util.concurrent.FutureTask;
/**
* Default implementation of FutureIds.
*/
public class QueryFutureIds<T> extends BaseFuture<List<Object>> implements FutureIds<T> {
private final CallableQueryIds<T> call;
public QueryFutureIds(CallableQueryIds<T> call) {
super(new FutureTask<>(call));
this.call = call;
}
public FutureTask<List<Object>> getFutureTask() {
return futureTask;
}
public Transaction getTransaction() {
return call.transaction;
}
public Query<T> getQuery() {
return call.query;
}
public boolean cancel(boolean mayInterruptIfRunning) {
call.query.cancel();
return super.cancel(mayInterruptIfRunning);
}
}
@@ -0,0 +1,75 @@
package io.ebeaninternal.server.query;
import io.ebean.FutureList;
import io.ebean.Query;
import io.ebean.Transaction;
import javax.persistence.PersistenceException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Default implementation for FutureList.
*/
public class QueryFutureList<T> extends BaseFuture<List<T>> implements FutureList<T> {
private final CallableQueryList<T> call;
public QueryFutureList(CallableQueryList<T> call) {
super(new FutureTask<>(call));
this.call = call;
}
public FutureTask<List<T>> getFutureTask() {
return futureTask;
}
public Transaction getTransaction() {
return call.transaction;
}
@Override
public Query<T> getQuery() {
return call.query;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
call.query.cancel();
return super.cancel(mayInterruptIfRunning);
}
@Override
public List<T> getUnchecked() {
try {
return get();
} catch (InterruptedException e) {
// restore the interrupted status (so client can check for that)
Thread.currentThread().interrupt();
throw new PersistenceException(e);
} catch (ExecutionException e) {
throw new PersistenceException(e);
}
}
@Override
public List<T> getUnchecked(long timeout, TimeUnit unit) throws TimeoutException {
try {
return get(timeout, unit);
} catch (InterruptedException e) {
// restore the interrupted status (so client can check for that)
Thread.currentThread().interrupt();
throw new PersistenceException(e);
} catch (ExecutionException e) {
throw new PersistenceException(e);
}
}
}
@@ -0,0 +1,39 @@
package io.ebeaninternal.server.query;
import io.ebean.FutureRowCount;
import io.ebean.Query;
import io.ebean.Transaction;
import java.util.concurrent.FutureTask;
/**
* Future implementation for the row count query.
*/
public class QueryFutureRowCount<T> extends BaseFuture<Integer> implements FutureRowCount<T> {
private final CallableQueryRowCount<T> call;
public QueryFutureRowCount(CallableQueryRowCount<T> call) {
super(new FutureTask<>(call));
this.call = call;
}
public FutureTask<Integer> getFutureTask() {
return futureTask;
}
public Transaction getTransaction() {
return call.transaction;
}
public Query<T> getQuery() {
return call.query;
}
public boolean cancel(boolean mayInterruptIfRunning) {
call.query.cancel();
return super.cancel(mayInterruptIfRunning);
}
}
@@ -0,0 +1,56 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.CQueryPlanKey;
/**
* QueryPlanKey for RawSql queries.
*/
class RawSqlQueryPlanKey implements CQueryPlanKey {
private final String sql;
private final boolean rawSql;
private final boolean rowNumberIncluded;
private final String logWhereSql;
RawSqlQueryPlanKey(String sql, boolean rawSql, boolean rowNumberIncluded, String logWhereSql) {
this.sql = sql;
this.rawSql = rawSql;
this.rowNumberIncluded = rowNumberIncluded;
this.logWhereSql = logWhereSql;
}
public String toString() {
return getPartialKey() + ":r";
}
/**
* Return as a partial key. For rawSql hash the sql is part of the key and as such
* needs to be included in order to have a complete key. Typically the MD5 of the sql
* can be used as a short form proxy for the actual sql.
*/
public String getPartialKey() {
return hashCode() + "_0";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RawSqlQueryPlanKey that = (RawSqlQueryPlanKey) o;
if (rawSql != that.rawSql) return false;
if (rowNumberIncluded != that.rowNumberIncluded) return false;
if (!sql.equals(that.sql)) return false;
return logWhereSql != null ? logWhereSql.equals(that.logWhereSql) : that.logWhereSql == null;
}
@Override
public int hashCode() {
int result = sql.hashCode();
result = 92821 * result + (rawSql ? 1 : 0);
result = 92821 * result + (rowNumberIncluded ? 1 : 0);
result = 92821 * result + logWhereSql.hashCode();
return result;
}
}
@@ -0,0 +1,81 @@
package io.ebeaninternal.server.query;
/**
* Helper for dot notation property paths.
*/
public class SplitName {
private static final char PERIOD = '.';
/**
* Add the two name sections together in dot notation.
*/
public static String add(String prefix, String name) {
if (prefix != null) {
return prefix + "." + name;
} else {
return name;
}
}
/**
* Return the number of occurrences of char in name.
*/
public static int count(String name) {
int count = 0;
for (int i = 0; i < name.length(); i++) {
if (PERIOD == name.charAt(i)) {
count++;
}
}
return count;
}
/**
* Return the parent part of the path.
*/
public static String parent(String name) {
if (name == null) {
return null;
} else {
String[] s = split(name, true);
return s[0];
}
}
/**
* Return the name split by last.
*/
public static String[] split(String name) {
return split(name, true);
}
/**
* Return the first part of the name.
*/
public static String begin(String name) {
return splitBegin(name)[0];
}
public static String[] splitBegin(String name) {
return split(name, false);
}
private static String[] split(String name, boolean last) {
int pos = last ? name.lastIndexOf('.') : name.indexOf('.');
if (pos == -1) {
if (last) {
return new String[]{null, name};
} else {
return new String[]{name, null};
}
} else {
String s0 = name.substring(0, pos);
String s1 = name.substring(pos + 1);
return new String[]{s0, s1};
}
}
}
@@ -0,0 +1,89 @@
package io.ebeaninternal.server.query;
import io.ebean.bean.EntityBean;
import io.ebean.bean.EntityBeanIntercept;
import io.ebeaninternal.api.SpiQuery.Mode;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DbReadContext;
import javax.persistence.PersistenceException;
/**
* Controls the loading of property data into a bean.
* <p>
* Takes into account the differences of lazy loading and
* partial objects.
* </p>
*/
public class SqlBeanLoad {
private final DbReadContext ctx;
private final EntityBean bean;
private final EntityBeanIntercept ebi;
private final Class<?> type;
private final boolean lazyLoading;
private final boolean refreshLoading;
private final boolean rawSql;
SqlBeanLoad(DbReadContext ctx, Class<?> type, EntityBean bean, Mode queryMode) {
this.ctx = ctx;
this.rawSql = ctx.isRawSql();
this.type = type;
this.lazyLoading = queryMode.equals(Mode.LAZYLOAD_BEAN);
this.refreshLoading = queryMode.equals(Mode.REFRESH_BEAN);
this.bean = bean;
this.ebi = bean == null ? null : bean._ebean_getIntercept();
}
/**
* Return true if this is a lazy loading.
*/
public boolean isLazyLoad() {
return lazyLoading;
}
/**
* Increment the resultSet index 1.
*/
public void loadIgnore(int increment) {
ctx.getDataReader().incrementPos(increment);
}
public Object load(BeanProperty prop) {
if (!rawSql && !prop.isLoadProperty(ctx.isDraftQuery())) {
return null;
}
if ((bean == null)
|| (lazyLoading && ebi.isLoadedProperty(prop.getPropertyIndex()))
|| (type != null && !prop.isAssignableFrom(type))) {
// ignore this property
// ... null: bean already in persistence context
// ... lazyLoading: partial bean that is lazy loading
// ... type: inheritance and not assignable to this instance
prop.loadIgnore(ctx);
return null;
}
try {
Object dbVal = prop.read(ctx);
if (!refreshLoading) {
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
return dbVal;
} catch (Exception e) {
String msg = "Error loading on " + prop.getFullBeanName();
throw new PersistenceException(msg, e);
}
}
}
@@ -0,0 +1,70 @@
package io.ebeaninternal.server.query;
/**
* Inner join, Outer join or automatic determination based on cardinality and optionality.
*/
public enum SqlJoinType {
/**
* It is an inner join.
*/
INNER("join"),
/**
* It is an outer join.
*/
OUTER("left join"),
/**
* It is automatically determined based on cardinality and optionality.
*/
AUTO("JOIN-TYPE-AUTO-LITERAL-NOT-USED");
final String literal;
SqlJoinType(String literal) {
this.literal = literal;
}
/**
* Return the SQL join literal.
*/
public String getLiteral() {
return literal;
}
/**
* Return the actual SQL join literal taking into account the current join type and the 'default
* join type as per deployment cardinality and optionality'.
*/
public String getLiteral(SqlJoinType deploymentJoinType) {
if (this == SqlJoinType.AUTO) {
return deploymentJoinType.getLiteral();
}
return this.getLiteral();
}
/**
* If this is an AUTO join set it to OUTER as we are joining to a Many.
*/
public SqlJoinType autoToOuter() {
if (this == AUTO) {
return OUTER;
} else {
return this;
}
}
/**
* If join is AUTO but deploymentJoinType is OUTER then go into OUTER join mode.
*/
public SqlJoinType autoToOuter(SqlJoinType deploymentJoinType) {
if (this == AUTO && deploymentJoinType == OUTER) {
return OUTER;
} else {
return this;
}
}
}
@@ -0,0 +1,157 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Represents the SELECT clause part of the SQL query.
*/
class SqlTree {
private final SqlTreeNode rootNode;
/**
* Property if resultSet contains master and detail rows.
*/
private final BeanPropertyAssocMany<?> manyProperty;
private final Set<String> includes;
/**
* Summary of the select being generated.
*/
private final String summary;
private final String selectSql;
private final String fromSql;
private final String groupBy;
/**
* Encrypted Properties require additional binding.
*/
private final BeanProperty[] encryptedProps;
/**
* Where clause for inheritance.
*/
private final String inheritanceWhereSql;
private final boolean includeJoins;
/**
* Create the SqlSelectClause.
*/
SqlTree(String summary, SqlTreeNode rootNode, String selectSql, String fromSql, String groupBy, String inheritanceWhereSql,
BeanProperty[] encryptedProps, BeanPropertyAssocMany<?> manyProperty, Set<String> includes, boolean includeJoins) {
this.summary = summary;
this.rootNode = rootNode;
this.selectSql = selectSql;
this.fromSql = fromSql;
this.groupBy = groupBy;
this.inheritanceWhereSql = inheritanceWhereSql;
this.encryptedProps = encryptedProps;
this.manyProperty = manyProperty;
this.includes = includes;
this.includeJoins = includeJoins;
}
/**
* Return true if the query includes joins (not valid for rawSql).
*/
boolean isIncludeJoins() {
return includeJoins;
}
/**
* Recurse through the tree adding an table alias' for @History entity beans.
*/
void addAsOfTableAlias(SpiQuery<?> query) {
rootNode.addAsOfTableAlias(query);
}
/**
* Recurse through the tree adding soft delete predicates as necessary.
*/
void addSoftDeletePredicate(SpiQuery<?> query) {
rootNode.addSoftDeletePredicate(query);
}
/**
* Build a select expression chain for RawSql.
*/
public List<String> buildRawSqlSelectChain() {
ArrayList<String> list = new ArrayList<>();
rootNode.buildRawSqlSelectChain(list);
return list;
}
/**
* Return the includes. Associated beans lists etc.
*/
public Set<String> getIncludes() {
return includes;
}
/**
* Return the String for the actual SQL.
*/
String getSelectSql() {
return selectSql;
}
String getFromSql() {
return fromSql;
}
/**
* Return the groupBy clause.
*/
String getGroupBy() {
return groupBy;
}
/**
* Return the where clause for inheritance.
*/
String getInheritanceWhereSql() {
return inheritanceWhereSql;
}
/**
* Return a summary of the select clause.
*/
public String getSummary() {
return summary;
}
SqlTreeNode getRootNode() {
return rootNode;
}
/**
* Return the property that is associated with the many. There can only be one
* per SqlSelect. This can be null.
*/
BeanPropertyAssocMany<?> getManyProperty() {
return manyProperty;
}
BeanProperty[] getEncryptedProps() {
return encryptedProps;
}
/**
* Return true if the query has a many join.
*/
boolean hasMany() {
return manyProperty != null || rootNode.hasMany();
}
}
@@ -0,0 +1,217 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.el.ElPropertyDeploy;
import javax.persistence.PersistenceException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* Special Map of the logical property joins to table alias.
*/
class SqlTreeAlias {
private int counter;
private int manyWhereCounter;
private final TreeSet<String> joinProps = new TreeSet<>();
private HashSet<String> embeddedPropertyJoins;
private final TreeSet<String> manyWhereJoinProps = new TreeSet<>();
private final HashMap<String, String> aliasMap = new HashMap<>();
private final HashMap<String, String> manyWhereAliasMap = new HashMap<>();
private final String rootTableAlias;
SqlTreeAlias(String rootTableAlias) {
this.rootTableAlias = rootTableAlias;
}
/**
* Add joins to support where predicates
*/
void addManyWhereJoins(Set<String> manyWhereJoins) {
if (manyWhereJoins != null) {
for (String include : manyWhereJoins) {
addPropertyJoin(include, manyWhereJoinProps);
}
}
}
private void addEmbeddedPropertyJoin(String embProp) {
if (embeddedPropertyJoins == null) {
embeddedPropertyJoins = new HashSet<>();
}
embeddedPropertyJoins.add(embProp);
}
/**
* Add joins.
*/
public void addJoin(Set<String> propJoins, BeanDescriptor<?> desc) {
if (propJoins != null) {
for (String propJoin : propJoins) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(propJoin);
if (elProp == null) {
throw new PersistenceException("Invalid path " + propJoin + " from " + desc.getFullName());
} else if (elProp.getBeanProperty().isEmbedded()) {
addEmbeddedPropertyJoin(propJoin);
} else {
addPropertyJoin(propJoin, joinProps);
}
}
}
}
private void addPropertyJoin(String include, TreeSet<String> set) {
if (set.add(include)) {
String[] split = SplitName.split(include);
if (split[0] != null) {
addPropertyJoin(split[0], set);
}
}
}
/**
* Build a set of table alias for the given bean and fetch joined properties.
*/
void buildAlias() {
for (String joinProp : joinProps) {
calcAlias(joinProp);
}
for (String joinProp : manyWhereJoinProps) {
calcAliasManyWhere(joinProp);
}
mapEmbeddedPropertyAlias();
}
private void mapEmbeddedPropertyAlias() {
if (embeddedPropertyJoins != null) {
for (String propJoin : embeddedPropertyJoins) {
String[] split = SplitName.split(propJoin);
// the table alias of the parent path
String alias = getTableAlias(split[0]);
aliasMap.put(propJoin, alias);
}
}
}
private String calcAlias(String prefix) {
String alias = nextTableAlias();
aliasMap.put(prefix, alias);
return alias;
}
private void calcAliasManyWhere(String prefix) {
String alias = nextManyWhereTableAlias();
manyWhereAliasMap.put(prefix, alias);
}
/**
* Return the table alias for a given property name.
*/
String getTableAlias(String prefix) {
if (prefix == null) {
return rootTableAlias;
} else {
String s = aliasMap.get(prefix);
if (s == null) {
return calcAlias(prefix);
}
return s;
}
}
/**
* Return an alias using "Many where joins".
*/
String getTableAliasManyWhere(String prefix) {
if (prefix == null) {
return rootTableAlias;
}
String s = manyWhereAliasMap.get(prefix);
if (s == null) {
s = aliasMap.get(prefix);
}
if (s == null) {
String msg = "Could not determine table alias for [" + prefix + "] manyMap[" + manyWhereAliasMap + "] aliasMap[" + aliasMap + "]";
throw new RuntimeException(msg);
}
return s;
}
/**
* Parse for where clauses that uses "Many where joins"
*/
String parseWhere(String clause) {
clause = parseRootAlias(clause);
clause = parseAliasMap(clause, manyWhereAliasMap);
return parseAliasMap(clause, aliasMap);
}
/**
* Parse without using any extra "Many where joins".
*/
public String parse(String clause) {
clause = parseRootAlias(clause);
return parseAliasMap(clause, aliasMap);
}
/**
* Parse the clause replacing the table alias place holders.
*/
private String parseRootAlias(String clause) {
if (rootTableAlias == null) {
return clause.replace("${}", "");
} else {
return clause.replace("${}", rootTableAlias + ".");
}
}
/**
* Parse the clause replacing the table alias place holders.
*/
private String parseAliasMap(String clause, HashMap<String, String> parseAliasMap) {
for (Map.Entry<String, String> e : parseAliasMap.entrySet()) {
String k = "${" + e.getKey() + "}";
clause = clause.replace(k, e.getValue() + ".");
}
return clause;
}
/**
* Return the next valid table alias given the preferred table alias.
*/
private String nextTableAlias() {
return "t" + (++counter);
}
private String nextManyWhereTableAlias() {
return "u" + (++manyWhereCounter);
}
/**
* Return true if there are joins included in the query.
*/
boolean isIncludeJoins() {
return !aliasMap.isEmpty() || !manyWhereAliasMap.isEmpty();
}
}
@@ -0,0 +1,682 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.api.PropertyJoin;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiQuery.Type;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.TableJoin;
import io.ebeaninternal.server.el.ElPropertyValue;
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
import io.ebeaninternal.server.querydefn.OrmQueryProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Factory for SqlTree.
*/
public class SqlTreeBuilder {
private static final Logger logger = LoggerFactory.getLogger(SqlTreeBuilder.class);
private final SpiQuery<?> query;
private final BeanDescriptor<?> desc;
private final OrmQueryDetail queryDetail;
private final StringBuilder summary = new StringBuilder();
private final CQueryPredicates predicates;
private final boolean subQuery;
/**
* Property if resultSet contains master and detail rows.
*/
private BeanPropertyAssocMany<?> manyProperty;
private final SqlTreeAlias alias;
private final DefaultDbSqlContext ctx;
private final HashSet<String> selectIncludes = new HashSet<>();
private final ManyWhereJoins manyWhereJoins;
private final TableJoin includeJoin;
private final boolean rawSql;
/**
* rawNoId true if the RawSql does not include the @Id property
*/
private final boolean rawNoId;
private final boolean disableLazyLoad;
private final SpiQuery.TemporalMode temporalMode;
private SqlTreeNode rootNode;
/**
* Construct for RawSql query.
*/
public SqlTreeBuilder(OrmQueryRequest<?> request, CQueryPredicates predicates, OrmQueryDetail queryDetail, boolean rawNoId) {
this.rawSql = true;
this.desc = request.getBeanDescriptor();
this.rawNoId = rawNoId;
this.disableLazyLoad = request.getQuery().isDisableLazyLoading();
this.query = null;
this.subQuery = false;
this.queryDetail = queryDetail;
this.predicates = predicates;
this.temporalMode = SpiQuery.TemporalMode.CURRENT;
this.includeJoin = null;
this.manyWhereJoins = null;
this.alias = null;
this.ctx = null;
}
/**
* The predicates are used to determine if 'extra' joins are required to
* support the where and/or order by clause. If so these extra joins are added
* to the root node.
*/
public SqlTreeBuilder(String tableAliasPlaceHolder, String columnAliasPrefix,
OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
this.rawSql = false;
this.rawNoId = false;
this.desc = request.getBeanDescriptor();
this.query = request.getQuery();
this.temporalMode = SpiQuery.TemporalMode.of(query);
this.disableLazyLoad = query.isDisableLazyLoading();
this.subQuery = Type.SUBQUERY.equals(query.getType()) || Type.ID_LIST.equals(query.getType());
this.includeJoin = query.getM2mIncludeJoin();
this.manyWhereJoins = query.getManyWhereJoins();
this.queryDetail = query.getDetail();
this.predicates = predicates;
this.alias = new SqlTreeAlias(request.getBaseTableAlias());
this.ctx = new DefaultDbSqlContext(alias, tableAliasPlaceHolder, columnAliasPrefix, !subQuery, historySupport, draftSupport);
}
/**
* Build based on the includes and using the BeanJoinTree.
*/
public SqlTree build() {
summary.append(desc.getName());
// build the appropriate chain of SelectAdapter's
buildRoot(desc);
// build the actual String
String selectSql = null;
String fromSql = null;
String inheritanceWhereSql = null;
String groupBy = null;
BeanProperty[] encryptedProps = null;
if (!rawSql) {
selectSql = buildSelectClause();
fromSql = buildFromClause();
inheritanceWhereSql = buildWhereClause();
groupBy = buildGroupByClause();
encryptedProps = ctx.getEncryptedProps();
}
boolean includeJoins = alias != null && alias.isIncludeJoins();
return new SqlTree(summary.toString(), rootNode, selectSql, fromSql, groupBy, inheritanceWhereSql, encryptedProps,
manyProperty, queryDetail.getFetchPaths(), includeJoins);
}
private String buildSelectClause() {
if (rawSql) {
return "Not Used";
}
rootNode.appendSelect(ctx, subQuery);
return trimComma(ctx.getContent());
}
private String buildGroupByClause() {
if (rawSql || !rootNode.isAggregation()) {
return null;
}
ctx.startGroupBy();
rootNode.appendGroupBy(ctx, subQuery);
String groupBy = ctx.getContent();
return trimComma(groupBy);
}
/**
* Trim the first comma.
*/
private String trimComma(String groupBy) {
if (groupBy.length() >= SqlTreeNode.COMMA.length()) {
groupBy = groupBy.substring(SqlTreeNode.COMMA.length());
}
return groupBy;
}
private String buildWhereClause() {
if (rawSql) {
return "Not Used";
}
rootNode.appendWhere(ctx);
return ctx.getContent();
}
private String buildFromClause() {
if (rawSql) {
return "Not Used";
}
rootNode.appendFrom(ctx, SqlJoinType.AUTO);
return ctx.getContent();
}
private void buildRoot(BeanDescriptor<?> desc) {
rootNode = buildSelectChain(null, null, desc, null);
if (!rawSql) {
alias.addJoin(queryDetail.getFetchPaths(), desc);
alias.addJoin(predicates.getPredicateIncludes(), desc);
alias.addManyWhereJoins(manyWhereJoins.getPropertyNames());
// build set of table alias
alias.buildAlias();
predicates.parseTableAlias(alias);
}
}
/**
* Recursively build the query tree depending on what leaves in the tree
* should be included.
*/
private SqlTreeNode buildSelectChain(String prefix, BeanPropertyAssoc<?> prop,
BeanDescriptor<?> desc, List<SqlTreeNode> joinList) {
List<SqlTreeNode> myJoinList = new ArrayList<>();
BeanPropertyAssocOne<?>[] ones = desc.propertiesOne();
for (BeanPropertyAssocOne<?> one : ones) {
String propPrefix = SplitName.add(prefix, one.getName());
if (isIncludeBean(propPrefix)) {
selectIncludes.add(propPrefix);
buildSelectChain(propPrefix, one, one.getTargetDescriptor(), myJoinList);
}
}
BeanPropertyAssocMany<?>[] manys = desc.propertiesMany();
for (BeanPropertyAssocMany<?> many : manys) {
String propPrefix = SplitName.add(prefix, many.getName());
if (isIncludeMany(propPrefix, many)) {
selectIncludes.add(propPrefix);
buildSelectChain(propPrefix, many, many.getTargetDescriptor(), myJoinList);
}
}
OrmQueryProperties queryProps = queryDetail.getChunk(prefix, false);
SqlTreeProperties props = getBaseSelect(desc, queryProps);
if (prefix == null && !rawSql) {
if (props.requireSqlDistinct(manyWhereJoins)) {
query.setSqlDistinct(true);
}
addManyWhereJoins(myJoinList);
}
SqlTreeNode selectNode = buildNode(prefix, prop, desc, myJoinList, props);
if (joinList != null) {
joinList.add(selectNode);
}
return selectNode;
}
/**
* Add joins used to support where clause predicates on 'many' properties.
* <p>
* These joins are effectively independent of any fetch joins on 'many'
* properties.
* </p>
*/
private void addManyWhereJoins(List<SqlTreeNode> myJoinList) {
Collection<PropertyJoin> includes = manyWhereJoins.getPropertyJoins();
for (PropertyJoin joinProp : includes) {
BeanPropertyAssoc<?> beanProperty = (BeanPropertyAssoc<?>) desc.getBeanPropertyFromPath(joinProp.getProperty());
SqlTreeNodeManyWhereJoin nodeJoin = new SqlTreeNodeManyWhereJoin(joinProp.getProperty(), beanProperty, joinProp.getSqlJoinType());
myJoinList.add(nodeJoin);
}
}
private SqlTreeNode buildNode(String prefix, BeanPropertyAssoc<?> prop, BeanDescriptor<?> desc, List<SqlTreeNode> myList, SqlTreeProperties props) {
if (prefix == null) {
buildExtraJoins(desc, myList);
// Optional many property for lazy loading query
BeanPropertyAssocMany<?> lazyLoadMany = (query == null) ? null : query.getLazyLoadMany();
boolean withId = !rawNoId && !subQuery && (query == null || query.isWithId());
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany, temporalMode, disableLazyLoad);
} else if (prop instanceof BeanPropertyAssocMany<?>) {
return new SqlTreeNodeManyRoot(prefix, (BeanPropertyAssocMany<?>) prop, props, myList, temporalMode, disableLazyLoad);
} else {
return new SqlTreeNodeBean(prefix, prop, props, myList, temporalMode, disableLazyLoad);
}
}
/**
* Build extra joins to support properties used in where clause but not
* already in select clause.
*/
private void buildExtraJoins(BeanDescriptor<?> desc, List<SqlTreeNode> myList) {
if (rawSql) {
return;
}
Set<String> predicateIncludes = predicates.getPredicateIncludes();
if (predicateIncludes == null) {
return;
}
// Note includes - basically means joins.
// The selectIncludes is the set of joins that are required to support
// the 'select' part of the query. We may need to add other joins to
// support the predicates or order by clauses.
// remove ManyWhereJoins from the predicateIncludes
predicateIncludes.removeAll(manyWhereJoins.getPropertyNames());
predicateIncludes.addAll(predicates.getOrderByIncludes());
// look for predicateIncludes that are not in selectIncludes and add
// them as extra joins to the query
IncludesDistiller extraJoinDistill = new IncludesDistiller(desc, selectIncludes, predicateIncludes);
Collection<SqlTreeNodeExtraJoin> extraJoins = extraJoinDistill.getExtraJoinRootNodes();
if (!extraJoins.isEmpty()) {
// add extra joins required to support predicates
// and/or order by clause
for (SqlTreeNodeExtraJoin extraJoin : extraJoins) {
myList.add(extraJoin);
if (extraJoin.isManyJoin()) {
// as we are now going to join to the many then we need
// to add the distinct to the sql query to stop duplicate
// rows...
query.setSqlDistinct(true);
}
}
}
}
/**
* A subQuery has slightly different rules in that it just generates SQL (into
* the where clause) and its properties are not required to read the resultSet
* etc.
* <p>
* This means it can included individual properties of an embedded bean.
* </p>
*/
private void addPropertyToSubQuery(SqlTreeProperties selectProps, BeanDescriptor<?> desc, String propName) {
BeanProperty p = desc.findBeanProperty(propName);
if (p == null) {
logger.error("property [" + propName + "]not found on " + desc + " for query - excluding it.");
} else if (p instanceof BeanPropertyAssoc<?> && p.isEmbedded()) {
// if the property is embedded we need to lookup the real column name
int pos = propName.indexOf(".");
if (pos > -1) {
String name = propName.substring(pos + 1);
p = ((BeanPropertyAssoc<?>) p).getTargetDescriptor().findBeanProperty(name);
}
}
selectProps.add(p);
}
private void addProperty(SqlTreeProperties selectProps, BeanDescriptor<?> desc,
OrmQueryProperties queryProps, String propName) {
if (subQuery) {
addPropertyToSubQuery(selectProps, desc, propName);
return;
}
int basePos = propName.indexOf('.');
if (basePos > -1) {
// property on an embedded bean. Embedded beans do not yet
// support being partially populated so we include the
// 'base' property and make sure we only do that once
String baseName = propName.substring(0, basePos);
// make sure we only included the base/embedded bean once
if (!selectProps.containsProperty(baseName)) {
BeanProperty p = desc.findBeanProperty(baseName);
if (p == null) {
logger.error("property [" + propName + "] not found on " + desc + " for query - excluding it.");
} else if (p.isEmbedded()) {
// add the embedded bean (and effectively
// all its properties)
selectProps.add(p);
} else {
String m = "property [" + p.getFullBeanName() + "] expected to be an embedded bean for query - excluding it.";
logger.error(m);
}
}
} else {
// find the property including searching the
// sub class hierarchy if required
BeanProperty p = desc.findBeanProperty(propName);
if (p == null) {
logger.error("property [" + propName + "] not found on " + desc + " for query - excluding it.");
p = desc.findBeanProperty("id");
selectProps.add(p);
} else if (p.isId()) {
// do not bother to include id for normal queries as the
// id is always added (except for subQueries)
} else if (p instanceof BeanPropertyAssoc<?>) {
// need to check if this property should be
// excluded. This occurs when this property is
// included as a bean join. With a bean join
// the property should be excluded as the bean
// join has its own node in the SqlTree.
if (!queryProps.isIncludedBeanJoin(p.getName())) {
// include the property... which basically
// means include the foreign key column(s)
selectProps.add(p);
}
} else {
selectProps.add(p);
}
}
}
private SqlTreeProperties getBaseSelectPartial(BeanDescriptor<?> desc, OrmQueryProperties queryProps) {
SqlTreeProperties selectProps = new SqlTreeProperties();
selectProps.setReadOnly(queryProps.isReadOnly());
// add properties in the order in which they appear
// in the query. Gives predictable sql/properties for
// use with SqlSelect type queries.
// Also note that this can include transient properties.
// This makes sense for transient properties used to
// hold sum() count() type values (with SqlSelect)
for (String propName : queryProps.getSelectProperties()) {
if (!propName.isEmpty()) {
addProperty(selectProps, desc, queryProps, propName);
}
}
return selectProps;
}
private SqlTreeProperties getBaseSelect(BeanDescriptor<?> desc, OrmQueryProperties queryProps) {
boolean partial = queryProps != null && !queryProps.allProperties();
if (partial) {
return getBaseSelectPartial(desc, queryProps);
}
SqlTreeProperties selectProps = new SqlTreeProperties();
selectProps.setAllProperties();
// normal simple properties of the bean
selectProps.add(desc.propertiesBaseScalar());
selectProps.add(desc.propertiesEmbedded());
BeanPropertyAssocOne<?>[] propertiesOne = desc.propertiesOne();
for (BeanPropertyAssocOne<?> aPropertiesOne : propertiesOne) {
//noinspection StatementWithEmptyBody
if (queryProps != null && queryProps.isIncludedBeanJoin(aPropertiesOne.getName())) {
// if it is a joined bean... then don't add the property
// as it will have its own entire Node in the SqlTree
} else {
selectProps.add(aPropertiesOne);
}
}
InheritInfo inheritInfo = desc.getInheritInfo();
if (inheritInfo != null) {
// add sub type properties
inheritInfo.addChildrenProperties(selectProps);
}
return selectProps;
}
/**
* Return true if this many node should be included in the query.
*/
private boolean isIncludeMany(String propName, BeanPropertyAssocMany<?> manyProp) {
if (queryDetail.isJoinsEmpty()) {
return false;
}
if (queryDetail.includesPath(propName)) {
if (manyProperty != null) {
// only one many associated allowed to be included in fetch
if (logger.isDebugEnabled()) {
logger.debug("Not joining [" + propName + "] as already joined to a Many[" + manyProperty + "].");
}
return false;
}
manyProperty = manyProp;
summary.append(" +many:").append(propName);
return true;
}
return false;
}
/**
* Test to see if we are including this node into the query.
* <p>
* Return true if this node is FULLY included resulting in table join. If the
* node is not included but its parent has been included then a "bean proxy"
* is added and false is returned.
* </p>
*/
private boolean isIncludeBean(String prefix) {
if (queryDetail.includesPath(prefix)) {
// explicitly included
summary.append(", ").append(prefix);
String[] splitNames = SplitName.split(prefix);
queryDetail.includeBeanJoin(splitNames[0], splitNames[1]);
return true;
}
return false;
}
/**
* Takes the select includes and the predicates includes and determines the
* extra joins required to support the predicates (that are not already
* supported by the select includes).
* <p>
* This returns ONLY the leaves. The joins for the leaves
* </p>
*/
private static class IncludesDistiller {
private final Set<String> selectIncludes;
private final Set<String> predicateIncludes;
/**
* Contains the 'root' extra joins. We only return the roots back.
*/
private final Map<String, SqlTreeNodeExtraJoin> joinRegister = new HashMap<>();
/**
* Register of all the extra join nodes.
*/
private final Map<String, SqlTreeNodeExtraJoin> rootRegister = new HashMap<>();
private final BeanDescriptor<?> desc;
private IncludesDistiller(BeanDescriptor<?> desc, Set<String> selectIncludes,
Set<String> predicateIncludes) {
this.desc = desc;
this.selectIncludes = selectIncludes;
this.predicateIncludes = predicateIncludes;
}
/**
* Build the collection of extra joins returning just the roots.
* <p>
* each root returned here could contain a little tree of joins. This
* follows the more natural pattern and allows for forcing outer joins from
* a join to a 'many' down through the rest of its tree.
* </p>
*/
private Collection<SqlTreeNodeExtraJoin> getExtraJoinRootNodes() {
String[] extras = findExtras();
if (extras.length == 0) {
return rootRegister.values();
}
// sort so we process only getting the leaves
// excluding nodes between root and the leaf
Arrays.sort(extras);
// reverse order so get the leaves first...
for (String extra : extras) {
createExtraJoin(extra);
}
return rootRegister.values();
}
private void createExtraJoin(String includeProp) {
SqlTreeNodeExtraJoin extraJoin = createJoinLeaf(includeProp);
if (extraJoin != null) {
// add the extra join...
// find root of this extra join... linking back to the
// parents (creating the tree) as it goes.
SqlTreeNodeExtraJoin root = findExtraJoinRoot(includeProp, extraJoin);
// register the root because these are the only ones we
// return back.
rootRegister.put(root.getName(), root);
}
}
/**
* Create a SqlTreeNodeExtraJoin, register and return it.
*/
private SqlTreeNodeExtraJoin createJoinLeaf(String propertyName) {
ElPropertyValue elGetValue = desc.getElGetValue(propertyName);
if (elGetValue == null) {
// this can occur for master detail queries
// with concatenated keys (so not an error now)
return null;
}
BeanProperty beanProperty = elGetValue.getBeanProperty();
if (beanProperty instanceof BeanPropertyAssoc<?>) {
BeanPropertyAssoc<?> assocProp = (BeanPropertyAssoc<?>) beanProperty;
if (assocProp.isEmbedded()) {
// no extra join required for embedded beans
return null;
}
SqlTreeNodeExtraJoin extraJoin = new SqlTreeNodeExtraJoin(propertyName, assocProp, elGetValue.containsMany());
joinRegister.put(propertyName, extraJoin);
return extraJoin;
}
return null;
}
/**
* Find the root the this extra join tree.
* <p>
* This may need to create a parent join implicitly if a predicate join
* 'skips' a level. e.g. where details.user.id = 1 (maybe join to details is
* not specified and is implicitly created.
* </p>
*/
private SqlTreeNodeExtraJoin findExtraJoinRoot(String includeProp,
SqlTreeNodeExtraJoin childJoin) {
int dotPos = includeProp.lastIndexOf('.');
if (dotPos == -1) {
// no parent possible(parent is root)
return childJoin;
} else {
// look in register ...
String parentPropertyName = includeProp.substring(0, dotPos);
if (selectIncludes.contains(parentPropertyName)) {
// parent already handled by select
return childJoin;
}
SqlTreeNodeExtraJoin parentJoin = joinRegister.get(parentPropertyName);
if (parentJoin == null) {
// we need to create this the parent implicitly...
parentJoin = createJoinLeaf(parentPropertyName);
}
parentJoin.addChild(childJoin);
return findExtraJoinRoot(parentPropertyName, parentJoin);
}
}
/**
* Find the extra joins required by predicates and not already taken care of
* by the select.
*/
private String[] findExtras() {
List<String> extras = new ArrayList<>();
for (String predProp : predicateIncludes) {
if (!selectIncludes.contains(predProp)) {
extras.add(predProp);
}
}
return extras.toArray(new String[extras.size()]);
}
}
}
@@ -0,0 +1,82 @@
package io.ebeaninternal.server.query;
import io.ebean.Version;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import java.sql.SQLException;
import java.util.List;
interface SqlTreeNode {
String COMMA = ", ";
/**
* Build the select chain for a RawSql query.
*/
void buildRawSqlSelectChain(List<String> selectChain);
/**
* Return true if this node includes an aggregation.
*/
boolean isAggregation();
/**
* Append the required column information to the SELECT part of the sql
* statement.
*/
void appendSelect(DbSqlContext ctx, boolean subQuery);
/**
* Append the group by clause.
*/
void appendGroupBy(DbSqlContext ctx, boolean subQuery);
/**
* Append to the FROM part of the sql.
*/
void appendFrom(DbSqlContext ctx, SqlJoinType joinType);
/**
* Append any where predicates for inheritance.
*/
void appendWhere(DbSqlContext ctx);
/**
* Recurse through the tree adding an table alias' for @History entity beans.
*/
void addAsOfTableAlias(SpiQuery<?> query);
/**
* Recurse through the tree adding soft delete predicates if necessary.
*/
void addSoftDeletePredicate(SpiQuery<?> query);
/**
* Load the appropriate information from the SqlSelectReader.
* <p>
* At a high level this actually controls the reading of the data from the
* jdbc resultSet and putting it into the bean etc.
* </p>
*/
EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean contextBean) throws SQLException;
/**
* Load a version of a @History bean with effective dates.
*/
<T> Version<T> loadVersion(DbReadContext ctx) throws SQLException;
/**
* Return true if the query has a many join.
*/
boolean hasMany();
/**
* Return the property for singleAttribute query.
*/
BeanProperty getSingleProperty();
}
@@ -0,0 +1,612 @@
package io.ebeaninternal.server.query;
import io.ebean.Version;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.bean.EntityBeanIntercept;
import io.ebean.bean.PersistenceContext;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiQuery.Mode;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.TableJoin;
import io.ebeaninternal.server.deploy.id.IdBinder;
import io.ebeaninternal.server.lib.util.StringHelper;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Normal bean included in the query.
*/
class SqlTreeNodeBean implements SqlTreeNode {
private static final SqlTreeNode[] NO_CHILDREN = new SqlTreeNode[0];
protected final BeanDescriptor<?> desc;
private final IdBinder idBinder;
/**
* The children which will be other SelectBean or SelectProxyBean.
*/
protected final SqlTreeNode[] children;
/**
* Set to true if this is a partial object fetch.
*/
private final boolean partialObject;
protected final BeanProperty[] properties;
/**
* Extra where clause added by Where annotation on associated many.
*/
private final String extraWhere;
private final BeanPropertyAssoc<?> nodeBeanProp;
/**
* False if report bean and has no id property.
*/
private final boolean readId;
private final boolean disableLazyLoad;
protected final InheritInfo inheritInfo;
protected final String prefix;
private final Map<String, String> pathMap;
final BeanPropertyAssocMany<?> lazyLoadParent;
final SpiQuery.TemporalMode temporalMode;
private final boolean temporalVersions;
private final IdBinder lazyLoadParentIdBinder;
String baseTableAlias;
/**
* Table alias set if this bean node includes a join to a intersection
* table and that table has history support.
*/
private boolean intersectionAsOfTableAlias;
private final boolean aggregation;
/**
* Construct for leaf node.
*/
SqlTreeNodeBean(String prefix, BeanPropertyAssoc<?> beanProp, SqlTreeProperties props,
List<SqlTreeNode> myChildren, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
this(prefix, beanProp, beanProp.getTargetDescriptor(), props, myChildren, true, null, temporalMode, disableLazyLoad);
}
/**
* Construct for root node.
*/
SqlTreeNodeBean(BeanDescriptor<?> desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId,
BeanPropertyAssocMany<?> many, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
this(null, null, desc, props, myList, withId, many, temporalMode, disableLazyLoad);
}
/**
* Create with the appropriate node.
*/
private SqlTreeNodeBean(String prefix, BeanPropertyAssoc<?> beanProp, BeanDescriptor<?> desc, SqlTreeProperties props,
List<SqlTreeNode> myChildren, boolean withId, BeanPropertyAssocMany<?> lazyLoadParent,
SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
this.lazyLoadParent = lazyLoadParent;
this.lazyLoadParentIdBinder = (lazyLoadParent == null) ? null : lazyLoadParent.getBeanDescriptor().getIdBinder();
this.prefix = prefix;
this.desc = desc;
this.inheritInfo = desc.getInheritInfo();
this.idBinder = desc.getIdBinder();
this.temporalMode = temporalMode;
this.temporalVersions = temporalMode == SpiQuery.TemporalMode.VERSIONS;
this.nodeBeanProp = beanProp;
this.extraWhere = (beanProp == null) ? null : beanProp.getExtraWhere();
// the bean has an Id property and we want to use it
this.readId = withId && (desc.getIdProperty() != null);
this.disableLazyLoad = disableLazyLoad || !readId || desc.isRawSqlBased() || temporalVersions;
this.partialObject = props.isPartialObject();
this.properties = props.getProps();
this.aggregation = props.isAggregation();
this.children = myChildren == null ? NO_CHILDREN : myChildren.toArray(new SqlTreeNode[myChildren.size()]);
pathMap = createPathMap(prefix, desc);
}
@Override
public BeanProperty getSingleProperty() {
return properties[0];
}
private Map<String, String> createPathMap(String prefix, BeanDescriptor<?> desc) {
BeanPropertyAssocMany<?>[] manys = desc.propertiesMany();
HashMap<String, String> m = new HashMap<>();
for (BeanPropertyAssocMany<?> many : manys) {
String name = many.getName();
m.put(name, getPath(prefix, name));
}
return m;
}
private String getPath(String prefix, String propertyName) {
if (prefix == null) {
return propertyName;
} else {
return prefix + "." + propertyName;
}
}
public void buildRawSqlSelectChain(List<String> selectChain) {
if (readId) {
if (desc.hasInheritance()) {
// discriminator column always proceeds id column
selectChain.add(getPath(prefix, desc.getInheritInfo().getDiscriminatorColumn()));
}
idBinder.buildRawSqlSelectChain(prefix, selectChain);
}
for (BeanProperty property : properties) {
property.buildRawSqlSelectChain(prefix, selectChain);
}
// recursively continue reading...
for (SqlTreeNode aChildren : children) {
// read each child... and let them set their
// values back to this localBean
aChildren.buildRawSqlSelectChain(selectChain);
}
}
/**
* Read the version bean.
*/
@SuppressWarnings("unchecked")
public <T> Version<T> loadVersion(DbReadContext ctx) throws SQLException {
// read the sys period lower and upper bounds
// these are always the first 2 columns in the resultSet
Timestamp start = ctx.getDataReader().getTimestamp();
Timestamp end = ctx.getDataReader().getTimestamp();
T bean = (T) load(ctx, null, null);
return new Version<>(bean, start, end);
}
/**
* read the properties from the resultSet.
*/
public EntityBean load(DbReadContext ctx, EntityBean parentBean, EntityBean contextParent) throws SQLException {
Object lazyLoadParentId = null;
if (lazyLoadParentIdBinder != null) {
lazyLoadParentId = lazyLoadParentIdBinder.read(ctx);
}
// bean already existing in the persistence context
EntityBean contextBean = null;
Class<?> localType;
BeanDescriptor<?> localDesc;
IdBinder localIdBinder;
EntityBean localBean;
if (inheritInfo != null) {
InheritInfo localInfo = inheritInfo.readType(ctx);
if (localInfo == null) {
// the bean must be null
localIdBinder = idBinder;
localBean = null;
localType = null;
localDesc = desc;
} else {
localBean = localInfo.createEntityBean();
localType = localInfo.getType();
localIdBinder = localInfo.getIdBinder();
localDesc = localInfo.desc();
}
} else {
localType = null;
localDesc = desc;
localBean = desc.createEntityBean();
localIdBinder = idBinder;
}
Mode queryMode = ctx.getQueryMode();
PersistenceContext persistenceContext = (!readId || temporalVersions) ? null : ctx.getPersistenceContext();
if (readId) {
Object id = localIdBinder.readSet(ctx, localBean);
if (id == null) {
// bean must be null...
localBean = null;
} else if (!temporalVersions) {
// check the PersistenceContext to see if the bean already exists
contextBean = (EntityBean) localDesc.contextPutIfAbsent(persistenceContext, id, localBean);
if (contextBean == null) {
// bean just added to the persistenceContext
contextBean = localBean;
} else {
// bean already exists in persistenceContext
if (isLoadContextBeanNeeded(queryMode, contextBean)) {
// refresh it anyway (lazy loading for example)
localBean = contextBean;
} else {
// ignore the DB data...
localBean = null;
}
}
}
}
ctx.setCurrentPrefix(prefix, pathMap);
ctx.propagateState(localBean);
SqlBeanLoad sqlBeanLoad = new SqlBeanLoad(ctx, localType, localBean, queryMode);
if (inheritInfo == null) {
// normal behavior with no inheritance
for (BeanProperty property : properties) {
property.load(sqlBeanLoad);
}
} else {
// take account of inheritance and due to subclassing approach
// need to get a 'local' version of the property
for (BeanProperty property : properties) {
// get a local version of the BeanProperty
BeanProperty p = localDesc.getBeanProperty(property.getName());
if (p != null) {
p.load(sqlBeanLoad);
} else {
property.loadIgnore(ctx);
}
}
}
boolean lazyLoadMany = false;
if (localBean == null && queryMode.equals(Mode.LAZYLOAD_MANY)) {
// batch lazy load many into existing contextBean
localBean = contextBean;
lazyLoadMany = true;
}
// recursively continue reading...
for (SqlTreeNode aChildren : children) {
// read each child... and let them set their
// values back to this localBean
aChildren.load(ctx, localBean, contextBean);
}
if (!lazyLoadMany && localBean != null) {
ctx.setCurrentPrefix(prefix, pathMap);
if (readId && !temporalVersions) {
createListProxies(localDesc, ctx, localBean, disableLazyLoad);
}
if (temporalMode == SpiQuery.TemporalMode.DRAFT) {
localDesc.setDraft(localBean);
}
localDesc.postLoad(localBean);
EntityBeanIntercept ebi = localBean._ebean_getIntercept();
ebi.setPersistenceContext(persistenceContext);
if (Mode.LAZYLOAD_BEAN.equals(queryMode)) {
// Lazy Load does not reset the dirty state
ebi.setLoadedLazy();
} else if (readId) {
// normal bean loading
ebi.setLoaded();
}
if (disableLazyLoad) {
// bean does not have an Id or is SqlSelect based
ebi.setDisableLazyLoad(true);
} else if (partialObject) {
if (readId) {
// register for lazy loading
ctx.register(null, ebi);
}
} else {
ebi.setFullyLoadedBean(true);
}
if (ctx.isAutoTuneProfiling() && !disableLazyLoad) {
// collect autoTune profiling for this bean...
ctx.profileBean(ebi, prefix);
}
}
if (parentBean != null) {
// set this back to the parentBean
nodeBeanProp.setValue(parentBean, contextBean);
}
if (!readId || temporalVersions) {
// a bean with no Id (never found in context)
return localBean;
} else {
if (lazyLoadParentId != null) {
ctx.setLazyLoadedChildBean(contextBean, lazyLoadParentId);
}
return contextBean;
}
}
/**
* Create lazy loading proxies for the Many's except for the one that is
* included in the actual query.
*/
private void createListProxies(BeanDescriptor<?> localDesc, DbReadContext ctx, EntityBean localBean, boolean disableLazyLoad) {
BeanPropertyAssocMany<?> fetchedMany = ctx.getManyProperty();
// load the List/Set/Map proxy objects (deferred fetching of lists)
BeanPropertyAssocMany<?>[] manys = localDesc.propertiesMany();
for (BeanPropertyAssocMany<?> many : manys) {
if (fetchedMany == null || !fetchedMany.equals(many)) {
// create a proxy for the many (deferred fetching)
BeanCollection<?> ref = many.createReferenceIfNull(localBean);
if (ref != null) {
if (disableLazyLoad) {
ref.setDisableLazyLoad(true);
}
if (!ref.isRegisteredWithLoadContext()) {
ctx.register(many.getName(), ref);
}
}
}
}
}
@Override
public void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
ctx.pushJoin(prefix);
ctx.pushTableAlias(prefix);
if (readId) {
appendSelectId(ctx, idBinder.getBeanProperty());
}
for (BeanProperty property : properties) {
if (!property.isAggregation()) {
property.appendSelect(ctx, subQuery);
}
}
ctx.popTableAlias();
ctx.popJoin();
}
/**
* Append the property columns to the buffer.
*/
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
ctx.pushJoin(prefix);
ctx.pushTableAlias(prefix);
if (temporalVersions) {
// select sys_period lower and upper columns
ctx.appendHistorySysPeriod();
}
if (lazyLoadParent != null) {
lazyLoadParent.addSelectExported(ctx, prefix);
}
if (!subQuery && inheritInfo != null) {
ctx.appendColumn(inheritInfo.getDiscriminatorColumn());
}
if (readId) {
appendSelectId(ctx, idBinder.getBeanProperty());
}
appendSelect(ctx, subQuery, properties);
for (SqlTreeNode aChildren : children) {
// read each child... and let them set their
// values back to this localBean
aChildren.appendSelect(ctx, subQuery);
}
ctx.popTableAlias();
ctx.popJoin();
}
public boolean isAggregation() {
return aggregation;
}
/**
* Append the properties to the buffer.
*/
private void appendSelect(DbSqlContext ctx, boolean subQuery, BeanProperty[] props) {
for (BeanProperty prop : props) {
prop.appendSelect(ctx, subQuery);
}
}
private void appendSelectId(DbSqlContext ctx, BeanProperty prop) {
if (prop != null) {
prop.appendSelect(ctx, false);
}
}
public void appendWhere(DbSqlContext ctx) {
// Only apply inheritance to root node as any join will already have the inheritance join include - see TableJoin
if (inheritInfo != null && nodeBeanProp == null) {
if (!inheritInfo.isRoot()) {
// restrict to this type and sub types of this type.
if (ctx.length() > 0) {
ctx.append(" and");
}
ctx.append(" ").append(ctx.getTableAlias(prefix)).append(".");
ctx.append(inheritInfo.getWhere()).append(" ");
}
}
if (extraWhere != null) {
if (ctx.length() > 0) {
ctx.append(" and");
}
String ta = ctx.getTableAlias(prefix);
String ew = StringHelper.replaceString(extraWhere, "${ta}", ta);
ctx.append(" ").append(ew).append(" ");
}
for (SqlTreeNode aChildren : children) {
// recursively add to the where clause any
// fixed predicates (extraWhere etc)
aChildren.appendWhere(ctx);
}
}
/**
* Append to the FROM clause for this node.
*/
public void appendFrom(DbSqlContext ctx, SqlJoinType joinType) {
ctx.pushJoin(prefix);
ctx.pushTableAlias(prefix);
baseTableAlias = ctx.getTableAlias(prefix);
// join and return SqlJoinType to use for child joins
joinType = appendFromBaseTable(ctx, joinType);
for (BeanProperty property : properties) {
// usually nothing... except for 1-1 Exported
property.appendFrom(ctx, joinType);
}
for (SqlTreeNode aChildren : children) {
aChildren.appendFrom(ctx, joinType);
}
ctx.popTableAlias();
ctx.popJoin();
}
public void addSoftDeletePredicate(SpiQuery<?> query) {
if (desc.isSoftDelete()) {
query.addSoftDeletePredicate(desc.getSoftDeletePredicate(baseTableAlias));
}
}
public void addAsOfTableAlias(SpiQuery<?> query) {
// if history on this bean type add it's alias
// for each alias we add an effect date predicate
if (desc.isHistorySupport()) {
query.incrementAsOfTableCount();
}
if (lazyLoadParent != null && lazyLoadParent.isManyToManyWithHistory()) {
query.incrementAsOfTableCount();
}
if (intersectionAsOfTableAlias) {
query.incrementAsOfTableCount();
}
for (SqlTreeNode aChildren : children) {
aChildren.addAsOfTableAlias(query);
}
}
/**
* Join to base table for this node. This includes a join to the intersection
* table if this is a ManyToMany node.
*/
public SqlJoinType appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) {
SqlJoinType sqlJoinType = appendFromAsJoin(ctx, joinType);
if (temporalMode != SpiQuery.TemporalMode.SOFT_DELETED && desc.isSoftDelete()) {
// add the soft delete predicate to the join clause
ctx.append("and ").append(desc.getSoftDeletePredicate(ctx.getTableAlias(prefix))).append(" ");
}
return sqlJoinType;
}
private SqlJoinType appendFromAsJoin(DbSqlContext ctx, SqlJoinType joinType) {
if (nodeBeanProp instanceof BeanPropertyAssocMany<?>) {
BeanPropertyAssocMany<?> manyProp = (BeanPropertyAssocMany<?>) nodeBeanProp;
if (manyProp.isManyToMany()) {
String alias = ctx.getTableAlias(prefix);
String[] split = SplitName.split(prefix);
String parentAlias = ctx.getTableAlias(split[0]);
String alias2 = alias + "z_";
// adding the additional join to the intersection table
TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin();
manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx);
if (!manyProp.isExcludedFromHistory()) {
intersectionAsOfTableAlias = true;
}
return nodeBeanProp.addJoin(joinType, alias2, alias, ctx);
}
}
return nodeBeanProp.addJoin(joinType, prefix, ctx);
}
/**
* Summary description.
*/
public String toString() {
return "SqlTreeNodeBean: " + desc;
}
private boolean isLoadContextBeanNeeded(Mode queryMode, EntityBean contextBean) {
// if explicitly set loadContextBean to true, then reload
if (queryMode.isLoadContextBean()) {
return true;
}
if (contextBean._ebean_getIntercept().isFullyLoadedBean()) {
// reload if contextBean is partial object
return false;
}
// return true by default
return true;
}
@Override
public boolean hasMany() {
for (SqlTreeNode child : children) {
if (child.hasMany()) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,169 @@
package io.ebeaninternal.server.query;
import io.ebean.Version;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.TableJoin;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* The purpose is to add an extra join to the query.
* <p>
* This is used to support the where clause or order by clause that refers
* to properties that are NOT included in the select. To support the where clause
* etc in this case we must add an extra join.
* </p>
*/
class SqlTreeNodeExtraJoin implements SqlTreeNode {
private final BeanPropertyAssoc<?> assocBeanProperty;
private final String prefix;
private final boolean manyJoin;
private final boolean pathContainsMany;
private List<SqlTreeNodeExtraJoin> children;
SqlTreeNodeExtraJoin(String prefix, BeanPropertyAssoc<?> assocBeanProperty, boolean pathContainsMany) {
this.prefix = prefix;
this.assocBeanProperty = assocBeanProperty;
this.pathContainsMany = pathContainsMany;
this.manyJoin = assocBeanProperty instanceof BeanPropertyAssocMany<?>;
}
@Override
public void buildRawSqlSelectChain(List<String> selectChain) {
// nothing to add
}
@Override
public void addAsOfTableAlias(SpiQuery<?> query) {
// nothing to do here
}
@Override
public void addSoftDeletePredicate(SpiQuery<?> query) {
// nothing to do here
}
@Override
public boolean isAggregation() {
return false;
}
@Override
public void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
// nothing to do here
}
@Override
public BeanProperty getSingleProperty() {
throw new IllegalStateException("No expected");
}
/**
* Return true if the extra join is a many join.
* <p>
* This means we need to add distinct to the sql query.
* </p>
*/
boolean isManyJoin() {
return manyJoin;
}
public String getName() {
return prefix;
}
public void addChild(SqlTreeNodeExtraJoin child) {
if (children == null) {
children = new ArrayList<>();
}
children.add(child);
}
public void appendFrom(DbSqlContext ctx, SqlJoinType joinType) {
boolean manyToMany = false;
if (assocBeanProperty instanceof BeanPropertyAssocMany<?>) {
BeanPropertyAssocMany<?> manyProp = (BeanPropertyAssocMany<?>) assocBeanProperty;
if (manyProp.isManyToMany()) {
manyToMany = true;
String alias = ctx.getTableAlias(prefix);
String[] split = SplitName.split(prefix);
String parentAlias = ctx.getTableAlias(split[0]);
String alias2 = alias + "z_";
TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin();
manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx);
assocBeanProperty.addJoin(joinType, alias2, alias, ctx);
}
}
if (pathContainsMany) {
// "promote" to left join as the path contains a many
joinType = SqlJoinType.OUTER;
}
if (!manyToMany) {
assocBeanProperty.addJoin(joinType, prefix, ctx);
}
if (children != null) {
if (manyJoin || pathContainsMany) {
// if AUTO then make all descendants use OUTER JOIN
joinType = joinType.autoToOuter();
}
for (SqlTreeNodeExtraJoin child : children) {
child.appendFrom(ctx, joinType);
}
}
}
/**
* Does nothing.
*/
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
}
/**
* Does nothing.
*/
public void appendWhere(DbSqlContext ctx) {
}
/**
* Does nothing.
*/
public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException {
return null;
}
/**
* Does nothing.
*/
@Override
public <T> Version<T> loadVersion(DbReadContext ctx) throws SQLException {
return null;
}
@Override
public boolean hasMany() {
return manyJoin;
}
}
@@ -0,0 +1,47 @@
package io.ebeaninternal.server.query;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import java.sql.SQLException;
import java.util.List;
final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
private final BeanPropertyAssocMany<?> manyProp;
SqlTreeNodeManyRoot(String prefix, BeanPropertyAssocMany<?> prop, SqlTreeProperties props, List<SqlTreeNode> myList,
SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
super(prefix, prop, props, myList, temporalMode, disableLazyLoad);
this.manyProp = prop;
}
@Override
public EntityBean load(DbReadContext cquery, EntityBean parentBean, EntityBean contextParent) throws SQLException {
// pass in null for parentBean because the localBean
// that is built is added to a collection rather than
// being set to the parentBean directly
EntityBean detailBean = super.load(cquery, null, null);
// initialise the collection and add detailBean if it is not null
if (contextParent != null) {
manyProp.addBeanToCollectionWithCreate(contextParent, detailBean, false);
}
return detailBean;
}
/**
* Force outer join for everything after the many property.
*/
@Override
public void appendFrom(DbSqlContext ctx, SqlJoinType joinType) {
super.appendFrom(ctx, joinType.autoToOuter());
}
@Override
public boolean hasMany() {
return true;
}
}
@@ -0,0 +1,133 @@
package io.ebeaninternal.server.query;
import io.ebean.Version;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.TableJoin;
import java.sql.SQLException;
import java.util.List;
/**
* Join to Many (or child of a many) to support where clause predicates on many properties.
*/
class SqlTreeNodeManyWhereJoin implements SqlTreeNode {
private final String parentPrefix;
private final String prefix;
private final BeanPropertyAssoc<?> nodeBeanProp;
/**
* The many where join which is either INNER or OUTER.
*/
private final SqlJoinType manyJoinType;
SqlTreeNodeManyWhereJoin(String prefix, BeanPropertyAssoc<?> prop, SqlJoinType manyJoinType) {
this.nodeBeanProp = prop;
this.prefix = prefix;
this.manyJoinType = manyJoinType;
String[] split = SplitName.split(prefix);
this.parentPrefix = split[0];
}
@Override
public BeanProperty getSingleProperty() {
throw new IllegalStateException("No expected");
}
@Override
public void addAsOfTableAlias(SpiQuery<?> query) {
// do nothing here ...
}
@Override
public void addSoftDeletePredicate(SpiQuery<?> query) {
// do nothing here ...
}
@Override
public boolean isAggregation() {
return false;
}
@Override
public void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
// do nothing here
}
/**
* Append to the FROM clause for this node.
*/
@Override
public void appendFrom(DbSqlContext ctx, SqlJoinType currentJoinType) {
// always use the join type as per this many where join
// (OUTER for disjunction and otherwise INNER)
appendFromBaseTable(ctx, manyJoinType);
}
/**
* Join to base table for this node. This includes a join to the
* intersection table if this is a ManyToMany node.
*/
void appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) {
String alias = ctx.getTableAliasManyWhere(prefix);
String parentAlias = ctx.getTableAliasManyWhere(parentPrefix);
if (nodeBeanProp instanceof BeanPropertyAssocOne<?>) {
nodeBeanProp.addJoin(joinType, parentAlias, alias, ctx);
} else {
BeanPropertyAssocMany<?> manyProp = (BeanPropertyAssocMany<?>) nodeBeanProp;
if (!manyProp.isManyToMany()) {
manyProp.addJoin(joinType, parentAlias, alias, ctx);
} else {
String alias2 = alias + "z_";
TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin();
manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx);
manyProp.addJoin(joinType, alias2, alias, ctx);
}
}
}
public void buildRawSqlSelectChain(List<String> selectChain) {
// nothing to add
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
// nothing to do here
}
public void appendWhere(DbSqlContext ctx) {
// nothing to do here
}
public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException {
// nothing to do here
return null;
}
@Override
public <T> Version<T> loadVersion(DbReadContext ctx) throws SQLException {
// nothing to do here
return null;
}
@Override
public boolean hasMany() {
return true;
}
}
@@ -0,0 +1,62 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.TableJoin;
import java.util.List;
/**
* Represents the root node of the Sql Tree.
*/
final class SqlTreeNodeRoot extends SqlTreeNodeBean {
private final TableJoin includeJoin;
/**
* Specify for SqlSelect to include an Id property or not.
*/
SqlTreeNodeRoot(BeanDescriptor<?> desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId,
TableJoin includeJoin, BeanPropertyAssocMany<?> many, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad) {
super(desc, props, myList, withId, many, temporalMode, disableLazyLoad);
this.includeJoin = includeJoin;
}
/**
* Set AsOf support (at root level).
*/
public void addAsOfTableAlias(SpiQuery<?> query) {
if (desc.isHistorySupport()) {
query.setAsOfBaseTable();
query.incrementAsOfTableCount();
}
if (lazyLoadParent != null && lazyLoadParent.isManyToManyWithHistory()) {
query.incrementAsOfTableCount();
}
for (SqlTreeNode aChildren : children) {
aChildren.addAsOfTableAlias(query);
}
}
/**
* For the root node there is no join type or on clause etc.
*/
@Override
public SqlJoinType appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) {
ctx.append(desc.getBaseTable(temporalMode));
ctx.append(" ").append(baseTableAlias);
if (includeJoin != null) {
String a1 = baseTableAlias;
String a2 = "int_"; // unique alias for intersection join
includeJoin.addJoin(joinType, a1, a2, ctx);
}
return joinType;
}
}
@@ -0,0 +1,110 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.server.deploy.BeanProperty;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
/**
* The select properties for a node in the SqlTree.
*/
public class SqlTreeProperties {
/**
* True if this node of the tree should have read only entity beans.
*/
private boolean readOnly;
/**
* The bean properties in order.
*/
private final List<BeanProperty> propsList = new ArrayList<>();
/**
* Maintain a list of property names to detect embedded bean additions.
*/
private final LinkedHashSet<String> propNames = new LinkedHashSet<>();
private boolean allProperties;
private boolean aggregation;
SqlTreeProperties() {
}
boolean containsProperty(String propName) {
return propNames.contains(propName);
}
public void add(BeanProperty[] props) {
//noinspection ManualArrayToCollectionCopy
for (BeanProperty prop : props) {
propsList.add(prop);
}
}
public void add(BeanProperty prop) {
propsList.add(prop);
propNames.add(prop.getName());
}
public BeanProperty[] getProps() {
return propsList.toArray(new BeanProperty[propsList.size()]);
}
boolean isPartialObject() {
return !allProperties;
}
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
void setAllProperties() {
this.allProperties = true;
}
/**
* Check for an aggregation property and set manyWhereJoin as needed.
* <p>
* Return true if a Sql distinct is required.
* </p>
*/
boolean requireSqlDistinct(ManyWhereJoins manyWhereJoins) {
String joinProperty = aggregationJoin();
if (joinProperty != null) {
aggregation = true;
manyWhereJoins.addAggregationJoin(joinProperty);
return false;
} else {
return manyWhereJoins.requireSqlDistinct();
}
}
/**
* Return true if this contains an aggregation property.
*/
public boolean isAggregation() {
return aggregation;
}
/**
* Return the property to join for aggregation.
*/
private String aggregationJoin() {
if (!allProperties) {
for (BeanProperty beanProperty : propsList) {
if (beanProperty.isAggregation()) {
return beanProperty.getElPrefix();
}
}
}
return null;
}
}
@@ -0,0 +1,34 @@
package io.ebeaninternal.server.query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UtilJdbc {
private static final Logger logger = LoggerFactory.getLogger(UtilJdbc.class);
public static void close(ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
logger.error("Error closing ResultSet", e);
}
}
public static void close(PreparedStatement statement) {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
logger.error("Error closing PreparedStatement", e);
}
}
}
@@ -0,0 +1 @@
package io.ebeaninternal.server.query;