#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

@@ -112,6 +112,8 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
* The base database table.
*/
private final String baseTable;
private final String baseTableAsOf;
private final boolean historySupport;
/**
* Map of BeanProperty Linked so as to preserve order.
@@ -332,8 +334,9 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
this.updateChangesOnly = deploy.isUpdateChangesOnly();
this.compoundUniqueConstraints = deploy.getCompoundUniqueConstraints();
this.historySupport = deploy.isHistorySupport();
this.baseTable = InternString.intern(deploy.getBaseTable());
this.baseTableAsOf = deploy.getBaseTableAsOf();
this.autoFetchTunable = EntityType.ORM.equals(entityType) && (beanFinder == null);
// helper object used to derive lists of properties
@@ -496,13 +499,19 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
* These properties need to be initialised prior to the association properties
* as they are used to get the imported and exported properties.
* </p>
* @param withHistoryTables map populated if @History is supported on this entity bean
*/
public void initialiseId() {
public void initialiseId(Map<String, String> withHistoryTables) {
if (logger.isTraceEnabled()) {
logger.trace("BeanDescriptor initialise " + fullName);
}
if (historySupport) {
// add mapping (used to swap out baseTable for asOf queries)
withHistoryTables.put(baseTable, baseTableAsOf);
}
if (inheritInfo != null) {
inheritInfo.setDescriptor(this);
}
@@ -522,8 +531,25 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
/**
* Initialise the exported and imported parts for associated properties.
*
* @param asOfTableMap the map of base tables to associated 'with history' tables
* @param asOfViewSuffix the suffix added to the table name to derive the 'with history' view name
*/
public void initialiseOther() {
public void initialiseOther(Map<String, String> asOfTableMap, String asOfViewSuffix) {
if (historySupport) {
// history support on this bean so check all associated intersection tables
// and if they are not excluded register the associated 'with history' table
for (int i = 0; i < propertiesManyToMany.length; i++) {
if (!propertiesManyToMany[i].isExcludedFromHistory()) {
// this intersection table has history support so also register
// it into the asOfTableMap
TableJoin intersectionTableJoin = propertiesManyToMany[i].getIntersectionTableJoin();
String intersectionTableName = intersectionTableJoin.getTable();
asOfTableMap.put(intersectionTableName, intersectionTableName + asOfViewSuffix);
}
}
}
if (!isEmbedded()) {
// initialise all the non-id properties
@@ -761,7 +787,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
}
public void cacheBeanPut(T bean) {
cacheBeanPutData((EntityBean)bean);
cacheBeanPutData((EntityBean) bean);
}
/**
@@ -1628,6 +1654,24 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return baseTable;
}
/**
* Return the base table to use given the query temporal mode.
*/
public String getBaseTable(SpiQuery.TemporalMode mode) {
switch (mode) {
case VERSIONS: return baseTableAsOf;
case AS_OF: return baseTableAsOf;
default: return baseTable;
}
}
/**
* Return true if this entity bean has history support.
*/
public boolean isHistorySupport() {
return historySupport;
}
/**
* Return the identity generation type.
*/
@@ -10,6 +10,7 @@ import com.avaje.ebean.cache.ServerCacheManager;
import com.avaje.ebean.config.EncryptKey;
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.DbIdentity;
import com.avaje.ebean.config.dbplatform.IdGenerator;
@@ -51,7 +52,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
private static final BeanDescComparator beanDescComparator = new BeanDescComparator();
private final ReadAnnotations readAnnotations = new ReadAnnotations();
private final ReadAnnotations readAnnotations;
private final TransientProperties transientProperties;
@@ -127,34 +128,45 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
private final boolean eagerFetchLobs;
private final String asOfViewSuffix;
/**
* Map of base tables to 'with history views' used to support 'as of' queries.
*/
private final Map<String,String> asOfTableMap = new HashMap<String, String>();
/**
* Create for a given database dbConfig.
*/
public BeanDescriptorManager(InternalConfiguration config) {
this.serverName = InternString.intern(config.getServerConfig().getName());
ServerConfig serverConfig = config.getServerConfig();
this.serverName = InternString.intern(serverConfig.getName());
this.cacheManager = config.getCacheManager();
this.xmlConfig = config.getXmlConfig();
this.dbSequenceBatchSize = config.getServerConfig().getDatabaseSequenceBatchSize();
this.dbSequenceBatchSize = serverConfig.getDatabaseSequenceBatchSize();
this.backgroundExecutor = config.getBackgroundExecutor();
this.dataSource = config.getServerConfig().getDataSource();
this.encryptKeyManager = config.getServerConfig().getEncryptKeyManager();
this.databasePlatform = config.getServerConfig().getDatabasePlatform();
this.dataSource = serverConfig.getDataSource();
this.encryptKeyManager = serverConfig.getEncryptKeyManager();
this.databasePlatform = serverConfig.getDatabasePlatform();
this.idBinderFactory = new IdBinderFactory(databasePlatform.isIdInExpandedForm());
this.eagerFetchLobs = config.getServerConfig().isEagerFetchLobs();
this.eagerFetchLobs = serverConfig.isEagerFetchLobs();
this.asOfViewSuffix = serverConfig.getAsOfViewSuffix();
this.readAnnotations = new ReadAnnotations(asOfViewSuffix);
this.bootupClasses = config.getBootupClasses();
this.createProperties = config.getDeployCreateProperties();
this.typeManager = config.getTypeManager();
this.namingConvention = config.getServerConfig().getNamingConvention();
this.namingConvention = serverConfig.getNamingConvention();
this.dbIdentity = config.getDatabasePlatform().getDbIdentity();
this.deplyInherit = config.getDeployInherit();
this.deployOrmXml = config.getDeployOrmXml();
this.deployUtil = config.getDeployUtil();
this.beanManagerFactory = new BeanManagerFactory(config.getServerConfig(), config.getDatabasePlatform());
this.beanManagerFactory = new BeanManagerFactory(serverConfig, config.getDatabasePlatform());
this.updateChangesOnly = config.getServerConfig().isUpdateChangesOnly();
this.updateChangesOnly = serverConfig.isUpdateChangesOnly();
this.beanLifecycleAdapterFactory = new BeanLifecycleAdapterFactory();
this.persistControllerManager = new PersistControllerManager(bootupClasses);
@@ -205,7 +217,10 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
return idBinderFactory.createIdBinder(idProperty);
}
public void deploy() {
/**
* Deploy returning the asOfTableMap (which is required by the SQL builders).
*/
public Map<String,String> deploy() {
try {
createListeners();
@@ -237,6 +252,9 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
deplyInfoMap.clear();
deplyInfoMap = null;
return asOfTableMap;
} catch (RuntimeException e) {
String msg = "Error in deployment";
logger.error(msg, e);
@@ -324,7 +342,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
// first (as they are needed to initialise the
// associated properties in the second pass).
for (BeanDescriptor<?> d : descMap.values()) {
d.initialiseId();
d.initialiseId(asOfTableMap);
}
// PASS 2:
@@ -336,7 +354,10 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
// PASS 3:
// now initialise all the associated properties
for (BeanDescriptor<?> d : descMap.values()) {
d.initialiseOther();
// also look for intersection tables with
// associated history support and register them
// into the asOfTableMap
d.initialiseOther(asOfTableMap, asOfViewSuffix);
}
// create BeanManager for each non-embedded entity bean
@@ -973,7 +994,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
if (!EntityType.ORM.equals(desc.getEntityType())) {
// not using base table
desc.setBaseTable(null);
desc.setBaseTable(null, null);
}
// mark transient properties
@@ -173,6 +173,8 @@ public class BeanProperty implements ElPropertyValue {
*/
final int dbType;
final boolean excludedFromHistory;
/**
* The default value to insert if null.
*/
@@ -256,6 +258,7 @@ public class BeanProperty implements ElPropertyValue {
this.dbRead = deploy.isDbRead();
this.dbInsertable = deploy.isDbInsertable();
this.dbUpdatable = deploy.isDbUpdateable();
this.excludedFromHistory = deploy.isExcludedFromHistory();
this.secondaryTable = deploy.isSecondaryTable();
if (secondaryTable) {
@@ -341,6 +344,7 @@ public class BeanProperty implements ElPropertyValue {
this.sqlFormulaSelect = InternString.intern(override.getSqlFormulaSelect());
this.formula = sqlFormulaSelect != null;
this.excludedFromHistory = source.excludedFromHistory;
this.fetchEager = source.fetchEager;
this.unidirectionalShadow = source.unidirectionalShadow;
this.discriminator = source.discriminator;
@@ -1004,6 +1008,13 @@ public class BeanProperty implements ElPropertyValue {
return dbEncryptedType;
}
/**
* Return true if this property is excluded from history.
*/
public boolean isExcludedFromHistory() {
return excludedFromHistory;
}
/**
* Return true if this property should be included in an Insert.
*/
@@ -10,108 +10,108 @@ public interface DbSqlContext {
/**
* Add a join to the sql query.
*/
public void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2, String inheritance);
void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2, String inheritance);
public void pushSecondaryTableAlias(String alias);
void pushSecondaryTableAlias(String alias);
/**
* Push the current table alias onto the stack.
*/
public void pushTableAlias(String tableAlias);
void pushTableAlias(String tableAlias);
/**
* Pop the current table alias from the stack.
*/
public void popTableAlias();
void popTableAlias();
/**
* Add an encrypted property which will require additional binding.
*/
public void addEncryptedProp(BeanProperty prop);
void addEncryptedProp(BeanProperty prop);
/**
* Return a list of encrypted properties which require additional binding.
*/
public BeanProperty[] getEncryptedProps();
BeanProperty[] getEncryptedProps();
/**
* Append a char directly to the SQL buffer.
*/
public DbSqlContext append(char s);
DbSqlContext append(char s);
/**
* Append a string directly to the SQL buffer.
*/
public DbSqlContext append(String s);
DbSqlContext append(String s);
/**
* Peek the current table alias.
*/
public String peekTableAlias();
String peekTableAlias();
/**
* Add a raw column to the sql.
*/
public void appendRawColumn(String rawcolumnWithTableAlias);
void appendRawColumn(String rawcolumnWithTableAlias);
/**
* Append a column with an explicit table alias.
*/
public void appendColumn(String tableAlias, String column);
void appendColumn(String tableAlias, String column);
/**
* Append a column with the current table alias.
*/
public void appendColumn(String column);
void appendColumn(String column);
/**
* Append a Sql Formula select. This converts the "${ta}" keyword to the
* current table alias.
*/
public void appendFormulaSelect(String sqlFormulaSelect);
void appendFormulaSelect(String sqlFormulaSelect);
/**
* Append a Sql Formula join. This converts the "${ta}" keyword to the current
* table alias.
*/
public void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType);
void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType);
/**
* Return the current content length.
*/
public int length();
int length();
/**
* Return the current context of the sql context.
*/
public String getContent();
String getContent();
/**
* Return the current join node.
*/
public String peekJoin();
String peekJoin();
/**
* Push a join node onto the stack.
*/
public void pushJoin(String prefix);
void pushJoin(String prefix);
/**
* Pop a join node off the stack.
*/
public void popJoin();
void popJoin();
/**
* Return a table alias without many where clause joins. Typically this is for
* the select clause (fetch joins).
*/
public String getTableAlias(String prefix);
String getTableAlias(String prefix);
/**
* Return a table alias that takes into account many where joins.
*/
public String getTableAliasManyWhere(String prefix);
String getTableAliasManyWhere(String prefix);
public String getRelativePrefix(String propName);
String getRelativePrefix(String propName);
}
@@ -87,6 +87,11 @@ public class DeployBeanDescriptor<T> {
* The base database table.
*/
private String baseTable;
private String baseTableAsOf;
private boolean historySupport;
private TableName baseTableFull;
private String[] properties;
@@ -135,6 +140,20 @@ public class DeployBeanDescriptor<T> {
return Modifier.isAbstract(beanType.getModifiers());
}
/**
* Set to true for @History entity beans that have history.
*/
public void setHistorySupport(boolean historySupport) {
this.historySupport = historySupport;
}
/**
* Return true if this is an @History entity bean.
*/
public boolean isHistorySupport() {
return historySupport;
}
public boolean isScalaObject() {
Class<?>[] interfaces = beanType.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
@@ -405,6 +424,10 @@ public class DeployBeanDescriptor<T> {
return baseTable;
}
public String getBaseTableAsOf() {
return baseTableAsOf;
}
/**
* Return the base table with full structure.
*/
@@ -413,12 +436,12 @@ public class DeployBeanDescriptor<T> {
}
/**
* Set the base table. Only properties mapped to the base table are by default
* persisted.
* Set the base table. Only properties mapped to the base table are by default persisted.
*/
public void setBaseTable(TableName baseTableFull) {
public void setBaseTable(TableName baseTableFull, String asOfSuffix) {
this.baseTableFull = baseTableFull;
this.baseTable = baseTableFull == null ? null : baseTableFull.getQualifiedName();
this.baseTableAsOf = baseTable + asOfSuffix;
}
public void sortProperties() {
@@ -215,6 +215,8 @@ public class DeployBeanProperty {
private boolean indexed;
private String indexName;
private boolean excludedFromHistory;
public DeployBeanProperty(DeployBeanDescriptor<?> desc, Class<?> propertyType, ScalarType<?> scalarType, ScalarTypeConverter<?, ?> typeConverter) {
this.desc = desc;
this.propertyType = propertyType;
@@ -912,4 +914,12 @@ public class DeployBeanProperty {
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public boolean isExcludedFromHistory() {
return excludedFromHistory;
}
public void setExcludedFromHistory(boolean excludedFromHistory) {
this.excludedFromHistory = excludedFromHistory;
}
}
@@ -8,6 +8,7 @@ import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import com.avaje.ebean.annotation.HistoryExclude;
import com.avaje.ebean.annotation.PrivateOwned;
import com.avaje.ebean.annotation.Where;
import com.avaje.ebean.bean.BeanCollection.ModifyListenMode;
@@ -65,7 +66,11 @@ public class AnnotationAssocManys extends AnnotationParser {
readToMany(manyToMany, prop);
}
OrderBy orderBy = get(prop, OrderBy.class);
if (get(prop, HistoryExclude.class) != null) {
prop.setExcludedFromHistory(true);
}
OrderBy orderBy = get(prop, OrderBy.class);
if (orderBy != null) {
prop.setFetchOrderBy(orderBy.value());
}
@@ -10,6 +10,7 @@ import javax.persistence.UniqueConstraint;
import com.avaje.ebean.annotation.CacheStrategy;
import com.avaje.ebean.annotation.CacheTuning;
import com.avaje.ebean.annotation.EntityConcurrencyMode;
import com.avaje.ebean.annotation.History;
import com.avaje.ebean.annotation.NamedUpdate;
import com.avaje.ebean.annotation.NamedUpdates;
import com.avaje.ebean.annotation.UpdateMode;
@@ -26,8 +27,11 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
*/
public class AnnotationClass extends AnnotationParser {
public AnnotationClass(DeployBeanInfo<?> info) {
private final String asOfViewSuffix;
public AnnotationClass(DeployBeanInfo<?> info, String asOfViewSuffix) {
super(info);
this.asOfViewSuffix = asOfViewSuffix;
}
/**
@@ -48,7 +52,7 @@ public class AnnotationClass extends AnnotationParser {
// default the TableName using NamingConvention.
TableName tableName = namingConvention.getTableName(descriptor.getBeanType());
descriptor.setBaseTable(tableName);
descriptor.setBaseTable(tableName, asOfViewSuffix);
}
}
@@ -84,6 +88,11 @@ public class AnnotationClass extends AnnotationParser {
}
}
History history = cls.getAnnotation(History.class);
if (history != null) {
descriptor.setHistorySupport(true);
}
UpdateMode updateMode = cls.getAnnotation(UpdateMode.class);
if (updateMode != null) {
descriptor.setUpdateChangesOnly(updateMode.updateChangesOnly());
@@ -8,6 +8,7 @@ import com.avaje.ebean.annotation.EmbeddedColumns;
import com.avaje.ebean.annotation.Encrypted;
import com.avaje.ebean.annotation.Expose;
import com.avaje.ebean.annotation.Formula;
import com.avaje.ebean.annotation.HistoryExclude;
import com.avaje.ebean.annotation.Index;
import com.avaje.ebean.annotation.JsonIgnore;
import com.avaje.ebean.annotation.UpdatedTimestamp;
@@ -217,6 +218,10 @@ public class AnnotationFields extends AnnotationParser {
generatedPropFactory.setUpdateTimestamp(prop);
}
if (get(prop, HistoryExclude.class) != null) {
prop.setExcludedFromHistory(true);
}
if (validationAnnotations) {
NotNull notNull = get(prop, NotNull.class);
if (notNull != null && isNotNullOnAllValidationGroups(notNull.groups())) {
@@ -8,7 +8,17 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager;
*/
public class ReadAnnotations {
/**
/**
* Typically _with_history and when appended to the base table derives the name of
* the view that unions the base table with the history table to support asOf queries.
*/
private final String asOfViewSuffix;
public ReadAnnotations(String asOfViewSuffix) {
this.asOfViewSuffix = asOfViewSuffix;
}
/**
* Read the initial non-relationship annotations included Id and EmbeddedId.
* <p>
* We then have enough to create BeanTables which are used in readAssociations
@@ -18,7 +28,7 @@ public class ReadAnnotations {
public void readInitial(DeployBeanInfo<?> info, boolean eagerFetchLobs){
try {
new AnnotationClass(info).parse();
new AnnotationClass(info, asOfViewSuffix).parse();
new AnnotationFields(info, eagerFetchLobs).parse();
} catch (RuntimeException e){