|
|
@@ -42,13 +42,58 @@ public class KbSqlFileWork extends AbstractFileWork { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void doAlterSql(CanalVO canalVO) { |
|
|
|
String sql = canalVO.getSql().replaceAll("`", "").replace("\r\n", ""); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
//添加sql |
|
|
|
if (sql.contains("ADD")) { |
|
|
|
sb.append("ALTER TABLE public.").append(canalVO.getTableName()); |
|
|
|
sql = sql.substring(sql.indexOf("ADD COLUMN")); |
|
|
|
String[] sqls = sql.split(","); |
|
|
|
for (String str : sqls) { |
|
|
|
String[] ss = str.split(" "); |
|
|
|
sb.append(" ADD COLUMN ").append(ss[2]).append(" ").append(convertFieldType(ss[3])).append(" NULL ").append(","); |
|
|
|
} |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
} else if (sql.contains("DROP")) { |
|
|
|
sb.append("ALTER TABLE public.").append(canalVO.getTableName()); |
|
|
|
sql = sql.substring(sql.indexOf("DROP COLUMN")); |
|
|
|
String[] sqls = sql.split(","); |
|
|
|
for (String str : sqls) { |
|
|
|
String[] ss = str.split(" "); |
|
|
|
sb.append(" DROP COLUMN ").append(ss[2]).append(","); |
|
|
|
} |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
|
|
|
|
} else if (sql.contains("CHANGE")) { |
|
|
|
sql = sql.substring(sql.indexOf("CHANGE COLUMN")); |
|
|
|
String[] sqls = sql.split(","); |
|
|
|
for (String str : sqls) { |
|
|
|
String[] ss = str.split(" "); |
|
|
|
sb.append("ALTER TABLE public.").append(canalVO.getTableName()).append(" RENAME COLUMN ").append(ss[2]).append(" TO ").append(ss[3]).append(";"); |
|
|
|
} |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
} else if (sql.contains("MODIFY")) { |
|
|
|
sb.append("ALTER TABLE public.").append(canalVO.getTableName()); |
|
|
|
sql = sql.substring(sql.indexOf("MODIFY COLUMN")); |
|
|
|
String[] sqls = sql.split(","); |
|
|
|
for (String str : sqls) { |
|
|
|
String[] ss = str.split(" "); |
|
|
|
sb.append(" ALTER COLUMN ").append(ss[2]).append(" TYPE ").append(convertFieldType(ss[3])).append(","); |
|
|
|
} |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
} |
|
|
|
log.debug("sql:::" + sb.toString()); |
|
|
|
getDbEngine().update(sb.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String convertFieldType(String type) { |
|
|
|
if (type.contains("bigint")) return "bigint"; |
|
|
|
if (type.contains("tinyint")) return "tinyint"; |
|
|
|
if (fieldMapping == null) return type; |
|
|
|
String[] types = type.replace("(", "-").replace(")", "").split("-"); |
|
|
|
for (Map<String, String> field : fieldMapping.getFields()) { |
|
|
|
String key = types[0].toUpperCase(); |
|
|
|
if (field.containsKey(key)) return field.get(key); |
|
|
|
} |
|
|
|
return type; |
|
|
|
} |
|
|
|
} |