达梦表字段、字段类型,精度比对及更改字段SQL生成:
- 依赖
<!-- 达梦 Connector --><dependency><groupId>com.dameng</groupId><artifactId>DmJdbcDriver18</artifactId><version>8.1.3.62</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.0</version></dependency>
- 数据库配置文件参考
url = jdbc:dm://xxxx:xxxx?schema=xxx&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
username = SYSDBA
password = SYSDBA
- Java代码
java">package com.lhq.datacontrast;import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.setting.Setting;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;@SpringBootApplication
public class DataContrastApplication {public static void main(String[] args) throws SQLException {Setting prodSetting = new Setting("classpath: config/prod.setting");DataSource prodDs = DSFactory.create(prodSetting).getDataSource();Setting devSetting = new Setting("classpath: config/dev.setting");DataSource devDs = DSFactory.create(devSetting).getDataSource();List<Entity> devTable = Db.use(devDs).query("select Table_Name from SYS.ALL_TABLES where OWNER = 'HEALTH_RECORDS'");List<Entity> prodTable = Db.use(prodDs).query("select Table_Name from SYS.ALL_TABLES where OWNER = 'HZJJK_JBGW'");List<String> names = new ArrayList<>();for (Entity table : prodTable) {names.add(table.getStr("table_name"));}for (Entity entity : devTable) {if(entity.getStr("table_name").startsWith("GW_")){String tableName = entity.getStr("table_name").replace("GW_","");if(names.contains( tableName)) {contrastTable(prodDs, devDs, entity.getStr("table_name").replace("GW_", ""), entity.getStr("table_name"));}}}}private static void contrastTable(DataSource prodDs, DataSource devDs, String progTable, String devTable) throws SQLException {List<Entity> devResult = Db.use(devDs).query("\n" +"select * from all_tab_columns where Table_Name=? and OWNER = 'HEALTH_RECORDS';", devTable);List<Entity> progResult = Db.use(prodDs).query("\n" +"select * from all_tab_columns where Table_Name=? and OWNER = 'HZJJK_JBGW';", progTable);Map<String, Entity> progMap = new HashMap<>();for (Entity entity : progResult) {progMap.put(entity.getStr("column_name"), entity);}Map<String, Entity> devMap = new HashMap<>();for (Entity entity : devResult) {devMap.put(entity.getStr("column_name"), entity);}for (String s : progMap.keySet()) {if (devMap.containsKey(s)){if ( progMap.get(s).getStr("data_length").equals(devMap.get(s).getStr("data_length"))) {// 长度相同if(progMap.get(s).getStr("data_type").equals(devMap.get(s).getStr("data_type"))|| (Objects.equals(progMap.get(s).getStr("data_type"), "VARCHAR") && Objects.equals(devMap.get(s).getStr("data_type"), "VARCHAR2"))|| (Objects.equals(progMap.get(s).getStr("data_type"), "VARCHAR2") && Objects.equals(devMap.get(s).getStr("data_type"), "VARCHAR"))){continue;}else{Entity entity = progMap.get(s);System.out.println(String.format("字段名:%s,数据类型不一致,prod:%s->%s->%s,dev:%s->%s->%s",s, progTable,progMap.get(s).getStr("data_type"), progMap.get(s).getStr("data_length"),devTable,devMap.get(s).getStr("data_type"), devMap.get(s).getStr("data_length")));System.out.println(String.format("ALTER TABLE \"%s\".\"%s\" MODIFY \"%s\" %s(%s);", "HEALTH_RECORDS",devTable,s,entity.getStr("data_type"),entity.getStr("data_length")) );}} else {if(progMap.get(s).getStr("data_type").equals(devMap.get(s).getStr("data_type"))){// Entity entity = progMap.get(s);// System.out.println(String.format("字段名:%s,数据长度不一致,prod:%s->%s->%s,dev:%s->%s->%s",// s, progTable,// progMap.get(s).getStr("data_type"), progMap.get(s).getStr("data_length"),// devTable,// devMap.get(s).getStr("data_type"), devMap.get(s).getStr("data_length")));// System.out.println(String.format("ALTER TABLE \"%s\".\"%s\" MODIFY \"%s\" %s(%s);", "HEALTH_RECORDS",// devTable,s,entity.getStr("data_type"),entity.getStr("data_length")) );// System.out.println("commit ");}else{Entity entity = progMap.get(s);System.out.println(String.format("字段名:%s,数据长度不一致,类型也不一致,prod:%s->%s->%s,dev:%s->%s->%s",s, progTable,progMap.get(s).getStr("data_type"), progMap.get(s).getStr("data_length"),devTable,devMap.get(s).getStr("data_type"), devMap.get(s).getStr("data_length")));System.out.println(String.format("ALTER TABLE \"%s\".\"%s\" MODIFY \"%s\" %s(%s);", "HEALTH_RECORDS",devTable,s,entity.getStr("data_type"),entity.getStr("data_length")) );}}}else{Entity entity = progMap.get(s);System.out.println(String.format("字段名:%s 在表\"%s\"中不存在", s, devTable));System.out.println(String.format("ALTER TABLE \"%s\".\"%s\" ADD %s %s(%s);","HEALTH_RECORDS",devTable,s,entity.getStr("data_type"),entity.getStr("data_length")) );System.out.println(String.format("COMMENT ON COLUMN \"%s\".\"%s\".\"%s\" IS '%s';","HEALTH_RECORDS",devTable,s, getComments(prodDs, progTable,s)) );System.out.println("commit;\n");}}}private static String getComments(DataSource ds, String table, String s) throws SQLException {List<Entity> query = Db.use(ds).query("select COMMENTS from all_col_comments where " +"OWNER='目标库' and TABLE_NAME =? and COLUMN_NAME = ?;", table, s);if(query.size() == 0 ){return null;}return query.get(0).getStr("COMMENTS");}}