mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#461 - ENH: Add @Draftable support - phase 1
This commit is contained in:
1 parent
42e7103aa6
commit
7b76e13bce
56 files changed
+944
-214
No files matched your search
@@ -234,6 +234,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDraftQuery() {
|
||||
return query.isAsDraft();
|
||||
}
|
||||
|
||||
public Boolean isReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
@@ -47,15 +47,18 @@ public class CQueryBuilder {
|
||||
|
||||
private final CQueryHistorySupport historySupport;
|
||||
|
||||
private final CQueryDraftSupport draftSupport;
|
||||
|
||||
private final DatabasePlatform dbPlatform;
|
||||
|
||||
/**
|
||||
* Create the SqlGenSelect.
|
||||
*/
|
||||
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, CQueryHistorySupport historySupport) {
|
||||
public CQueryBuilder(DatabasePlatform dbPlatform, Binder binder, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
|
||||
|
||||
this.dbPlatform = dbPlatform;
|
||||
this.binder = binder;
|
||||
this.draftSupport = draftSupport;
|
||||
this.historySupport = historySupport;
|
||||
this.tableAliasPlaceHolder = dbPlatform.getTableAliasPlaceHolder();
|
||||
this.columnAliasPrefix = dbPlatform.getColumnAliasPrefix();
|
||||
@@ -103,7 +106,7 @@ public class CQueryBuilder {
|
||||
|
||||
predicates.prepare(true);
|
||||
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query));
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
|
||||
|
||||
boolean includeJoins = sqlTree.isIncludeJoins();
|
||||
|
||||
@@ -146,7 +149,7 @@ public class CQueryBuilder {
|
||||
// use RawSql or generated Sql
|
||||
predicates.prepare(true);
|
||||
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query));
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
|
||||
SqlLimitResponse s = buildSql(null, request, predicates, sqlTree);
|
||||
String sql = s.getSql();
|
||||
|
||||
@@ -164,6 +167,13 @@ public class CQueryBuilder {
|
||||
return query.getTemporalMode() != SpiQuery.TemporalMode.CURRENT ? historySupport : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the draft support (or null) for a 'asDraft' query.
|
||||
*/
|
||||
private <T> CQueryDraftSupport getDraftSupport(SpiQuery<T> query) {
|
||||
return query.getTemporalMode() == SpiQuery.TemporalMode.DRAFT ? draftSupport : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the row count query.
|
||||
*/
|
||||
@@ -205,7 +215,7 @@ public class CQueryBuilder {
|
||||
|
||||
predicates.prepare(true);
|
||||
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query));
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
|
||||
SqlLimitResponse s = buildSql(sqlSelect, request, predicates, sqlTree);
|
||||
String sql = s.getSql();
|
||||
if (hasMany || query.isRawSql()) {
|
||||
@@ -255,7 +265,7 @@ public class CQueryBuilder {
|
||||
// Build the tree structure that represents the query.
|
||||
SpiQuery<T> query = request.getQuery();
|
||||
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query));
|
||||
SqlTree sqlTree = createSqlTree(request, predicates, getHistorySupport(query), getDraftSupport(query));
|
||||
if (query.isAsOfQuery()) {
|
||||
sqlTree.addAsOfTableAlias(query);
|
||||
}
|
||||
@@ -294,13 +304,13 @@ public class CQueryBuilder {
|
||||
* order by clauses that are not already included for the select clause.
|
||||
* </p>
|
||||
*/
|
||||
private SqlTree createSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport) {
|
||||
private SqlTree createSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
|
||||
|
||||
if (request.isRawSql()) {
|
||||
return createRawSqlSqlTree(request, predicates);
|
||||
}
|
||||
|
||||
return new SqlTreeBuilder(tableAliasPlaceHolder, columnAliasPrefix, request, predicates, historySupport).build();
|
||||
return new SqlTreeBuilder(tableAliasPlaceHolder, columnAliasPrefix, request, predicates, historySupport, draftSupport).build();
|
||||
}
|
||||
|
||||
private SqlTree createRawSqlSqlTree(OrmQueryRequest<?> request, CQueryPredicates predicates) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.avaje.ebeaninternal.server.query;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Support 'asDraft' queries.
|
||||
*/
|
||||
public class CQueryDraftSupport {
|
||||
|
||||
/**
|
||||
* The mapping of base tables to their matching 'draft' table.
|
||||
*/
|
||||
private final Map<String, String> tableMap;
|
||||
|
||||
public CQueryDraftSupport(Map<String, String> tableMap) {
|
||||
this.tableMap = tableMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the draft table associated to the base table.
|
||||
*
|
||||
* This returns null for entities that are not draftable and in that case
|
||||
* the usual base table is used.
|
||||
*/
|
||||
public String getDraftTable(String table) {
|
||||
return tableMap.get(table);
|
||||
}
|
||||
}
|
||||
@@ -41,10 +41,10 @@ public class CQueryEngine {
|
||||
|
||||
private final CQueryHistorySupport historySupport;
|
||||
|
||||
public CQueryEngine(DatabasePlatform dbPlatform, Binder binder, Map<String, String> asOfTableMapping, String asOfSysPeriod) {
|
||||
public CQueryEngine(DatabasePlatform dbPlatform, Binder binder, Map<String, String> asOfTableMapping, String asOfSysPeriod, Map<String, String> draftTableMap) {
|
||||
this.forwardOnlyHintOnFindIterate = dbPlatform.isForwardOnlyHintOnFindIterate();
|
||||
this.historySupport = new CQueryHistorySupport(dbPlatform.getHistorySupport(), asOfTableMapping, asOfSysPeriod);
|
||||
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, historySupport);
|
||||
this.queryBuilder = new CQueryBuilder(dbPlatform, binder, historySupport, new CQueryDraftSupport(draftTableMap));
|
||||
}
|
||||
|
||||
public <T> CQuery<T> buildQuery(OrmQueryRequest<T> request) {
|
||||
|
||||
@@ -266,6 +266,10 @@ public class CQueryFetchIds {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDraftQuery() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,6 +45,8 @@ public class DefaultDbSqlContext implements DbSqlContext {
|
||||
|
||||
private ArrayList<BeanProperty> encryptedProps;
|
||||
|
||||
private final CQueryDraftSupport draftSupport;
|
||||
|
||||
private final CQueryHistorySupport historySupport;
|
||||
|
||||
private final boolean historyQuery;
|
||||
@@ -53,12 +55,13 @@ public class DefaultDbSqlContext implements DbSqlContext {
|
||||
* Construct for SELECT clause (with column alias settings).
|
||||
*/
|
||||
public DefaultDbSqlContext(SqlTreeAlias alias, String tableAliasPlaceHolder,
|
||||
String columnAliasPrefix, boolean alwaysUseColumnAlias, CQueryHistorySupport historySupport) {
|
||||
String columnAliasPrefix, boolean alwaysUseColumnAlias, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
|
||||
|
||||
this.alias = alias;
|
||||
this.tableAliasPlaceHolder = tableAliasPlaceHolder;
|
||||
this.columnAliasPrefix = columnAliasPrefix;
|
||||
this.useColumnAlias = alwaysUseColumnAlias;
|
||||
this.draftSupport = draftSupport;
|
||||
this.historySupport = historySupport;
|
||||
this.historyQuery = (historySupport != null);
|
||||
}
|
||||
@@ -105,22 +108,18 @@ public class DefaultDbSqlContext implements DbSqlContext {
|
||||
|
||||
sb.append(" ");
|
||||
sb.append(type);
|
||||
if (!historyQuery) {
|
||||
if (draftSupport != null) {
|
||||
appendTable(table, draftSupport.getDraftTable(table));
|
||||
|
||||
} else if (!historyQuery) {
|
||||
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 = historySupport.getAsOfView(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(" ");
|
||||
}
|
||||
appendTable(table, historySupport.getAsOfView(table));
|
||||
}
|
||||
|
||||
sb.append(a2);
|
||||
sb.append(" on ");
|
||||
|
||||
@@ -149,6 +148,21 @@ public class DefaultDbSqlContext implements DbSqlContext {
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
private void appendTable(String table, String draftTable) {
|
||||
if (draftTable != null) {
|
||||
// there is an associated history table and view so use that
|
||||
sb.append(" ").append(draftTable).append(" ");
|
||||
|
||||
} else {
|
||||
sb.append(" ").append(table).append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDraftQuery() {
|
||||
return draftSupport != null;
|
||||
}
|
||||
|
||||
public String getTableAlias(String prefix) {
|
||||
return alias.getTableAlias(prefix);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class SqlBeanLoad {
|
||||
|
||||
public Object load(BeanProperty prop) {
|
||||
|
||||
if (!rawSql && !prop.isLoadProperty()) {
|
||||
if (!rawSql && !prop.isLoadProperty(ctx.isDraftQuery())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ public class SqlTreeBuilder {
|
||||
* to the root node.
|
||||
*/
|
||||
public SqlTreeBuilder(String tableAliasPlaceHolder, String columnAliasPrefix,
|
||||
OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport) {
|
||||
OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryHistorySupport historySupport, CQueryDraftSupport draftSupport) {
|
||||
|
||||
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, historySupport);
|
||||
this.ctx = new DefaultDbSqlContext(alias, tableAliasPlaceHolder, columnAliasPrefix, !subQuery, historySupport, draftSupport);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user