Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Refactored delete-by-id #3178

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.ebeaninternal.server.deploy;

import io.ebean.Query;
import io.ebean.SqlUpdate;
import io.ebean.Transaction;
import io.ebean.bean.EntityBean;
import io.ebean.core.type.DocPropertyType;
import io.ebean.text.PathProperties;
Expand Down Expand Up @@ -570,4 +572,26 @@ ExportedProperty findMatch(boolean embedded, BeanProperty prop, String matchColu
+ " or a @JoinColumn needs an explicit referencedColumnName specified?";
throw new PersistenceException(msg);
}

/**
* Create SqlUpdate statement to delete all child beans of the parent <code>id</code>.
*/
public abstract SqlUpdate deleteByParentId(Object id);

/**
* Create SqlUpdate statement to delete all child beans of the parent ids in <code>idList</code>.
*/
public abstract SqlUpdate deleteByParentIdList(List<Object> idList);

/**
* Find child beans of the parent <code>id</code>.
*/
public abstract List<Object> findIdsByParentId(Object id, Transaction transaction, boolean hard);

/**
* Find child beans of the parent ids in <code>idList</code>.
*/
public abstract List<Object> findIdsByParentIdList(List<Object> idList, Transaction transaction, boolean hard);

}

Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,38 @@ public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
// do not add to the selectChain at the top level of the Many bean
}

public SpiSqlUpdate deleteByParentId(Object parentId, List<Object> parentIdist) {
if (parentId != null) {
return sqlHelp.deleteByParentId(parentId);
} else {
return sqlHelp.deleteByParentIdList(parentIdist);
}
@Override
public SpiSqlUpdate deleteByParentId(Object parentId) {
return sqlHelp.deleteByParentId(parentId);
}

@Override
public SpiSqlUpdate deleteByParentIdList(List<Object> parentIdist) {
return sqlHelp.deleteByParentIdList(parentIdist);
}


/**
* Find the Id's of detail beans given a parent Id or list of parent Id's.
* Find the Id's of detail beans given a parent Id
*/
public List<Object> findIdsByParentId(Object parentId, List<Object> parentIdList, Transaction t, List<Object> excludeDetailIds, boolean hard) {
if (parentId != null) {
return sqlHelp.findIdsByParentId(parentId, t, excludeDetailIds, hard);
} else {
return sqlHelp.findIdsByParentIdList(parentIdList, t, excludeDetailIds, hard);
}
@Override
public List<Object> findIdsByParentId(Object parentId, Transaction t, boolean hard) {
return sqlHelp.findIdsByParentId(parentId, t, hard, null);
}

/**
* Find the Id's of detail beans given a parent Id and optionally exclude detail IDs
*/
public List<Object> findIdsByParentId(Object parentId, Transaction t, boolean hard, List<Object> excludeDetailIds) {
return sqlHelp.findIdsByParentId(parentId, t, hard, excludeDetailIds);
}

/**
* Find the Id's of detail beans given a list of parent Id's.
*/
@Override
public List<Object> findIdsByParentIdList(List<Object> parentIdList, Transaction t, boolean hard) {
return sqlHelp.findIdsByParentIdList(parentIdList, t, hard);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiSqlUpdate;
import io.ebeaninternal.server.core.DefaultSqlUpdate;
import io.ebeaninternal.server.deploy.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.server.deploy.visitor.VisitProperties;
import io.ebeaninternal.server.core.DefaultSqlUpdate;
import io.ebeaninternal.server.util.Str;

import java.util.List;
Expand Down Expand Up @@ -123,7 +123,7 @@ void addWhereParentIdIn(SpiQuery<?> query, List<Object> parentIds) {
many.bindParentIdsIn(rawWhere, parentIds, query);
}

List<Object> findIdsByParentId(Object parentId, Transaction t, List<Object> excludeDetailIds, boolean hard) {
List<Object> findIdsByParentId(Object parentId, Transaction t, boolean hard, List<Object> excludeDetailIds) {
final SpiEbeanServer server = descriptor.ebeanServer();
final SpiQuery<?> query = many.newQuery(server);
many.bindParentIdEq(rawParentIdEQ(""), parentId, query);
Expand All @@ -136,16 +136,13 @@ List<Object> findIdsByParentId(Object parentId, Transaction t, List<Object> excl
return server.findIds(query, t);
}

List<Object> findIdsByParentIdList(List<Object> parentIds, Transaction t, List<Object> excludeDetailIds, boolean hard) {
List<Object> findIdsByParentIdList(List<Object> parentIds, Transaction t, boolean hard) {
final SpiEbeanServer server = descriptor.ebeanServer();
final SpiQuery<?> query = many.newQuery(server);
many.bindParentIdsIn(rawParentIdIN("", parentIds.size()), parentIds, query);
if (hard) {
query.setIncludeSoftDeletes();
}
if (excludeDetailIds != null && !excludeDetailIds.isEmpty()) {
query.where().not(query.getExpressionFactory().idIn(excludeDetailIds));
}
return server.findIds(query, t);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,44 +237,32 @@ public String elPlaceholder(boolean encrypted) {
return encrypted ? elPlaceHolderEncrypted : elPlaceHolder;
}

public SqlUpdate deleteByParentId(Object parentId, List<Object> parentIdist) {
if (parentId != null) {
return deleteByParentId(parentId);
} else {
return deleteByParentIdList(parentIdist);
}
}

private SqlUpdate deleteByParentIdList(List<Object> parentIds) {
@Override
public SqlUpdate deleteByParentIdList(List<Object> parentIds) {
String sql = deleteByParentIdInSql + targetIdBinder.idInValueExpr(false, parentIds.size());
DefaultSqlUpdate delete = new DefaultSqlUpdate(sql);
bindParentIds(delete, parentIds);
return delete;
}

private SqlUpdate deleteByParentId(Object parentId) {
@Override
public SqlUpdate deleteByParentId(Object parentId) {
DefaultSqlUpdate delete = new DefaultSqlUpdate(deleteByParentIdSql);
bindParentId(delete, parentId);
return delete;
}

public List<Object> findIdsByParentId(Object parentId, List<Object> parentIds, Transaction t) {
if (parentId != null) {
return findIdsByParentId(parentId, t);
} else {
return findIdsByParentIdList(parentIds, t);
}
}

private List<Object> findIdsByParentId(Object parentId, Transaction t) {
public List<Object> findIdsByParentId(Object parentId, Transaction t, boolean hard) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing override: Check if abstract method def. is OK

String rawWhere = deriveWhereParentIdSql(false);
SpiEbeanServer server = server();
Query<?> q = server.find(type());
bindParentIdEq(rawWhere, parentId, q);
return server.findIds(q, t);
}

private List<Object> findIdsByParentIdList(List<Object> parentIds, Transaction t) {
public List<Object> findIdsByParentIdList(List<Object> parentIds, Transaction t, boolean hard) {
String rawWhere = deriveWhereParentIdSql(true);
String inClause = idBinder().idInValueExpr(false, parentIds.size());
String expr = rawWhere + inClause;
Expand Down
Loading