From 12dc05d38bea0762a5bb294ffcd349f61bf4a7b7 Mon Sep 17 00:00:00 2001 From: yaoq Date: Tue, 20 Sep 2022 19:57:36 +0800 Subject: [PATCH] =?UTF-8?q?canal=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/canal/file/common/AbstractFileWork.java | 18 +++++++++++++- .../system/canal/file/impl/KbSqlFileWork.java | 28 +++++++++++++++++++++- .../src/main/resources/config/application.yaml | 11 +++++---- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java b/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java index 35fbe83..00276a7 100644 --- a/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java +++ b/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java @@ -18,6 +18,7 @@ import java.util.*; @Component public abstract class AbstractFileWork { private final static String suffix = ".cf"; + protected final static String idField = "idField"; protected FileDecodeConfig fileConfig; @@ -136,7 +137,7 @@ public abstract class AbstractFileWork { sql.append("?").append(","); String value = data.get(field); if (StringUtils.isEmpty(value)) { - value = null; + args.add(null); } else { args.add(value); } @@ -170,4 +171,19 @@ public abstract class AbstractFileWork { protected void doDeleteSql(CanalVO canalVO) { getDbEngine().update("delete from " + canalVO.getTableName() + " where " + canalVO.getIdField() + " = ? ", canalVO.getId()); } + + protected Map decodeCreateSql(String sql) { + Map map = new HashMap<>(); + String data = sql.substring(sql.indexOf("(") + 1, sql.lastIndexOf(")")).trim().replaceAll("`", ""); + String[] rows = data.split(","); + for (String row : rows) { + if (row.contains("PRIMARY KEY")) { + map.put(idField, row.substring(row.indexOf("(") + 1, row.lastIndexOf(")"))); + continue; + } + String[] fields = row.trim().split(" "); + map.put(fields[0], fields[1]); + } + return map; + } } diff --git a/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java b/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java index b4a72de..1f505ae 100644 --- a/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java +++ b/smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java @@ -3,12 +3,16 @@ package cc.smtweb.system.canal.file.impl; import cc.smtweb.framework.core.common.CanalVO; import cc.smtweb.framework.core.common.SwEnum; import cc.smtweb.system.canal.file.common.AbstractFileWork; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; /** * @Author yaoq * @Date 2022年09月20日 10:29 * @Description */ +@Slf4j public class KbSqlFileWork extends AbstractFileWork { @Override @@ -18,11 +22,33 @@ public class KbSqlFileWork extends AbstractFileWork { @Override protected void doCreateSql(CanalVO canalVO) { - + Map sqlMap = decodeCreateSql(canalVO.getSql()); + StringBuilder sql = new StringBuilder(); + sql.append("create table public.").append(canalVO.getTableName()).append(" ( "); + sqlMap.forEach((k, v) -> { + if (idField.equals(k)) return; + sql.append(k).append(" ").append(convertFieldType(v)); + if (k.equals(sqlMap.get(idField))) { + sql.append(" ").append("NOT NULL DEFAULT -1"); + } else { + sql.append(" ").append("NULL"); + } + sql.append(","); + }); + sql.append(" CONSTRAINT pk_").append(canalVO.getTableName()).append(" PRIMARY KEY (").append(sqlMap.get(idField)).append(") ENABLE VALIDATE ) "); + log.debug("sql:::" + sql); + getDbEngine().update(sql.toString()); } @Override protected void doAlterSql(CanalVO canalVO) { } + + + private String convertFieldType(String type) { + if (type.contains("bigint")) return "bigint"; + if (type.contains("tinyint")) return "tinyint"; + return type; + } } diff --git a/smtweb-framework/canal/file/src/main/resources/config/application.yaml b/smtweb-framework/canal/file/src/main/resources/config/application.yaml index a7771d7..84d5ad5 100644 --- a/smtweb-framework/canal/file/src/main/resources/config/application.yaml +++ b/smtweb-framework/canal/file/src/main/resources/config/application.yaml @@ -6,7 +6,7 @@ smtweb: debug: true code-java-path: 'd:/work/smtweb2/smtweb-framework/canal/file' db: - type: mysql + type: kbsql default: rule: prefix: _smt_ @@ -33,11 +33,12 @@ spring: port: 6379 password: datasource: - driver-class-name: com.mysql.cj.jdbc.Driver + driver-class-name: com.kingbase8.Driver # url: jdbc:mysql://139.9.38.43:6032/smt?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false - url: jdbc:mysql://127.0.0.1:3306/tzrs?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false - username: root - password: root + # url: jdbc:mysql://127.0.0.1:3306/tzrs?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false + url: jdbc:kingbase8://172.28.123.205:54321/HLJTY?useUnicode=true&characterEncoding=utf-8 + username: system + password: system servlet: multipart: max-file-size: 104857600000