#347 - @History support with query.asOf(Timestamp)

This commit is contained in:
Robin Bygrave committed 2015-07-24 10:18:17 +12:00
1 parent 3cafb37a73
commit 82489636d5
37 files changed
+601 -92

No files matched your search

@@ -23,6 +23,8 @@ import com.avaje.ebeaninternal.server.querydefn.OrmQueryLimitRequest;
import javax.persistence.PersistenceException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -43,14 +45,20 @@ public class CQueryBuilder implements Constants {
private final boolean selectCountWithAlias;
private final Map<String,String> asOfTableMapping;
private final String asOfSysPeriod;
private DatabasePlatform dbPlatform;
/**
* Create the SqlGenSelect.
*/
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder) {
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, Map<String,String> asOfTableMapping, String asOfSysPeriod) {
this.binder = binder;
this.asOfTableMapping = asOfTableMapping;
this.asOfSysPeriod = asOfSysPeriod;
this.tableAliasPlaceHolder = dbPlatform.getTableAliasPlaceHolder();
this.columnAliasPrefix = dbPlatform.getColumnAliasPrefix();
this.sqlSelectBuilder = new RawSqlSelectClauseBuilder(dbPlatform, binder);
@@ -103,7 +111,8 @@ public class CQueryBuilder implements Constants {
// use RawSql or generated Sql
predicates.prepare(true);
SqlTree sqlTree = createSqlTree(request, predicates);
Map<String,String> asOfMap = query.isAsOfQuery() ? asOfTableMapping : null;
SqlTree sqlTree = createSqlTree(request, predicates, asOfMap);
SqlLimitResponse s = buildSql(null, request, predicates, sqlTree);
String sql = s.getSql();
@@ -155,7 +164,8 @@ public class CQueryBuilder implements Constants {
predicates.prepare(true);
SqlTree sqlTree = createSqlTree(request, predicates);
Map<String,String> asOfMap = query.isAsOfQuery() ? asOfTableMapping : null;
SqlTree sqlTree = createSqlTree(request, predicates, asOfMap);
SqlLimitResponse s = buildSql(sqlSelect, request, predicates, sqlTree);
String sql = s.getSql();
if (hasMany || query.isRawSql()) {
@@ -203,7 +213,14 @@ public class CQueryBuilder implements Constants {
predicates.prepare(true);
// Build the tree structure that represents the query.
SqlTree sqlTree = createSqlTree(request, predicates);
SpiQuery<T> query = request.getQuery();
Map<String,String> asOfMap = query.isAsOfQuery() ? asOfTableMapping : null;
SqlTree sqlTree = createSqlTree(request, predicates, asOfMap);
if (query.isAsOfQuery()) {
sqlTree.addAsOfTableAlias(query);
}
SqlLimitResponse res = buildSql(null, request, predicates, sqlTree);
boolean rawSql = request.isRawSql();
@@ -232,13 +249,13 @@ public class CQueryBuilder implements Constants {
* order by clauses that are not already included for the select clause.
* </p>
*/
private SqlTree createSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates) {
private SqlTree createSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates, Map<String,String> withHistoryTables) {
if (request.isRawSql()) {
return createRawSqlSqlTree(request, predicates);
}
return new SqlTreeBuilder(tableAliasPlaceHolder, columnAliasPrefix, request, predicates).build();
return new SqlTreeBuilder(tableAliasPlaceHolder, columnAliasPrefix, request, predicates, withHistoryTables).build();
}
private SqlTree createRawSqlSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates) {
@@ -380,6 +397,7 @@ public class CQueryBuilder implements Constants {
String dbFilterMany = predicates.getDbFilterMany();
if (!isEmpty(dbFilterMany)) {
if (!hasWhere) {
hasWhere = true;
sb.append(" where ");
} else {
sb.append("and ");
@@ -387,7 +405,23 @@ public class CQueryBuilder implements Constants {
sb.append(dbFilterMany);
}
List<String> asOfTableAlias = query.getAsOfTableAlias();
if (asOfTableAlias != null) {
// append the effective date predicates for each table alias
// that maps to a @History entity involved in this query
if (!hasWhere) {
sb.append(" where ");
} else {
sb.append("and ");
}
for (int i = 0; i < asOfTableAlias.size(); i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(dbPlatform.getAsOfPredicate(asOfTableAlias.get(i), asOfSysPeriod));
}
}
if (dbOrderBy != null) {
sb.append(" order by ").append(dbOrderBy);
}
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -31,9 +32,9 @@ public class CQueryEngine {
private final CQueryBuilder queryBuilder;
public CQueryEngine(DatabasePlatform dbPlatform, Binder binder) {
public CQueryEngine(DatabasePlatform dbPlatform, Binder binder, Map<String,String> asOfTableMapping, String asOfSysPeriod) {
this.forwardOnlyHintOnFindIterate = dbPlatform.isForwardOnlyHintOnFindIterate();
this.queryBuilder = new CQueryBuilder(dbPlatform, binder);
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, asOfTableMapping, asOfSysPeriod);
}
public <T> CQuery<T> buildQuery(OrmQueryRequest<T> request) {
@@ -1,8 +1,10 @@
package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.avaje.ebean.RawSql;
@@ -174,6 +176,17 @@ public class CQueryPredicates {
}
}
List<String> historyTableAlias = query.getAsOfTableAlias();
if (historyTableAlias != null) {
// bind the asAt value for each table alias
// there is one effective date predicate per table alias
Timestamp asOf = query.getAsOf();
bindLog.append(" asOf ").append(asOf);
for (int i = 0; i < historyTableAlias.size(); i++) {
binder.bindObject(dataBind, asOf);
}
}
if (havingNamedParams != null) {
// bind named parameters in having...
bindLog.append(" havingNamed ");
@@ -1,14 +1,15 @@
package com.avaje.ebeaninternal.server.query;
import java.util.ArrayList;
import java.util.HashSet;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
import com.avaje.ebeaninternal.server.deploy.TableJoinColumn;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.util.ArrayStack;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
public class DefaultDbSqlContext implements DbSqlContext {
private static final String COMMA = ", ";
@@ -45,25 +46,22 @@ public class DefaultDbSqlContext implements DbSqlContext {
private ArrayList<BeanProperty> encryptedProps;
/**
* Construct for FROM clause (no column alias used).
*/
public DefaultDbSqlContext(SqlTreeAlias alias, String tableAliasPlaceHolder) {
this.tableAliasPlaceHolder = tableAliasPlaceHolder;
this.columnAliasPrefix = null;
this.useColumnAlias = false;
this.alias = alias;
}
private final Map<String,String> asOfTableMap;
private final boolean asOfQuery;
/**
* Construct for SELECT clause (with column alias settings).
*/
public DefaultDbSqlContext(SqlTreeAlias alias, String tableAliasPlaceHolder,
String columnAliasPrefix, boolean alwaysUseColumnAlias) {
String columnAliasPrefix, boolean alwaysUseColumnAlias, Map<String,String> asOfTableMap) {
this.alias = alias;
this.tableAliasPlaceHolder = tableAliasPlaceHolder;
this.columnAliasPrefix = columnAliasPrefix;
this.useColumnAlias = alwaysUseColumnAlias;
this.asOfTableMap = asOfTableMap;
this.asOfQuery = (asOfTableMap != null);
}
public void addEncryptedProp(BeanProperty p) {
@@ -108,7 +106,21 @@ public class DefaultDbSqlContext implements DbSqlContext {
sb.append(" ");
sb.append(type);
sb.append(" ").append(table).append(" ");
if (!asOfQuery) {
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 withHistoryTable = asOfTableMap.get(table);
if (withHistoryTable != null) {
// there is an associated history table and view so use that
sb.append(" ").append(withHistoryTable).append(" ");
} else {
sb.append(" ").append(table).append(" ");
}
}
sb.append(a2);
sb.append(" on ");
@@ -222,7 +234,7 @@ public class DefaultDbSqlContext implements DbSqlContext {
public void appendColumn(String tableAlias, String column) {
sb.append(COMMA);
if (column.indexOf("${}") > -1) {
if (column.contains("${}")) {
// support DB functions such as lower() etc
// with the use of secondary columns
String x = StringHelper.replaceString(column, "${}", tableAlias);
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
@@ -80,6 +81,13 @@ public class SqlTree {
this.includes = null;
}
/**
* Recurse through the tree adding an table alias' for @History entity beans.
*/
public void addAsOfTableAlias(SpiQuery<?> query) {
rootNode.addAsOfTableAlias(query);
}
/**
* Build a select expression chain for RawSql.
*/
@@ -98,7 +98,7 @@ public class SqlTreeBuilder {
* to the root node.
*/
public SqlTreeBuilder(String tableAliasPlaceHolder, String columnAliasPrefix,
OrmQueryRequest<?> request, CQueryPredicates predicates) {
OrmQueryRequest<?> request, CQueryPredicates predicates, Map<String,String> asOfTables) {
this.rawSql = false;
this.rawNoId = false;
@@ -112,7 +112,7 @@ public class SqlTreeBuilder {
this.predicates = predicates;
this.alias = new SqlTreeAlias(request.getQuery().getAlias()==null?request.getBeanDescriptor().getBaseTableAlias():request.getQuery().getAlias());
this.ctx = new DefaultDbSqlContext(alias, tableAliasPlaceHolder, columnAliasPrefix, !subQuery);
this.ctx = new DefaultDbSqlContext(alias, tableAliasPlaceHolder, columnAliasPrefix, !subQuery, asOfTables);
}
/**
@@ -265,7 +265,8 @@ public class SqlTreeBuilder {
// Optional many property for lazy loading query
BeanPropertyAssocMany<?> lazyLoadMany = (query == null) ? null : query.getLazyLoadForParentsProperty();
boolean withId = !rawNoId && !subQuery && (query == null || !query.isDistinct());
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany);
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany, SpiQuery.TemporalMode.of(query));
} else if (prop instanceof BeanPropertyAssocMany<?>) {
return new SqlTreeNodeManyRoot(prefix, (BeanPropertyAssocMany<?>) prop, props, myList);
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -32,6 +33,11 @@ public interface SqlTreeNode {
*/
void appendWhere(DbSqlContext ctx);
/**
* Recurse through the tree adding an table alias' for @History entity beans.
*/
void addAsOfTableAlias(SpiQuery<?> query);
/**
* Load the appropriate information from the SqlSelectReader.
* <p>
@@ -9,6 +9,7 @@ import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiQuery.Mode;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
@@ -70,25 +71,36 @@ public class SqlTreeNodeBean implements SqlTreeNode {
protected final BeanPropertyAssocMany<?> lazyLoadParent;
protected final SpiQuery.TemporalMode temporalMode;
private final IdBinder lazyLoadParentIdBinder;
protected String baseTableAlias;
/**
* Table alias set if this bean node includes a join to a intersection
* table and that table has history support.
*/
protected String intersectionAsOfTableAlias;
public SqlTreeNodeBean(String prefix, BeanPropertyAssoc<?> beanProp, SqlTreeProperties props,
List<SqlTreeNode> myChildren, boolean withId) {
this(prefix, beanProp, beanProp.getTargetDescriptor(), props, myChildren, withId, null);
this(prefix, beanProp, beanProp.getTargetDescriptor(), props, myChildren, withId, null, SpiQuery.TemporalMode.CURRENT);
}
/**
* Create with the appropriate node.
*/
public SqlTreeNodeBean(String prefix, BeanPropertyAssoc<?> beanProp, BeanDescriptor<?> desc,
SqlTreeProperties props, List<SqlTreeNode> myChildren, boolean withId, BeanPropertyAssocMany<?> lazyLoadParent) {
SqlTreeProperties props, List<SqlTreeNode> myChildren, boolean withId, BeanPropertyAssocMany<?> lazyLoadParent, SpiQuery.TemporalMode temporalMode) {
this.lazyLoadParent = lazyLoadParent;
this.lazyLoadParentIdBinder = (lazyLoadParent == null) ? null : lazyLoadParent.getBeanDescriptor().getIdBinder();
this.prefix = prefix;
this.nodeBeanProp = beanProp;
this.desc = desc;
this.temporalMode = temporalMode;
this.inheritInfo = desc.getInheritInfo();
this.extraWhere = (beanProp == null) ? null : beanProp.getExtraWhere();
@@ -441,6 +453,8 @@ public class SqlTreeNodeBean implements SqlTreeNode {
ctx.pushJoin(prefix);
ctx.pushTableAlias(prefix);
baseTableAlias = ctx.getTableAlias(prefix);
// join and return SqlJoinType to use for child joins
joinType = appendFromBaseTable(ctx, joinType);
@@ -457,6 +471,21 @@ public class SqlTreeNodeBean implements SqlTreeNode {
ctx.popJoin();
}
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.addAsOfTableAlias(baseTableAlias);
}
if (intersectionAsOfTableAlias != null) {
// adds the 'as of' predicate for this intersection table
query.addAsOfTableAlias(intersectionAsOfTableAlias);
}
for (int i = 0; i < children.length; i++) {
children[i].addAsOfTableAlias(query);
}
}
/**
* Join to base table for this node. This includes a join to the intersection
* table if this is a ManyToMany node.
@@ -472,8 +501,12 @@ public class SqlTreeNodeBean implements SqlTreeNode {
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 = alias2;
}
return nodeBeanProp.addJoin(joinType, alias2, alias, ctx);
}
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
@@ -40,6 +41,11 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
// nothing to add
}
@Override
public void addAsOfTableAlias(SpiQuery<?> query) {
// nothing to do here
}
/**
* Return true if the extra join is a many join.
* <p>
@@ -13,7 +13,7 @@ public final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
final BeanPropertyAssocMany<?> manyProp;
public SqlTreeNodeManyRoot(String prefix, BeanPropertyAssocMany<?> prop, SqlTreeProperties props, List<SqlTreeNode> myList) {
super(prefix, prop, prop.getTargetDescriptor(), props, myList, true, null);
super(prefix, prop, prop.getTargetDescriptor(), props, myList, true, null, null);
this.manyProp = prop;
}
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -37,6 +38,11 @@ public class SqlTreeNodeManyWhereJoin implements SqlTreeNode {
this.parentPrefix = split[0];
}
@Override
public void addAsOfTableAlias(SpiQuery<?> query) {
// do nothing here ...
}
/**
* Append to the FROM clause for this node.
*/
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.query;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -17,13 +18,14 @@ public final class SqlTreeNodeRoot extends SqlTreeNodeBean {
/**
* Specify for SqlSelect to include an Id property or not.
*/
public SqlTreeNodeRoot(BeanDescriptor<?> desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId, TableJoin includeJoin, BeanPropertyAssocMany<?> many) {
super(null, null, desc, props, myList, withId, many);
public SqlTreeNodeRoot(BeanDescriptor<?> desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId,
TableJoin includeJoin, BeanPropertyAssocMany<?> many, SpiQuery.TemporalMode temporalMode) {
super(null, null, desc, props, myList, withId, many, temporalMode);
this.includeJoin = includeJoin;
}
public SqlTreeNodeRoot(BeanDescriptor<?> desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId) {
super(null, null, desc, props, myList, withId, null);
super(null, null, desc, props, myList, withId, null, null);
this.includeJoin = null;
}
@@ -33,11 +35,11 @@ public final class SqlTreeNodeRoot extends SqlTreeNodeBean {
@Override
public SqlJoinType appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) {
ctx.append(desc.getBaseTable());
ctx.append(" ").append(ctx.getTableAlias(null));
ctx.append(desc.getBaseTable(temporalMode));
ctx.append(" ").append(baseTableAlias);
if (includeJoin != null) {
String a1 = ctx.getTableAlias(null);
String a1 = baseTableAlias;
String a2 = "int_"; // unique alias for intersection join
includeJoin.addJoin(joinType, a1, a2, ctx);
}