|
| 1 | +package com.dtstack.chunjun.connector.oceanbase.converter; |
| 2 | + |
| 3 | +import com.dtstack.chunjun.config.CommonConfig; |
| 4 | +import com.dtstack.chunjun.connector.oracle.converter.BlobType; |
| 5 | +import com.dtstack.chunjun.connector.oracle.converter.ClobType; |
| 6 | +import com.dtstack.chunjun.connector.oracle.converter.ConvertUtil; |
| 7 | +import com.dtstack.chunjun.connector.oracle.converter.OracleSyncConverter; |
| 8 | +import com.dtstack.chunjun.converter.IDeserializationConverter; |
| 9 | +import com.dtstack.chunjun.element.column.BytesColumn; |
| 10 | +import com.dtstack.chunjun.element.column.StringColumn; |
| 11 | +import com.dtstack.chunjun.element.column.TimestampColumn; |
| 12 | +import com.dtstack.chunjun.element.column.ZonedTimestampColumn; |
| 13 | + |
| 14 | +import org.apache.flink.table.types.logical.LocalZonedTimestampType; |
| 15 | +import org.apache.flink.table.types.logical.LogicalType; |
| 16 | +import org.apache.flink.table.types.logical.RowType; |
| 17 | +import org.apache.flink.table.types.logical.TimestampType; |
| 18 | +import org.apache.flink.table.types.logical.ZonedTimestampType; |
| 19 | + |
| 20 | +import com.oceanbase.jdbc.Blob; |
| 21 | +import com.oceanbase.jdbc.Clob; |
| 22 | +import com.oceanbase.jdbc.extend.datatype.DataTypeUtilities; |
| 23 | +import com.oceanbase.jdbc.extend.datatype.TIMESTAMPLTZ; |
| 24 | +import com.oceanbase.jdbc.extend.datatype.TIMESTAMPTZ; |
| 25 | + |
| 26 | +import java.sql.Timestamp; |
| 27 | +import java.util.TimeZone; |
| 28 | + |
| 29 | +public class OceanbaseOracleSyncConverter extends OracleSyncConverter { |
| 30 | + |
| 31 | + public OceanbaseOracleSyncConverter(RowType rowType, CommonConfig commonConfig) { |
| 32 | + super(rowType, commonConfig); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected IDeserializationConverter createInternalConverter(LogicalType type) { |
| 37 | + switch (type.getTypeRoot()) { |
| 38 | + case VARCHAR: |
| 39 | + if (type instanceof ClobType) { |
| 40 | + return val -> { |
| 41 | + Clob clob = (Clob) val; |
| 42 | + return new StringColumn(ConvertUtil.convertClob(clob)); |
| 43 | + }; |
| 44 | + } |
| 45 | + return val -> new StringColumn(val.toString()); |
| 46 | + case VARBINARY: |
| 47 | + return val -> { |
| 48 | + if (type instanceof BlobType) { |
| 49 | + Blob blob = (Blob) val; |
| 50 | + byte[] bytes = blob.getBytes(1, (int) blob.length()); |
| 51 | + return new BytesColumn(bytes); |
| 52 | + } else { |
| 53 | + return new BytesColumn((byte[]) val); |
| 54 | + } |
| 55 | + }; |
| 56 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 57 | + final int precision = ((TimestampType) type).getPrecision(); |
| 58 | + if (precision == 6) { |
| 59 | + return val -> new TimestampColumn((Timestamp) val, 0); // java.sql.Timestamp |
| 60 | + } |
| 61 | + case TIMESTAMP_WITH_TIME_ZONE: |
| 62 | + if (type instanceof ZonedTimestampType) { |
| 63 | + final int zonedPrecision = ((ZonedTimestampType) type).getPrecision(); |
| 64 | + return val -> { |
| 65 | + TIMESTAMPTZ timestamptz = (TIMESTAMPTZ) val; |
| 66 | + Timestamp timestamp = timestamptz.timestampValue(); |
| 67 | + return new ZonedTimestampColumn(timestamp, zonedPrecision); |
| 68 | + }; |
| 69 | + } |
| 70 | + case TIMESTAMP_WITH_LOCAL_TIME_ZONE: |
| 71 | + if (type instanceof LocalZonedTimestampType) { |
| 72 | + final int localPrecision = ((LocalZonedTimestampType) type).getPrecision(); |
| 73 | + return val -> { |
| 74 | + TIMESTAMPLTZ timestamptz = (TIMESTAMPLTZ) val; |
| 75 | + // 重写处理12个字节情况,TIMESTAMPLTZ#toTimestamp |
| 76 | + byte[] bytes = timestamptz.toBytes(); // 获取字节码 |
| 77 | + TimeZone timeZone; |
| 78 | + if (bytes.length >= 14) { |
| 79 | + // 字节数组长度足够,尝试提取时区信息 |
| 80 | + String tzStr = |
| 81 | + DataTypeUtilities.toTimezoneStr( |
| 82 | + bytes[12], bytes[13], "GMT", true); |
| 83 | + timeZone = TimeZone.getTimeZone(tzStr); |
| 84 | + } else { |
| 85 | + // 字节数组长度不足,使用默认时区 |
| 86 | + timeZone = TimeZone.getDefault(); |
| 87 | + } |
| 88 | + Timestamp timestamp = |
| 89 | + new Timestamp(DataTypeUtilities.getOriginTime(bytes, timeZone)); |
| 90 | + timestamp.setNanos(DataTypeUtilities.getNanos(bytes, 7)); |
| 91 | + return new ZonedTimestampColumn(timestamp, timeZone, localPrecision); |
| 92 | + }; |
| 93 | + } |
| 94 | + } |
| 95 | + return super.createInternalConverter(type); |
| 96 | + } |
| 97 | +} |
0 commit comments