| | |
| | | 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"; |
| | | } |
| | |
| | | INCREMENTAL_UPDATE_CAN_NOT_SAME_FIELD("å¢éæ´æ°æ è¯ä¸è½æ¯åä¸å段", "029"), |
| | | |
| | | NONSUPPORT_DATABASE_TYPE("䏿¯æçæ°æ®åºç±»å", "030"), |
| | | |
| | | |
| | | SAVE_DATASOURCE_CONFIG_SAVE_FAIL("ä¿åæ°æ®æºé
置失败", "031"), |
| | | |
| | | |
| | | ; |
| | | |
| | | private String errorMsg; |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | import com.product.common.lang.StringUtils; |
| | | import com.product.core.entity.FieldSetEntity; |
| | | import com.product.data.config.CmnConst; |
| | | import com.product.data.config.DatabaseType; |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | 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"); |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | 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; |
| | |
| | | * @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, ");//--åå |
| | |
| | | * @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, "); |
| | |
| | | * @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 "); |
| | |
| | | * @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 "); |
| | |
| | | * @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 "); |
| | |
| | | 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); |
| | |
| | | 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()); |
| | | //åæ®µå |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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; |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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; |
| | | } |