fix:更改数据表结构,在测试的同时异步添加表结构 bug修改,不会重复添加,可区分添加和修改

master
Saisai Liu 2024-04-23 08:42:27 +08:00
parent 8839f7fe20
commit 5da4983e6d
5 changed files with 52 additions and 77 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.etl.domain.TableInfo; import com.muyu.etl.domain.TableInfo;
import org.apache.ibatis.annotations.Param;
/** /**
* Mapper * Mapper
@ -60,4 +61,7 @@ public interface TableInfoMapper extends BaseMapper<TableInfo>
* @return * @return
*/ */
public int deleteTableInfoByIds(Long[] ids); public int deleteTableInfoByIds(Long[] ids);
TableInfo selectTableInfoByName(TableInfo table);
} }

View File

@ -60,4 +60,7 @@ public interface TableInfoService extends IService<TableInfo>
* @return * @return
*/ */
public int deleteTableInfoById(Long id); public int deleteTableInfoById(Long id);
TableInfo selectTableInfoByName(TableInfo build);
} }

View File

@ -1,5 +1,6 @@
package com.muyu.etl.service.impl; package com.muyu.etl.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -154,10 +155,7 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMapp
PreparedStatement ps = conn.prepareStatement("select * from " + tableName1); PreparedStatement ps = conn.prepareStatement("select * from " + tableName1);
ResultSet rset = ps.executeQuery(); ResultSet rset = ps.executeQuery();
Long rowCount = 0L; Long rowCount = 0L;
while(rset.next()) while(rset.next()) {rowCount++;}
{
rowCount++;
}
TableInfo build = TableInfo.builder() TableInfo build = TableInfo.builder()
.basicId(basicConfigInfo.getId()) .basicId(basicConfigInfo.getId())
.tableName(tableName1) .tableName(tableName1)
@ -172,17 +170,19 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMapp
eq(TableInfo::getTableName,build.getTableName()); eq(TableInfo::getTableName,build.getTableName());
eq(TableInfo::getBasicId,basicConfigInfo.getId()); eq(TableInfo::getBasicId,basicConfigInfo.getId());
}}); }});
new Runnable() { TableInfo table = tableInfoService.selectTableInfoByName(build);
Runnable thread = new Runnable() {
@SneakyThrows @SneakyThrows
@Override @Override
public void run() { public void run() {
try { try {
syncData(finalConn,databaseName,build); syncData(finalConn, databaseName, table);
} catch (SQLException e) { } catch (SQLException e) {
throw new ServletException("连接失败"); throw new ServletException("连接失败");
} }
} }
}; };
thread.run();
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new ServletException("连接失败"); throw new ServletException("连接失败");
@ -198,6 +198,7 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMapp
" CASE WHEN COLUMN_KEY = 'PRI' THEN '是' ELSE '否' END AS '是否主键'," + " CASE WHEN COLUMN_KEY = 'PRI' THEN '是' ELSE '否' END AS '是否主键'," +
" CASE \n" + " CASE \n" +
" WHEN DATA_TYPE = 'int' THEN 'Integer' " + " WHEN DATA_TYPE = 'int' THEN 'Integer' " +
" WHEN DATA_TYPE = 'bigint' THEN 'Long' " +
" WHEN DATA_TYPE = 'varchar' THEN 'String' " + " WHEN DATA_TYPE = 'varchar' THEN 'String' " +
" WHEN DATA_TYPE = 'decimal' THEN 'BigDecimal' " + " WHEN DATA_TYPE = 'decimal' THEN 'BigDecimal' " +
" WHEN DATA_TYPE = 'tinyint' AND COLUMN_TYPE = 'tinyint(1)' THEN 'Boolean'" + " WHEN DATA_TYPE = 'tinyint' AND COLUMN_TYPE = 'tinyint(1)' THEN 'Boolean'" +
@ -210,46 +211,44 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMapp
" IS_NULLABLE AS '是否为空', \n" + " IS_NULLABLE AS '是否为空', \n" +
" COLUMN_DEFAULT AS '默认值' \n" + " COLUMN_DEFAULT AS '默认值' \n" +
"FROM INFORMATION_SCHEMA.COLUMNS WHERE \n" + "FROM INFORMATION_SCHEMA.COLUMNS WHERE \n" +
" TABLE_SCHEMA = '" + databaseName + "' -- 替换为你的数据库名称 \n" + "TABLE_SCHEMA = '" + databaseName + "' -- 替换为你的数据库名称 \n" +
" AND TABLE_NAME = '" + table.getTableName() + "'"); "AND TABLE_NAME = '" + table.getTableName() + "'");
// "SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_KEY,COLUMN_DEFAULT,
// COLUMN_COMMENT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,
// NUMERIC_SCALE FROM INFORMATION_SCHEMA.COLUMNS
// WHERE TABLE_SCHEMA = '数据库名' AND TABLE_NAME = '表名'"
ResultSet resultSet = ps.executeQuery(); ResultSet resultSet = ps.executeQuery();
ArrayList<Structure> structureList = new ArrayList<>();
while (resultSet.next()){ while (resultSet.next()){
String columnName = resultSet.getObject(0).toString(); String columnName = (String) resultSet.getString(1);
String columnComment = resultSet.getObject(1).toString(); String columnComment = (String) resultSet.getObject(2);
String columnKey = resultSet.getObject(2).toString(); String columnKey = (String) resultSet.getObject(3);
String end = resultSet.getObject(3).toString(); String end = (String) resultSet.getObject(4);
String dataType = resultSet.getObject(4).toString(); String dataType = (String) resultSet.getObject(5);
String columnType = resultSet.getObject(5).toString(); String columnType = (String) resultSet.getObject(6);
String characterMaximumLength = resultSet.getObject(6).toString(); String characterMaximumLength = String.valueOf(resultSet.getInt(7));
String NumericScale = resultSet.getObject(7).toString(); String NumericScale = String.valueOf(resultSet.getInt(8));
String isNullable = resultSet.getObject(8).toString(); String isNullable = (String) resultSet.getObject(9);
String columnDefault = resultSet.getObject(9).toString(); String columnDefault = (String) resultSet.getObject(10);
System.out.println(columnName);
System.out.println(columnComment);
System.out.println(columnKey);
System.out.println(end);
System.out.println(dataType);
System.out.println(columnType);
System.out.println(characterMaximumLength);
System.out.println(NumericScale);
System.out.println(isNullable);
System.out.println(columnDefault);
Structure build = Structure.builder() Structure build = Structure.builder()
.tableId(table.getId()) .tableId(table.getId())
.columnName(columnName) .columnName(String.valueOf(columnName))
.columnRemark(columnComment) .columnRemark(columnComment)
.isPrimary("是".equals(columnKey) ? "Y" : "N") .isPrimary("是".equals(columnKey) ? "Y" : "N")
.javaType(end) .javaType( end)
.columnType(dataType) .columnType( dataType)
.columnType(columnType) .columnType(columnType)
.columnLength(characterMaximumLength) .columnLength(characterMaximumLength)
.columnDecimals(NumericScale) .columnDecimals( NumericScale)
.isNull("YES".equals(isNullable) ? "Y" : "N") .isNull("YES".equals(isNullable) ? "Y" : "N")
.defaultValue(columnDefault) .defaultValue( columnDefault)
.build(); .build();
log.info(build); log.info(build);
structureList.add(build); structureService.saveOrUpdate(build,new LambdaUpdateWrapper<Structure>(){{
eq(Structure::getTableId,build.getTableId());
eq(Structure::getColumnName,build.getColumnName());
eq(Structure::getRemark,build.getRemark());
}});
} }
} }
@ -287,48 +286,6 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMapp
}catch (Exception exception){ }catch (Exception exception){
continue; continue;
} }
// ResultSet tables = metaData.getTables(
// catalogs,
// info.getDatabaseName(),
// "%",
// new String[]{"TABLE", "VIEW"});
// while (tables.next()) {
// //表名
// String tableName = tables.getString("TABLE_NAME");
// ResultSet rs = metaData.getColumns(catalogs, info.getDatabaseName(), tableName, "%");
// try {
// while (rs.next()) {
//
// HashMap<String, String> map = new HashMap<>();
// // 列明
// map.put("columnName", rs.getString("COLUMN_NAME"));
// // 列类型
// map.put("typeName", rs.getString("TYPE_NAME"));
// // 列备注
// map.put("remarks", rs.getString("REMARKS"));
//// list.add(map);
// //库名
// System.out.println("TABLE_CAT" + "===" + rs.getString("TABLE_CAT"));
//
// System.out.println("TABLE_SCHEM" + "===" + rs.getString("TABLE_SCHEM"));
// System.out.println("TABLE_NAME" + "===" + rs.getString("TABLE_NAME"));
//// System.out.println("NON_UNIQUE" + "===" + rs.getString("NON_UNIQUE"));
// System.out.println("INDEX_QUALIFIER" + "===" + rs.getString("INDEX_QUALIFIER"));
// System.out.println("INDEX_NAME" + "===" + rs.getString("INDEX_NAME"));
// System.out.println("TYPE" + "===" + rs.getString("TYPE"));
// System.out.println("ORDINAL_POSITION" + "===" + rs.getString("ORDINAL_POSITION"));
// System.out.println("COLUMN_NAME" + "===" + rs.getString("COLUMN_NAME"));
// System.out.println("ASC_OR_DESC" + "===" + rs.getString("ASC_OR_DESC"));
// System.out.println("CARDINALITY" + "===" + rs.getString("CARDINALITY"));
// System.out.println("PAGES" + "===" + rs.getString("PAGES"));
// System.out.println("FILTER_CONDITION" + "===" + rs.getString("FILTER_CONDITION"));
//
// }
// }catch (Exception e){
// log.error(e.getMessage());
// continue;
// }
// }
} }
} catch (Exception e){ } catch (Exception e){
log.error(e.getMessage()); log.error(e.getMessage());

View File

@ -95,4 +95,9 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
{ {
return tableInfoMapper.deleteTableInfoById(id); return tableInfoMapper.deleteTableInfoById(id);
} }
@Override
public TableInfo selectTableInfoByName(TableInfo table) {
return tableInfoMapper.selectTableInfoByName(table);
}
} }

View File

@ -40,6 +40,12 @@
where id = #{id} where id = #{id}
</select> </select>
<select id="selectTableInfoByName" parameterType="com.muyu.etl.domain.TableInfo" resultMap="TableInfoResult">
select * from table_info
where basic_id = #{basicId} and table_name = #{tableName}
</select>
<insert id="insertTableInfo" parameterType="com.muyu.etl.domain.TableInfo" useGeneratedKeys="true" keyProperty="id"> <insert id="insertTableInfo" parameterType="com.muyu.etl.domain.TableInfo" useGeneratedKeys="true" keyProperty="id">
insert into table_info insert into table_info
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">