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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.spring.bean.Logic;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -89,10 +90,11 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
return selectAllByExample(context, tableName, entity) + BaseMapper.LIMIT_1;
|
||||
public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
return selectAllByExample(context, tableName, entity, logic) + BaseMapper.LIMIT_1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,11 +103,12 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildSelectAllByExampleSQL(meta, tableName, entity, "entity.");
|
||||
return buildSelectAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,11 +156,12 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity.");
|
||||
return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,15 +178,16 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据示例批量逻辑删除
|
||||
* 根据示例批量物理删除
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity.");
|
||||
return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.spring.bean.Logic;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
@@ -28,17 +30,17 @@ public class RawSQLProvider extends SQLProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String selectByExample(Object entity) {
|
||||
return selectAllByExample(entity) + BaseMapper.LIMIT_1;
|
||||
public String selectByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
return selectAllByExample(entity, logic) + BaseMapper.LIMIT_1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String selectAllByExample(Object entity) {
|
||||
public String selectAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
String conditionClause = meta.fieldColumnList.stream()
|
||||
.filter(fc -> fc.isNotEmpty(entity))
|
||||
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
.map(fc -> "`%s` = #{entity.%s}".formatted(fc.columnName, fc.fieldName))
|
||||
.collect(Collectors.joining(" %s ".formatted(logic)));
|
||||
return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.spring.bean.Logic;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -89,21 +90,23 @@ public class SQLProvider extends BaseSQLProvider {
|
||||
* 根据实体非空字段使用等号查询
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectByExample(Object entity) {
|
||||
return selectAllByExample(entity) + BaseMapper.LIMIT_1;
|
||||
public String selectByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
return selectAllByExample(entity, logic) + BaseMapper.LIMIT_1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体非空字段使用等号查询
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectAllByExample(Object entity) {
|
||||
public String selectAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
return buildSelectAllByExampleSQL(meta, meta.table, entity, "");
|
||||
return buildSelectAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,11 +147,12 @@ public class SQLProvider extends BaseSQLProvider {
|
||||
* 根据示例批量逻辑删除
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String deleteAllByExample(Object entity) {
|
||||
public String deleteAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
return buildDeleteAllByExampleSQL(meta, meta.table, entity, "");
|
||||
return buildDeleteAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,10 +171,11 @@ public class SQLProvider extends BaseSQLProvider {
|
||||
* 根据示例批量销毁(物理删除)
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param logic 条件连接逻辑
|
||||
* @return SQL
|
||||
*/
|
||||
public String destroyAllByExample(Object entity) {
|
||||
public String destroyAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
return buildDestroyAllByExampleSQL(meta, meta.table, entity, "");
|
||||
return buildDestroyAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user