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);