达梦表字段、字段类型,精度比对及更改字段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");
    }


}
相关推荐
何曾参静谧2 分钟前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
程序媛小果8 分钟前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot
追风林13 分钟前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
芒果披萨28 分钟前
El表达式和JSTL
java·el
q5673152328 分钟前
在 Bash 中获取 Python 模块变量列
开发语言·python·bash
许野平1 小时前
Rust: 利用 chrono 库实现日期和字符串互相转换
开发语言·后端·rust·字符串·转换·日期·chrono
也无晴也无风雨1 小时前
在JS中, 0 == [0] 吗
开发语言·javascript
狂奔solar1 小时前
yelp数据集上识别潜在的热门商家
开发语言·python
duration~1 小时前
Maven随笔
java·maven
zmgst1 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql