Pārlūkot izejas kodu

`db`模块新增排除方法

mxd 3 gadi atpakaļ
vecāks
revīzija
fbe5bd237f

+ 12 - 0
magic-api/src/main/java/org/ssssssss/magicapi/modules/BoundSql.java

@@ -10,6 +10,7 @@ import org.ssssssss.script.parsing.ast.literal.BooleanLiteral;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Supplier;
 import java.util.regex.Pattern;
@@ -38,6 +39,8 @@ public class BoundSql {
 
 	private long ttl;
 
+	private Set<String> excludeColumns;
+
 	public BoundSql(String sql, List<Object> parameters, SQLModule sqlModule) {
 		this.sql = sql;
 		this.parameters = parameters;
@@ -98,9 +101,18 @@ public class BoundSql {
 		boundSql.cacheName = this.cacheName;
 		boundSql.sqlCache = this.sqlCache;
 		boundSql.sql = newSql;
+		boundSql.excludeColumns = this.excludeColumns;
 		return boundSql;
 	}
 
+	public Set<String> getExcludeColumns() {
+		return excludeColumns;
+	}
+
+	public void setExcludeColumns(Set<String> excludeColumns) {
+		this.excludeColumns = excludeColumns;
+	}
+
 	/**
 	 * 添加SQL参数
 	 */

+ 15 - 3
magic-api/src/main/java/org/ssssssss/magicapi/modules/SQLModule.java

@@ -292,7 +292,15 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	@UnableCall
 	public List<Map<String, Object>> select(BoundSql boundSql) {
 		assertDatasourceNotNull();
-		return boundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters()));
+		return boundSql.getCacheValue(this.sqlInterceptors, () -> queryForList(boundSql));
+	}
+
+	private List<Map<String, Object>> queryForList(BoundSql boundSql){
+		List<Map<String, Object>> list = dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters());
+		if(boundSql.getExcludeColumns() != null){
+			list.forEach(row -> boundSql.getExcludeColumns().forEach(row::remove));
+		}
+		return list;
 	}
 
 	private void assertDatasourceNotNull(){
@@ -407,7 +415,7 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 		List<Map<String, Object>> list = null;
 		if (count > 0) {
 			BoundSql pageBoundSql = buildPageBoundSql(dialect, boundSql, page.getOffset(), page.getLimit());
-			list = pageBoundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(pageBoundSql.getSql(), this.columnMapRowMapper, pageBoundSql.getParameters()));
+			list = pageBoundSql.getCacheValue(this.sqlInterceptors, () -> queryForList(pageBoundSql));
 		}
 		RequestEntity requestEntity = RequestContext.getRequestEntity();
 		return resultProvider.buildPageResult(requestEntity, page, count, list);
@@ -441,7 +449,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 		return boundSql.getCacheValue(this.sqlInterceptors, () -> {
 			Dialect dialect = dataSourceNode.getDialect(dialectAdapter);
 			BoundSql pageBoundSql = buildPageBoundSql(dialect, boundSql, 0, 1);
-			return dataSourceNode.getJdbcTemplate().query(pageBoundSql.getSql(), new SingleRowResultSetExtractor<>(this.columnMapRowMapper), pageBoundSql.getParameters());
+			Map<String, Object> row = dataSourceNode.getJdbcTemplate().query(pageBoundSql.getSql(), new SingleRowResultSetExtractor<>(this.columnMapRowMapper), pageBoundSql.getParameters());
+			if(row != null && boundSql.getExcludeColumns() != null){
+				boundSql.getExcludeColumns().forEach(row::remove);
+			}
+			return row;
 		});
 	}
 

+ 55 - 24
magic-api/src/main/java/org/ssssssss/magicapi/modules/table/NamedTable.java

