Răsfoiți Sursa

!134 merge
Merge pull request !134 from AE86/V_1.0.0_RC

AE86 2 ani în urmă
părinte
comite
f3561fa50f

+ 1 - 1
dbsyncer-biz/pom.xml

@@ -5,7 +5,7 @@
 	<parent>
 	<parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
 	<modelVersion>4.0.0</modelVersion>
 	<modelVersion>4.0.0</modelVersion>
 	<artifactId>dbsyncer-biz</artifactId>
 	<artifactId>dbsyncer-biz</artifactId>

+ 41 - 15
dbsyncer-biz/src/main/java/org/dbsyncer/biz/checker/impl/tablegroup/TableGroupChecker.java

@@ -9,6 +9,7 @@ import org.dbsyncer.common.util.StringUtil;
 import org.dbsyncer.connector.model.Field;
 import org.dbsyncer.connector.model.Field;
 import org.dbsyncer.connector.model.MetaInfo;
 import org.dbsyncer.connector.model.MetaInfo;
 import org.dbsyncer.connector.model.Table;
 import org.dbsyncer.connector.model.Table;
+import org.dbsyncer.connector.util.PrimaryKeyUtil;
 import org.dbsyncer.manager.Manager;
 import org.dbsyncer.manager.Manager;
 import org.dbsyncer.parser.enums.ModelEnum;
 import org.dbsyncer.parser.enums.ModelEnum;
 import org.dbsyncer.parser.model.ConfigModel;
 import org.dbsyncer.parser.model.ConfigModel;
