Skip to content

Commit

Permalink
[Bitsail] Refactor Oracle connector util classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ysamchu committed Nov 2, 2022
1 parent cd174d7 commit 2f94fe3
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected Object extract(ResultSet rs,
int columnIndex,
int columnType,
String columnTypeName,
String columnName,
String encoding) throws Exception {
switch (columnType) {
case Types.CHAR:
Expand Down Expand Up @@ -113,9 +112,8 @@ protected Object extract(ResultSet rs,
.asBitSailException(
DBUtilErrorCode.UNSUPPORTED_TYPE,
String.format(
"JDBC extract: The column data type in your configuration is not support. Column name:[%s], Column type:[%s]." +
"JDBC extract: The column data type in your configuration is not support. Column type name:[%s]." +
" Please try to change the column data type or don't transmit this column.",
columnName,
columnTypeName));

}
Expand All @@ -126,16 +124,14 @@ public final Object convert(ResultSetMetaData metaData,
int columnIndex,
String encoding) throws Exception {
int columnType = metaData.getColumnType(columnIndex);
String columnName = metaData.getColumnName(columnIndex);
String columnTypeName = metaData.getColumnTypeName(columnIndex);
Object value = extract(rs, metaData, columnIndex, columnType, columnTypeName, columnName, encoding);
Object value = extract(rs, metaData, columnIndex, columnType, columnTypeName, encoding);
LOG.debug("value: {}", value);
return convert(value, columnType, columnName, columnTypeName);
return convert(value, columnType, columnTypeName);
}

protected Object convert(Object value,
int columnType,
String columnName,
String columnTypeName) throws Exception {
switch (columnType) {
case Types.CHAR:
Expand All @@ -147,53 +143,52 @@ protected Object convert(Object value,
case Types.CLOB:
case Types.NCLOB:
case Types.ROWID:
return convertStringValue(value, columnName, columnTypeName);
return convertStringValue(value);
case Types.BIT:
case Types.TINYINT:
case Types.SMALLINT:
return convertShortValue(value, columnName, columnTypeName);
return convertShortValue(value);
case Types.INTEGER:
return convertLongValue(value, columnName, columnTypeName);
return convertLongValue(value);

case Types.BIGINT:
return convertBigIntegerValue(value, columnName, columnTypeName);
return convertBigIntegerValue(value);

case Types.NUMERIC:
case Types.DECIMAL:
return convertBigDecimalValue(value, columnName, columnTypeName);
return convertBigDecimalValue(value);

case Types.FLOAT:
case Types.REAL:
case Types.DOUBLE:
return convertDoubleValue(value, columnName, columnTypeName);
return convertDoubleValue(value);

case Types.TIME:
case Types.DATE:
case Types.TIMESTAMP:
return convertTimeValue(value, columnName, columnTypeName);
return convertTimeValue(value);
case Types.BINARY:
case Types.VARBINARY:
case Types.BLOB:
case Types.LONGVARBINARY:
return convertBinaryValue(value, columnName, columnTypeName);
return convertBinaryValue(value);

case Types.BOOLEAN:
return convertBooleanValue(value, columnName, columnTypeName);
return convertBooleanValue(value);

case Types.ARRAY:
return convertArrayValue(value, columnName, columnTypeName);
return convertArrayValue(value);
case Types.NULL:
case Types.OTHER:
case Types.STRUCT:
return convertObjectValue(value, columnName, columnTypeName);
return convertObjectValue(value);
default:
throw BitSailException
.asBitSailException(
DBUtilErrorCode.UNSUPPORTED_TYPE,
String.format(
"JDBC convert: The column data type in your configuration is not support. Column name:[%s], Column type:[%s]." +
"JDBC convert: The column data type in your configuration is not support. Column type:[%s]." +
" Please try to change the column data type or don't transmit this column.",
columnName,
columnTypeName));

}
Expand Down Expand Up @@ -279,18 +274,18 @@ private Object extractObjectValue(ResultSet rs, int columnIndex) throws SQLExcep
return objectValue;
}

protected String convertStringValue(Object value, String columnName, String columnTypeName) {
protected String convertStringValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
if (value instanceof String) {
return (String) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to String", value));
}

private Short convertShortValue(Object value, String columnName, String columnTypeName) {
private Short convertShortValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -301,10 +296,10 @@ private Short convertShortValue(Object value, String columnName, String columnTy
return ((Number) value).shortValue();
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Short", value));
}

private Long convertLongValue(Object value, String columnName, String columnTypeName) {
private Long convertLongValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -321,10 +316,10 @@ private Long convertLongValue(Object value, String columnName, String columnType
return ((Number) value).longValue();
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Long", value));
}

private BigInteger convertBigIntegerValue(Object value, String columnName, String columnTypeName) {
private BigInteger convertBigIntegerValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -335,10 +330,10 @@ private BigInteger convertBigIntegerValue(Object value, String columnName, Strin
return (BigInteger) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to BigInteger", value));
}

private BigDecimal convertBigDecimalValue(Object value, String columnName, String columnTypeName) {
private BigDecimal convertBigDecimalValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -352,10 +347,10 @@ private BigDecimal convertBigDecimalValue(Object value, String columnName, Strin
return (BigDecimal) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to BigDecimal", value));
}

protected Double convertDoubleValue(Object value, String columnName, String columnTypeName) {
protected Double convertDoubleValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -369,10 +364,10 @@ protected Double convertDoubleValue(Object value, String columnName, String colu
return ((BigDecimal) value).doubleValue();
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Double", value));
}

protected Object convertTimeValue(Object value, String columnName, String columnTypeName) {
protected Object convertTimeValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -389,32 +384,32 @@ protected Object convertTimeValue(Object value, String columnName, String column
return (Date) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Time", value));
}

private byte[] convertBinaryValue(Object value, String columnName, String columnTypeName) {
private byte[] convertBinaryValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
if (value instanceof byte[]) {
return (byte[]) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to byte[]", value));
}

protected Boolean convertBooleanValue(Object value, String columnName, String columnTypeName) {
protected Boolean convertBooleanValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Boolean", value));
}

private List<Object> convertArrayValue(Object value, String columnName, String columnTypeName) throws Exception {
private List<Object> convertArrayValue(Object value) throws Exception {
if (Objects.isNull(value)) {
return null;
}
Expand All @@ -423,15 +418,15 @@ private List<Object> convertArrayValue(Object value, String columnName, String c
Object[] arrays = (Object[]) arrayElements.getArray();
List<Object> tmpArrays = Lists.newArrayList();
for (Object element : arrays) {
tmpArrays.add(convert(element, arrayElements.getBaseType(), columnName, arrayElements.getBaseTypeName()));
tmpArrays.add(convert(element, arrayElements.getBaseType(), arrayElements.getBaseTypeName()));
}
return tmpArrays;
}
throw new IllegalArgumentException(String
.format("Unexpected value for JDBC type: %s and column %s", columnTypeName, columnName));
.format("Unexpected value %s while converting to Array", value));
}

private Object convertObjectValue(Object value, String columnName, String columnTypeName) throws SQLException {
private Object convertObjectValue(Object value) {
if (Objects.isNull(value)) {
return null;
}
Expand Down
Loading

0 comments on commit 2f94fe3

Please sign in to comment.