mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-27 22:21:34 +00:00
修复问题I7ZH83
This commit is contained in:
@@ -12,12 +12,11 @@ package com.gitee.dbswitch.product.dm;
|
||||
import com.gitee.dbswitch.annotation.Product;
|
||||
import com.gitee.dbswitch.common.type.ProductTypeEnum;
|
||||
import com.gitee.dbswitch.features.ProductFeatures;
|
||||
import com.gitee.dbswitch.product.oracle.OracleTableDataWriteProvider;
|
||||
import com.gitee.dbswitch.product.oracle.OracleTableOperateProvider;
|
||||
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.meta.MetadataProvider;
|
||||
import com.gitee.dbswitch.provider.operate.TableOperateProvider;
|
||||
import com.gitee.dbswitch.provider.sync.DefaultTableDataSynchronizer;
|
||||
import com.gitee.dbswitch.provider.sync.AutoCastTableDataSynchronizer;
|
||||
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
|
||||
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
|
||||
import javax.sql.DataSource;
|
||||
@@ -40,7 +39,7 @@ public class DmFactoryProvider extends AbstractFactoryProvider {
|
||||
|
||||
@Override
|
||||
public TableDataWriteProvider createTableDataWriteProvider(boolean useInsert) {
|
||||
return new OracleTableDataWriteProvider(this);
|
||||
return new DmTableDataWriteProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,7 +49,7 @@ public class DmFactoryProvider extends AbstractFactoryProvider {
|
||||
|
||||
@Override
|
||||
public TableDataSynchronizer createTableDataSynchronizer() {
|
||||
return new DefaultTableDataSynchronizer(this);
|
||||
return new AutoCastTableDataSynchronizer(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,32 @@
|
||||
// Copyright tang. All rights reserved.
|
||||
// https://gitee.com/inrgihc/dbswitch
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.product.dm;
|
||||
|
||||
import com.gitee.dbswitch.common.util.ObjectCastUtils;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.write.DefaultTableDataWriteProvider;
|
||||
import java.util.List;
|
||||
|
||||
public class DmTableDataWriteProvider extends DefaultTableDataWriteProvider {
|
||||
|
||||
public DmTableDataWriteProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long write(List<String> fieldNames, List<Object[]> recordValues) {
|
||||
recordValues.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
row[i] = ObjectCastUtils.castByDetermine(row[i]);
|
||||
}
|
||||
});
|
||||
return super.write(fieldNames, recordValues);
|
||||
}
|
||||
}
|
@@ -7,10 +7,11 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.core.SqlTypeValue;
|
||||
|
||||
@Slf4j
|
||||
@UtilityClass
|
||||
public class OracleCastUtils {
|
||||
|
||||
@@ -24,40 +25,32 @@ public class OracleCastUtils {
|
||||
* oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical
|
||||
*/
|
||||
public static Object castByJdbcType(int jdbcType, Object value, List<InputStream> iss) {
|
||||
try {
|
||||
switch (jdbcType) {
|
||||
case Types.CLOB:
|
||||
case Types.NCLOB:
|
||||
return Objects.isNull(value)
|
||||
? null
|
||||
: ObjectCastUtils.castToString(value);
|
||||
case Types.BLOB:
|
||||
final byte[] bytes = Objects.isNull(value)
|
||||
? null
|
||||
: ObjectCastUtils.castToByteArray(value);
|
||||
return new SqlTypeValue() {
|
||||
@Override
|
||||
public void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType,
|
||||
String typeName) throws SQLException {
|
||||
if (null != bytes) {
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
ps.setBlob(paramIndex, is);
|
||||
iss.add(is);
|
||||
} else {
|
||||
ps.setNull(paramIndex, sqlType);
|
||||
}
|
||||
}
|
||||
};
|
||||
case Types.ROWID:
|
||||
case Types.ARRAY:
|
||||
case Types.REF:
|
||||
case Types.SQLXML:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (null == value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (jdbcType == Types.BLOB) {
|
||||
try {
|
||||
final byte[] bytes = ObjectCastUtils.castToByteArray(value);
|
||||
return new SqlTypeValue() {
|
||||
@Override
|
||||
public void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType,
|
||||
String typeName) throws SQLException {
|
||||
if (null != bytes) {
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
ps.setBlob(paramIndex, is);
|
||||
iss.add(is);
|
||||
} else {
|
||||
ps.setNull(paramIndex, sqlType);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (Exception e) {
|
||||
log.warn("Convert from {} to Oracle BLOB failed:{}", value.getClass().getName(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return ObjectCastUtils.castByJdbcType(jdbcType, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -9,18 +9,11 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.product.oracle;
|
||||
|
||||
import com.gitee.dbswitch.common.util.ObjectCastUtils;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.write.DefaultTableDataWriteProvider;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.springframework.jdbc.core.SqlTypeValue;
|
||||
|
||||
public class OracleTableDataWriteProvider extends DefaultTableDataWriteProvider {
|
||||
|
||||
|
Reference in New Issue
Block a user