@@ -26,9 +27,11 @@ import org.springframework.util.Assert;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -76,7 +79,7 @@ public class TableGroupChecker extends AbstractChecker {
         this.modifyConfigModel(tableGroup, params);
         this.modifyConfigModel(tableGroup, params);
 
 
         // 匹配相似字段映射关系
         // 匹配相似字段映射关系
-        mergeFieldMapping(tableGroup);
+        matchSimilarFieldMapping(tableGroup);
 
 
         // 合并配置
         // 合并配置
         mergeConfig(mapping, tableGroup);
         mergeConfig(mapping, tableGroup);
@@ -116,7 +119,7 @@ public class TableGroupChecker extends AbstractChecker {
      *
      *
      * @param tableGroup
      * @param tableGroup
      */
      */
-    public void refreshTableFields(TableGroup tableGroup){
+    public void refreshTableFields(TableGroup tableGroup) {
         Mapping mapping = manager.getMapping(tableGroup.getMappingId());
         Mapping mapping = manager.getMapping(tableGroup.getMappingId());
         Assert.notNull(mapping, "mapping can not be null.");
         Assert.notNull(mapping, "mapping can not be null.");
 
 
@@ -184,29 +187,52 @@ public class TableGroupChecker extends AbstractChecker {
         }
         }
     }
     }
 
 
-    private void mergeFieldMapping(TableGroup tableGroup) {
+    private void matchSimilarFieldMapping(TableGroup tableGroup) {
         List<Field> sCol = tableGroup.getSourceTable().getColumn();
         List<Field> sCol = tableGroup.getSourceTable().getColumn();
         List<Field> tCol = tableGroup.getTargetTable().getColumn();
         List<Field> tCol = tableGroup.getTargetTable().getColumn();
         if (CollectionUtils.isEmpty(sCol) || CollectionUtils.isEmpty(tCol)) {
         if (CollectionUtils.isEmpty(sCol) || CollectionUtils.isEmpty(tCol)) {
             return;
             return;
         }
         }
 
 
-        // Set集合去重
         Map<String, Field> m1 = new HashMap<>();
         Map<String, Field> m1 = new HashMap<>();
         Map<String, Field> m2 = new HashMap<>();
         Map<String, Field> m2 = new HashMap<>();
-        List<String> k1 = new LinkedList<>();
-        List<String> k2 = new LinkedList<>();
-        shuffleColumn(sCol, k1, m1);
-        shuffleColumn(tCol, k2, m2);
-        k1.retainAll(k2);
-
-        // 有相似字段
-        if (!CollectionUtils.isEmpty(k1)) {
-            k1.forEach(k -> tableGroup.getFieldMapping().add(new FieldMapping(m1.get(k), m2.get(k))));
+        Set<String> sourceFieldNames = new LinkedHashSet<>();
+        Set<String> targetFieldNames = new LinkedHashSet<>();
+        shuffleColumn(sCol, sourceFieldNames, m1);
+        shuffleColumn(tCol, targetFieldNames, m2);
+
+        // 模糊匹配相似字段
+        AtomicBoolean existSourcePKFieldMapping = new AtomicBoolean();
+        AtomicBoolean existTargetPKFieldMapping = new AtomicBoolean();
+        sourceFieldNames.forEach(s -> {
+            for (String t : targetFieldNames) {
+                if (StringUtil.equalsIgnoreCase(s, t)) {
+                    Field f1 = m1.get(s);
+                    Field f2 = m2.get(t);
+                    tableGroup.getFieldMapping().add(new FieldMapping(f1, f2));
+                    if (f1.isPk()) {
+                        existSourcePKFieldMapping.set(true);
+                    }
+                    if (f2.isPk()) {
+                        existTargetPKFieldMapping.set(true);
+                    }
+                    break;
+                }
+            }
+        });
+
+        // 沒有主键映射关系,取第一个主键作为映射关系
+        if (!existSourcePKFieldMapping.get() || !existTargetPKFieldMapping.get()) {
+            List<String> sourceTablePrimaryKeys = PrimaryKeyUtil.findTablePrimaryKeys(tableGroup.getSourceTable());
+            List<String> targetTablePrimaryKeys = PrimaryKeyUtil.findTablePrimaryKeys(tableGroup.getTargetTable());
+            Assert.isTrue(!CollectionUtils.isEmpty(sourceTablePrimaryKeys) && !CollectionUtils.isEmpty(targetTablePrimaryKeys), "数据源表和目标源表必须包含主键.");
+            String sPK = sourceTablePrimaryKeys.stream().findFirst().get();
+            String tPK = targetTablePrimaryKeys.stream().findFirst().get();
+            tableGroup.getFieldMapping().add(new FieldMapping(m1.get(sPK), m2.get(tPK)));
         }
         }
     }
     }
 
 
-    private void shuffleColumn(List<Field> col, List<String> key, Map<String, Field> map) {
+    private void shuffleColumn(List<Field> col, Set<String> key, Map<String, Field> map) {
         col.forEach(f -> {
         col.forEach(f -> {
             if (!key.contains(f.getName())) {
             if (!key.contains(f.getName())) {
                 key.add(f.getName());
                 key.add(f.getName());

+ 1 - 1
dbsyncer-cache/pom.xml

@@ -4,7 +4,7 @@
 	<parent>
 	<parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
 	<modelVersion>4.0.0</modelVersion>
 	<modelVersion>4.0.0</modelVersion>
 	<artifactId>dbsyncer-cache</artifactId>
 	<artifactId>dbsyncer-cache</artifactId>

+ 1 - 1
dbsyncer-cluster/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-cluster</artifactId>
     <artifactId>dbsyncer-cluster</artifactId>

+ 1 - 1
dbsyncer-common/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-common</artifactId>
     <artifactId>dbsyncer-common</artifactId>

+ 1 - 1
dbsyncer-connector/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-connector</artifactId>
     <artifactId>dbsyncer-connector</artifactId>

+ 3 - 7
dbsyncer-connector/src/main/java/org/dbsyncer/connector/schema/BitValueMapper.java

@@ -23,20 +23,16 @@ public class BitValueMapper extends AbstractValueMapper<byte[]> {
         if (val instanceof Integer) {
         if (val instanceof Integer) {
             ByteBuffer buffer = ByteBuffer.allocate(4);
             ByteBuffer buffer = ByteBuffer.allocate(4);
             buffer.putInt((Integer) val);
             buffer.putInt((Integer) val);
-            byte[] bytes = new byte[4];
-            buffer.get(bytes);
-            return bytes;
+            return buffer.array();
         }
         }
         if (val instanceof Boolean) {
         if (val instanceof Boolean) {
             Boolean b = (Boolean) val;
             Boolean b = (Boolean) val;
             ByteBuffer buffer = ByteBuffer.allocate(2);
             ByteBuffer buffer = ByteBuffer.allocate(2);
             buffer.putShort((short) (b ? 1 : 0));
             buffer.putShort((short) (b ? 1 : 0));
-            byte[] bytes = new byte[2];
-            buffer.get(bytes);
-            return bytes;
+            return buffer.array();
         }
         }
 
 
         throw new ConnectorException(String.format("%s can not find type [%s], val [%s]", getClass().getSimpleName(), val.getClass(), val));
         throw new ConnectorException(String.format("%s can not find type [%s], val [%s]", getClass().getSimpleName(), val.getClass(), val));
     }
     }
-    
+
 }
 }

+ 1 - 1
dbsyncer-listener/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-listener</artifactId>
     <artifactId>dbsyncer-listener</artifactId>

+ 1 - 1
dbsyncer-manager/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-manager</artifactId>
     <artifactId>dbsyncer-manager</artifactId>

+ 1 - 1
dbsyncer-monitor/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-monitor</artifactId>
     <artifactId>dbsyncer-monitor</artifactId>

+ 1 - 1
dbsyncer-parser/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-parser</artifactId>
     <artifactId>dbsyncer-parser</artifactId>

+ 1 - 1
dbsyncer-plugin/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-plugin</artifactId>
     <artifactId>dbsyncer-plugin</artifactId>

+ 1 - 1
dbsyncer-storage/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-storage</artifactId>
     <artifactId>dbsyncer-storage</artifactId>

+ 1 - 1
dbsyncer-web/pom.xml

@@ -5,7 +5,7 @@
     <parent>
     <parent>
         <artifactId>dbsyncer</artifactId>
         <artifactId>dbsyncer</artifactId>
         <groupId>org.ghi</groupId>
         <groupId>org.ghi</groupId>
-        <version>1.2.3-RC_0421</version>
+        <version>1.2.3-RC_0427</version>
     </parent>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>dbsyncer-web</artifactId>
     <artifactId>dbsyncer-web</artifactId>

+ 0 - 1
dbsyncer-web/src/main/resources/application.properties

@@ -1,4 +1,3 @@
-# see more: https://my.oschina.net/dbsyncer/blog/8652905
 # 服务IP
 # 服务IP
 server.ip=127.0.0.1
 server.ip=127.0.0.1
 # 访问端口
 # 访问端口

+ 1 - 1
pom.xml

@@ -6,7 +6,7 @@
 
 
     <groupId>org.ghi</groupId>
     <groupId>org.ghi</groupId>
     <artifactId>dbsyncer</artifactId>
     <artifactId>dbsyncer</artifactId>
-    <version>1.2.3-RC_0421</version>
+    <version>1.2.3-RC_0427</version>
     <packaging>pom</packaging>
     <packaging>pom</packaging>
     <name>dbsyncer</name>
     <name>dbsyncer</name>
     <url>https://gitee.com/ghi/dbsyncer</url>
     <url>https://gitee.com/ghi/dbsyncer</url>