Commit b369724d by inrgihc

代码逻辑优化

parent 553f8934
......@@ -138,9 +138,10 @@ curl -k -sSL https://gitee.com/inrgihc/sqlrest/attach_files/2241027/download -o
(3) 物理机方式部署
- 步骤1:准备好一个MySQL5.7+/PostgreSQL11+的数据库
- 步骤1:准备好一个MySQL5.7+PostgreSQL11+的数据库
> 当使用MySQL数据库时,config.ini里的DB_TYPE配置mysql,并需要配置 MYSQLDB_ 前缀的参数
> 当使用MySQL数据库时,config.ini里的DB_TYPE配置mysql,并需要配置 MYSQLDB_ 前缀的参数;
>
> 当使用PostgreSQL数据库时,config.ini里的DB_TYPE配置postgres,并需要配置 PGDB_ 前缀的参数
- 步骤2:修改sqlrest-relase-x.x.x/conf/config.ini配置文件
......
......@@ -300,7 +300,7 @@ public class ApiAssignmentService {
.getExecutor(request.getEngine(), dataSource, dataSourceEntity.getType())
.execute(scripts, params, request.getNamingStrategy());
Object answer = results.size() > 1 ? results : (1 == results.size()) ? results.get(0) : null;
List<OutParam> types = JacksonUtils.parseFieldTypes(results);
List<OutParam> types = JacksonUtils.parseFiledTypesAndFillNullAsString(results);
String logs = Optional.ofNullable(SqlExecuteLogger.get())
.orElseGet(ArrayList::new).stream().map(ExecuteSqlRecord::getDisplayText)
.collect(Collectors.toList()).stream().collect(Collectors.joining("\n\n"));
......
......@@ -10,6 +10,7 @@
package com.gitee.sqlrest.core.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
......@@ -35,16 +36,17 @@ import org.apache.commons.collections4.CollectionUtils;
public final class JacksonUtils {
public static String toJsonStr(Object obj) {
return toJsonStr(obj, Collections.emptyMap());
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
}
public static String toJsonStr(Object obj, Map<DataTypeFormatEnum, String> formatMap) {
// https://www.jianshu.com/p/1368547350c6
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(createSerializeModule(formatMap));
ObjectMapper mapper = objectMapper.copy();
mapper.registerModule(createSerializeModule(formatMap));
try {
return objectMapper.writeValueAsString(obj);
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
......@@ -63,6 +65,23 @@ public final class JacksonUtils {
return module;
}
public static List<OutParam> parseFiledTypesAndFillNullAsString(Object obj) {
List<OutParam> types = parseFieldTypes(obj);
for (OutParam param : types) {
if (null == param.getType()) {
param.setType(ParamTypeEnum.STRING);
}
if (CollectionUtils.isNotEmpty(param.getChildren())) {
for (OutParam subParam : param.getChildren()) {
if (null == subParam.getType()) {
subParam.setType(ParamTypeEnum.STRING);
}
}
}
}
return types;
}
public static List<OutParam> parseFieldTypes(Object obj) {
List<OutParam> results = new LinkedList<>();
if (null == obj) {
......
......@@ -12,6 +12,7 @@ package com.gitee.sqlrest.manager.model;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.sqlrest.common.dto.BaseParam;
import com.gitee.sqlrest.common.dto.ItemParam;
......@@ -37,6 +38,7 @@ public class McpToolCallHandler {
private static final String FN_ID = "id";
private static final String FN_DESCRIPTION = "description";
private static final String FN_PROPERTIES = "properties";
private static final String FN_REQUIRED = "required";
private static final String FN_ITEMS = "items";
private static final String FV_ID = "urn:jsonschema:Operation";
private static final String FV_OBJECT = "object";
......@@ -61,31 +63,42 @@ public class McpToolCallHandler {
rootNode.put(FN_TYPE, FV_OBJECT);
rootNode.put(FN_ID, FV_ID);
ObjectNode propertiesNode = objectMapper.createObjectNode();
ArrayNode rootRequired = objectMapper.createArrayNode();
for (ItemParam param : params) {
ObjectNode node = objectMapper.createObjectNode();
if (param.getIsArray()) {
rootRequired.add(param.getName());
ObjectNode items = objectMapper.createObjectNode();
items.put(FN_TYPE, param.getType().getJsType());
node.put(FN_TYPE, FV_ARRAY);
node.put(FN_DESCRIPTION, param.getRemark());
node.set(FN_ITEMS, items);
} else {
if (param.getRequired()) {
rootRequired.add(param.getName());
}
node.put(FN_TYPE, param.getType().getJsType());
node.put(FN_DESCRIPTION, param.getRemark());
if (CollectionUtils.isNotEmpty(param.getChildren())) {
ArrayNode subRequired = objectMapper.createArrayNode();
ObjectNode properties = objectMapper.createObjectNode();
for (BaseParam subParam : param.getChildren()) {
if (subParam.getRequired()) {
subRequired.add(subParam.getName());
}
ObjectNode item = objectMapper.createObjectNode();
item.put(FN_TYPE, subParam.getType().getJsType());
item.put(FN_DESCRIPTION, subParam.getRemark());
properties.set(subParam.getName(), item);
}
node.set(FN_PROPERTIES, properties);
node.set(FN_REQUIRED, subRequired);
}
}
propertiesNode.set(param.getName(), node);
}
rootNode.set(FN_PROPERTIES, propertiesNode);
rootNode.set(FN_REQUIRED, rootRequired);
try {
String schema = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment