|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.executor; |
| 18 | + |
| 19 | +import org.apache.seatunnel.api.table.catalog.TableSchema; |
| 20 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 21 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 22 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 23 | +import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; |
| 24 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorErrorCode; |
| 25 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.connection.JdbcConnectionProvider; |
| 27 | + |
| 28 | +import org.apache.commons.csv.CSVFormat; |
| 29 | +import org.apache.commons.csv.CSVPrinter; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.StringReader; |
| 33 | +import java.lang.reflect.InvocationTargetException; |
| 34 | +import java.math.BigDecimal; |
| 35 | +import java.sql.Connection; |
| 36 | +import java.sql.SQLException; |
| 37 | +import java.time.LocalDate; |
| 38 | +import java.time.LocalDateTime; |
| 39 | +import java.time.LocalTime; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +public class CopyManagerBatchStatementExecutor implements JdbcBatchStatementExecutor<SeaTunnelRow> { |
| 44 | + |
| 45 | + private final String copySql; |
| 46 | + private final TableSchema tableSchema; |
| 47 | + CopyManagerProxy copyManagerProxy; |
| 48 | + CSVFormat csvFormat = CSVFormat.POSTGRESQL_CSV; |
| 49 | + CSVPrinter csvPrinter; |
| 50 | + |
| 51 | + public CopyManagerBatchStatementExecutor(String copySql, TableSchema tableSchema) { |
| 52 | + this.copySql = copySql; |
| 53 | + this.tableSchema = tableSchema; |
| 54 | + } |
| 55 | + |
| 56 | + public static void copyManagerProxyChecked(JdbcConnectionProvider connectionProvider) { |
| 57 | + try (Connection connection = connectionProvider.getConnection()) { |
| 58 | + new CopyManagerProxy(connection); |
| 59 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 60 | + throw new JdbcConnectorException( |
| 61 | + JdbcConnectorErrorCode.NO_SUPPORT_OPERATION_FAILED, |
| 62 | + "unable to open CopyManager Operation in this JDBC writer. Please configure option use_copy_statement = false.", |
| 63 | + e); |
| 64 | + } catch (SQLException e) { |
| 65 | + throw new JdbcConnectorException( |
| 66 | + JdbcConnectorErrorCode.CREATE_DRIVER_FAILED, "unable to open JDBC writer", e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void prepareStatements(Connection connection) throws SQLException { |
| 72 | + try { |
| 73 | + this.copyManagerProxy = new CopyManagerProxy(connection); |
| 74 | + this.csvPrinter = new CSVPrinter(new StringBuilder(), csvFormat); |
| 75 | + } catch (NoSuchMethodException |
| 76 | + | IllegalAccessException |
| 77 | + | InvocationTargetException |
| 78 | + | IOException e) { |
| 79 | + throw new JdbcConnectorException( |
| 80 | + JdbcConnectorErrorCode.NO_SUPPORT_OPERATION_FAILED, |
| 81 | + "unable to open CopyManager Operation in this JDBC writer. Please configure option use_copy_statement = false.", |
| 82 | + e); |
| 83 | + } catch (SQLException e) { |
| 84 | + throw new JdbcConnectorException( |
| 85 | + JdbcConnectorErrorCode.CREATE_DRIVER_FAILED, "unable to open JDBC writer", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void addToBatch(SeaTunnelRow record) throws SQLException { |
| 91 | + try { |
| 92 | + this.csvPrinter.printRecord(toExtract(record)); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private List<Object> toExtract(SeaTunnelRow record) { |
| 99 | + SeaTunnelRowType rowType = tableSchema.toPhysicalRowDataType(); |
| 100 | + List<Object> csvRecord = new ArrayList<>(); |
| 101 | + for (int fieldIndex = 0; fieldIndex < rowType.getTotalFields(); fieldIndex++) { |
| 102 | + SeaTunnelDataType<?> seaTunnelDataType = rowType.getFieldType(fieldIndex); |
| 103 | + Object fieldValue = record.getField(fieldIndex); |
| 104 | + if (fieldValue == null) { |
| 105 | + csvRecord.add(null); |
| 106 | + continue; |
| 107 | + } |
| 108 | + switch (seaTunnelDataType.getSqlType()) { |
| 109 | + case STRING: |
| 110 | + csvRecord.add((String) record.getField(fieldIndex)); |
| 111 | + break; |
| 112 | + case BOOLEAN: |
| 113 | + csvRecord.add((Boolean) record.getField(fieldIndex)); |
| 114 | + break; |
| 115 | + case TINYINT: |
| 116 | + csvRecord.add((Byte) record.getField(fieldIndex)); |
| 117 | + break; |
| 118 | + case SMALLINT: |
| 119 | + csvRecord.add((Short) record.getField(fieldIndex)); |
| 120 | + break; |
| 121 | + case INT: |
| 122 | + csvRecord.add((Integer) record.getField(fieldIndex)); |
| 123 | + break; |
| 124 | + case BIGINT: |
| 125 | + csvRecord.add((Long) record.getField(fieldIndex)); |
| 126 | + break; |
| 127 | + case FLOAT: |
| 128 | + csvRecord.add((Float) record.getField(fieldIndex)); |
| 129 | + break; |
| 130 | + case DOUBLE: |
| 131 | + csvRecord.add((Double) record.getField(fieldIndex)); |
| 132 | + break; |
| 133 | + case DECIMAL: |
| 134 | + csvRecord.add((BigDecimal) record.getField(fieldIndex)); |
| 135 | + break; |
| 136 | + case DATE: |
| 137 | + LocalDate localDate = (LocalDate) record.getField(fieldIndex); |
| 138 | + csvRecord.add((java.sql.Date) java.sql.Date.valueOf(localDate)); |
| 139 | + break; |
| 140 | + case TIME: |
| 141 | + LocalTime localTime = (LocalTime) record.getField(fieldIndex); |
| 142 | + csvRecord.add((java.sql.Time) java.sql.Time.valueOf(localTime)); |
| 143 | + break; |
| 144 | + case TIMESTAMP: |
| 145 | + LocalDateTime localDateTime = (LocalDateTime) record.getField(fieldIndex); |
| 146 | + csvRecord.add((java.sql.Timestamp) java.sql.Timestamp.valueOf(localDateTime)); |
| 147 | + break; |
| 148 | + case BYTES: |
| 149 | + csvRecord.add( |
| 150 | + org.apache.commons.codec.binary.Base64.encodeBase64String( |
| 151 | + (byte[]) record.getField(fieldIndex))); |
| 152 | + break; |
| 153 | + case NULL: |
| 154 | + csvRecord.add(null); |
| 155 | + break; |
| 156 | + case MAP: |
| 157 | + case ARRAY: |
| 158 | + case ROW: |
| 159 | + default: |
| 160 | + throw new JdbcConnectorException( |
| 161 | + CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE, |
| 162 | + "Unexpected value: " + seaTunnelDataType); |
| 163 | + } |
| 164 | + } |
| 165 | + return csvRecord; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void executeBatch() throws SQLException { |
| 170 | + try { |
| 171 | + this.csvPrinter.flush(); |
| 172 | + this.copyManagerProxy.doCopy( |
| 173 | + copySql, new StringReader(this.csvPrinter.getOut().toString())); |
| 174 | + } catch (InvocationTargetException | IllegalAccessException | IOException e) { |
| 175 | + throw new JdbcConnectorException( |
| 176 | + CommonErrorCodeDeprecated.SQL_OPERATION_FAILED, "Sql command: " + copySql); |
| 177 | + } finally { |
| 178 | + try { |
| 179 | + this.csvPrinter.close(); |
| 180 | + this.csvPrinter = new CSVPrinter(new StringBuilder(), csvFormat); |
| 181 | + } catch (Exception ignore) { |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void closeStatements() throws SQLException { |
| 188 | + this.copyManagerProxy = null; |
| 189 | + try { |
| 190 | + this.csvPrinter.close(); |
| 191 | + this.csvPrinter = null; |
| 192 | + } catch (Exception ignore) { |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments