Query.of(entity)构建时传入fields

This commit is contained in:
Looly 2023-07-20 11:50:23 +08:00
parent e91d5d8e44
commit f424fc09c4
4 changed files with 41 additions and 16 deletions

View File

@ -14,6 +14,7 @@
* 【core 】 身份证工具类isValidCard18、isValidCard15入参null直接返回nullpr#1034@Gitee
* 【http 】 使用multiparty方式支持body参数issue#3158@Github
* 【core 】 ZipReader增加setMaxSizeDiff方法自定义或关闭ZipBombissue#3018@Github
* 【db 】 Query.of(entity)构建时传入fieldsissue#I7M5JU@Gitee
### 🐞Bug修复
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题issue#3162@Github

View File

@ -1,5 +1,6 @@
package cn.hutool.db.sql;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.db.DbRuntimeException;
@ -7,12 +8,13 @@ import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import java.util.Collection;
import java.util.Set;
/**
* 查询对象用于传递查询所需的字段值<br>
* 查询对象根据表名可以多个多个条件 {@link Condition} 构建查询对象完成查询<br>
* 如果想自定义返回结果则可在查询对象中自定义要查询的字段名分页{@link Page}信息来自定义结果
*
*
* @author Looly
*
*/
@ -34,13 +36,19 @@ public class Query {
* @since 5.5.3
*/
public static Query of(Entity where){
return new Query(SqlUtil.buildConditions(where), where.getTableName());
final Query query = new Query(SqlUtil.buildConditions(where), where.getTableName());
final Set<String> fieldNames = where.getFieldNames();
if(CollUtil.isNotEmpty(fieldNames)){
query.setFields(fieldNames);
}
return query;
}
// --------------------------------------------------------------- Constructor start
/**
* 构造
*
*
* @param tableNames 表名
*/
public Query(String... tableNames) {
@ -50,7 +58,7 @@ public class Query {
/**
* 构造
*
*
* @param where 条件语句
* @param tableNames 表名
*/
@ -60,7 +68,7 @@ public class Query {
/**
* 构造
*
*
* @param where 条件语句
* @param page 分页
* @param tableNames 表名
@ -71,7 +79,7 @@ public class Query {
/**
* 构造
*
*
* @param fields 字段
* @param tableNames 表名
* @param where 条件
@ -88,7 +96,7 @@ public class Query {
// --------------------------------------------------------------- Getters and Setters start
/**
* 获得查询的字段名列表
*
*
* @return 查询的字段名列表
*/
public Collection<String> getFields() {
@ -97,7 +105,7 @@ public class Query {
/**
* 设置查询的字段名列表
*
*
* @param fields 查询的字段名列表
* @return this
*/
@ -108,7 +116,7 @@ public class Query {
/**
* 设置查询的字段名列表
*
*
* @param fields 查询的字段名列表
* @return this
*/
@ -119,7 +127,7 @@ public class Query {
/**
* 获得表名数组
*
*
* @return 表名数组
*/
public String[] getTableNames() {
@ -128,7 +136,7 @@ public class Query {
/**
* 设置表名
*
*
* @param tableNames 表名
* @return this
*/
@ -139,7 +147,7 @@ public class Query {
/**
* 获得条件语句
*
*
* @return 条件语句
*/
public Condition[] getWhere() {
@ -148,7 +156,7 @@ public class Query {
/**
* 设置条件语句
*
*
* @param where 条件语句
* @return this
*/
@ -159,7 +167,7 @@ public class Query {
/**
* 获得分页对象无分页返回{@code null}
*
*
* @return 分页对象 or {@code null}
*/
public Page getPage() {
@ -168,7 +176,7 @@ public class Query {
/**
* 设置分页对象
*
*
* @param page 分页对象
* @return this
*/
@ -180,7 +188,7 @@ public class Query {
/**
* 获得第一个表名
*
*
* @return 表名
* @throws DbRuntimeException 没有表
*/

View File

@ -14,6 +14,10 @@ import java.util.Map;
*/
public class JfireELEngine implements ExpressionEngine {
static {
checkEngineExist(Expression.class);
}
/**
* 构造
*/
@ -24,4 +28,8 @@ public class JfireELEngine implements ExpressionEngine {
public Object eval(String expression, Map<String, Object> context) {
return Expression.parse(expression).calculate(context);
}
private static void checkEngineExist(Class<?> clazz){
// do nothing
}
}

View File

@ -17,6 +17,10 @@ import java.util.Map;
*/
public class RhinoEngine implements ExpressionEngine {
static {
checkEngineExist(Context.class);
}
@Override
public Object eval(String expression, Map<String, Object> context) {
final Context ctx = Context.enter();
@ -31,4 +35,8 @@ public class RhinoEngine implements ExpressionEngine {
Context.exit();
return result;
}
private static void checkEngineExist(Class<?> clazz){
// do nothing
}
}