mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#384 - ENH: Add findVersionsBetween(start, end) ... including implementation support for Oracle total recall
This commit is contained in:
1 parent
75e9770d5e
commit
580b51381e
16 files changed
+170
-19
No files matched your search
@@ -133,6 +133,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
*/
|
||||
private final String baseTable;
|
||||
private final String baseTableAsOf;
|
||||
private final String baseTableVersionsBetween;
|
||||
private final boolean historySupport;
|
||||
|
||||
/**
|
||||
@@ -354,6 +355,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
this.historySupport = deploy.isHistorySupport();
|
||||
this.baseTable = InternString.intern(deploy.getBaseTable());
|
||||
this.baseTableAsOf = deploy.getBaseTableAsOf();
|
||||
this.baseTableVersionsBetween = deploy.getBaseTableVersionsBetween();
|
||||
this.autoFetchTunable = EntityType.ORM.equals(entityType) && (beanFinder == null);
|
||||
|
||||
// helper object used to derive lists of properties
|
||||
@@ -1686,7 +1688,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
*/
|
||||
public String getBaseTable(SpiQuery.TemporalMode mode) {
|
||||
switch (mode) {
|
||||
case VERSIONS: return baseTableAsOf;
|
||||
case VERSIONS: return baseTableVersionsBetween;
|
||||
case AS_OF: return baseTableAsOf;
|
||||
default: return baseTable;
|
||||
}
|
||||
|
||||
@@ -176,7 +176,8 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
this.eagerFetchLobs = serverConfig.isEagerFetchLobs();
|
||||
|
||||
this.asOfViewSuffix = getAsOfViewSuffix(databasePlatform, serverConfig);
|
||||
this.readAnnotations = new ReadAnnotations(config.getGeneratedPropertyFactory(), asOfViewSuffix);
|
||||
String versionsBetweenSuffix = getVersionsBetweenSuffix(databasePlatform, serverConfig);
|
||||
this.readAnnotations = new ReadAnnotations(config.getGeneratedPropertyFactory(), asOfViewSuffix, versionsBetweenSuffix);
|
||||
this.bootupClasses = config.getBootupClasses();
|
||||
this.createProperties = config.getDeployCreateProperties();
|
||||
this.namingConvention = serverConfig.getNamingConvention();
|
||||
@@ -201,8 +202,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the AsOfViewSuffix allowing the DbHistorySupport to override the value in
|
||||
* the case where there is no view (Oracle, DB2, MS SQL Server).
|
||||
* Return the AsOfViewSuffix based on the DbHistorySupport.
|
||||
*/
|
||||
private String getAsOfViewSuffix(DatabasePlatform databasePlatform, ServerConfig serverConfig) {
|
||||
|
||||
@@ -211,6 +211,16 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
return (historySupport == null ) ? serverConfig.getAsOfViewSuffix() : historySupport.getAsOfViewSuffix(serverConfig.getAsOfViewSuffix());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the versions between timestamp suffix based on the DbHistorySupport.
|
||||
*/
|
||||
private String getVersionsBetweenSuffix(DatabasePlatform databasePlatform, ServerConfig serverConfig) {
|
||||
|
||||
DbHistorySupport historySupport = databasePlatform.getHistorySupport();
|
||||
// with historySupport returns a simple view suffix or the sql2011 versions between timestamp suffix
|
||||
return (historySupport == null ) ? serverConfig.getAsOfViewSuffix() : historySupport.getVersionsBetweenSuffix(serverConfig.getAsOfViewSuffix());
|
||||
}
|
||||
|
||||
public BeanDescriptor<?> getBeanDescriptorById(String descriptorId) {
|
||||
return idDescMap.get(descriptorId);
|
||||
}
|
||||
@@ -1029,7 +1039,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
|
||||
if (!EntityType.ORM.equals(desc.getEntityType())) {
|
||||
// not using base table
|
||||
desc.setBaseTable(null, null);
|
||||
desc.setBaseTable(null, null, null);
|
||||
}
|
||||
|
||||
// mark transient properties
|
||||
|
||||
@@ -115,6 +115,8 @@ public class DeployBeanDescriptor<T> {
|
||||
|
||||
private String baseTableAsOf;
|
||||
|
||||
private String baseTableVersionsBetween;
|
||||
|
||||
private boolean historySupport;
|
||||
|
||||
private TableName baseTableFull;
|
||||
@@ -464,10 +466,20 @@ public class DeployBeanDescriptor<T> {
|
||||
return baseTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base table with as of suffix.
|
||||
*/
|
||||
public String getBaseTableAsOf() {
|
||||
return baseTableAsOf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base table with versions between suffix.
|
||||
*/
|
||||
public String getBaseTableVersionsBetween() {
|
||||
return baseTableVersionsBetween;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base table with full structure.
|
||||
*/
|
||||
@@ -478,10 +490,11 @@ public class DeployBeanDescriptor<T> {
|
||||
/**
|
||||
* Set the base table. Only properties mapped to the base table are by default persisted.
|
||||
*/
|
||||
public void setBaseTable(TableName baseTableFull, String asOfSuffix) {
|
||||
public void setBaseTable(TableName baseTableFull, String asOfSuffix, String versionsBetweenSuffix) {
|
||||
this.baseTableFull = baseTableFull;
|
||||
this.baseTable = baseTableFull == null ? null : baseTableFull.getQualifiedName();
|
||||
this.baseTableAsOf = baseTable + asOfSuffix;
|
||||
this.baseTableVersionsBetween = baseTable + versionsBetweenSuffix;
|
||||
}
|
||||
|
||||
public void sortProperties() {
|
||||
|
||||
@@ -29,9 +29,12 @@ public class AnnotationClass extends AnnotationParser {
|
||||
|
||||
private final String asOfViewSuffix;
|
||||
|
||||
public AnnotationClass(DeployBeanInfo<?> info, boolean validationAnnotations, String asOfViewSuffix) {
|
||||
private final String versionsBetweenSuffix;
|
||||
|
||||
public AnnotationClass(DeployBeanInfo<?> info, boolean validationAnnotations, String asOfViewSuffix, String versionsBetweenSuffix) {
|
||||
super(info, validationAnnotations);
|
||||
this.asOfViewSuffix = asOfViewSuffix;
|
||||
this.versionsBetweenSuffix = versionsBetweenSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +55,7 @@ public class AnnotationClass extends AnnotationParser {
|
||||
// default the TableName using NamingConvention.
|
||||
TableName tableName = namingConvention.getTableName(descriptor.getBeanType());
|
||||
|
||||
descriptor.setBaseTable(tableName, asOfViewSuffix);
|
||||
descriptor.setBaseTable(tableName, asOfViewSuffix, versionsBetweenSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ public class ReadAnnotations {
|
||||
*/
|
||||
private final String asOfViewSuffix;
|
||||
|
||||
private final String versionsBetweenSuffix;
|
||||
|
||||
/**
|
||||
* True if the javax validation annotations are present in the classpath.
|
||||
*/
|
||||
@@ -31,9 +33,10 @@ public class ReadAnnotations {
|
||||
*/
|
||||
private final boolean jacksonAnnotations;
|
||||
|
||||
public ReadAnnotations(GeneratedPropertyFactory generatedPropFactory, String asOfViewSuffix) {
|
||||
public ReadAnnotations(GeneratedPropertyFactory generatedPropFactory, String asOfViewSuffix, String versionsBetweenSuffix) {
|
||||
this.generatedPropFactory = generatedPropFactory;
|
||||
this.asOfViewSuffix = asOfViewSuffix;
|
||||
this.versionsBetweenSuffix = versionsBetweenSuffix;
|
||||
this.javaxValidationAnnotations = ClassUtil.isJavaxValidationAnnotationsPresent();
|
||||
this.jacksonAnnotations = ClassUtil.isJacksonAnnotationsPresent();
|
||||
}
|
||||
@@ -48,12 +51,11 @@ public class ReadAnnotations {
|
||||
public void readInitial(DeployBeanInfo<?> info, boolean eagerFetchLobs) {
|
||||
|
||||
try {
|
||||
new AnnotationClass(info, javaxValidationAnnotations, asOfViewSuffix).parse();
|
||||
new AnnotationClass(info, javaxValidationAnnotations, asOfViewSuffix, versionsBetweenSuffix).parse();
|
||||
new AnnotationFields(generatedPropFactory, info, javaxValidationAnnotations, jacksonAnnotations, eagerFetchLobs).parse();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
String msg = "Error reading annotations for " + info;
|
||||
throw new RuntimeException(msg, e);
|
||||
throw new RuntimeException("Error reading annotations for " + info, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -228,6 +228,11 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
|
||||
return exprList.findVersions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Version<T>> findVersionsBetween(Timestamp start, Timestamp end) {
|
||||
return exprList.findVersionsBetween(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> apply(PathProperties pathProperties) {
|
||||
return exprList.apply(pathProperties);
|
||||
|
||||
@@ -173,6 +173,12 @@ public class CQueryEngine {
|
||||
|
||||
SpiQuery<T> query = request.getQuery();
|
||||
|
||||
if (query.isVersionsBetween() && !historySupport.isBindAtFromClause()) {
|
||||
// just add as normal predicates using the lower bound
|
||||
query.where().gt(getSysPeriodLower(query), query.getVersionStart());
|
||||
query.where().lt(getSysPeriodLower(query), query.getVersionEnd());
|
||||
}
|
||||
|
||||
// order by id asc, lower sys period desc
|
||||
query.orderBy().asc(request.getBeanDescriptor().getIdProperty().getName());
|
||||
query.orderBy().desc(getSysPeriodLower(query));
|
||||
@@ -207,14 +213,18 @@ public class CQueryEngine {
|
||||
|
||||
BeanDescriptor<T> descriptor = request.getBeanDescriptor();
|
||||
|
||||
Version<T> current = versions.get(0);
|
||||
for (int i = 1; i < versions.size(); i++) {
|
||||
Version<T> next = versions.get(i);
|
||||
deriveVersionDiff(current, next, descriptor);
|
||||
current = next;
|
||||
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<String, ValuePair>());
|
||||
}
|
||||
// put an empty map into the last one
|
||||
current.setDiff(new LinkedHashMap<String, ValuePair>());
|
||||
}
|
||||
|
||||
private <T> void deriveVersionDiff(Version<T> current, Version<T> prior, BeanDescriptor<T> descriptor) {
|
||||
|
||||
@@ -131,6 +131,16 @@ public class CQueryPredicates {
|
||||
|
||||
StringBuilder bindLog = new StringBuilder();
|
||||
|
||||
if (query.isVersionsBetween() && binder.isBindAsOfWithFromClause()) {
|
||||
// sql2011 based versions between timestamp syntax
|
||||
Timestamp start = query.getVersionStart();
|
||||
Timestamp end = query.getVersionEnd();
|
||||
bindLog.append("between ").append(start).append(" and ").append(end);
|
||||
binder.bindObject(dataBind, start);
|
||||
binder.bindObject(dataBind, end);
|
||||
bindLog.append(", ");
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -170,6 +170,9 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
|
||||
private TemporalMode temporalMode = TemporalMode.CURRENT;
|
||||
|
||||
private Timestamp versionsStart;
|
||||
private Timestamp versionsEnd;
|
||||
|
||||
private int bufferFetchSizeHint;
|
||||
|
||||
private boolean usageProfiling = true;
|
||||
@@ -714,6 +717,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
builder.add(disableLazyLoading);
|
||||
builder.add(id != null);
|
||||
builder.add(asOf != null);
|
||||
builder.add(versionsStart != null);
|
||||
builder.add(rawSql == null ? 0 : rawSql.queryHash());
|
||||
builder.add(includeTableJoin != null ? includeTableJoin.queryHash() : 0);
|
||||
builder.add(rootTableAlias);
|
||||
@@ -785,6 +789,8 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
hc = hc * 31 + (havingExpressions == null ? 0 : havingExpressions.queryBindHash());
|
||||
hc = hc * 31 + (bindParams == null ? 0 : bindParams.queryBindHash());
|
||||
hc = hc * 31 + (asOf == null ? 0 : asOf.hashCode());
|
||||
hc = hc * 31 + (versionsStart == null ? 0 : versionsStart.hashCode());
|
||||
hc = hc * 31 + (versionsEnd == null ? 0 : versionsEnd.hashCode());
|
||||
return hc;
|
||||
}
|
||||
|
||||
@@ -843,6 +849,21 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
return maxRows > 0 || firstRow > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVersionsBetween() {
|
||||
return versionsStart != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp getVersionStart() {
|
||||
return versionsStart;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp getVersionEnd() {
|
||||
return versionsEnd;
|
||||
}
|
||||
|
||||
public Boolean isReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
@@ -957,6 +978,17 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
return server.findVersions(this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Version<T>> findVersionsBetween(Timestamp start, Timestamp end) {
|
||||
if (start == null || end == null) {
|
||||
throw new IllegalArgumentException("start and end must not be null");
|
||||
}
|
||||
this.temporalMode = TemporalMode.VERSIONS;
|
||||
this.versionsStart = start;
|
||||
this.versionsEnd = end;
|
||||
return server.findVersions(this, null);
|
||||
}
|
||||
|
||||
public QueryIterator<T> findIterate() {
|
||||
return server.findIterate(this, null);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user