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


##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/ImporterFactory.java:
##########
@@ -26,15 +28,20 @@ public final class ImporterFactory {
      * Get importer.
      *
      * @param databaseType database type
+     * @param dataSourceParameter data source parameter
      * @return importer
      */
     // TODO use SPI

Review Comment:
   `TODO` could be removed



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/MySQLImporter.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.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);
+        }
+    }
+    
+    @Override
+    protected SQLBuilder getSQLBuilder() {
+        return sqlBuilder;
+    }
+    
+    @Override
+    public void close() throws Exception {
+        connection.close();
+    }

Review Comment:
   Could getSQLBuilder() and close() be extracted to AbstractDataSourceImporter?



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/PostgreSQLSQLBuilder.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * PostgreSQL SQL builder.
+ */
+public final class PostgreSQLSQLBuilder extends AbstractSQLBuilder {
+    
+    private static final List<String> RESERVED_KEYWORDS = Arrays.asList("ALL", 
"ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY", "AS", "ASC", "ASYMMETRIC", 
"AUTHORIZATION", "BETWEEN", "BIGINT", "BINARY",
+            "BIT", "BOOLEAN", "BOTH", "CASE", "CAST", "CHAR", "CHARACTER", 
"CHECK", "COALESCE", "COLLATE", "COLLATION", "COLUMN", "CONCURRENTLY", 
"CONSTRAINT", "CREATE", "CROSS", "CURRENT_CATALOG",
+            "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_SCHEMA", "CURRENT_TIME", 
"CURRENT_TIMESTAMP", "CURRENT_USER", "DEC", "DECIMAL", "DEFAULT", "DEFERRABLE", 
"DESC", "DISTINCT", "DO", "ELSE", "END",
+            "EXCEPT", "EXISTS", "EXTRACT", "FALSE", "FETCH", "FLOAT", "FOR", 
"FOREIGN", "FREEZE", "FROM", "FULL", "GRANT", "GREATEST", "GROUP", "GROUPING", 
"HAVING", "ILIKE", "IN", "INITIALLY",
+            "INNER", "INOUT", "INT", "INTEGER", "INTERSECT", "INTERVAL", 
"INTO", "IS", "ISNULL", "JOIN", "LATERAL", "LEADING", "LEAST", "LEFT", "LIKE", 
"LIMIT", "LOCALTIME", "LOCALTIMESTAMP",
+            "NATIONAL", "NATURAL", "NCHAR", "NONE", "NORMALIZE", "NOT", 
"NOTNULL", "NULL", "NULLIF", "NUMERIC", "OFFSET", "ON", "ONLY", "OR", "ORDER", 
"OUT", "OUTER", "OVERLAPS", "OVERLAY", "PLACING",
+            "POSITION", "PRECISION", "PRIMARY", "REAL", "REFERENCES", 
"RETURNING", "RIGHT", "ROW", "SELECT", "SESSION_USER", "SETOF", "SIMILAR", 
"SMALLINT", "SOME", "SUBSTRING", "SYMMETRIC", "TABLE",
+            "TABLESAMPLE", "THEN", "TIME", "TIMESTAMP", "TO", "TRAILING", 
"TREAT", "TRIM", "TRUE", "UNION", "UNIQUE", "USER", "USING", "VALUES", 
"VARCHAR", "VARIADIC", "VERBOSE", "WHEN", "WHERE",
+            "WINDOW", "WITH", "XMLATTRIBUTES", "XMLCONCAT", "XMLELEMENT", 
"XMLEXISTS", "XMLFOREST", "XMLNAMESPACES", "XMLPARSE", "XMLPI", "XMLROOT", 
"XMLSERIALIZE", "XMLTABLE");
+    
+    @Override
+    protected boolean isKeyword(final String item) {
+        return RESERVED_KEYWORDS.contains(item.toUpperCase());
+    }
+    
+    @Override
+    protected String getLeftIdentifierQuoteString() {
+        return "`";
+    }
+    
+    @Override
+    protected String getRightIdentifierQuoteString() {
+        return "`";
+    }
+    
+    @Override
+    public String buildInsertSQL(final Record record) {
+        String insertSql = super.buildInsertSQL(record);
+        if (!record.getTableMetaData().getUniqueKeyNamesList().isEmpty()) {
+            return insertSql + " ON DUPLICATE KEY UPDATE NOTHING";

Review Comment:
   It's better to overwrite old record, but not `ON DUPLICATE KEY UPDATE 
NOTHING`



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/MySQLImporter.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.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");

Review Comment:
   It's better not use default value, in case of when some parameters missing 
and it doesn't notify error



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/PostgreSQLImporter.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.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;
+
+/**
+ * PostgreSQL importer.
+ */
+@RequiredArgsConstructor
+@Slf4j
+public final class PostgreSQLImporter extends AbstractDataSourceImporter {
+    
+    private final SQLBuilder sqlBuilder = 
SQLBuilderFactory.getSQLBuilder("MySQL");

Review Comment:
   Looks `MySQL` should be `PostgreSQL`



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/parameter/ImportDataSourceParameter.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.parameter;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * Import data source parameter.
+ */
+@Getter
+@Setter
+public final class ImportDataSourceParameter {
+    
+    private String url;
+    
+    private Integer port;
+    
+    private String database;
+    
+    private String username;
+    
+    private String password;

Review Comment:
   1, It's better to use `final` for fields.
   
   2, It's better to simplify `url`, `port` and `database` to url field, just 
like DriverManager.
   



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/AbstractDataSourceImporter.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.importer;
+
+import com.google.protobuf.Any;
+import com.google.protobuf.ProtocolStringList;
+import lombok.extern.slf4j.Slf4j;
+import 
org.apache.shardingsphere.data.pipeline.cdc.client.sqlbuilder.SQLBuilder;
+import org.apache.shardingsphere.data.pipeline.cdc.client.util.AnyValueConvert;
+import 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+@Slf4j
+public abstract class AbstractDataSourceImporter implements Importer {

Review Comment:
   Class javadoc is required



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/importer/MySQLImporter.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.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) {

Review Comment:
   Could base code be extracted to AbstractDataSourceImporter?



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/parameter/StartCDCClientParameter.java:
##########
@@ -50,4 +50,6 @@ public final class StartCDCClientParameter {
     private SubscriptionMode subscriptionMode = SubscriptionMode.INCREMENTAL;
     
     private boolean incrementalGlobalOrderly;
+    
+    private ImportDataSourceParameter importDataSourceParameter;

Review Comment:
   Could fields be `final`?



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/PostgreSQLSQLBuilder.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * PostgreSQL SQL builder.
+ */
+public final class PostgreSQLSQLBuilder extends AbstractSQLBuilder {
+    
+    private static final List<String> RESERVED_KEYWORDS = Arrays.asList("ALL", 
"ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY", "AS", "ASC", "ASYMMETRIC", 
"AUTHORIZATION", "BETWEEN", "BIGINT", "BINARY",
+            "BIT", "BOOLEAN", "BOTH", "CASE", "CAST", "CHAR", "CHARACTER", 
"CHECK", "COALESCE", "COLLATE", "COLLATION", "COLUMN", "CONCURRENTLY", 
"CONSTRAINT", "CREATE", "CROSS", "CURRENT_CATALOG",
+            "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_SCHEMA", "CURRENT_TIME", 
"CURRENT_TIMESTAMP", "CURRENT_USER", "DEC", "DECIMAL", "DEFAULT", "DEFERRABLE", 
"DESC", "DISTINCT", "DO", "ELSE", "END",
+            "EXCEPT", "EXISTS", "EXTRACT", "FALSE", "FETCH", "FLOAT", "FOR", 
"FOREIGN", "FREEZE", "FROM", "FULL", "GRANT", "GREATEST", "GROUP", "GROUPING", 
"HAVING", "ILIKE", "IN", "INITIALLY",
+            "INNER", "INOUT", "INT", "INTEGER", "INTERSECT", "INTERVAL", 
"INTO", "IS", "ISNULL", "JOIN", "LATERAL", "LEADING", "LEAST", "LEFT", "LIKE", 
"LIMIT", "LOCALTIME", "LOCALTIMESTAMP",
+            "NATIONAL", "NATURAL", "NCHAR", "NONE", "NORMALIZE", "NOT", 
"NOTNULL", "NULL", "NULLIF", "NUMERIC", "OFFSET", "ON", "ONLY", "OR", "ORDER", 
"OUT", "OUTER", "OVERLAPS", "OVERLAY", "PLACING",
+            "POSITION", "PRECISION", "PRIMARY", "REAL", "REFERENCES", 
"RETURNING", "RIGHT", "ROW", "SELECT", "SESSION_USER", "SETOF", "SIMILAR", 
"SMALLINT", "SOME", "SUBSTRING", "SYMMETRIC", "TABLE",
+            "TABLESAMPLE", "THEN", "TIME", "TIMESTAMP", "TO", "TRAILING", 
"TREAT", "TRIM", "TRUE", "UNION", "UNIQUE", "USER", "USING", "VALUES", 
"VARCHAR", "VARIADIC", "VERBOSE", "WHEN", "WHERE",
+            "WINDOW", "WITH", "XMLATTRIBUTES", "XMLCONCAT", "XMLELEMENT", 
"XMLEXISTS", "XMLFOREST", "XMLNAMESPACES", "XMLPARSE", "XMLPI", "XMLROOT", 
"XMLSERIALIZE", "XMLTABLE");
+    
+    @Override
+    protected boolean isKeyword(final String item) {
+        return RESERVED_KEYWORDS.contains(item.toUpperCase());
+    }
+    
+    @Override
+    protected String getLeftIdentifierQuoteString() {
+        return "`";
+    }
+    
+    @Override
+    protected String getRightIdentifierQuoteString() {
+        return "`";
+    }

Review Comment:
   Is PostgreSQL quoted char ` ?



##########
kernel/data-pipeline/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/sqlbuilder/MySQLSQLBuilder.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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 
org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * MySQL SQL builder.
+ */
+public final class MySQLSQLBuilder extends AbstractSQLBuilder {
+    
+    private static final List<String> RESERVED_KEYWORDS = Arrays.asList("ADD", 
"ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "BEFORE", "BETWEEN", "BIGINT", 
"BINARY", "BLOB", "BOTH", "BY", "CALL",
+            "CASCADE", "CASE", "CHANGE", "CHAR", "CHARACTER", "CHECK", 
"COLLATE", "COLUMN", "CONDITION", "CONSTRAINT", "CONTINUE", "CONVERT", 
"CREATE", "CROSS", "CUBE", "CUME_DIST", "CURRENT_DATE",
+            "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR", 
"DATABASE", "DATABASES", "DAY_HOUR", "DAY_MICROSECOND", "DAY_MINUTE", 
"DAY_SECOND", "DEC", "DECIMAL", "DECLARE", "DEFAULT",
+            "DELAYED", "DELETE", "DENSE_RANK", "DESC", "DESCRIBE", 
"DETERMINISTIC", "DISTINCT", "DISTINCTROW", "DIV", "DOUBLE", "DROP", "DUAL", 
"ELSE", "ELSEIF", "ENCLOSED", "ESCAPED", "EACH", "ELSE",
+            "ELSEIF", "EMPTY", "ENCLOSED", "ESCAPED", "EXCEPT", "EXISTS", 
"EXIT", "EXPLAIN", "FALSE", "FETCH", "FIRST_VALUE", "FLOAT4", "FLOAT8", "FOR", 
"FORCE", "FOREIGN", "FROM", "FULLTEXT",
+            "FUNCTION", "GENERATED", "GET", "GRANT", "GROUP", "GROUPING", 
"GROUPS", "HAVING", "HIGH_PRIORITY", "HOUR_MICROSECOND", "HOUR_MINUTE", 
"HOUR_SECOND", "IF", "IGNORE", "IN", "INDEX",
+            "INFILE", "INNER", "INOUT", "INSENSITIVE", "INSERT", "INT", 
"INT1", "INT2", "INT3", "INT4", "INT8", "INTEGER", "INTERSECT", "INTERVAL", 
"INTO", "IO_AFTER_GTIDS", "IO_BEFORE_GTIDS", "IS",
+            "ITERATE", "JOIN", "JSON_TABLE", "KEY", "KEYS", "KILL", "LAG", 
"LAST_VALUE", "LATERAL", "LEAD", "LEADING", "LEAVE", "LEFT", "LIKE", "LIMIT", 
"LINES", "LOAD", "LOCALTIME", "LOCALTIMESTAMP",
+            "LOCK", "LONG", "LONGBLOB", "LONGTEXT", "LOOP", "LOW_PRIORITY", 
"MASTER_BIND", "MASTER_SSL_VERIFY_SERVER_CERT", "MATCH", "MAXVALUE", 
"MEDIUMBLOB", "MEDIUMINT", "MEDIUMTEXT", "MIDDLEINT",
+            "MINUTE_MICROSECOND", "MINUTE_SECOND", "MOD", "MODIFIES", 
"NATURAL", "NOT", "NO_WRITE_TO_BINLOG", "NTH_VALUE", "NTILE", "NULL", 
"NUMERIC", "OF", "ON", "OPTIMIZE", "OPTIMIZER_COSTS",
+            "OPTION", "OPTIONALLY", "OR", "ORDER", "OUT", "OUTER", "OUTFILE", 
"OVER", "PARTITION", "PERCENT_RANK", "PRECISION", "PRIMARY", "PROCEDURE", 
"PURGE", "RANK", "READ", "REAL", "RECURSIVE",
+            "REFERENCES", "REGEXP", "RELEASE", "RENAME", "REPEAT", "REPLACE", 
"REQUIRE", "RESIGNAL", "RESTRICT", "RETURN", "REVOKE", "RIGHT", "RLIKE", "ROW", 
"ROWS", "ROW_NUMBER", "SCHEMA", "SCHEMAS",
+            "SELECT", "SENSITIVE", "SEPARATOR", "SET", "SHOW", "SIGNAL", 
"SMALLINT", "SPATIAL", "SPECIFIC", "SQL", "SQLEXCEPTION", "SQLSTATE", 
"SQLWARNING", "SQL_BIG_RESULT", "SQL_CALC_FOUND_ROWS",
+            "SQL_SMALL_RESULT", "SSL", "STARTING", "STORED", "STRAIGHT_JOIN", 
"SYSTEM", "TABLE", "TERMINATED", "THEN", "TINYBLOB", "TINYINT", "TINYTEXT", 
"TO", "TRAILING", "TRIGGER", "TRUE", "UNDO",
+            "UNION", "UNIQUE", "UNLOCK", "UNSIGNED", "UPDATE", "USAGE", "USE", 
"USING", "UTC_DATE", "UTC_TIME", "UTC_TIMESTAMP", "VALUES", "VARBINARY", 
"VARCHAR", "VARCHARACTER", "VARYING", "VIRTUAL",
+            "WHEN", "WHERE", "WHILE", "WINDOW", "WITH", "WRITE", "XOR", 
"YEAR_MONTH", "ZEROFILL");
+    
+    private static final Pattern PATTERN_INSERT_TABLE = 
Pattern.compile("INSERT\\s+TABLE\\s+", Pattern.CASE_INSENSITIVE);
+    
+    @Override
+    protected boolean isKeyword(final String item) {
+        return RESERVED_KEYWORDS.contains(item.toUpperCase());
+    }
+    
+    @Override
+    protected String getLeftIdentifierQuoteString() {
+        return "`";
+    }
+    
+    @Override
+    protected String getRightIdentifierQuoteString() {
+        return "`";
+    }
+    
+    @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 ");

Review Comment:
   Why use `INSERT IGNORE TABLE` here



-- 
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