#382 - ENH: Add support for @History using Oracle Total Recall ... for 'as of' queries

This commit is contained in:
Robin Bygrave committed 2015-08-18 22:19:55 +12:00
1 parent 18aeedc153
commit 10937de24b
13 files changed
+234 -52

No files matched your search

@@ -122,7 +122,7 @@ public class InternalConfiguration {
DatabasePlatform databasePlatform = serverConfig.getDatabasePlatform();
this.binder = new Binder(typeManager, getAsOfBindCount(databasePlatform));
this.binder = getBinder(typeManager, databasePlatform);
this.cQueryEngine = new CQueryEngine(databasePlatform, binder, asOfTableMapping, serverConfig.getAsOfSysPeriod());
ExternalTransactionManager externalTransactionManager = serverConfig.getExternalTransactionManager();
@@ -142,9 +142,12 @@ public class InternalConfiguration {
/**
* For 'As Of' queries return the number of bind variables per predicate.
*/
private int getAsOfBindCount(DatabasePlatform databasePlatform) {
private Binder getBinder(TypeManager typeManager, DatabasePlatform databasePlatform) {
DbHistorySupport historySupport = databasePlatform.getHistorySupport();
return historySupport == null ? 0 : historySupport.getBindCount();
if (historySupport == null) {
return new Binder(typeManager, 0, false);
}
return new Binder(typeManager, historySupport.getBindCount(), historySupport.isBindWithFromClause());
}
/**
@@ -12,6 +12,7 @@ import com.avaje.ebean.config.EncryptKeyManager;
import com.avaje.ebean.config.NamingConvention;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebean.config.dbplatform.DbHistorySupport;
import com.avaje.ebean.config.dbplatform.DbIdentity;
import com.avaje.ebean.config.dbplatform.IdGenerator;
import com.avaje.ebean.config.dbplatform.IdType;
@@ -174,7 +175,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
this.idBinderFactory = new IdBinderFactory(databasePlatform.isIdInExpandedForm());
this.eagerFetchLobs = serverConfig.isEagerFetchLobs();
this.asOfViewSuffix = serverConfig.getAsOfViewSuffix();
this.asOfViewSuffix = getAsOfViewSuffix(databasePlatform, serverConfig);
this.readAnnotations = new ReadAnnotations(config.getGeneratedPropertyFactory(), asOfViewSuffix);
this.bootupClasses = config.getBootupClasses();
this.createProperties = config.getDeployCreateProperties();
@@ -199,6 +200,17 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
this.transientProperties = new TransientProperties();
}
/**
* Return the AsOfViewSuffix allowing the DbHistorySupport to override the value in
* the case where there is no view (Oracle, DB2, MS SQL Server).
*/
private String getAsOfViewSuffix(DatabasePlatform databasePlatform, ServerConfig serverConfig) {
DbHistorySupport historySupport = databasePlatform.getHistorySupport();
// with historySupport returns a simple view suffix or the sql2011 as of timestamp suffix
return (historySupport == null ) ? serverConfig.getAsOfViewSuffix() : historySupport.getAsOfViewSuffix(serverConfig.getAsOfViewSuffix());
}
public BeanDescriptor<?> getBeanDescriptorById(String descriptorId) {
return idDescMap.get(descriptorId);
}
@@ -28,12 +28,15 @@ public class Binder {
private final int asOfBindCount;
private final boolean bindAsOfWithFromClause;
/**
* Set the PreparedStatement with which to bind variables to.
*/
public Binder(TypeManager typeManager, int asOfBindCount) {
public Binder(TypeManager typeManager, int asOfBindCount, boolean bindAsOfWithFromClause) {
this.typeManager = typeManager;
this.asOfBindCount = asOfBindCount;
this.bindAsOfWithFromClause = bindAsOfWithFromClause;
}
/**
@@ -43,6 +46,15 @@ public class Binder {
return asOfBindCount;
}
/**
* Return true if the 'as of' predicates are in the from/join clause in which case the timestamp is
* bound early (before all the other predicates ala Oracle). Return false if the 'as of' predicates are
* appended to the end of the predicates and the timestamp is bound last (Postgres, MySql).
*/
public boolean isBindAsOfWithFromClause() {
return bindAsOfWithFromClause;
}
/**
* Bind the values to the Prepared Statement.
*/
@@ -403,9 +403,10 @@ public class CQueryBuilder {
}
List<String> asOfTableAlias = query.getAsOfTableAlias();
if (asOfTableAlias != null) {
if (asOfTableAlias != null && !historySupport.isBindAtFromClause()) {
// append the effective date predicates for each table alias
// that maps to a @History entity involved in this query
// Do this when history using separate tables/views (PG, MySql etc)
if (!hasWhere) {
sb.append(" where ");
} else {
@@ -30,22 +30,42 @@ public class CQueryHistorySupport {
this.sysPeriod = sysPeriod;
}
/**
* Return true if the bind of 'as of' timestamp occurs with the from clause
* rather than at the end.
*/
public boolean isBindAtFromClause() {
return dbHistorySupport.isBindWithFromClause();
}
/**
* Return the 'as of' history view for the given base table.
*/
public String getAsOfView(String table) {
return asOfTableMap.get(table);
}
/**
* Return the lower bound column.
*/
public String getSysPeriodLower(String tableAlias) {
return dbHistorySupport.getSysPeriodLower(tableAlias, sysPeriod);
}
/**
* Return the upper bound column.
*/
public String getSysPeriodUpper(String tableAlias) {
return dbHistorySupport.getSysPeriodUpper(tableAlias, sysPeriod);
}
/**
* Return the predicate appended to the end of the query.
*
* Note used for Oracle total recall etc with the more standard approach.
*/
public String getAsOfPredicate(String tableAlias) {
return dbHistorySupport.getAsOfPredicate(tableAlias, sysPeriod);
}
}
@@ -131,6 +131,18 @@ public class CQueryPredicates {
StringBuilder bindLog = new StringBuilder();
List<String> historyTableAlias = query.getAsOfTableAlias();
if (historyTableAlias != null && binder.isBindAsOfWithFromClause()) {
// 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();
bindLog.append("asOf ").append(asOf);
for (int i = 0; i < historyTableAlias.size() * binder.getAsOfBindCount(); i++) {
binder.bindObject(dataBind, asOf);
}
bindLog.append(", ");
}
if (idValue != null) {
// this is a find by id type query...
request.getBeanDescriptor().bindId(dataBind, idValue);
@@ -143,7 +155,6 @@ public class CQueryPredicates {
}
if (whereExprBindValues != null) {
for (int i = 0; i < whereExprBindValues.size(); i++) {
Object bindValue = whereExprBindValues.get(i);
bindValue = binder.bindObject(dataBind, bindValue);
@@ -155,7 +166,6 @@ public class CQueryPredicates {
}
if (filterManyExprBindValues != null) {
for (int i = 0; i < filterManyExprBindValues.size(); i++) {
Object bindValue = filterManyExprBindValues.get(i);
bindValue = binder.bindObject(dataBind, bindValue);
@@ -166,9 +176,8 @@ public class CQueryPredicates {
}
}
List<String> historyTableAlias = query.getAsOfTableAlias();
if (historyTableAlias != null) {
// bind the asAt value for each table alias
if (historyTableAlias != null && !binder.isBindAsOfWithFromClause()) {
// bind the asOf value for each table alias after all the normal predicates
// there is one effective date predicate per table alias
Timestamp asOf = query.getAsOf();
bindLog.append(" asOf ").append(asOf);