|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.sink.polardb; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.sink.rdb.dialect.JDBCDialect; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +/** |
| 28 | + * Date: 2020/1/15 |
| 29 | + * Company: www.dtstack.com |
| 30 | + * @author maqi |
| 31 | + */ |
| 32 | +public class PolardbDialect implements JDBCDialect { |
| 33 | + |
| 34 | + @Override |
| 35 | + public boolean canHandle(String url) { |
| 36 | + return url.startsWith("jdbc:mysql:"); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Optional<String> defaultDriverName() { |
| 41 | + return Optional.of("com.mysql.cj.jdbc.Driver"); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String quoteIdentifier(String identifier) { |
| 46 | + return "`" + identifier + "`"; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 根据ALLReplace参数,选择使用replace语句还是ON DUPLICATE KEY UPDATE 语句 |
| 51 | + * @param tableName |
| 52 | + * @param fieldNames |
| 53 | + * @param uniqueKeyFields |
| 54 | + * @param allReplace |
| 55 | + * @return |
| 56 | + */ |
| 57 | + @Override |
| 58 | + public Optional<String> getUpsertStatement(String schema, String tableName, String[] fieldNames, String[] uniqueKeyFields, boolean allReplace) { |
| 59 | + return allReplace ? buildReplaceIntoStatement(tableName, fieldNames) : buildDuplicateUpsertStatement(tableName, fieldNames); |
| 60 | + } |
| 61 | + |
| 62 | + public Optional<String> buildDuplicateUpsertStatement(String tableName, String[] fieldNames) { |
| 63 | + String updateClause = Arrays.stream(fieldNames).map(f -> quoteIdentifier(f) + "=IFNULL(VALUES(" + quoteIdentifier(f) + ")," + quoteIdentifier(f) + ")") |
| 64 | + .collect(Collectors.joining(", ")); |
| 65 | + return Optional.of(getInsertIntoStatement("", tableName, fieldNames) + |
| 66 | + " ON DUPLICATE KEY UPDATE " + updateClause |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public Optional<String> buildReplaceIntoStatement(String tableName, String[] fieldNames) { |
| 71 | + String columns = Arrays.stream(fieldNames) |
| 72 | + .map(this::quoteIdentifier) |
| 73 | + .collect(Collectors.joining(", ")); |
| 74 | + String placeholders = Arrays.stream(fieldNames) |
| 75 | + .map(f -> "?") |
| 76 | + .collect(Collectors.joining(", ")); |
| 77 | + return Optional.of("REPLACE INTO " + quoteIdentifier(tableName) + |
| 78 | + "(" + columns + ")" + " VALUES (" + placeholders + ")"); |
| 79 | + } |
| 80 | +} |
0 commit comments