mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-25 21:28:01 +00:00
version for 1.6.12
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-parent</artifactId>
|
||||
<version>1.6.11</version>
|
||||
<version>1.6.12</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dbswitch-dbwriter</artifactId>
|
||||
|
@@ -18,6 +18,7 @@ import com.gitee.dbswitch.dbwriter.kingbase.KingbaseInsertWriterImpl;
|
||||
import com.gitee.dbswitch.dbwriter.mssql.SqlServerWriterImpl;
|
||||
import com.gitee.dbswitch.dbwriter.mysql.MySqlWriterImpl;
|
||||
import com.gitee.dbswitch.dbwriter.oracle.OracleWriterImpl;
|
||||
import com.gitee.dbswitch.dbwriter.oscar.OscarWriterImpl;
|
||||
import com.gitee.dbswitch.dbwriter.sqlite.Sqlite3WriterImpl;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -47,6 +48,7 @@ public class DatabaseWriterFactory {
|
||||
put(DatabaseTypeEnum.DM, DmWriterImpl::new);
|
||||
//对于kingbase当前只能使用insert模式
|
||||
put(DatabaseTypeEnum.KINGBASE, KingbaseInsertWriterImpl::new);
|
||||
put(DatabaseTypeEnum.OSCAR, OscarWriterImpl::new);
|
||||
put(DatabaseTypeEnum.SQLITE3, Sqlite3WriterImpl::new);
|
||||
}
|
||||
};
|
||||
|
@@ -42,14 +42,4 @@ public interface IDatabaseWriter {
|
||||
* @return 返回实际写入的数据记录条数
|
||||
*/
|
||||
long write(List<String> fieldNames, List<Object[]> recordValues);
|
||||
|
||||
/**
|
||||
* 值字段转换,主要用于处理某些数据库不兼容java值类型,例如:sqlserver驱动(sqljdbc4-4.0)不支持BigInteger
|
||||
*
|
||||
* @param columnValue 列字段值
|
||||
* @return 转换后的值
|
||||
*/
|
||||
default Object format(Object columnValue) {
|
||||
return columnValue;
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* DM数据库写入实现类
|
||||
* 达梦数据库写入实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
|
@@ -12,8 +12,6 @@ package com.gitee.dbswitch.dbwriter.mssql;
|
||||
import com.gitee.dbswitch.dbwriter.AbstractDatabaseWriter;
|
||||
import com.gitee.dbswitch.dbwriter.IDatabaseWriter;
|
||||
import com.gitee.dbswitch.dbwriter.util.ObjectCastUtils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -110,12 +108,4 @@ public class SqlServerWriterImpl extends AbstractDatabaseWriter implements IData
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object format(Object columnValue) {
|
||||
if (columnValue instanceof BigInteger) {
|
||||
return ((BigInteger) columnValue).longValue();
|
||||
}
|
||||
return columnValue;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,48 @@
|
||||
// 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.dbwriter.oscar;
|
||||
|
||||
import com.gitee.dbswitch.dbwriter.AbstractDatabaseWriter;
|
||||
import com.gitee.dbswitch.dbwriter.IDatabaseWriter;
|
||||
import com.gitee.dbswitch.dbwriter.util.ObjectCastUtils;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 神通数据库写入实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class OscarWriterImpl extends AbstractDatabaseWriter implements IDatabaseWriter {
|
||||
|
||||
public OscarWriterImpl(DataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseProductName() {
|
||||
return "Oscar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public long write(List<String> fieldNames, List<Object[]> recordValues) {
|
||||
recordValues.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
try {
|
||||
row[i] = ObjectCastUtils.castByDetermine(row[i]);
|
||||
} catch (Exception e) {
|
||||
row[i] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return super.write(fieldNames, recordValues);
|
||||
}
|
||||
}
|
@@ -1,9 +1,9 @@
|
||||
package com.gitee.dbswitch.dbwriter.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import com.gitee.dbswitch.common.util.TypeConvertUtils;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.SQLException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -16,6 +16,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
public final class ObjectCastUtils {
|
||||
|
||||
private ObjectCastUtils() {
|
||||
throw new IllegalStateException("Utility class can not create instance!");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,23 +26,7 @@ public final class ObjectCastUtils {
|
||||
* @return java.lang.String类型数据
|
||||
*/
|
||||
public static String clob2Str(java.sql.Clob clob) {
|
||||
if (null == clob) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try (java.io.Reader is = clob.getCharacterStream()) {
|
||||
java.io.BufferedReader reader = new java.io.BufferedReader(is);
|
||||
String line = reader.readLine();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
line = reader.readLine();
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (SQLException | java.io.IOException e) {
|
||||
log.warn("Field Value convert from java.sql.Clob to java.lang.String failed:", e);
|
||||
return null;
|
||||
}
|
||||
return TypeConvertUtils.clob2Str(clob);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,45 +36,17 @@ public final class ObjectCastUtils {
|
||||
* @return byte数组
|
||||
*/
|
||||
public static byte[] blob2Bytes(java.sql.Blob blob) {
|
||||
if (null == blob) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try (java.io.InputStream inputStream = blob.getBinaryStream();) {
|
||||
try (java.io.BufferedInputStream is = new java.io.BufferedInputStream(inputStream)) {
|
||||
byte[] bytes = new byte[(int) blob.length()];
|
||||
int len = bytes.length;
|
||||
int offset = 0;
|
||||
int read = 0;
|
||||
while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
|
||||
offset += read;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return TypeConvertUtils.blob2Bytes(blob);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Object对象转换为字节数组
|
||||
*
|
||||
* @param obj 对象
|
||||
* @param in 对象
|
||||
* @return 字节数组
|
||||
*/
|
||||
public static byte[] toByteArray(Object obj) {
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
|
||||
oos.writeObject(obj);
|
||||
oos.flush();
|
||||
return bos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
public static byte[] toByteArray(Object in) {
|
||||
return TypeConvertUtils.castToByteArray(in);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,94 +56,7 @@ public final class ObjectCastUtils {
|
||||
* @return java.lang.String类型
|
||||
*/
|
||||
public static String castToString(final Object in) {
|
||||
if (in instanceof java.lang.Character) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.lang.String) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.lang.Character) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.sql.Clob) {
|
||||
return clob2Str((java.sql.Clob) in);
|
||||
} else if (in instanceof java.lang.Number) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.sql.RowId) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.lang.Boolean) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.util.Date) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.time.LocalDate) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.time.LocalTime) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.time.LocalDateTime) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.time.OffsetDateTime) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.util.UUID) {
|
||||
return in.toString();
|
||||
} else if (in instanceof org.postgresql.util.PGobject) {
|
||||
return in.toString();
|
||||
} else if (in instanceof org.postgresql.jdbc.PgSQLXML) {
|
||||
try {
|
||||
return ((org.postgresql.jdbc.PgSQLXML) in).getString();
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
} else if (in instanceof java.sql.SQLXML) {
|
||||
return in.toString();
|
||||
} else if (in instanceof java.sql.Array) {
|
||||
return in.toString();
|
||||
} else if (in.getClass().getName().equals("oracle.sql.INTERVALDS")) {
|
||||
return in.toString();
|
||||
} else if (in.getClass().getName().equals("oracle.sql.INTERVALYM")) {
|
||||
return in.toString();
|
||||
} else if (in.getClass().getName().equals("oracle.sql.TIMESTAMPLTZ")) {
|
||||
return in.toString();
|
||||
} else if (in.getClass().getName().equals("oracle.sql.TIMESTAMPTZ")) {
|
||||
return in.toString();
|
||||
} else if (in.getClass().getName().equals("oracle.sql.BFILE")) {
|
||||
Class<?> clz = in.getClass();
|
||||
try {
|
||||
Method methodFileExists = clz.getMethod("fileExists");
|
||||
boolean exists = (boolean) methodFileExists.invoke(in);
|
||||
if (!exists) {
|
||||
return "";
|
||||
}
|
||||
|
||||
Method methodOpenFile = clz.getMethod("openFile");
|
||||
methodOpenFile.invoke(in);
|
||||
|
||||
try {
|
||||
Method methodCharacterStreamValue = clz.getMethod("getBinaryStream");
|
||||
java.io.InputStream is = (java.io.InputStream) methodCharacterStreamValue.invoke(in);
|
||||
|
||||
String line;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
} finally {
|
||||
Method methodCloseFile = clz.getMethod("closeFile");
|
||||
methodCloseFile.invoke(in);
|
||||
}
|
||||
} catch (java.lang.reflect.InvocationTargetException ex) {
|
||||
log.warn("Error for handle oracle.sql.BFILE: ", ex);
|
||||
return "";
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else if (in.getClass().getName().equals("microsoft.sql.DateTimeOffset")) {
|
||||
return in.toString();
|
||||
} else if (in instanceof byte[]) {
|
||||
return new String((byte[]) in);
|
||||
}
|
||||
|
||||
return null;
|
||||
return TypeConvertUtils.castToString(in);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -867,7 +737,15 @@ public final class ObjectCastUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (in instanceof java.sql.Clob) {
|
||||
if (in instanceof BigInteger) {
|
||||
return ((BigInteger) in).longValue();
|
||||
} else if (in instanceof BigDecimal) {
|
||||
BigDecimal decimal = (BigDecimal) in;
|
||||
if (decimal.doubleValue() > 2.147483647E9D || decimal.doubleValue() < -2.147483648E9D) {
|
||||
return 0D;
|
||||
}
|
||||
return decimal.doubleValue();
|
||||
} else if (in instanceof java.sql.Clob) {
|
||||
return clob2Str((java.sql.Clob) in);
|
||||
} else if (in instanceof java.sql.Array
|
||||
|| in instanceof java.sql.SQLXML) {
|
||||
|
Reference in New Issue
Block a user