@@ -29,6 +29,8 @@ public class NamedTable {
 
 	List<String> orders = new ArrayList<>();
 
+	Set<String> excludeColumns = new HashSet<>();
+
 	Function<String, String> rowMapColumnMapper;
 
 	Object defaultPrimaryValue;
@@ -92,6 +94,30 @@ public class NamedTable {
 		return this;
 	}
 
+	@Comment("设置要排除的列")
+	public NamedTable exclude(String column){
+		if(column != null){
+			excludeColumns.add(column);
+		}
+		return this;
+	}
+
+	@Comment("设置要排除的列")
+	public NamedTable excludes(String ... columns){
+		if(columns != null){
+			excludeColumns.addAll(Arrays.asList(columns));
+		}
+		return this;
+	}
+
+	@Comment("设置要排除的列")
+	public NamedTable excludes(List<String> columns){
+		if(columns != null){
+			excludeColumns.addAll(columns);
+		}
+		return this;
+	}
+
 	@Comment("设置查询的列,如`columns(['a','b','c'])`")
 	public NamedTable columns(Collection<String> columns) {
 		if (columns != null) {
@@ -132,10 +158,15 @@ public class NamedTable {
 
 	private Collection<Map.Entry<String, Object>> filterNotBlanks() {
 		if(this.withBlank){
-			return this.columns.entrySet();
+			return this.columns.entrySet()
+					.stream()
+					.filter(it -> !excludeColumns.contains(it.getKey()))
+					.collect(Collectors.toList());
 		}
-		return this.columns.entrySet().stream()
+		return this.columns.entrySet()
+				.stream()
 				.filter(it -> StringUtils.isNotBlank(Objects.toString(it.getValue(), "")))
+				.filter(it -> !excludeColumns.contains(it.getKey()))
 				.collect(Collectors.toList());
 	}
 
@@ -242,12 +273,30 @@ public class NamedTable {
 	private BoundSql buildSelect() {
 		StringBuilder builder = new StringBuilder();
 		builder.append("select ");
-		if (this.fields.isEmpty()) {
+		List<String> fields = this.fields.stream()
+				.filter(it -> !excludeColumns.contains(it))
+				.collect(Collectors.toList());
+		if (fields.isEmpty()) {
 			builder.append("*");
 		} else {
-			builder.append(StringUtils.join(this.fields, ","));
+			builder.append(StringUtils.join(fields, ","));
 		}
 		builder.append(" from ").append(tableName);
+		List<Object> params = buildWhere(builder);
+		if (!orders.isEmpty()) {
+			builder.append(" order by ");
+			builder.append(String.join(",", orders));
+		}
+		if (!groups.isEmpty()) {
+			builder.append(" group by ");
+			builder.append(String.join(",", groups));
+		}
+		BoundSql boundSql = new BoundSql(builder.toString(), params, sqlModule);
+		boundSql.setExcludeColumns(excludeColumns);
+		return boundSql;
+	}
+
+	private List<Object> buildWhere(StringBuilder builder) {
 		List<Object> params = new ArrayList<>();
 		if (!where.isEmpty()) {
 			where.and();
@@ -259,15 +308,7 @@ public class NamedTable {
 			builder.append(where.getSql());
 			params.addAll(where.getParams());
 		}
-		if (!orders.isEmpty()) {
-			builder.append(" order by ");
-			builder.append(String.join(",", orders));
-		}
-		if (!groups.isEmpty()) {
-			builder.append(" group by ");
-			builder.append(String.join(",", groups));
-		}
-		return new BoundSql(builder.toString(), params, sqlModule);
+		return params;
 	}
 
 	@Comment("执行分页查询")
@@ -328,17 +369,7 @@ public class NamedTable {
 	public int count(){
 		StringBuilder builder = new StringBuilder();
 		builder.append("select count(1) from ").append(tableName);
-		List<Object> params = new ArrayList<>();
-		if (!where.isEmpty()) {
-			where.and();
-			where.ne(useLogic, logicDeleteColumn, logicDeleteValue);
-			builder.append(where.getSql());
-			params.addAll(where.getParams());
-		}else if(useLogic){
-			where.ne(logicDeleteColumn, logicDeleteValue);
-			builder.append(where.getSql());
-			params.addAll(where.getParams());
-		}
+		List<Object> params = buildWhere(builder);
 		return sqlModule.selectInt(new BoundSql(builder.toString(), params, sqlModule));
 	}