improve SQLProvider selective filter

This commit is contained in:
Timi
2025-12-01 11:00:03 +08:00
parent 113af72208
commit 23598242f0

View File

@ -153,16 +153,8 @@ public class SQLProvider {
public String selectAllByExample(Object entity) { public String selectAllByExample(Object entity) {
EntityMeta meta = getEntityMeta(entity.getClass()); EntityMeta meta = getEntityMeta(entity.getClass());
String conditionClause = meta.fieldColumnList.stream() String conditionClause = meta.fieldColumnList.stream()
.filter(fc -> { .filter(fc -> fc.isNotNull(entity))
try { .map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
return Ref.getFieldValue(entity, fc.field, Object.class) != null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.map(fc -> {
return "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName);
})
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" AND "));
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
@ -187,14 +179,12 @@ public class SQLProvider {
TimiException.required(meta.idFieldColumn, "not found id field in %s".formatted(meta.entityClass)); TimiException.required(meta.idFieldColumn, "not found id field in %s".formatted(meta.entityClass));
TimiException.required(meta.canUpdate, "not allow update for %s".formatted(meta.entityClass)); TimiException.required(meta.canUpdate, "not allow update for %s".formatted(meta.entityClass));
if (entity instanceof Updatable updatable) {
updatable.setUpdatedAt(Time.now());
}
String setClause = meta.fieldColumnList.stream() String setClause = meta.fieldColumnList.stream()
.filter(fc -> !fc.isId) .filter(FieldColumn::isNotId)
.map(fc -> { .map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
if (entity instanceof Updatable updatableEntity) {
updatableEntity.setUpdatedAt(Time.now());
}
return "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName);
})
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName); return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName);
} }
@ -213,26 +203,16 @@ public class SQLProvider {
if (entity instanceof Creatable creatable) { if (entity instanceof Creatable creatable) {
creatable.setCreatedAt(null); creatable.setCreatedAt(null);
} }
if (entity instanceof Updatable updatable) {
updatable.setUpdatedAt(Time.now());
}
if (entity instanceof Deletable deletable) { if (entity instanceof Deletable deletable) {
deletable.setDeletedAt(null); deletable.setDeletedAt(null);
} }
String setClause = meta.fieldColumnList.stream() String setClause = meta.fieldColumnList.stream()
.filter(fc -> { .filter(FieldColumn::isNotId)
if (fc.isId) { .filter(fc -> fc.isNotNull(entity))
return false; .map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
}
try {
return Ref.getFieldValue(entity, fc.field, Object.class) != null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.map(fc -> {
if (entity instanceof Updatable updatableEntity) {
updatableEntity.setUpdatedAt(Time.now());
}
return "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName);
})
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName); return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName);
} }
@ -459,6 +439,18 @@ public class SQLProvider {
} }
} }
public boolean isNull(Object entity) {
try {
return Ref.getFieldValue(entity, field, Object.class) == null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public boolean isNotNull(Object entity) {
return !isNull(entity);
}
public Field getField() { public Field getField() {
return field; return field;
} }
@ -475,6 +467,10 @@ public class SQLProvider {
return isId; return isId;
} }
public boolean isNotId() {
return !isId();
}
public boolean isAutoUUID() { public boolean isAutoUUID() {
return isAutoUUID; return isAutoUUID;
} }