|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * * * or more contributor license agreements. See the NOTICE file |
| 6 | + * * * distributed with this work for additional information |
| 7 | + * * * regarding copyright ownership. The ASF licenses this file |
| 8 | + * * * to you under the Apache License, Version 2.0 (the |
| 9 | + * * * "License"); you may not use this file except in compliance |
| 10 | + * * * with the License. You may obtain a copy of the License at |
| 11 | + * * * |
| 12 | + * * * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * * * |
| 14 | + * * * Unless required by applicable law or agreed to in writing, software |
| 15 | + * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * * * See the License for the specific language governing permissions and |
| 18 | + * * * limitations under the License. |
| 19 | + * * |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +package com.dtstack.chunjun.connector.postgresql.converter; |
| 24 | + |
| 25 | +import com.dtstack.chunjun.conf.ChunJunCommonConf; |
| 26 | +import com.dtstack.chunjun.connector.jdbc.converter.JdbcColumnConverter; |
| 27 | +import com.dtstack.chunjun.connector.jdbc.statement.FieldNamedPreparedStatement; |
| 28 | +import com.dtstack.chunjun.converter.IDeserializationConverter; |
| 29 | +import com.dtstack.chunjun.converter.ISerializationConverter; |
| 30 | +import com.dtstack.chunjun.element.AbstractBaseColumn; |
| 31 | +import com.dtstack.chunjun.element.ColumnRowData; |
| 32 | +import com.dtstack.chunjun.element.column.ArrayColumn; |
| 33 | +import com.dtstack.chunjun.element.column.BigDecimalColumn; |
| 34 | +import com.dtstack.chunjun.element.column.BooleanColumn; |
| 35 | +import com.dtstack.chunjun.element.column.BytesColumn; |
| 36 | +import com.dtstack.chunjun.element.column.SqlDateColumn; |
| 37 | +import com.dtstack.chunjun.element.column.StringColumn; |
| 38 | +import com.dtstack.chunjun.element.column.TimeColumn; |
| 39 | +import com.dtstack.chunjun.element.column.TimestampColumn; |
| 40 | + |
| 41 | +import org.apache.flink.table.data.RowData; |
| 42 | +import org.apache.flink.table.types.logical.LogicalType; |
| 43 | +import org.apache.flink.table.types.logical.RowType; |
| 44 | +import org.apache.flink.table.types.logical.TimestampType; |
| 45 | +import org.apache.flink.table.types.logical.YearMonthIntervalType; |
| 46 | + |
| 47 | +import org.postgresql.core.BaseConnection; |
| 48 | +import org.postgresql.core.Oid; |
| 49 | +import org.postgresql.jdbc.PgArray; |
| 50 | + |
| 51 | +import java.math.BigDecimal; |
| 52 | +import java.sql.Array; |
| 53 | +import java.sql.Date; |
| 54 | +import java.sql.Time; |
| 55 | +import java.sql.Timestamp; |
| 56 | +import java.util.HashMap; |
| 57 | +import java.util.List; |
| 58 | +import java.util.Map; |
| 59 | + |
| 60 | +/** |
| 61 | + * Company:www.dtstack.com. |
| 62 | + * |
| 63 | + * @author shitou |
| 64 | + * @date 2022/4/14 |
| 65 | + */ |
| 66 | +public class PostgresqlColumnConverter extends JdbcColumnConverter { |
| 67 | + |
| 68 | + private List<String> fieldTypeList; |
| 69 | + private transient BaseConnection connection; |
| 70 | + private static final Map<String, Integer> arrayType = new HashMap<>(); |
| 71 | + |
| 72 | + public PostgresqlColumnConverter(RowType rowType, ChunJunCommonConf commonConf) { |
| 73 | + super(rowType, commonConf); |
| 74 | + } |
| 75 | + |
| 76 | + static { |
| 77 | + arrayType.put("_int4", Oid.INT4_ARRAY); |
| 78 | + arrayType.put("_int8", Oid.INT8_ARRAY); |
| 79 | + arrayType.put("_float4", Oid.FLOAT4_ARRAY); |
| 80 | + arrayType.put("_text", Oid.TEXT_ARRAY); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public FieldNamedPreparedStatement toExternal( |
| 85 | + RowData rowData, FieldNamedPreparedStatement statement) throws Exception { |
| 86 | + for (int index = 0; index < rowData.getArity(); index++) { |
| 87 | + if (arrayType.containsKey(fieldTypeList.get(index))) { |
| 88 | + // eg: {1000,1000,10001}、{{1000,1000,10001},{1,2,3}} |
| 89 | + String field = ((ColumnRowData) rowData).getField(index).asString(); |
| 90 | + Array array = |
| 91 | + new PgArray(connection, arrayType.get(fieldTypeList.get(index)), field); |
| 92 | + AbstractBaseColumn arrayColumn = new ArrayColumn(array); |
| 93 | + ((ColumnRowData) rowData).setField(index, arrayColumn); |
| 94 | + } |
| 95 | + toExternalConverters.get(index).serialize(rowData, index, statement); |
| 96 | + } |
| 97 | + return statement; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected IDeserializationConverter createInternalConverter(LogicalType type) { |
| 102 | + switch (type.getTypeRoot()) { |
| 103 | + case BOOLEAN: |
| 104 | + return val -> new BooleanColumn(Boolean.parseBoolean(val.toString())); |
| 105 | + case TINYINT: |
| 106 | + return val -> new BigDecimalColumn(((Integer) val).byteValue()); |
| 107 | + case SMALLINT: |
| 108 | + case INTEGER: |
| 109 | + return val -> new BigDecimalColumn((Integer) val); |
| 110 | + case INTERVAL_YEAR_MONTH: |
| 111 | + return (IDeserializationConverter<Object, AbstractBaseColumn>) |
| 112 | + val -> { |
| 113 | + YearMonthIntervalType yearMonthIntervalType = |
| 114 | + (YearMonthIntervalType) type; |
| 115 | + switch (yearMonthIntervalType.getResolution()) { |
| 116 | + case YEAR: |
| 117 | + return new BigDecimalColumn( |
| 118 | + Integer.parseInt(String.valueOf(val).substring(0, 4))); |
| 119 | + case MONTH: |
| 120 | + case YEAR_TO_MONTH: |
| 121 | + default: |
| 122 | + throw new UnsupportedOperationException( |
| 123 | + "jdbc converter only support YEAR"); |
| 124 | + } |
| 125 | + }; |
| 126 | + case FLOAT: |
| 127 | + return val -> new BigDecimalColumn((Float) val); |
| 128 | + case DOUBLE: |
| 129 | + return val -> new BigDecimalColumn((Double) val); |
| 130 | + case BIGINT: |
| 131 | + return val -> new BigDecimalColumn((Long) val); |
| 132 | + case DECIMAL: |
| 133 | + return val -> new BigDecimalColumn((BigDecimal) val); |
| 134 | + case CHAR: |
| 135 | + case VARCHAR: |
| 136 | + return val -> new StringColumn((String) val); |
| 137 | + case DATE: |
| 138 | + return val -> new SqlDateColumn((Date) val); |
| 139 | + case TIME_WITHOUT_TIME_ZONE: |
| 140 | + return val -> new TimeColumn((Time) val); |
| 141 | + case TIMESTAMP_WITH_TIME_ZONE: |
| 142 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 143 | + return (IDeserializationConverter<Object, AbstractBaseColumn>) |
| 144 | + val -> |
| 145 | + new TimestampColumn( |
| 146 | + (Timestamp) val, ((TimestampType) (type)).getPrecision()); |
| 147 | + |
| 148 | + case BINARY: |
| 149 | + case VARBINARY: |
| 150 | + return val -> new BytesColumn((byte[]) val); |
| 151 | + case ARRAY: |
| 152 | + // integer[] -> {1000,1000,10001} |
| 153 | + // integer[][]-> {{1000,1000,10001},{1,2,3}} |
| 154 | + return val -> new StringColumn(((Array) val).toString()); |
| 155 | + default: |
| 156 | + throw new UnsupportedOperationException("Unsupported type:" + type); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected ISerializationConverter<FieldNamedPreparedStatement> createExternalConverter( |
| 162 | + LogicalType type) { |
| 163 | + switch (type.getTypeRoot()) { |
| 164 | + case BOOLEAN: |
| 165 | + return (val, index, statement) -> |
| 166 | + statement.setBoolean( |
| 167 | + index, ((ColumnRowData) val).getField(index).asBoolean()); |
| 168 | + case TINYINT: |
| 169 | + return (val, index, statement) -> statement.setByte(index, val.getByte(index)); |
| 170 | + case SMALLINT: |
| 171 | + case INTEGER: |
| 172 | + case INTERVAL_YEAR_MONTH: |
| 173 | + return (val, index, statement) -> |
| 174 | + statement.setInt(index, ((ColumnRowData) val).getField(index).asYearInt()); |
| 175 | + case FLOAT: |
| 176 | + return (val, index, statement) -> |
| 177 | + statement.setFloat(index, ((ColumnRowData) val).getField(index).asFloat()); |
| 178 | + case DOUBLE: |
| 179 | + return (val, index, statement) -> |
| 180 | + statement.setDouble( |
| 181 | + index, ((ColumnRowData) val).getField(index).asDouble()); |
| 182 | + |
| 183 | + case BIGINT: |
| 184 | + return (val, index, statement) -> |
| 185 | + statement.setLong(index, ((ColumnRowData) val).getField(index).asLong()); |
| 186 | + case DECIMAL: |
| 187 | + return (val, index, statement) -> |
| 188 | + statement.setBigDecimal( |
| 189 | + index, ((ColumnRowData) val).getField(index).asBigDecimal()); |
| 190 | + case CHAR: |
| 191 | + case VARCHAR: |
| 192 | + return (val, index, statement) -> |
| 193 | + statement.setString( |
| 194 | + index, ((ColumnRowData) val).getField(index).asString()); |
| 195 | + case DATE: |
| 196 | + return (val, index, statement) -> |
| 197 | + statement.setDate(index, ((ColumnRowData) val).getField(index).asSqlDate()); |
| 198 | + case TIME_WITHOUT_TIME_ZONE: |
| 199 | + return (val, index, statement) -> |
| 200 | + statement.setTime(index, ((ColumnRowData) val).getField(index).asTime()); |
| 201 | + case TIMESTAMP_WITH_TIME_ZONE: |
| 202 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 203 | + return (val, index, statement) -> |
| 204 | + statement.setTimestamp( |
| 205 | + index, ((ColumnRowData) val).getField(index).asTimestamp()); |
| 206 | + |
| 207 | + case BINARY: |
| 208 | + case VARBINARY: |
| 209 | + return (val, index, statement) -> |
| 210 | + statement.setBytes(index, ((ColumnRowData) val).getField(index).asBytes()); |
| 211 | + case ARRAY: |
| 212 | + return (val, index, statement) -> |
| 213 | + statement.setArray( |
| 214 | + index, (Array) ((ColumnRowData) val).getField(index).getData()); |
| 215 | + default: |
| 216 | + throw new UnsupportedOperationException("Unsupported type:" + type); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + public void setFieldTypeList(List<String> fieldTypeList) { |
| 221 | + this.fieldTypeList = fieldTypeList; |
| 222 | + } |
| 223 | + |
| 224 | + public void setConnection(BaseConnection connection) { |
| 225 | + this.connection = connection; |
| 226 | + } |
| 227 | +} |
0 commit comments