瀏覽代碼

映射关系加入自定义主键!弊端:表多选改为了单选

芦明宝 2 年之前
父節點
當前提交
367ac4a4ee

+ 10 - 2
dbsyncer-biz/src/main/java/org/dbsyncer/biz/checker/impl/tablegroup/TableGroupChecker.java

@@ -49,6 +49,8 @@ public class TableGroupChecker extends AbstractChecker {
         String mappingId = params.get("mappingId");
         String sourceTable = params.get("sourceTable");
         String targetTable = params.get("targetTable");
+        String sourceTablePrimary = params.get("sourceTablePrimary");
+        String targetTablePrimary = params.get("targetTablePrimary");
         Assert.hasText(mappingId, "tableGroup mappingId is empty.");
         Assert.hasText(sourceTable, "tableGroup sourceTable is empty.");
         Assert.hasText(targetTable, "tableGroup targetTable is empty.");
@@ -64,8 +66,8 @@ public class TableGroupChecker extends AbstractChecker {
         tableGroup.setName(ConfigConstant.TABLE_GROUP);
         tableGroup.setType(ConfigConstant.TABLE_GROUP);
         tableGroup.setMappingId(mappingId);
-        tableGroup.setSourceTable(getTable(mapping.getSourceConnectorId(), sourceTable));
-        tableGroup.setTargetTable(getTable(mapping.getTargetConnectorId(), targetTable));
+        tableGroup.setSourceTable(getTable(mapping.getSourceConnectorId(), sourceTable, sourceTablePrimary));
+        tableGroup.setTargetTable(getTable(mapping.getTargetConnectorId(), targetTable, targetTablePrimary));
         tableGroup.setParams(new HashMap<>());
 
         // 修改基本配置
@@ -136,6 +138,12 @@ public class TableGroupChecker extends AbstractChecker {
         return new Table(tableName, metaInfo.getTableType(), metaInfo.getColumn());
     }
 
+    private Table getTable(String connectorId, String tableName, String primaryKey) {
+        MetaInfo metaInfo = manager.getMetaInfo(connectorId, tableName);
+        Assert.notNull(metaInfo, "无法获取连接器表信息.");
+        return new Table(tableName, primaryKey, metaInfo.getTableType(), metaInfo.getColumn());
+    }
+
     private void checkRepeatedTable(String mappingId, String sourceTable, String targetTable) {
         List<TableGroup> list = manager.getTableGroupAll(mappingId);
         if (!CollectionUtils.isEmpty(list)) {

+ 3 - 3
dbsyncer-connector/src/main/java/org/dbsyncer/connector/database/AbstractDatabaseConnector.java

@@ -211,7 +211,7 @@ public abstract class AbstractDatabaseConnector extends AbstractConnector implem
         map.put(delete, buildSql(delete, commandConfig, schema, null));
 
         // 获取查询数据行是否存在
-        String pk = findOriginalTablePrimaryKey(commandConfig, quotation);
+        String pk = StringUtil.isNotBlank(commandConfig.getTable().getPrimaryKey())?commandConfig.getTable().getPrimaryKey(): findOriginalTablePrimaryKey(commandConfig, quotation);
         StringBuilder queryCount = new StringBuilder().append("SELECT COUNT(1) FROM ").append(schema).append(quotation).append(
                 commandConfig.getTable().getName()).append(quotation).append(" WHERE ").append(pk).append(" = ?");
         String queryCountExist = ConnectorConstant.OPERTION_QUERY_COUNT_EXIST;
@@ -372,7 +372,7 @@ public abstract class AbstractDatabaseConnector extends AbstractConnector implem
      */
     protected String getQueryCountSql(CommandConfig commandConfig, String schema, String quotation, String queryFilterSql) {
         String table = commandConfig.getTable().getName();
-        String pk = findOriginalTablePrimaryKey(commandConfig, quotation);
+        String pk = StringUtil.isNotBlank(commandConfig.getTable().getPrimaryKey())?commandConfig.getTable().getPrimaryKey():findOriginalTablePrimaryKey(commandConfig, quotation);
         StringBuilder queryCount = new StringBuilder();
         queryCount.append("SELECT COUNT(1) FROM (SELECT 1 FROM ").append(schema).append(quotation).append(table).append(quotation);
         if (StringUtil.isNotBlank(queryFilterSql)) {
@@ -494,7 +494,7 @@ public abstract class AbstractDatabaseConnector extends AbstractConnector implem
             throw new ConnectorException("Table name can not be empty.");
         }
         if (StringUtil.isBlank(pk)) {
-            pk = findOriginalTablePrimaryKey(commandConfig, "");
+            pk = StringUtil.isNotBlank(commandConfig.getTable().getPrimaryKey())?commandConfig.getTable().getPrimaryKey():findOriginalTablePrimaryKey(commandConfig, "");
         }
 
         SqlBuilderConfig config = new SqlBuilderConfig(this, schema, tableName, pk, fields, queryFilterSQL, buildSqlWithQuotation());

+ 20 - 0
dbsyncer-connector/src/main/java/org/dbsyncer/connector/model/Table.java

@@ -21,6 +21,11 @@ public class Table {
      */
     private String type;
 
+    /**
+     * 主键
+     */
+    private String primaryKey;
+
     /**
      * 属性字段
      * 格式:[{"name":"ID","typeName":"INT","type":"4"},{"name":"NAME","typeName":"VARCHAR","type":"12"}]
@@ -49,6 +54,13 @@ public class Table {
         this.column = column;
     }
 
+    public Table(String name, String primaryKey, String type, List<Field> column) {
+        this.name = name;
+        this.primaryKey = primaryKey;
+        this.type = type;
+        this.column = column;
+    }
+
     public String getName() {
         return name;
     }
@@ -67,6 +79,14 @@ public class Table {
         return this;
     }
 
+    public String getPrimaryKey() {
+        return primaryKey;
+    }
+
+    public void setPrimaryKey(String primaryKey) {
+        this.primaryKey = primaryKey;
+    }
+
     public List<Field> getColumn() {
         return column;
     }

+ 1 - 1
dbsyncer-manager/src/main/java/org/dbsyncer/manager/puller/IncrementPuller.java

@@ -143,7 +143,7 @@ public class IncrementPuller extends AbstractPuller implements ScheduledTaskJob
             AbstractQuartzExtractor extractor = listener.getExtractor(ListenerTypeEnum.TIMING, connectorConfig.getConnectorType(), AbstractQuartzExtractor.class);
             extractor.setCommands(list.stream().map(t -> {
                 Picker picker = new Picker(t.getFieldMapping());
-                return new TableGroupCommand(picker.getSourcePrimaryKeyName(connectorConfig), t.getCommand());
+                return new TableGroupCommand(picker.getSourcePrimaryKeyName(t), t.getCommand());
             }).collect(Collectors.toList()));
             setExtractorConfig(extractor, connectorConfig, listenerConfig, meta.getSnapshot(), new QuartzListener(mapping, list));
             return extractor;

+ 3 - 3
dbsyncer-parser/src/main/java/org/dbsyncer/parser/ParserFactory.java

@@ -146,8 +146,8 @@ public class ParserFactory implements Parser {
         AbstractConnectorConfig tConnConfig = getConnectorConfig(mapping.getTargetConnectorId());
         Table sourceTable = tableGroup.getSourceTable();
         Table targetTable = tableGroup.getTargetTable();
-        Table sTable = new Table(sourceTable.getName(), sourceTable.getType(), new ArrayList<>());
-        Table tTable = new Table(targetTable.getName(), targetTable.getType(), new ArrayList<>());
+        Table sTable = new Table(sourceTable.getName(), sourceTable.getPrimaryKey(), sourceTable.getType(), new ArrayList<>());
+        Table tTable = new Table(targetTable.getName(), targetTable.getPrimaryKey(), targetTable.getType(), new ArrayList<>());
         List<FieldMapping> fieldMapping = tableGroup.getFieldMapping();
         if (!CollectionUtils.isEmpty(fieldMapping)) {
             fieldMapping.forEach(m -> {
@@ -246,7 +246,7 @@ public class ParserFactory implements Parser {
         Assert.notEmpty(fieldMapping, String.format("数据源表[%s]同步到目标源表[%s], 映射关系不能为空.", sTableName, tTableName));
         // 获取同步字段
         Picker picker = new Picker(fieldMapping);
-        String pk = picker.getSourcePrimaryKeyName(sConfig);
+        String pk = picker.getSourcePrimaryKeyName(tableGroup);
 
         int pageSize = mapping.getReadNum();
         int batchSize = mapping.getBatchNum();

+ 2 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/model/Picker.java

@@ -57,14 +57,14 @@ public class Picker {
         }
     }
 
-    public String getSourcePrimaryKeyName(AbstractConnectorConfig config) {
+    public String getSourcePrimaryKeyName(TableGroup tableGroup) {
         for (Field f : sourceFields) {
             if (null != f && f.isPk()) {
                 return f.getName();
             }
         }
 
-        String primaryKey = config.getPrimaryKey();
+        String primaryKey = tableGroup.getSourceTable().getPrimaryKey();
         Assert.hasText(primaryKey, "主键为空");
         return primaryKey;
     }

+ 24 - 2
dbsyncer-web/src/main/resources/public/mapping/editTable.html

@@ -8,7 +8,7 @@
         <div class="col-md-5">
             <label class="col-sm-3 control-label text-right">数据源表</label>
             <div class="col-sm-9">
-                <select id="sourceTable" class="form-control select-control-table" multiple="multiple">
+                <select id="sourceTable" class="form-control select-control-table">
                     <option th:each="t,s:${mapping?.sourceConnector?.table}" th:text="${t?.name} + ' ('+${t?.type}+')'"
                             th:value="${t?.name}"/>
                 </select>
@@ -23,7 +23,7 @@
             <div class="form-group">
                 <label class="col-sm-3 control-label text-right">目标源表</label>
                 <div class="col-sm-9">
-                    <select id="targetTable" class="form-control select-control-table" multiple="multiple">
+                    <select id="targetTable" class="form-control select-control-table">
                         <option th:each="t,s:${mapping?.targetConnector?.table}"
                                 th:text="${t?.name} + ' ('+${t?.type}+')'" th:value="${t?.name}"/>
                     </select>
@@ -31,6 +31,28 @@
             </div>
         </div>
     </div>
+    <div class="row">
+        <!-- 数据源配置 -->
+        <div class="col-md-5">
+            <label class="col-sm-3 control-label text-right">源表主键</label>
+            <div class="col-sm-9">
+                <input id="sourceTablePrimary" class="form-control" type="text" />
+            </div>
+        </div>
+        <!-- 中间图标 -->
+        <div class="col-md-2 text-center">
+            <span class="fa fa-angle-double-right fa-2x"></span>
+        </div>
+        <!-- 目标源配置 -->
+        <div class="col-md-5">
+            <div class="form-group">
+                <label class="col-sm-3 control-label text-right">目标表主键</label>
+                <div class="col-sm-9">
+                    <input id="targetTablePrimary" class="form-control" type="text" />
+                </div>
+            </div>
+        </div>
+    </div>
 
     <div class="form-group">
         <div class="row">

+ 22 - 18
dbsyncer-web/src/main/resources/static/js/mapping/edit.js

@@ -128,32 +128,36 @@ function bindMappingTableGroupAddClick($sourceSelect, $targetSelect) {
     $addBtn.bind('click', function () {
         var m = {};
         m.mappingId = $(this).attr("mappingId");
-        m.sourceTable = $sourceSelect.selectpicker('val');
-        m.targetTable = $targetSelect.selectpicker('val');
-        if(undefined == m.sourceTable){
+
+        m.sourceTablePrimary = $("#sourceTablePrimary").val();
+        m.targetTablePrimary = $("#targetTablePrimary").val();
+
+        m.sourceTable = $sourceSelect.val();
+        m.targetTable = $targetSelect.val();
+        if(undefined == m.sourceTable || "" == m.sourceTable){
             bootGrowl("请选择数据源表", "danger");
             return;
         }
-        if(undefined == m.targetTable){
+        if(undefined == m.targetTable || "" == m.targetTable){
             bootGrowl("请选择目标源表", "danger");
             return;
         }
 
         // 如果存在多个选择,只筛选相似表
-        var sLen = m.sourceTable.length;
-        var tLen = m.targetTable.length;
-        if (1 < sLen || 1 < tLen) {
-            var mark = [];
-            for (j = 0; j < sLen; j++) {
-                if (-1 != m.targetTable.indexOf(m.sourceTable[j])) {
-                    mark.push(m.sourceTable[j]);
-                }
-            }
-            m.sourceTable = mark;
-            m.targetTable = mark;
-        }
-        m.sourceTable = m.sourceTable.join('|');
-        m.targetTable = m.targetTable.join('|');
+        // var sLen = m.sourceTable.length;
+        // var tLen = m.targetTable.length;
+        // if (1 < sLen || 1 < tLen) {
+        //     var mark = [];
+        //     for (j = 0; j < sLen; j++) {
+        //         if (-1 != m.targetTable.indexOf(m.sourceTable[j])) {
+        //             mark.push(m.sourceTable[j]);
+        //         }
+        //     }
+        //     m.sourceTable = mark;
+        //     m.targetTable = mark;
+        // }
+        // m.sourceTable = m.sourceTable.join('|');
+        // m.targetTable = m.targetTable.join('|');
 
         doPoster("/tableGroup/add", m, function (data) {
             if (data.success == true) {