|
| 1 | +package com.dbaagent.service.migration; |
| 2 | + |
| 3 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 4 | +import net.sf.jsqlparser.statement.Statement; |
| 5 | +import net.sf.jsqlparser.statement.alter.Alter; |
| 6 | +import net.sf.jsqlparser.statement.alter.AlterExpression; |
| 7 | +import net.sf.jsqlparser.statement.create.index.CreateIndex; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +@Component |
| 16 | +public class DdlStatementParser { |
| 17 | + |
| 18 | + private static final Pattern NOT_VALID = Pattern.compile("(?i)\\s+NOT\\s+VALID\\s*;?\\s*$"); |
| 19 | + private static final Pattern CONCURRENTLY = |
| 20 | + Pattern.compile("(?i)\\bCREATE\\s+(UNIQUE\\s+)?INDEX\\s+CONCURRENTLY\\b"); |
| 21 | + private static final Pattern DEFAULT_FN = |
| 22 | + Pattern.compile("(?i)DEFAULT\\s+([a-z_][a-z0-9_]*)\\s*\\("); |
| 23 | + private static final Pattern REFERENCES = |
| 24 | + Pattern.compile("(?i)REFERENCES\\s+([a-z_][a-z0-9_.\"]*)\\s*\\("); |
| 25 | + private static final Pattern VALIDATE_CONSTRAINT = |
| 26 | + Pattern.compile("(?i)^\\s*ALTER\\s+TABLE\\s+(\\S+)\\s+VALIDATE\\s+CONSTRAINT\\b"); |
| 27 | + |
| 28 | + public Optional<DdlFacts> parse(String sql) { |
| 29 | + if (sql == null || sql.isBlank()) return Optional.empty(); |
| 30 | + |
| 31 | + boolean notValid = NOT_VALID.matcher(sql).find(); |
| 32 | + String normalized = notValid ? NOT_VALID.matcher(sql).replaceAll("") : sql; |
| 33 | + |
| 34 | + boolean concurrently = CONCURRENTLY.matcher(normalized).find(); |
| 35 | + if (concurrently) { |
| 36 | + normalized = CONCURRENTLY.matcher(normalized) |
| 37 | + .replaceAll(m -> "CREATE " + (m.group(1) == null ? "" : m.group(1)) + "INDEX"); |
| 38 | + } |
| 39 | + |
| 40 | + // VALIDATE CONSTRAINT degrades to UNSPECIFIC in JSqlParser; match it textually first. |
| 41 | + var vc = VALIDATE_CONSTRAINT.matcher(sql); |
| 42 | + if (vc.find()) { |
| 43 | + return Optional.of(new DdlFacts(DdlOperation.VALIDATE_CONSTRAINT, strip(vc.group(1)), |
| 44 | + null, null, null, null, null, false, notValid, false, null, sql)); |
| 45 | + } |
| 46 | + |
| 47 | + Statement stmt; |
| 48 | + try { |
| 49 | + stmt = CCJSqlParserUtil.parse(normalized); |
| 50 | + } catch (Exception e) { |
| 51 | + return Optional.empty(); |
| 52 | + } |
| 53 | + |
| 54 | + if (stmt instanceof CreateIndex ci) { |
| 55 | + return Optional.of(new DdlFacts(DdlOperation.CREATE_INDEX, |
| 56 | + strip(ci.getTable().getName()), null, null, null, null, null, |
| 57 | + false, notValid, concurrently, null, sql)); |
| 58 | + } |
| 59 | + if (stmt instanceof Alter alter) { |
| 60 | + return fromAlter(alter, sql, notValid); |
| 61 | + } |
| 62 | + return Optional.empty(); |
| 63 | + } |
| 64 | + |
| 65 | + private Optional<DdlFacts> fromAlter(Alter alter, String rawSql, boolean notValid) { |
| 66 | + List<AlterExpression> exprs = alter.getAlterExpressions(); |
| 67 | + if (exprs == null || exprs.isEmpty()) return Optional.empty(); |
| 68 | + AlterExpression e = exprs.get(0); |
| 69 | + String table = strip(alter.getTable().getName()); |
| 70 | + String op = e.getOperation() == null ? "" : e.getOperation().name().toUpperCase(Locale.ROOT); |
| 71 | + |
| 72 | + if ("DROP".equals(op)) { |
| 73 | + return Optional.of(new DdlFacts(DdlOperation.DROP_COLUMN, table, strip(e.getColumnName()), |
| 74 | + null, null, null, null, false, notValid, false, null, rawSql)); |
| 75 | + } |
| 76 | + if ("RENAME".equals(op)) { |
| 77 | + // JSqlParser exposes only the NEW name here; the old name is not recoverable. |
| 78 | + return Optional.of(new DdlFacts(DdlOperation.RENAME_COLUMN, table, null, |
| 79 | + strip(e.getColumnName()), null, null, null, false, notValid, false, null, rawSql)); |
| 80 | + } |
| 81 | + if ("ADD".equals(op)) { |
| 82 | + if (e.getIndex() != null && e.getIndex().getType() != null |
| 83 | + && e.getIndex().getType().toUpperCase(Locale.ROOT).contains("FOREIGN")) { |
| 84 | + return Optional.of(new DdlFacts(DdlOperation.ADD_FOREIGN_KEY, table, null, null, null, |
| 85 | + null, null, false, notValid, false, referencedTable(rawSql), rawSql)); |
| 86 | + } |
| 87 | + if (rawSql.toUpperCase(Locale.ROOT).contains("CHECK")) { |
| 88 | + return Optional.of(new DdlFacts(DdlOperation.ADD_CHECK, table, null, null, null, |
| 89 | + null, null, false, notValid, false, null, rawSql)); |
| 90 | + } |
| 91 | + if (e.getColDataTypeList() != null && !e.getColDataTypeList().isEmpty()) { |
| 92 | + var cdt = e.getColDataTypeList().get(0); |
| 93 | + List<String> specs = cdt.getColumnSpecs() == null ? List.of() : cdt.getColumnSpecs(); |
| 94 | + String joined = String.join(" ", specs).toUpperCase(Locale.ROOT); |
| 95 | + return Optional.of(new DdlFacts(DdlOperation.ADD_COLUMN, table, |
| 96 | + strip(cdt.getColumnName()), null, |
| 97 | + String.valueOf(cdt.getColDataType()), joined.contains("DEFAULT") ? joined : null, |
| 98 | + defaultFunction(rawSql), joined.contains("NOT NULL"), |
| 99 | + notValid, false, null, rawSql)); |
| 100 | + } |
| 101 | + } |
| 102 | + if ("ALTER".equals(op) && e.getColDataTypeList() != null && !e.getColDataTypeList().isEmpty()) { |
| 103 | + var cdt = e.getColDataTypeList().get(0); |
| 104 | + String type = String.valueOf(cdt.getColDataType()); |
| 105 | + // "SET NOT NULL" arrives with type=SET, specs=[NOT, NULL]. |
| 106 | + if ("SET".equalsIgnoreCase(type)) { |
| 107 | + return Optional.of(new DdlFacts(DdlOperation.SET_NOT_NULL, table, |
| 108 | + strip(cdt.getColumnName()), null, null, null, null, true, |
| 109 | + notValid, false, null, rawSql)); |
| 110 | + } |
| 111 | + return Optional.of(new DdlFacts(DdlOperation.ALTER_COLUMN_TYPE, table, |
| 112 | + strip(cdt.getColumnName()), null, type, null, null, false, |
| 113 | + notValid, false, null, rawSql)); |
| 114 | + } |
| 115 | + return Optional.empty(); |
| 116 | + } |
| 117 | + |
| 118 | + private String defaultFunction(String sql) { |
| 119 | + var m = DEFAULT_FN.matcher(sql); |
| 120 | + return m.find() ? m.group(1).toLowerCase(Locale.ROOT) : null; |
| 121 | + } |
| 122 | + |
| 123 | + private String referencedTable(String sql) { |
| 124 | + var m = REFERENCES.matcher(sql); |
| 125 | + return m.find() ? strip(m.group(1)) : null; |
| 126 | + } |
| 127 | + |
| 128 | + private String strip(String s) { |
| 129 | + return s == null ? null : s.replace("\"", ""); |
| 130 | + } |
| 131 | +} |
0 commit comments