mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-24 04:54:50 +00:00
@@ -16,6 +16,7 @@ import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Oracle数据库写入实现类
|
||||
@@ -36,15 +37,14 @@ public class OracleWriterImpl extends AbstractDatabaseWriter implements IDatabas
|
||||
|
||||
@Override
|
||||
public long write(List<String> fieldNames, List<Object[]> recordValues) {
|
||||
/** 处理Oracle的Clob类型需写入String类型的数据的问题 */
|
||||
/** 处理Oracle的Clob/Array类型需写入String类型的数据的问题 */
|
||||
recordValues.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
try {
|
||||
row[i] = convertClobToString(row[i]);
|
||||
row[i] = convert(row[i]);
|
||||
} catch (Exception e) {
|
||||
log.warn(
|
||||
"Field Value convert from java.sql.Clob to java.lang.String failed, field name is : {} ",
|
||||
fieldNames.get(i));
|
||||
log.warn("Field Value convert from {} to java.lang.String failed, field name is : {} ",
|
||||
row[i].getClass().getName(), fieldNames.get(i));
|
||||
row[i] = null;
|
||||
}
|
||||
}
|
||||
@@ -53,15 +53,15 @@ public class OracleWriterImpl extends AbstractDatabaseWriter implements IDatabas
|
||||
return super.write(fieldNames, recordValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将java.sql.Clob 类型转换为java.lang.String
|
||||
*/
|
||||
private Object convertClobToString(Object o) {
|
||||
private Object convert(Object o) {
|
||||
if (null == o) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (o instanceof Clob) {
|
||||
/**
|
||||
* 将java.sql.Clob 类型转换为java.lang.String
|
||||
*/
|
||||
Clob clob = (Clob) o;
|
||||
try (java.io.Reader is = clob.getCharacterStream();
|
||||
java.io.BufferedReader reader = new java.io.BufferedReader(is)) {
|
||||
@@ -75,9 +75,23 @@ public class OracleWriterImpl extends AbstractDatabaseWriter implements IDatabas
|
||||
} catch (SQLException | java.io.IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
} else if (o instanceof java.sql.Array) {
|
||||
/**
|
||||
* 将java.sql.Array 类型转换为java.lang.String
|
||||
* <p>
|
||||
* Oracle 没有数组类型
|
||||
*/
|
||||
String v = o.toString();
|
||||
String a = o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
|
||||
if (a.length() == v.length() && StringUtils.equals(a, v)) {
|
||||
log.warn("Unsupported type for convert {} to java.lang.String");
|
||||
return null;
|
||||
}
|
||||
|
||||
return o;
|
||||
return v;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user