add example Logic
This commit is contained in:
@@ -12,6 +12,7 @@ import com.imyeyu.spring.annotation.table.Id;
|
||||
import com.imyeyu.spring.annotation.table.PageIgnore;
|
||||
import com.imyeyu.spring.annotation.table.Table;
|
||||
import com.imyeyu.spring.annotation.table.Transient;
|
||||
import com.imyeyu.spring.bean.Logic;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
import com.imyeyu.spring.entity.Creatable;
|
||||
import com.imyeyu.spring.entity.Deletable;
|
||||
@@ -143,12 +144,11 @@ public abstract class BaseSQLProvider {
|
||||
String conditionClause = metaExample.fieldColumnList.stream()
|
||||
.filter(fc -> fc.isNotEmpty(obj))
|
||||
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
.collect(Collectors.joining(" %s ".formatted(page.getEqualsLogic())));
|
||||
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||
sql.append(" AND ").append(conditionClause);
|
||||
}
|
||||
}
|
||||
|
||||
// 模糊查询
|
||||
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
||||
Object obj = page.getLikesExample();
|
||||
@@ -156,7 +156,7 @@ public abstract class BaseSQLProvider {
|
||||
String conditionClause = metaExample.fieldColumnList.stream()
|
||||
.filter(fc -> fc.isNotEmpty(obj))
|
||||
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
|
||||
.collect(Collectors.joining(" OR "));
|
||||
.collect(Collectors.joining(" %s ".formatted(page.getLikesLogic())));
|
||||
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||
sql.append(" AND (").append(conditionClause).append(')');
|
||||
}
|
||||
@@ -197,13 +197,14 @@ public abstract class BaseSQLProvider {
|
||||
* @param meta 实体元数据
|
||||
* @param entity 示例实体
|
||||
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
|
||||
* @param logic 条件连接逻辑
|
||||
* @return 条件子句
|
||||
*/
|
||||
protected String buildExampleConditions(EntityMeta meta, Object entity, String paramPrefix) {
|
||||
protected String buildExampleConditions(EntityMeta meta, Object entity, String paramPrefix, Logic logic) {
|
||||
return meta.fieldColumnList.stream()
|
||||
.filter(fc -> fc.isNotEmpty(entity))
|
||||
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
.collect(Collectors.joining(" %s ".formatted(logic)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,10 +271,11 @@ public abstract class BaseSQLProvider {
|
||||
* @param tableName 表名
|
||||
* @param entity 示例实体
|
||||
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
protected String buildSelectAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) {
|
||||
String conditionClause = buildExampleConditions(meta, entity, paramPrefix);
|
||||
protected String buildSelectAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
|
||||
String conditionClause = buildExampleConditions(meta, entity, paramPrefix, logic);
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, tableName, conditionClause));
|
||||
@@ -372,9 +374,10 @@ public abstract class BaseSQLProvider {
|
||||
* @param tableName 表名
|
||||
* @param entity 示例实体
|
||||
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
protected String buildDeleteAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) {
|
||||
protected String buildDeleteAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
|
||||
TimiException.required(meta.canDelete, "not allow delete for %s".formatted(meta.entityClass));
|
||||
|
||||
FieldColumn deleteColumn = meta.getFieldColumnList().stream()
|
||||
@@ -389,7 +392,7 @@ public abstract class BaseSQLProvider {
|
||||
.filter(FieldColumn::isNotId)
|
||||
.filter(fc -> fc.isNotEmpty(entity))
|
||||
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
.collect(Collectors.joining(" %s ".formatted(logic)));
|
||||
StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(tableName, deleteColumn.getColumnName()));
|
||||
sql.append("'").append(switch (deleteColumn.deleteColumnType) {
|
||||
case UNIX -> Time.now();
|
||||
@@ -415,21 +418,22 @@ public abstract class BaseSQLProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建批量逻辑删除 SQL
|
||||
* 构建批量物理删除 SQL
|
||||
*
|
||||
* @param meta 实体元数据
|
||||
* @param tableName 表名
|
||||
* @param entity 示例实体
|
||||
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
protected String buildDestroyAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) {
|
||||
protected String buildDestroyAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
|
||||
TimiException.required(meta.canDestroy, "not allow destroy for %s".formatted(meta.entityClass));
|
||||
String destroyClause = meta.fieldColumnList.stream()
|
||||
.filter(FieldColumn::isNotId)
|
||||
.filter(fc -> fc.isNotEmpty(entity))
|
||||
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
.collect(Collectors.joining(" %s ".formatted(logic)));
|
||||
return "DELETE FROM `%s` WHERE %s".formatted(tableName, destroyClause);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user