#118 - ENH: Add support for for Soft Deletes (Logical Deletion) ... - initial

This commit is contained in:
Robin Bygrave committed 2015-12-01 23:21:11 +13:00
1 parent 51bee836d4
commit 0ad52cf6a7
24 files changed
+531 -105

No files matched your search

@@ -149,6 +149,9 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
private final String baseTableVersionsBetween;
private final boolean historySupport;
private final BeanProperty softDeleteProperty;
private final boolean softDelete;
private final String draftTable;
/**
@@ -316,8 +319,9 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
private String idBinderIdSql;
private String deleteByIdSql;
private String deleteByIdInSql;
private String softDeleteByIdSql;
private String softDeleteByIdInSql;
private final String name;
@@ -395,6 +399,8 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
// helper object used to derive lists of properties
DeployBeanPropertyLists listHelper = new DeployBeanPropertyLists(owner, this, deploy);
this.softDeleteProperty = listHelper.getSoftDeleteProperty();
this.softDelete = (softDeleteProperty != null);
this.idProperty = listHelper.getId();
this.versionProperty = listHelper.getVersionProperty();
this.draftDirty = listHelper.getDraftDirty();
@@ -621,6 +627,14 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
deleteByIdSql = "delete from " + baseTable + " where " + idEqualsSql;
deleteByIdInSql = "delete from " + baseTable + " where " + idBinderInLHSSqlNoAlias + " ";
if (softDelete) {
softDeleteByIdSql = "update " + baseTable + " set " + getSoftDeleteDbSet() + " where " + idEqualsSql;
softDeleteByIdInSql = "update " + baseTable + " set " + getSoftDeleteDbSet() + " where " + idBinderInLHSSqlNoAlias + " ";
} else {
softDeleteByIdSql = null;
softDeleteByIdInSql = null;
}
if (!isEmbedded()) {
// parse every named update up front into sql dml
for (DeployNamedUpdate namedUpdate : namedUpdates.values()) {
@@ -710,11 +724,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
cacheHelp.initialise();
}
public SqlUpdate deleteById(Object id, List<Object> idList) {
public SqlUpdate deleteById(Object id, List<Object> idList, boolean softDelete) {
if (id != null) {
return deleteById(id);
return deleteById(id, softDelete);
} else {
return deleteByIdList(idList);
return deleteByIdList(idList, softDelete);
}
}
@@ -729,9 +743,10 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
* Return SQL that can be used to delete a list of Id's without any optimistic
* concurrency checking.
*/
private SqlUpdate deleteByIdList(List<Object> idList) {
private SqlUpdate deleteByIdList(List<Object> idList, boolean softDelete) {
StringBuilder sb = new StringBuilder(deleteByIdInSql);
String baseSql = softDelete ? softDeleteByIdInSql : deleteByIdInSql;
StringBuilder sb = new StringBuilder(baseSql);
String inClause = idBinder.getIdInValueExprDelete(idList.size());
sb.append(inClause);
@@ -746,9 +761,10 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
* Return SQL that can be used to delete by Id without any optimistic
* concurrency checking.
*/
private SqlUpdate deleteById(Object id) {
private SqlUpdate deleteById(Object id, boolean softDelete) {
DefaultSqlUpdate sqlDelete = new DefaultSqlUpdate(deleteByIdSql);
String baseSql = softDelete ? softDeleteByIdSql : deleteByIdSql;
DefaultSqlUpdate sqlDelete = new DefaultSqlUpdate(baseSql);
Object[] bindValues = idBinder.getBindValues(id);
for (int i = 0; i < bindValues.length; i++) {
@@ -1175,6 +1191,16 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
return deleteRecurseSkippable;
}
/**
* Return true if delete can use a single SQL statement.
*
* This implies cascade delete does not continue depth wise and that this is no
* associated L2 bean caching.
*/
public boolean isDeleteByStatement() {
return deleteRecurseSkippable && !isBeanCaching();
}
/**
* Find a property annotated with @WhenCreated or @CreatedTimestamp.
*/
@@ -1932,6 +1958,22 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
return readAuditing;
}
public boolean isSoftDelete() {
return softDelete;
}
public void setSoftDeleteValue(EntityBean bean) {
softDeleteProperty.setSoftDeleteValue(bean);
}
public String getSoftDeleteDbSet() {
return softDeleteProperty.getSoftDeleteDbSet();
}
public String getSoftDeletePredicate(String tableAlias) {
return softDeleteProperty.getSoftDeleteDbPredicate(tableAlias);
}
/**
* Return true if this entity type is draftable.
*/
@@ -229,6 +229,10 @@ public class BeanProperty implements ElPropertyValue {
final boolean draftReset;
final boolean softDelete;
final Object softDeleteValue;
final boolean indexed;
final String indexName;
@@ -258,6 +262,8 @@ public class BeanProperty implements ElPropertyValue {
this.draftDirty = deploy.isDraftDirty();
this.draftOnly = deploy.isDraftOnly();
this.draftReset = deploy.isDraftReset();
this.softDelete = deploy.isSoftDelete();
this.softDeleteValue = deploy.getSoftDeleteValue();
this.secondaryTable = deploy.isSecondaryTable();
if (secondaryTable) {
@@ -345,6 +351,8 @@ public class BeanProperty implements ElPropertyValue {
this.draftDirty = source.draftDirty;
this.draftOnly = source.draftOnly;
this.draftReset = source.draftReset;
this.softDelete = source.softDelete;
this.softDeleteValue = source.softDeleteValue;
this.fetchEager = source.fetchEager;
this.unidirectionalShadow = source.unidirectionalShadow;
this.discriminator = source.discriminator;
@@ -616,6 +624,25 @@ public class BeanProperty implements ElPropertyValue {
}
}
public String getSoftDeleteDbSet() {
return dbColumn +"=true";
}
/**
* Return the DB literal predicate used to filter out soft deleted rows from a query.
*/
public String getSoftDeleteDbPredicate(String tableAlias) {
return tableAlias+"."+dbColumn+"=false";
}
/**
* Set the soft delete property value on the bean without invoking lazy loading.
*/
public void setSoftDeleteValue(EntityBean bean) {
setValue(bean, softDeleteValue);
bean._ebean_getIntercept().setChangedProperty(propertyIndex);
}
/**
* Set the value of the property without interception or
* PropertyChangeSupport.
@@ -1056,6 +1083,13 @@ public class BeanProperty implements ElPropertyValue {
return draftReset;
}
/**
* Return true if this property is the soft delete property.
*/
public boolean isSoftDelete() {
return softDelete;
}
/**
* Return true if this property should be included in an Insert.
*/
@@ -800,7 +800,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
public IntersectionRow buildManyDeleteChildren(EntityBean parentBean, ArrayList<Object> excludeDetailIds) {
IntersectionRow row = new IntersectionRow(tableJoin.getTable());
IntersectionRow row = new IntersectionRow(tableJoin.getTable(), targetDescriptor);
if (excludeDetailIds != null && !excludeDetailIds.isEmpty()) {
row.setExcludeIds(excludeDetailIds, getTargetDescriptor());
}
@@ -15,13 +15,21 @@ public class IntersectionRow {
private final String tableName;
private final BeanDescriptor<?> targetDescriptor;
private final LinkedHashMap<String, Object> values = new LinkedHashMap<String, Object>();
private ArrayList<Object> excludeIds;
private BeanDescriptor<?> excludeDescriptor;
public IntersectionRow(String tableName, BeanDescriptor<?> targetDescriptor) {
this.tableName = tableName;
this.targetDescriptor = targetDescriptor;
}
public IntersectionRow(String tableName) {
this.tableName = tableName;
this.targetDescriptor = null;
}
/**
@@ -64,22 +72,20 @@ public class IntersectionRow {
return new DefaultSqlUpdate(server, sb.toString(), bindParams);
}
public SqlUpdate createDelete(EbeanServer server) {
public SqlUpdate createDelete(EbeanServer server, boolean softDelete) {
BindParams bindParams = new BindParams();
StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(tableName).append(" where ");
int count = 0;
for (Map.Entry<String, Object> entry : values.entrySet()) {
if (count++ > 0) {
sb.append(" and ");
}
sb.append(entry.getKey());
sb.append(" = ?");
bindParams.setParameter(count, entry.getValue());
if (softDelete) {
sb.append("update ").append(tableName).append(" set ");
sb.append(targetDescriptor.getSoftDeleteDbSet());
} else {
sb.append("delete from ").append(tableName);
}
sb.append(" where ");
int count = setBindParams(bindParams, sb);
if (excludeIds != null) {
IdInExpression idIn = new IdInExpression(excludeIds);
@@ -108,6 +114,13 @@ public class IntersectionRow {
StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(tableName).append(" where ");
setBindParams(bindParams, sb);
return new DefaultSqlUpdate(server, sb.toString(), bindParams);
}
private int setBindParams(BindParams bindParams, StringBuilder sb) {
int count = 0;
for (Map.Entry<String, Object> entry : values.entrySet()) {
if (count++ > 0) {
@@ -120,6 +133,6 @@ public class IntersectionRow {
bindParams.setParameter(count, entry.getValue());
}
return new DefaultSqlUpdate(server, sb.toString(), bindParams);
return count;
}
}
@@ -189,6 +189,9 @@ public class DeployBeanProperty {
private boolean draftDirty;
private boolean draftReset;
private boolean softDelete;
private String softDeleteValue = "";
public DeployBeanProperty(DeployBeanDescriptor<?> desc, Class<?> propertyType, ScalarType<?> scalarType, ScalarTypeConverter<?, ?> typeConverter) {
this.desc = desc;
this.propertyType = propertyType;
@@ -872,4 +875,43 @@ public class DeployBeanProperty {
public boolean isDraftReset() {
return draftReset;
}
public void setSoftDelete(String softDeleteValue) {
this.softDelete = true;
this.softDeleteValue = softDeleteValue;
}
public boolean isSoftDelete() {
return softDelete;
}
public Object getSoftDeleteValue() {
return !softDelete ? null : "".equals(softDeleteValue) ? defaultSoftDeleteValue() : parseSoftDeleteValue();
}
private Object parseSoftDeleteValue() {
if (Boolean.class.equals(propertyType) || boolean.class.equals(propertyType)) {
return Boolean.parseBoolean(softDeleteValue);
}
if (Integer.class.equals(propertyType) || int.class.equals(propertyType)) {
return Integer.parseInt(softDeleteValue);
}
if (Short.class.equals(propertyType) || short.class.equals(propertyType)) {
return Short.parseShort(softDeleteValue);
}
throw new IllegalStateException("@SoftDelete on ["+getFullBeanName()+"] mapped to unsupported type propertyType["+propertyType+"]");
}
private Object defaultSoftDeleteValue() {
if (Boolean.class.equals(propertyType) || boolean.class.equals(propertyType)) {
return Boolean.TRUE;
}
if (Integer.class.equals(propertyType) || int.class.equals(propertyType)) {
return Integer.valueOf(1);
}
if (Short.class.equals(propertyType) || short.class.equals(propertyType)) {
return Short.valueOf("1");
}
throw new IllegalStateException("@SoftDelete on ["+getFullBeanName()+"] mapped to unsupported type propertyType["+propertyType+"]");
}
}
@@ -293,6 +293,16 @@ public class DeployBeanPropertyLists {
return draftDirty;
}
public BeanProperty getSoftDeleteProperty() {
for (BeanProperty prop: nonManys) {
if (prop.isSoftDelete()) {
return prop;
}
}
return null;
}
/**
* Mode used to determine which BeanPropertyAssoc to include.
*/
@@ -158,6 +158,10 @@ public class AnnotationFields extends AnnotationParser {
if (get(prop, DraftReset.class) != null) {
prop.setDraftReset();
}
SoftDelete softDelete = get(prop, SoftDelete.class);
if (softDelete != null) {
prop.setSoftDelete(softDelete.value());
}
DbJson dbJson = get(prop, DbJson.class);
if (dbJson != null) {