当前位置: 首页 > news >正文

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

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

  1. 依赖
        <!-- 达梦 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
  1. 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");}}
http://www.lryc.cn/news/429583.html

相关文章:

  • 2.pandas--读取文件夹中所有excel文件进行合并
  • WPS Office两个严重漏洞曝光,已被武器化且在野利用
  • 基于Java爬取微博数据(五) 补充微博正文列表图片 or 视频 内容
  • 反射异常捕获 | InvocationTargetException 要用e.getCause()打印才能看到具体异常
  • 【计算机网络】网络版本计算器
  • 使用 Python 爬虫进行网站流量分析:Referer 头的利用
  • 梧桐数据库(WuTongDB):数据库技术中LL算法详解
  • 【秋招笔试】8.18大疆秋招(第一套)-后端岗
  • CSS 的text-size-adjust属性
  • 阿里MAXCOMPUTE数据专辑信息读取并同步数据表
  • rufus制作ubantu的U盘安装介质时,rufus界面上的分区类型选什么?
  • 【系统架构设计师-2018年】案例分析-答案及详解
  • linux驱动入门实验班——平台总线设备驱动模型和设备树
  • 零基础学习Python(六)
  • 微信小程序--31(todolist案例)
  • springboot项目使用本地依赖项,打包后出现NoClassDefFoundError的一种解决方法
  • Maven高级使用指南
  • windows docker 执行apt-get 权限问题
  • Linux系统信息排查
  • 《图解设计模式》笔记(四)分开考虑
  • Linux shell编程学习笔记74:sed命令——沧海横流任我行(中)
  • [数据集][目标检测]道路积水检测数据集VOC+YOLO格式2699张1类别
  • 不同路径
  • 【HTML】HTML学习之引入CSS样式表
  • shaushaushau1
  • 揭秘面试必备:高频算法与面试题全面解析
  • 设计模式-visit模式-在语法树的实践
  • ZK-Rollups测评
  • redis生产使用场景(一):并行流+二级缓存
  • EXCEL跨文件查询,指定条件列,返回满足条件的指定列