达梦表字段、字段类型,精度比对及更改字段SQL生成

达梦表字段、字段类型,精度比对及更改字段SQL生成:

  1. 依赖
xml 复制代码
        <!-- 达梦 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>
  1. 数据库配置文件参考

    url = jdbc:dm://xxxx:xxxx?schema=xxx&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
    username = SYSDBA
    password = SYSDBA

  2. 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");
    }


}
相关推荐
在下不上天25 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
陌小呆^O^39 分钟前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp
儿时可乖了1 小时前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite
ruleslol1 小时前
java基础概念37:正则表达式2-爬虫
java
I_Am_Me_1 小时前
【JavaEE进阶】 JavaScript
开发语言·javascript·ecmascript
重生之我是数学王子1 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
xmh-sxh-13141 小时前
jdk各个版本介绍
java
Ai 编码助手1 小时前
使用php和Xunsearch提升音乐网站的歌曲搜索效果
开发语言·php