refactor(oa): 重构数据库查询服务以优化数据处理和元信息包含逻辑
- 优化数据处理流程,优先使用查询结果的 key 生成 columns/fields - 改进元信息包含逻辑,通过列名映射获取表结构信息 - 调整数据结构组织方式,提高代码可读性和可维护性
This commit is contained in:
@@ -107,46 +107,56 @@ public class DatabaseQueryServiceImpl implements IDatabaseQueryService {
|
|||||||
List<Map<String, Object>> data = executeQuery(sql);
|
List<Map<String, Object>> data = executeQuery(sql);
|
||||||
|
|
||||||
DynamicDataVo result = new DynamicDataVo();
|
DynamicDataVo result = new DynamicDataVo();
|
||||||
|
|
||||||
// 设置渲染类型为表格
|
|
||||||
result.setRenderType("table");
|
result.setRenderType("table");
|
||||||
|
|
||||||
// 构建表格数据
|
|
||||||
DynamicDataVo.TableData tableData = new DynamicDataVo.TableData();
|
DynamicDataVo.TableData tableData = new DynamicDataVo.TableData();
|
||||||
|
tableData.setDatasource(data);
|
||||||
|
|
||||||
// 如果需要元信息,则生成字段元信息
|
// 动态生成 columns/fields,优先以 datasource 的 key 为主
|
||||||
if (includeMeta && tableName != null) {
|
|
||||||
List<TableColumnVo> columns = getTableColumns(tableName);
|
|
||||||
DynamicDataVo.Meta meta = new DynamicDataVo.Meta();
|
|
||||||
List<DynamicDataVo.Field> fields = new ArrayList<>();
|
List<DynamicDataVo.Field> fields = new ArrayList<>();
|
||||||
List<Map<String, Object>> columnsList = new ArrayList<>();
|
List<Map<String, Object>> columnsList = new ArrayList<>();
|
||||||
|
Map<String, TableColumnVo> columnMetaMap = new HashMap<>();
|
||||||
|
if (includeMeta && tableName != null) {
|
||||||
|
List<TableColumnVo> columns = getTableColumns(tableName);
|
||||||
|
for (TableColumnVo col : columns) {
|
||||||
|
columnMetaMap.put(col.getColumnName(), col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (TableColumnVo column : columns) {
|
if (data != null && !data.isEmpty()) {
|
||||||
|
Map<String, Object> firstRow = data.get(0);
|
||||||
|
for (String key : firstRow.keySet()) {
|
||||||
DynamicDataVo.Field field = new DynamicDataVo.Field();
|
DynamicDataVo.Field field = new DynamicDataVo.Field();
|
||||||
field.setFieldName(column.getColumnName());
|
field.setFieldName(key);
|
||||||
field.setLabel(column.getColumnComment() != null ? column.getColumnComment() : column.getColumnName());
|
TableColumnVo meta = columnMetaMap.get(key);
|
||||||
field.setType(getFieldType(column));
|
if (meta != null) {
|
||||||
field.setFormat(getFieldFormat(column));
|
field.setLabel(meta.getColumnComment() != null ? meta.getColumnComment() : key);
|
||||||
|
field.setType(getFieldType(meta));
|
||||||
|
field.setFormat(getFieldFormat(meta));
|
||||||
|
} else {
|
||||||
|
field.setLabel(key);
|
||||||
|
field.setType("string");
|
||||||
|
field.setFormat(null);
|
||||||
|
}
|
||||||
fields.add(field);
|
fields.add(field);
|
||||||
|
|
||||||
// 构建列信息
|
|
||||||
Map<String, Object> columnInfo = new HashMap<>();
|
Map<String, Object> columnInfo = new HashMap<>();
|
||||||
columnInfo.put("field", column.getColumnName());
|
columnInfo.put("field", key);
|
||||||
columnInfo.put("title", column.getColumnComment() != null ? column.getColumnComment() : column.getColumnName());
|
columnInfo.put("title", field.getLabel());
|
||||||
columnInfo.put("type", getFieldType(column));
|
columnInfo.put("type", field.getType());
|
||||||
columnInfo.put("format", getFieldFormat(column));
|
columnInfo.put("format", field.getFormat());
|
||||||
columnsList.add(columnInfo);
|
columnsList.add(columnInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
meta.setFields(fields);
|
|
||||||
result.setMeta(meta);
|
|
||||||
tableData.setColumns(columnsList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置数据源
|
// meta 只做补充
|
||||||
tableData.setDatasource(data);
|
if (includeMeta) {
|
||||||
|
DynamicDataVo.Meta meta = new DynamicDataVo.Meta();
|
||||||
|
meta.setFields(fields);
|
||||||
|
result.setMeta(meta);
|
||||||
|
}
|
||||||
|
tableData.setColumns(columnsList);
|
||||||
result.setResponse(tableData);
|
result.setResponse(tableData);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user