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


}
相关推荐
Hacker_LaoYi1 小时前
SQL注入的那些面试题总结
数据库·sql
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna2 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Hacker_LaoYi3 小时前
【渗透技术总结】SQL手工注入总结
数据库·sql
独行soc3 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
神仙别闹3 小时前
基于java的改良版超级玛丽小游戏
java
Dream_Snowar4 小时前
速通Python 第三节
开发语言·python
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫4 小时前
泛型(2)
java