Search in sources :

Example 1 with GenDsInfo

use of com.xhuicloud.common.datasource.entity.GenDsInfo in project XHuiCloud by sindaZeng.

the class GenCodeController method generator.

/**
 * 代码生成
 * 数据库名 需要生成的表名。必须严格传入此库拥有的表
 */
@SneakyThrows
@PostMapping
@PreAuthorize("@authorize.hasPermission('sys_download_code')")
public void generator(@RequestParam Integer dataSourceId, @Valid @RequestBody GenCodeDto genCodeDto, HttpServletResponse response) {
    GenDsInfo genDsInfo = genDsInfoService.getById(dataSourceId);
    JdbcHandle jdbcHandle = handle.get(genDsInfo.getType());
    if (ObjectUtil.isNotNull(jdbcHandle) && jdbcHandle.test(genDsInfo)) {
        DynamicDataSourceContextHolder.push(genDsInfo.getName());
        byte[] data = jdbcHandle.genCode(genCodeDto);
        response.reset();
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.zip", genCodeDto.getModuleName()));
        response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
        DynamicDataSourceContextHolder.clear();
    }
}
Also used : GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) JdbcHandle(com.xhuicloud.generator.handle.JdbcHandle) SneakyThrows(lombok.SneakyThrows) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with GenDsInfo

use of com.xhuicloud.common.datasource.entity.GenDsInfo in project XHuiCloud by sindaZeng.

the class XHuiDynamicDataSourceProvider method executeStmt.

/**
 * 执行语句获得数据源参数
 * @param statement 语句
 * @return 数据源参数
 * @throws SQLException sql异常
 */
@SneakyThrows
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
    ResultSet rs = statement.executeQuery(properties.getQueryDsSql());
    Map<String, DataSourceProperty> map = new HashMap<>(8);
    List<GenDsInfo> sysTenantDs = populate(rs, GenDsInfo.class);
    for (GenDsInfo ds : sysTenantDs) {
        String url;
        DsJdbcUrlEnum dsJdbcUrlEnum = DsJdbcUrlEnum.get(ds.getType());
        if (DsJdbcUrlEnum.MSSQL == dsJdbcUrlEnum) {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        } else {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        }
        DataSourceProperty property = new DataSourceProperty();
        property.setUsername(ds.getUsername());
        property.setPassword(ds.getPassword());
        property.setUrl(url);
        map.put(ds.getName(), property);
    }
    // 添加默认主数据源
    DataSourceProperty property = new DataSourceProperty();
    property.setUsername(properties.getUsername());
    property.setPassword(properties.getPassword());
    property.setUrl(properties.getUrl());
    map.put("master", property);
    return map;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) DsJdbcUrlEnum(com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum) SneakyThrows(lombok.SneakyThrows)

Example 3 with GenDsInfo

use of com.xhuicloud.common.datasource.entity.GenDsInfo in project XHuiCloud by sindaZeng.

the class GenDatasourceInfoController method getTableInfoById.

/**
 * 分页查询数据源的表信息
 *
 * @param id 数据源
 * @return
 */
@GetMapping("/{id}")
public Response getTableInfoById(Page page, @PathVariable Integer id) {
    GenDsInfo genDsInfo = genDsInfoService.getById(id);
    JdbcHandle jdbcHandle = handle.get(genDsInfo.getType());
    jdbcHandle.test(genDsInfo);
    return Response.success(jdbcHandle.getPageTableInfo(page));
}
Also used : GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) JdbcHandle(com.xhuicloud.generator.handle.JdbcHandle)

Example 4 with GenDsInfo

use of com.xhuicloud.common.datasource.entity.GenDsInfo in project XHuiCloud by sindaZeng.

the class GenDsInfoServiceImpl method updateDynamicDataSource.

@Override
public Boolean updateDynamicDataSource(GenDsInfo genDsInfo) {
    GenDsInfo _genDsInfo = getById(genDsInfo.getId());
    if (_genDsInfo == null) {
        throw SysException.sysFail("数据不存在");
    }
    // 先移除
    DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtil.getBean(DynamicRoutingDataSource.class);
    dynamicRoutingDataSource.removeDataSource(_genDsInfo.getName());
    // 再添加
    addDynamicDataSource(genDsInfo);
    if (StrUtil.isNotBlank(genDsInfo.getPassword())) {
        _genDsInfo.setPassword(AesUtil.decrypt(genDsInfo.getPassword()));
    }
    return updateById(_genDsInfo);
}
Also used : GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

Aggregations

GenDsInfo (com.xhuicloud.common.datasource.entity.GenDsInfo)4 JdbcHandle (com.xhuicloud.generator.handle.JdbcHandle)2 SneakyThrows (lombok.SneakyThrows)2 DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)1 DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)1 DsJdbcUrlEnum (com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1