Browse Source

Refactor bulk delete method (#12)

Merge branch 'master' into kris/refactor_bulk_delete_methods

Allow null as parameters in bulk delete method

Use StringBuilder instead of appending Strings

Use strict types instead of var

Add new method for bulk delete with old signature

Do not use SingletonMap so it may appended

Set bulkDelete's parameters to be non null

Refactor bulkDelete methods

Co-authored-by: jonathan <jonathan@noreply.git.bubblev.org>
Co-authored-by: Kristijan Mitrovic <kmitrovic@itekako.com>
Reviewed-on: https://git.bubblev.org/bubblev/cobbzilla-wizard/pulls/12
tags/2.0.1
Kristijan Mitrovic 4 years ago
committed by jonathan
parent
commit
e2783ac80f
1 changed files with 22 additions and 23 deletions
  1. +22
    -23
      wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractCRUDDAO.java

+ 22
- 23
wizard-server/src/main/java/org/cobbzilla/wizard/dao/AbstractCRUDDAO.java View File

@@ -302,13 +302,13 @@ public abstract class AbstractCRUDDAO<E extends Identifiable>
+ " SET " + String.join(" IS NULL, ", setFields) + " IS NULL" + " SET " + String.join(" IS NULL, ", setFields) + " IS NULL"
+ whereClause); + whereClause);
} else { } else {
final var setFieldsSQLPart = Arrays.stream(setFields)
.map(s -> s + (s == null ? " IS NULL" : " = :" + s))
.collect(Collectors.joining(", "));
final String setFieldsSQLPart = Arrays.stream(setFields)
.map(s -> s + (s == null ? " IS NULL" : " = :" + s))
.collect(Collectors.joining(", "));
queryBase = session.createQuery("UPDATE " + getEntityClass().getSimpleName() queryBase = session.createQuery("UPDATE " + getEntityClass().getSimpleName()
+ " SET " + setFieldsSQLPart + " SET " + setFieldsSQLPart
+ whereClause); + whereClause);
for (var i = 0; i < setValues.length; i++) {
for (int i = 0; i < setValues.length; i++) {
if (setValues[i] != null) queryBase.setParameter(setFields[i], setValues[i]); if (setValues[i] != null) queryBase.setParameter(setFields[i], setValues[i]);
} }
} }
@@ -339,31 +339,30 @@ public abstract class AbstractCRUDDAO<E extends Identifiable>


public static final String EX_UUID = "__exclude_uuid__"; public static final String EX_UUID = "__exclude_uuid__";


private int bulkDelete(String field, Object value, boolean notUuid) {
final Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
final Query query;
final String deleteSql = "DELETE FROM " + getEntityClass().getSimpleName() + " WHERE ";
if (value == null) {
query = session.createQuery(deleteSql + field + " IS NULL");

} else if (notUuid) {
query = session.createQuery(deleteSql + field + " = :" + field + " AND uuid != :" + EX_UUID)
.setParameter(field, value)
.setParameter(EX_UUID, value);
} else {
query = session.createQuery(deleteSql + field + " = :" + field)
.setParameter(field, value);
private int bulkDelete(@NonNull final String field, @Nullable final Object value, final boolean notUuid) {
if (value == null) return bulkDeleteWhere(field + " IS NULL");

final StringBuilder condition = new StringBuilder(field).append(" = :").append(field);
final HashMap<String, Object> params = new HashMap<>();
params.put(field, value);

if (notUuid) {
condition.append(" AND uuid != :").append(EX_UUID);
params.put(EX_UUID, value);
} }
final int count = query.executeUpdate();
session.setFlushMode(FlushMode.COMMIT);
session.flush();
return count;

return bulkDeleteWhere(condition.toString(), params);
}

public int bulkDeleteWhere(@NonNull final String whereClause) {
return bulkDeleteWhere(whereClause, null);
} }


public int bulkDeleteWhere(String whereClause) {
public int bulkDeleteWhere(@NonNull final String whereClause, @Nullable final Map<String, Object> parameters) {
final Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); final Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
final String deleteSql = "DELETE FROM " + getEntityClass().getSimpleName() + " WHERE " + whereClause; final String deleteSql = "DELETE FROM " + getEntityClass().getSimpleName() + " WHERE " + whereClause;
final Query query = session.createQuery(deleteSql); final Query query = session.createQuery(deleteSql);
if (!empty(parameters)) parameters.forEach(query::setParameter);
final int count = query.executeUpdate(); final int count = query.executeUpdate();
session.setFlushMode(FlushMode.COMMIT); session.setFlushMode(FlushMode.COMMIT);
session.flush(); session.flush();


Loading…
Cancel
Save