【MySQL建表语句转PostgreSQL建表语句】MySQL建表语句转PostgreSQL建表语句

该博客介绍了一个用于将MySQL的DDL语句转换为PostgreSQL兼容格式的Java工具。通过读取MySQL的DDL文件,应用正则表达式替换特定的数据类型和约束,以适应PostgreSQL的要求。同时,它还处理了注释的处理,确保转换后的SQL语句正确无误。
摘要由CSDN通过智能技术生成

1.首先加一下依赖
<dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>1.2</version> </dependency>
2.加下脚本
/**

  • @author Ben.Zhou

  • @version 1.0

  • @description

  • @data 2022/3/10 16:51
    */
    public class MysqlDdl2PgDdlUtil {
    public static void main(String[] args) throws IOException, JSQLParserException {
    // 你的MySQL DDL路径
    String mysqlDDLPath = “C:\Users\wb_xg20\Desktop\finance_super1.sql”;
    String dDLs = FileUtils.readFileToString(new File(mysqlDDLPath));

     System.out.println(dDLs);
     dDLs = dDLs.replaceAll("current_timestamp\\(\\) ON UPDATE current_timestamp\\(\\)", "current_timestamp")
             .replaceAll("current_timestamp\\(\\)", "current_timestamp")
             .replaceAll("current_timestamp\\(\\)", "current_timestamp")
             .replaceAll("int\\([0-9]*\\)", "INT")
             .replaceAll("tinyint\\([0-9]*\\)", "INT")
             .replaceAll("tinyINT","INT")
             .replaceAll("double\\([0-9]*,[0-9]*\\)","float8")
             .replaceAll("DEFAULT 00000000000","DEFAULT 0")
             .replaceAll("unsigned zerofill","")
             .replaceAll("bigint\\([0-9]*\\) NOT NULL AUTO_INCREMENT", "BIGSERIAL PRIMARY KEY")
             .replaceAll("bigint\\([0-9]*\\)","INT8");
     //dDLs = dDLs.replaceAll("bigint\\(19\\)","int8");
     System.out.println("++++++++++开始转换SQL语句+++++++++++++");
    
     Statements statements = CCJSqlParserUtil.parseStatements(dDLs);
     statements.getStatements()
             .stream()
             .map(statement -> (CreateTable) statement).forEach(ct -> {
         Table table = ct.getTable();
         List<ColumnDefinition> columnDefinitions = ct.getColumnDefinitions();
         List<String> comments = new ArrayList<>();
         List<ColumnDefinition> collect = columnDefinitions.stream()
                 .peek(columnDefinition -> {
                     List<String> columnSpecStrings = columnDefinition.getColumnSpecStrings();
    
                     int commentIndex = getCommentIndex(columnSpecStrings);
    
                     if (commentIndex != -1) {
                         int commentStringIndex = commentIndex + 1;
                         String commentString = columnSpecStrings.get(commentStringIndex);
    
                         String commentSql = genCommentSql(table.toString(), columnDefinition.getColumnName(), commentString);
                         comments.add(commentSql);
                         columnSpecStrings.remove(commentStringIndex);
                         columnSpecStrings.remove(commentIndex);
                     }
                     columnDefinition.setColumnSpecStrings(columnSpecStrings);
                 }).collect(Collectors.toList());
         ct.setColumnDefinitions(collect);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值