mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#350 - ENH: Add @History findVersions() query - add default ordering to lower sys period desc
This commit is contained in:
@@ -46,10 +46,6 @@ public class CQueryBuilder implements Constants {
|
||||
|
||||
private final boolean selectCountWithAlias;
|
||||
|
||||
private final Map<String,String> asOfTableMapping;
|
||||
|
||||
private final String asOfSysPeriod;
|
||||
private final DbHistorySupport dbHistorySupport;
|
||||
private final CQueryHistorySupport historySupport;
|
||||
|
||||
private final DatabasePlatform dbPlatform;
|
||||
@@ -57,23 +53,17 @@ public class CQueryBuilder implements Constants {
|
||||
/**
|
||||
* Create the SqlGenSelect.
|
||||
*/
|
||||
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, Map<String,String> asOfTableMapping, String asOfSysPeriod) {
|
||||
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, CQueryHistorySupport historySupport) {
|
||||
|
||||
this.dbPlatform = dbPlatform;
|
||||
this.binder = binder;
|
||||
this.asOfTableMapping = asOfTableMapping;
|
||||
this.asOfSysPeriod = asOfSysPeriod;
|
||||
this.historySupport = historySupport;
|
||||
this.tableAliasPlaceHolder = dbPlatform.getTableAliasPlaceHolder();
|
||||
this.columnAliasPrefix = dbPlatform.getColumnAliasPrefix();
|
||||
this.sqlSelectBuilder = new RawSqlSelectClauseBuilder(dbPlatform, binder);
|
||||
|
||||
this.dbHistorySupport = dbPlatform.getHistorySupport();
|
||||
this.historySupport = new CQueryHistorySupport(dbHistorySupport, asOfTableMapping, asOfSysPeriod);
|
||||
this.sqlLimiter = dbPlatform.getSqlLimiter();
|
||||
this.rawSqlHandler = new CQueryBuilderRawSql(sqlLimiter, dbPlatform);
|
||||
|
||||
this.selectCountWithAlias = dbPlatform.isSelectCountWithAlias();
|
||||
|
||||
this.dbPlatform = dbPlatform;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -427,7 +417,7 @@ public class CQueryBuilder implements Constants {
|
||||
if (i > 0) {
|
||||
sb.append(" and ");
|
||||
}
|
||||
sb.append(dbHistorySupport.getAsOfPredicate(asOfTableAlias.get(i), asOfSysPeriod));
|
||||
sb.append(historySupport.getAsOfPredicate(asOfTableAlias.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,13 +30,18 @@ public class CQueryEngine {
|
||||
|
||||
private static final int defaultSecondaryQueryBatchSize = 100;
|
||||
|
||||
private static final String T0 = "t0";
|
||||
|
||||
private final boolean forwardOnlyHintOnFindIterate;
|
||||
|
||||
private final CQueryBuilder queryBuilder;
|
||||
|
||||
private final CQueryHistorySupport historySupport;
|
||||
|
||||
public CQueryEngine(DatabasePlatform dbPlatform, Binder binder, Map<String,String> asOfTableMapping, String asOfSysPeriod) {
|
||||
this.forwardOnlyHintOnFindIterate = dbPlatform.isForwardOnlyHintOnFindIterate();
|
||||
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, asOfTableMapping, asOfSysPeriod);
|
||||
this.historySupport = new CQueryHistorySupport(dbPlatform.getHistorySupport(), asOfTableMapping, asOfSysPeriod);
|
||||
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, historySupport);
|
||||
}
|
||||
|
||||
public <T> CQuery<T> buildQuery(OrmQueryRequest<T> request) {
|
||||
@@ -161,6 +166,12 @@ public class CQueryEngine {
|
||||
*/
|
||||
public <T> List<Version<T>> findVersions(OrmQueryRequest<T> request) {
|
||||
|
||||
SpiQuery<T> query = request.getQuery();
|
||||
|
||||
// order by id asc, lower sys period desc
|
||||
query.orderBy().asc(request.getBeanDescriptor().getIdProperty().getName());
|
||||
query.orderBy().desc(getSysPeriodLower(query));
|
||||
|
||||
CQuery<T> cquery = queryBuilder.buildQuery(request);
|
||||
try {
|
||||
cquery.prepareBindExecuteQuery();
|
||||
@@ -185,6 +196,17 @@ public class CQueryEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@@ -22,12 +22,12 @@ public class CQueryHistorySupport {
|
||||
/**
|
||||
* The sys period column.
|
||||
*/
|
||||
private final String asOfSysPeriod;
|
||||
private final String sysPeriod;
|
||||
|
||||
public CQueryHistorySupport(DbHistorySupport dbHistorySupport, Map<String, String> asOfTableMap, String asOfSysPeriod) {
|
||||
public CQueryHistorySupport(DbHistorySupport dbHistorySupport, Map<String, String> asOfTableMap, String sysPeriod) {
|
||||
this.dbHistorySupport = dbHistorySupport;
|
||||
this.asOfTableMap = asOfTableMap;
|
||||
this.asOfSysPeriod = asOfSysPeriod;
|
||||
this.sysPeriod = sysPeriod;
|
||||
}
|
||||
|
||||
public String getAsOfView(String table) {
|
||||
@@ -36,12 +36,16 @@ public class CQueryHistorySupport {
|
||||
|
||||
public String getSysPeriodLower(String tableAlias) {
|
||||
|
||||
return dbHistorySupport.getSysPeriodLower(tableAlias, asOfSysPeriod);
|
||||
return dbHistorySupport.getSysPeriodLower(tableAlias, sysPeriod);
|
||||
}
|
||||
|
||||
public String getSysPeriodUpper(String tableAlias) {
|
||||
|
||||
return dbHistorySupport.getSysPeriodUpper(tableAlias, asOfSysPeriod);
|
||||
return dbHistorySupport.getSysPeriodUpper(tableAlias, sysPeriod);
|
||||
}
|
||||
|
||||
public String getAsOfPredicate(String tableAlias) {
|
||||
|
||||
return dbHistorySupport.getAsOfPredicate(tableAlias, sysPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user