sandynz commented on code in PR #23906:
URL: https://github.com/apache/shardingsphere/pull/23906#discussion_r1097288563


##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/MySQLImporter.java:
##########
@@ -17,50 +17,26 @@
 
 package org.apache.shardingsphere.data.pipeline.cdc.client.importer;
 
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import 
org.apache.shardingsphere.data.pipeline.cdc.client.parameter.ImportDataSourceParameter;
-import 
org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder.SQLBuilder;
-import 
org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder.SQLBuilderFactory;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.util.Optional;
 
 /**
  * MySQL importer.
  */
-@RequiredArgsConstructor
 @Slf4j
 public final class MySQLImporter extends AbstractDataSourceImporter {

Review Comment:
   Could we just remove MySQLImporter and use AbstractDataSourceImporter? And 
also PostgreSQL and openGauss sub-classes



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/MySQLImporter.java:
##########
@@ -17,50 +17,26 @@
 
 package org.apache.shardingsphere.data.pipeline.cdc.client.importer;
 
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import 
org.apache.shardingsphere.data.pipeline.cdc.client.parameter.ImportDataSourceParameter;
-import 
org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder.SQLBuilder;
-import 
org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder.SQLBuilderFactory;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.util.Optional;
 
 /**
  * MySQL importer.
  */
-@RequiredArgsConstructor
 @Slf4j
 public final class MySQLImporter extends AbstractDataSourceImporter {
     
-    private final SQLBuilder sqlBuilder = 
SQLBuilderFactory.getSQLBuilder("MySQL");
-    
-    @Getter
-    private final Connection connection;
-    
     public MySQLImporter(final ImportDataSourceParameter dataSourceParameter) {
-        String url = 
Optional.ofNullable(dataSourceParameter.getUrl()).orElse("localhost");
-        String port = 
Optional.ofNullable(dataSourceParameter.getPort()).orElse(3306).toString();
-        String database = 
Optional.ofNullable(dataSourceParameter.getDatabase()).orElse("cdc_db");
-        String username = 
Optional.ofNullable(dataSourceParameter.getUsername()).orElse("test_user");
-        String password = 
Optional.ofNullable(dataSourceParameter.getPassword()).orElse("Root@123");
-        try {
-            connection = 
DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true",
 url, port, database), username, password);
-        } catch (final SQLException ex) {
-            throw new RuntimeException(ex);
-        }
+        super(dataSourceParameter);
     }
     
     @Override
-    protected SQLBuilder getSQLBuilder() {
-        return sqlBuilder;
+    protected String buildJdbcUrl(final String url) {
+        return 
String.format("jdbc:mysql://%s?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true",
 url);
     }

Review Comment:
   1, It's better not hard-coded jdbc url for user, let user to define the jdbc 
url, they might need to add more parameters.
   
   2, Parameter `url` is confusing, looks it includes host and port.
   



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/MySQLSQLBuilder.java:
##########
@@ -65,9 +62,19 @@ protected String getRightIdentifierQuoteString() {
     @Override
     public String buildInsertSQL(final Record record) {
         String insertSql = super.buildInsertSQL(record);
-        if (!record.getTableMetaData().getUniqueKeyNamesList().isEmpty()) {
-            return 
PATTERN_INSERT_TABLE.matcher(insertSql).replaceFirst("INSERT IGNORE TABLE ");
+        List<String> uniqueKeyNamesList = 
record.getTableMetaData().getUniqueKeyNamesList();
+        if (uniqueKeyNamesList.isEmpty()) {
+            return insertSql;
+        } else {

Review Comment:
   `else` is not required. And also in another 2 impls



##########
kernel/data-pipeline/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/MySQLSQLBuilderTest.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder;
+
+import com.google.protobuf.Any;
+import com.google.protobuf.Int32Value;
+import com.google.protobuf.StringValue;
+import 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.TableMetaData;
+import org.junit.Test;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class MySQLSQLBuilderTest {
+    
+    @Test
+    public void assertBuildInsertSQLWithUniqueKey() {
+        MySQLSQLBuilder sqlBuilder = new MySQLSQLBuilder();
+        TableMetaData tableMetaData = 
TableMetaData.newBuilder().setTableName("t_order").addUniqueKeyNames("order_id").setDatabase("cdc_db").build();
+        Record record = 
Record.newBuilder().setTableMetaData(tableMetaData).putAllAfter(buildAfterMap()).build();
+        String actualSql = sqlBuilder.buildInsertSQL(record);
+        String expectedSql = "INSERT INTO t_order(order_id,user_id,status) 
VALUES(?,?,?) ON DUPLICATE KEY UPDATE 
user_id=VALUES(user_id),status=VALUES(status)";
+        assertThat(actualSql, is(expectedSql));
+    }
+    
+    private Map<String, Any> buildAfterMap() {
+        Map<String, Any> result = new LinkedHashMap<>();
+        result.put("order_id", Any.pack(Int32Value.of(1)));
+        result.put("user_id", Any.pack(Int32Value.of(2)));
+        result.put("status", Any.pack(StringValue.of("OK")));
+        return result;
+    }
+    
+    @Test
+    public void assertBuildInsertSQLWithoutUniqueKey() {
+        PostgreSQLSQLBuilder sqlBuilder = new PostgreSQLSQLBuilder();

Review Comment:
   Looks it should be `MySQLSQLBuilder`, but not `PostgreSQLSQLBuilder`



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/OpenGaussSQLBuilder.java:
##########
@@ -58,9 +59,20 @@ protected String getRightIdentifierQuoteString() {
     @Override
     public String buildInsertSQL(final Record record) {
         String insertSql = super.buildInsertSQL(record);
-        if (!record.getTableMetaData().getUniqueKeyNamesList().isEmpty()) {
-            return insertSql + " ON DUPLICATE KEY UPDATE NOTHING";
+        List<String> uniqueKeyNamesList = 
record.getTableMetaData().getUniqueKeyNamesList();
+        if (uniqueKeyNamesList.isEmpty()) {
+            return insertSql;
+        } else {
+            StringBuilder updateValue = new StringBuilder();
+            for (String each : record.getAfterMap().keySet()) {
+                if (uniqueKeyNamesList.contains(each)) {
+                    continue;
+                }
+                
updateValue.append(quote(each)).append("=EXCLUDED.").append(quote(each)).append(",");
+            }
+            updateValue.setLength(updateValue.length() - 1);
+            String uniqueKeyNames = 
uniqueKeyNamesList.stream().map(this::quote).collect(Collectors.joining(","));
+            return insertSql + String.format(" ON CONFLICT (%s) DO UPDATE SET 
%s", uniqueKeyNames, updateValue);

Review Comment:
   Is openGauss on duplicate SQL grammar the same as PostgreSQL?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to