18
18
19
19
package org .apache .flink .connector .redshift .converter ;
20
20
21
+ import org .apache .flink .connector .redshift .internal .statement .FieldNamedRedshiftPreparedStatement ;
21
22
import org .apache .flink .table .data .RowData ;
22
23
import org .apache .flink .table .types .logical .DecimalType ;
23
24
import org .apache .flink .table .types .logical .LocalZonedTimestampType ;
24
25
import org .apache .flink .table .types .logical .LogicalType ;
25
26
import org .apache .flink .table .types .logical .RowType ;
26
27
import org .apache .flink .table .types .logical .TimestampType ;
28
+ import org .apache .flink .util .FlinkRuntimeException ;
27
29
import org .apache .flink .util .Preconditions ;
28
30
29
- import com .amazon .redshift .jdbc .RedshiftPreparedStatement ;
30
-
31
- import java .io .Serializable ;
32
31
import java .sql .Date ;
33
32
import java .sql .SQLException ;
34
33
import java .sql .Timestamp ;
38
37
import static org .apache .flink .connector .redshift .converter .RedshiftConverterUtils .toEpochDayOneTimestamp ;
39
38
40
39
/** Row converter. */
41
- public class RedshiftJdbcRowConverter implements Serializable {
40
+ public class RedshiftJdbcModeRowConverter implements RedshiftRowConverter {
42
41
private static final long serialVersionUID = 1L ;
43
42
44
43
private final RowType rowType ;
45
44
46
45
private final SerializationConverter [] toExternalConverters ;
47
46
48
- public RedshiftJdbcRowConverter (RowType rowType ) {
47
+ public RedshiftJdbcModeRowConverter (RowType rowType ) {
49
48
this .rowType = Preconditions .checkNotNull (rowType );
50
49
LogicalType [] logicalTypes =
51
50
rowType .getFields ().stream ()
@@ -58,19 +57,22 @@ public RedshiftJdbcRowConverter(RowType rowType) {
58
57
}
59
58
}
60
59
61
- public void toExternal ( RowData rowData , RedshiftPreparedStatement insertStatement )
62
- throws SQLException {
60
+ @ Override
61
+ public void toExternal ( RowData rowData , FieldNamedRedshiftPreparedStatement insertStatement ) {
63
62
for (int index = 0 ; index < rowData .getArity (); index ++) {
64
- if (!rowData .isNullAt (index )) {
65
- toExternalConverters [index ].serialize (rowData , index , insertStatement );
66
- } else {
67
- insertStatement .setObject (index + 1 , null );
63
+ try {
64
+ if (rowData .isNullAt (index )) {
65
+ insertStatement .setObject (index + 1 , null );
66
+ } else {
67
+ toExternalConverters [index ].serialize (rowData , index , insertStatement );
68
+ }
69
+ } catch (SQLException e ) {
70
+ throw new FlinkRuntimeException (e );
68
71
}
69
72
}
70
73
}
71
74
72
- protected RedshiftJdbcRowConverter .SerializationConverter createToExternalConverter (
73
- LogicalType type ) {
75
+ public SerializationConverter createToExternalConverter (LogicalType type ) {
74
76
switch (type .getTypeRoot ()) {
75
77
case BOOLEAN :
76
78
return (val , index , statement ) ->
@@ -134,13 +136,6 @@ protected RedshiftJdbcRowConverter.SerializationConverter createToExternalConver
134
136
index + 1 ,
135
137
val .getDecimal (index , decimalPrecision , decimalScale )
136
138
.toBigDecimal ());
137
- // case ARRAY:
138
- // return (val, index, statement) ->
139
- // statement.setArray(
140
- // index + 1,
141
- // (Object[])
142
- // RedshiftConverterUtils.toExternal(
143
- // val.getArray(index), type));
144
139
case MAP :
145
140
return (val , index , statement ) ->
146
141
statement .setObject (
@@ -153,14 +148,4 @@ protected RedshiftJdbcRowConverter.SerializationConverter createToExternalConver
153
148
throw new UnsupportedOperationException ("Unsupported type:" + type );
154
149
}
155
150
}
156
-
157
- @ FunctionalInterface
158
- interface SerializationConverter extends Serializable {
159
- /**
160
- * Convert an internal field to java object and fill into the {@link
161
- * RedshiftJdbcRowConverter}.
162
- */
163
- void serialize (RowData rowData , int index , RedshiftPreparedStatement statement )
164
- throws SQLException ;
165
- }
166
151
}
0 commit comments