许鹏程
2024-08-15 112f25b820aea7b0487197d1516974589d1d9979
commit udpate
已添加3个文件
已修改4个文件
214 ■■■■■ 文件已修改
src/main/java/com/product/data/config/CmnConst.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/config/ErrorCode.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/controller/DataSourceConfigController.java 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/entity/DatabaseEntity.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/service/DataSourceConfigService.java 87 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/service/SyncDataConfigService.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/service/impl/IDataSourceConfigService.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/data/config/CmnConst.java
@@ -50,4 +50,7 @@
    public static final String TABLE_SYNC_MANAGER_LOG = "product_sys_data_sync_manager_log";
    public static final String TABLE_PROCESS_CONFIG = "product_sys_database_sync_processing_config";
    public static final String TABLE_PRODUCT_SYS_DATASOURCE_CONFIG = "product_sys_datasource_config";
    public static final String TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD = "product_sys_datasource_config_field";
}
src/main/java/com/product/data/config/ErrorCode.java
@@ -52,6 +52,11 @@
    INCREMENTAL_UPDATE_CAN_NOT_SAME_FIELD("增量更新标识不能是同一字段", "029"),
    NONSUPPORT_DATABASE_TYPE("不支持的数据库类型", "030"),
    SAVE_DATASOURCE_CONFIG_SAVE_FAIL("保存数据源配置失败", "031"),
    ;
    private String errorMsg;
src/main/java/com/product/data/controller/DataSourceConfigController.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,50 @@
package com.product.data.controller;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.data.config.CmnConst;
import com.product.data.config.ErrorCode;
import com.product.data.service.impl.IDataSourceConfigService;
import com.product.module.sys.version.ApiVersion;
import com.product.util.BaseUtil;
import com.product.util.support.AbstractBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
 * @Author cheng
 * @Date 2024/8/13 18:18
 * @Desc æ•°æ®æºé…ç½®
 */
@RequestMapping("/data-center/datasource-config")
@RestController
public class DataSourceConfigController extends AbstractBaseController {
    @Resource
    private IDataSourceConfigService dataSourceConfigService;
    @PostMapping("/save/{version}")
    @ApiVersion(1)
    public String saveConfig(HttpServletRequest request) {
        try {
            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG);
            IDataSourceConfigService service = (IDataSourceConfigService) getProxyInstance(dataSourceConfigService);
            service.save(fse);
            return OK();
        } catch (BaseException e) {
            e.printStackTrace();
            return error(e);
        } catch (Exception e) {
            e.printStackTrace();
            return error(ErrorCode.SAVE_DATASOURCE_CONFIG_SAVE_FAIL, e);
        }
    }
}
src/main/java/com/product/data/entity/DatabaseEntity.java
@@ -2,6 +2,7 @@
import com.product.common.lang.StringUtils;
import com.product.core.entity.FieldSetEntity;
import com.product.data.config.CmnConst;
import com.product.data.config.DatabaseType;
/**
@@ -90,6 +91,16 @@
    }
    protected void init(FieldSetEntity fse) {
        if (CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG.equals(fse.getTableName())) {
            DbType = fse.getInteger("datasource_type");
            userName = fse.getString("username");
            passWord = fse.getString("password");
            ip = fse.getString("ip_address");
            port = fse.getString("database_port");
            customParams = fse.getString("link_params");
            DbInstance = fse.getString("instance_name");
            return;
        }
        DbType = fse.getInteger("db_type");
        userName = fse.getString("user_name");
        passWord = fse.getString("pass_word");
src/main/java/com/product/data/service/DataSourceConfigService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,87 @@
package com.product.data.service;
import cn.hutool.core.lang.UUID;
import com.product.admin.service.PublicService;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.data.config.CmnConst;
import com.product.data.config.DatabaseType;
import com.product.data.connection.ConnectionManager;
import com.product.data.entity.DatabaseEntity;
import com.product.data.service.impl.IDataSourceConfigService;
import com.product.data.utli.QueryDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
/**
 * @Author cheng
 * @Date 2024/8/13 18:20
 * @Desc æ•°æ®æºé…ç½®ä¸šåС层
 */
