Browse Source

fix:大模型sql执行接口

zhongwei
haungxing 5 months ago
parent
commit
2f731cafa0
  1. 19
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/controller/DataSourceExecuteController.java
  2. 58
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/DataSourceService.java
  3. 14
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/IDataSourceService.java
  4. 32
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/vo/SqlVO.java
  5. 28
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/vo/TableAuthVO.java
  6. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInteractiveServiceImpl.java
  7. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AuthDataVO.java
  8. 54
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/controller/TableColumnController.java
  9. 61
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/controller/TableInfoController.java
  10. 17
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/DataRecordEntity.java
  11. 56
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/TableInfoEntity.java
  12. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/mapper/TableInfoMapper.java
  13. 38
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java
  14. 21
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableInfoService.java

19
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/controller/DataSourceExecuteController.java

@ -1,11 +1,16 @@
package com.hnac.hzims.bigmodel.datasource.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.datasource.service.DataSourceService;
import com.hnac.hzims.bigmodel.datasource.vo.SqlVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* @Author: huangxing
@ -18,9 +23,11 @@ public class DataSourceExecuteController {
private final DataSourceService dataSourceService;
@GetMapping("/executeQuery")
public R executeQuery(String sql,String dataSourceName) {
return R.data(dataSourceService.queryListOnSpecificDataSource(sql,dataSourceName));
@PostMapping("/executeQuery")
@ApiOperation("执行大模型sql")
@ApiOperationSupport(order = 1)
public R<List<Map<String, Object>>> executeQuery(@RequestBody @Valid SqlVO sqlVO) {
return R.data(dataSourceService.queryListOnSpecificDataSource(sqlVO));
}
}

58
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/DataSourceService.java

@ -1,14 +1,26 @@
package com.hnac.hzims.bigmodel.datasource.service;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.datasource.vo.SqlVO;
import com.hnac.hzims.bigmodel.datasource.vo.TableAuthVO;
import com.hnac.hzims.common.service.UserAuthDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @Author: huangxing
@ -20,12 +32,50 @@ import java.util.regex.Pattern;
public class DataSourceService {
private final JdbcTemplate jdbcTemplate;
private final UserAuthDataService userAuthDataService;
private static final Pattern UPDATE_PATTERN = Pattern.compile("^UPDATE\\s", Pattern.CASE_INSENSITIVE);
private static final Pattern DELETE_PATTERN = Pattern.compile("^DELETE\\s", Pattern.CASE_INSENSITIVE);
public List<Map<String, Object>> queryListOnSpecificDataSource(SqlVO sqlVO) {
// 过滤更新、删除语句
Assert.isTrue(!DataSourceService.isUpdateOrDelete(sqlVO.getSql()),() -> {
throw new ServiceException("执行sql语句包含更新/删除操作,执行失败!");
});
String sql = sqlVO.getSql();
String userAuthDataSQL = userAuthDataService.getUserAuthDataSQL(Long.parseLong(sqlVO.getUserId()));
List<Map<String,String>> tempViewList = Lists.newArrayList();
try {
for (TableAuthVO tableAuthVO : sqlVO.getTableAuthVOList()) {
// 创建视图语句
String viewName = "V_TEMP_" + UUID.randomUUID().toString().replace("-", "");
String createView = "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableAuthVO.getTableName() + " where " + userAuthDataSQL;
this.updateOnSpecificDataSource(createView,tableAuthVO.getDatasourceName());
Map<String,String> viewMap = new HashMap(2);
viewMap.put("datasource",tableAuthVO.getDatasourceName());
viewMap.put("viewName",viewName);
tempViewList.add(viewMap);
sql = sql.replace(tableAuthVO.getTableName(),viewName);
}
log.info("执行sql:{}",sql);
return this.queryListOnSpecificDataSource(sql, sqlVO.getTableAuthVOList().get(0).getDatasourceName());
}
catch(Exception e) {
log.error("An Error occurred!",e);
throw new ServiceException("sql执行失败!");
}
finally {
if(CollectionUtil.isNotEmpty(tempViewList)) {
tempViewList.forEach(viewMap -> {
this.updateOnSpecificDataSource("DROP VIEW IF EXISTS `" + viewMap.get("viewName")+"`;",viewMap.get("datasource"));
});
}
}
}
/**
* 指定
* sql
* @param sql
* @param dataSourceName
* @return
@ -52,11 +102,15 @@ public class DataSourceService {
}
}
/**
* 过滤更新删除语法的sql脚本
* @param sql 待执行sql脚本
* @return 执行结果
*/
public static boolean isUpdateOrDelete(String sql) {
if (sql == null) {
return false;
}
return UPDATE_PATTERN.matcher(sql).find() || DELETE_PATTERN.matcher(sql).find();
}
}

14
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/IDataSourceService.java

@ -1,14 +0,0 @@
package com.hnac.hzims.bigmodel.datasource.service;
import java.util.List;
import java.util.Map;
/**
* @Author: huangxing
* @Date: 2024/06/28 14:22
*/
public interface IDataSourceService {
List<Map<String,Object>> queryList(String sql);
}

32
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/vo/SqlVO.java

@ -0,0 +1,32 @@
package com.hnac.hzims.bigmodel.datasource.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.List;
/**
* @Author: huangxing
* @Date: 2024/07/05 08:19
*/
@ApiModel(value = "",description = "")
@Data
@EqualsAndHashCode
public class SqlVO implements Serializable {
@ApiModelProperty("执行sql")
@NotBlank
private String sql;
@ApiModelProperty("执行人")
@NotBlank
private String userId;
@ApiModelProperty("执行sql设计表信息")
private List<TableAuthVO> tableAuthVOList;
}

28
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/vo/TableAuthVO.java

@ -0,0 +1,28 @@
package com.hnac.hzims.bigmodel.datasource.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* @Author: huangxing
* @Date: 2024/07/04 17:29
*/
@ApiModel(value = "表鉴权对象",description = "表鉴权对象")
@Data
@EqualsAndHashCode
public class TableAuthVO implements Serializable {
@ApiModelProperty("鉴权类型,目前不为空即为平台数据权限")
private String auth;
@ApiModelProperty("数据源名称")
private String datasourceName;
@ApiModelProperty("数据表名称")
private String tableName;
}

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInteractiveServiceImpl.java

@ -56,7 +56,7 @@ public class HznlmInteractiveServiceImpl implements IHznlmInteractiveService {
public List<AuthDataVO> getStationAuthData(String userId) {
List<DeptStationDTO> deptStationDTOs = authenticationService.getStationPermissionsById(userId);
return deptStationDTOs.stream().map(this::convertStationAuthData).filter(d -> Func.isNotBlank(d.getId())).collect(Collectors.toList());
return deptStationDTOs.stream().map(this::convertStationAuthData).filter(d -> Func.isNotBlank(d.getItemId())).collect(Collectors.toList());
}
private AuthDataVO convertStationAuthData(DeptStationDTO req) {

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AuthDataVO.java

@ -19,9 +19,9 @@ import java.io.Serializable;
public class AuthDataVO implements Serializable {
@ApiModelProperty("数据ID")
private String id;
private String itemId;
@ApiModelProperty("数据名称")
private String name;
private String itemName;
}

54
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/controller/TableColumnController.java

@ -0,0 +1,54 @@
package com.hnac.hzims.bigmodel.maintenance.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.maintenance.service.TableColumnService;
import com.hnac.hzinfo.log.annotation.Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* @Author: huangxing
* @Date: 2024/07/04 09:28
*/
@RestController
@RequestMapping("/table/info")
@Api(value = "数据库表、列信息查询管理",tags = "数据库表、列信息查询管理")
@Business(module = BigModelConstants.APP_NAME,value = "数据库表、列信息查询管理",ignore = false)
@AllArgsConstructor
public class TableColumnController {
private final TableColumnService tableColumnService;
@ApiOperation("获取数据库")
@ApiOperationSupport(order = 1)
@GetMapping("/getDatabase")
public R<List<Map<String,Object>>> getDatabase() {
return R.data(tableColumnService.getDatabase());
}
@ApiOperation("获取数据库表列表")
@ApiOperationSupport(order = 2)
@GetMapping("/getTables")
public R<List<Map<String,Object>>> getTables(@RequestParam @ApiParam(value = "数据库名") String database) {
return R.data(tableColumnService.getTables(database));
}
@ApiOperation("获取数据库表列列表")
@ApiOperationSupport(order = 3)
@GetMapping("/getColumns")
public R<List<Map<String,Object>>> getColumns(@RequestParam @ApiParam(value = "数据库名") String database,
@RequestParam @ApiParam(value = "数据库表名") String tableName) {
return R.data(tableColumnService.getColumns(database,tableName));
}
}

61
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/controller/TableInfoController.java

@ -0,0 +1,61 @@
package com.hnac.hzims.bigmodel.maintenance.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.maintenance.entity.TableInfoEntity;
import com.hnac.hzims.bigmodel.maintenance.service.TableInfoService;
import com.hnac.hzinfo.log.annotation.Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @Author: huangxing
* @Date: 2024/07/04 15:26
*/
@RestController
@RequestMapping("/tableInfo")
@Api(value = "数据库信息管理",tags = "数据库信息管理")
@AllArgsConstructor
@Business(module = BigModelConstants.APP_NAME,value = "数据库信息管理",ignore = false)
public class TableInfoController {
private final TableInfoService tableInfoService;
@PostMapping("/save")
@ApiOperation("保存数据库信息")
@ApiOperationSupport(order = 1)
public R save(@RequestBody @Valid TableInfoEntity req) {
return R.data(tableInfoService.save(req));
}
@PostMapping("/update")
@ApiOperation("编辑数据库信息")
@ApiOperationSupport(order = 2)
public R update(@RequestBody TableInfoEntity req) {
return R.data(tableInfoService.updateById(req));
}
@GetMapping("/listPage")
@ApiOperation("数据库信息列表查询")
@ApiOperationSupport(order = 3)
public R<IPage<TableInfoEntity>> listPage(Query query, TableInfoEntity req) {
return R.data(tableInfoService.page(Condition.getPage(query),Condition.getQueryWrapper(req).lambda()));
}
@DeleteMapping("/removeByIds")
@ApiOperation("删除数据库信息")
@ApiOperationSupport(order = 4)
public R removeByIds(@RequestParam @ApiParam("id,按逗号分隔") String ids) {
return R.status(tableInfoService.removeByIds(Func.toLongList(",",ids)));
}
}

17
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/DataRecordEntity.java

@ -0,0 +1,17 @@
package com.hnac.hzims.bigmodel.maintenance.entity;
import lombok.Data;
import org.springblade.core.tenant.mp.TenantEntity;
/**
* @Author: huangxing
* @Date: 2024/07/04 15:45
*/
@Data
public class DataRecordEntity extends TenantEntity {
private String tableId;
}

56
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/TableInfoEntity.java

@ -0,0 +1,56 @@
package com.hnac.hzims.bigmodel.maintenance.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tenant.mp.TenantEntity;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* @Author: huangxing
* @Date: 2024/07/04 14:59
*/
@Data
@TableName("mysql_table_info")
@EqualsAndHashCode
@ApiModel(value = "数据库信息",description = "数据库信息")
public class TableInfoEntity extends TenantEntity implements Serializable {
@TableField("db_source")
@ApiModelProperty("数据源")
@NotBlank
@QueryField(condition = SqlCondition.LIKE)
private String datasource;
@TableField("db_name")
@ApiModelProperty("数据库")
@NotBlank
@QueryField(condition = SqlCondition.EQUAL)
private String database;
@ApiModelProperty("数据库表名")
@NotBlank
@QueryField(condition = SqlCondition.EQUAL)
private String tableName;
@ApiModelProperty("数据库表描述信息")
@NotBlank
@QueryField(condition = SqlCondition.LIKE)
private String tableDesc;
@ApiModelProperty("数据库表列信息")
@NotBlank
private String propDesc;
@ApiModelProperty("举例")
@NotBlank
private String example;
}

12
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/mapper/TableInfoMapper.java

@ -0,0 +1,12 @@
package com.hnac.hzims.bigmodel.maintenance.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hnac.hzims.bigmodel.maintenance.entity.TableInfoEntity;
/**
* @Author: huangxing
* @Date: 2024/07/04 15:25
*/
public interface TableInfoMapper extends BaseMapper<TableInfoEntity> {
}

38
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java

@ -0,0 +1,38 @@
package com.hnac.hzims.bigmodel.maintenance.service;
import com.hnac.hzims.bigmodel.datasource.service.DataSourceService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @Author: huangxing
* @Date: 2024/07/04 09:29
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class TableColumnService {
private final DataSourceService dataSourceService;
public List<Map<String,Object>> getDatabase() {
String sql = "select distinct `table_schema` from `tables` where `table_schema` <> 'information_schema';";
return dataSourceService.queryListOnSpecificDataSource(sql,"information");
}
public List<Map<String, Object>> getTables(String database) {
String sql = "select distinct `table_name` from `tables` where `table_schema` = '" + database + "';";
return dataSourceService.queryListOnSpecificDataSource(sql,"information");
}
public List<Map<String, Object>> getColumns(String database,String tableName) {
String sql = "select `COLUMN_NAME` from `COLUMNS` where `TABLE_SCHEMA` = '" + database + "' and `TABLE_NAME` = '" + tableName + "';";
return dataSourceService.queryListOnSpecificDataSource(sql,"information");
}
}

21
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableInfoService.java

@ -0,0 +1,21 @@
package com.hnac.hzims.bigmodel.maintenance.service;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.hnac.hzims.bigmodel.maintenance.entity.TableInfoEntity;
import com.hnac.hzims.bigmodel.maintenance.mapper.TableInfoMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
/**
* @Author: huangxing
* @Date: 2024/07/04 15:23
*/
@Service
@AllArgsConstructor
@Slf4j
@DS("hznlm")
public class TableInfoService extends BaseServiceImpl<TableInfoMapper, TableInfoEntity> {
}
Loading…
Cancel
Save