@Service
public class DataSourceConfigService extends AbstractBaseService implements IDataSourceConfigService {
    @Resource
    private SyncDataConfigService syncDataConfigService;
    @Resource
    private PublicService publicService;
    @Override
    public void save(FieldSetEntity fse) throws BaseException {
        DatabaseEntity dbe = new DatabaseEntity(fse);
        String uuid = fse.getUUID();
        if (StringUtils.isEmpty(uuid)) {
            fse.setValue("~type~", "add");
            fse.setValue("uuid", UUID.randomUUID().toString());
            uuid = fse.getUUID();
        }
        String finalUuid = uuid;
        try (Connection connection = ConnectionManager.getConnection(dbe)) {
            if (connection != null) {
                QueryDataService queryDataService = new QueryDataService(connection);
                String[] tableName = syncDataConfigService.getTableName(queryDataService, dbe);
                if ((tableName != null && tableName.length > 0) || DatabaseType.PSQL.equals(dbe.getDbType()) || DatabaseType.Oracle.equals(dbe.getDbType())) {
                    Map<String, String> fieldTypeReference = syncDataConfigService.getFieldTypeReference(dbe.getDbType());
                    DataTableEntity dt = new DataTableEntity();
                    if (DatabaseType.Oracle.equals(dbe.getDbType())) {
                        Map<String, List<Map<String, Object>>> tableFieldMap = syncDataConfigService.getOracleFieldInfo(queryDataService);
                        tableFieldMap.forEach((k, v) -> {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, k, dbe, finalUuid, dt, fieldTypeReference, v);
                        });
                    } else if (DatabaseType.PSQL.equals(dbe.getDbType())) {
                        Map<String, List<Map<String, Object>>> pSqlFieldInfo = syncDataConfigService.getPSqlFieldInfo(dbe.getDbInstance(), queryDataService);
                        pSqlFieldInfo.forEach((k, v) -> {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, k, dbe, finalUuid, dt, fieldTypeReference, v);
                        });
                    } else {
                        for (String table : tableName) {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, table, dbe, finalUuid, dt, fieldTypeReference, null);
                        }
                    }
                    if (!DataTableEntity.isEmpty(dt)) {
                        fse.addSubDataTable(dt);
                    }
                }
                getBaseDao().saveFieldSetEntity(fse);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }
    @Override
    public void delete(FieldSetEntity fse) throws BaseException {
        publicService.delete(fse);
    }
}
src/main/java/com/product/data/service/SyncDataConfigService.java
@@ -489,7 +489,7 @@
    }
    private Map<String, String> getFieldTypeReference(DatabaseType dbt) {
    public Map<String, String> getFieldTypeReference(DatabaseType dbt) {
        Map<String, String> fieldType = new CaseInsensitiveMap<>();
        if (dbt == null) {
            return fieldType;
@@ -510,7 +510,7 @@
     * @param queryDataService
     * @return
     */
    private Map<String, List<Map<String, Object>>> getOracleFieldInfo(QueryDataService queryDataService) {
    public Map<String, List<Map<String, Object>>> getOracleFieldInfo(QueryDataService queryDataService) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" T .column_name AS column_name,  ");//--列名
@@ -576,7 +576,7 @@
     * @param queryDataService
     * @return
     */
    private List<Map<String, Object>> getMysqlFieldInfo(String tableName, QueryDataService queryDataService) {
    public List<Map<String, Object>> getMysqlFieldInfo(String tableName, QueryDataService queryDataService) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" COLUMN_NAME column_name, ");
@@ -616,7 +616,7 @@
     * @param queryDataService
     * @return
     */
    private List<Map<String, Object>> getSqlServerFieldInfo(String tableName, QueryDataService queryDataService) {
    public List<Map<String, Object>> getSqlServerFieldInfo(String tableName, QueryDataService queryDataService) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT c.name as column_name,t.name as column_type,isnull(c.prec ,0)column_length,isnull(c.scale,0) column_scale ");
        sql.append(" FROM sysobjects o ");
@@ -636,7 +636,7 @@
     * @param queryDataService
     * @return
     */
    private List<Map<String, Object>> getInformixFieldInfo(String dbName, String tableName, QueryDataService queryDataService) {
    public List<Map<String, Object>> getInformixFieldInfo(String dbName, String tableName, QueryDataService queryDataService) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select c.colname column_name, ");
        sql.append(" case c.coltype ");
@@ -757,7 +757,7 @@
     * @param queryDataService
     * @return
     */
    private Map<String, List<Map<String, Object>>> getPSqlFieldInfo(String instance, QueryDataService queryDataService) {
    public Map<String, List<Map<String, Object>>> getPSqlFieldInfo(String instance, QueryDataService queryDataService) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT case when column_length<0  then 0 else  column_length end as column_length,table_name,\"column_name\",column_type,column_comment FROM ( ");
        sql.append(" SELECT C ");
@@ -828,15 +828,7 @@
        return result;
    }
    /**
     * èŽ·å–å­—æ®µåç§°
     *
     * @param queryDataService
     * @param tableName
     * @param dbe
     * @return
     */
    private void getFieldName(QueryDataService queryDataService, String tableName, DatabaseEntity dbe, String uuid, DataTableEntity dt, Map<String, String> fieldTypeReference, List<Map<String, Object>> resultList) {
    public void getFieldName(String saveTableName, QueryDataService queryDataService, String tableName, DatabaseEntity dbe, String uuid, DataTableEntity dt, Map<String, String> fieldTypeReference, List<Map<String, Object>> resultList) {
        DatabaseType dbt = dbe.getDbType();
        if (DatabaseType.MySql.equals(dbt)) {
            resultList = getMysqlFieldInfo(tableName, queryDataService);
@@ -881,8 +873,10 @@
                    column_scale = resultList.get(i).get("column_scale".toUpperCase());
                }
                FieldSetEntity field = new FieldSetEntity();
                field.setTableName(CmnConst.PRODUCT_SYS_DATABASE_TABLENAME_FIELD);
                field.setTableName(saveTableName);
                if (CmnConst.PRODUCT_SYS_DATABASE_TABLENAME_FIELD.equals(saveTableName)) {
                field.setValue("sync_manager_uuid", uuid);
                }
                //表名
                field.setValue("table_name", tableName.toLowerCase());
                //字段名
@@ -903,6 +897,19 @@
                dt.addFieldSetEntity(field);
            }
        }
    }
    /**
     * èŽ·å–å­—æ®µåç§°
     *
     * @param queryDataService
     * @param tableName
     * @param dbe
     * @return
     */
    public void getFieldName(QueryDataService queryDataService, String tableName, DatabaseEntity dbe, String uuid, DataTableEntity dt, Map<String, String> fieldTypeReference, List<Map<String, Object>> resultList) {
        getFieldName(CmnConst.PRODUCT_SYS_DATABASE_TABLENAME_FIELD, queryDataService, tableName, dbe, uuid, dt, fieldTypeReference, resultList);
    }
    /**
@@ -912,7 +919,7 @@
     * @param dbe
     * @return
     */
    private String[] getTableName(QueryDataService queryDataService, DatabaseEntity dbe) {
    public String[] getTableName(QueryDataService queryDataService, DatabaseEntity dbe) {
        if (dbe != null && dbe.getDbType() != null) {
            DatabaseType dbt = dbe.getDbType();
            String sql = null;
src/main/java/com/product/data/service/impl/IDataSourceConfigService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,17 @@
package com.product.data.service.impl;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
/**
 * @Author cheng
 * @Date 2024/8/13 18:20
 * @Desc æ•°æ®æºé…ç½®
 */
public interface IDataSourceConfigService {
    void save(FieldSetEntity fse) throws BaseException;
    void delete(FieldSetEntity fse) throws BaseException;
}