!71 BUG修复与优化

* 代码优化与BUG修复
This commit is contained in:
inrgihc
2022-01-30 05:17:15 +00:00
parent d6d78ceed5
commit d470678e95
11 changed files with 1223 additions and 1223 deletions

View File

@@ -43,7 +43,8 @@ public class UserController {
@ApiOperation(value = "用户详情", notes = "根据用户名获取用户详情")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "username", value = "账号登陆名", required = true, dataTypeClass = String.class)})
@ApiImplicitParam(paramType = "query", name = "username", value = "账号登陆名", required = true, dataTypeClass = String.class)
})
@GetMapping(value = "/detail/name", produces = MediaType.APPLICATION_JSON_VALUE)
public Result<SystemUserDetailResponse> getUserByName(@RequestParam("username") String username) {
return systemUserService.getUserDetailByUsername(username);
@@ -54,7 +55,8 @@ public class UserController {
@ApiOperation(value = "修改密码", notes = "用户修改自己的密码")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "oldPassword", value = "旧密码", required = true, dataTypeClass = String.class),
@ApiImplicitParam(paramType = "query", name = "newPassword", value = "新密码", required = true, dataTypeClass = String.class)})
@ApiImplicitParam(paramType = "query", name = "newPassword", value = "新密码", required = true, dataTypeClass = String.class)
})
public Result changeOwnPassword(@RequestParam("oldPassword") String oldPassword,
@RequestParam("newPassword") String newPassword) {
return systemUserService.changeOwnPassword(oldPassword, newPassword);

View File

@@ -21,8 +21,9 @@ import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = {"账号认证接口"})
@@ -35,20 +36,22 @@ public class AuthenticationController {
@NoToken
@AccessLog(value = "认证登陆", description = "'认证登陆的用户名为:'+#username")
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "账号登录", notes = "使用一个账号密码登录")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataTypeClass = String.class),
@ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataTypeClass = String.class)})
public Result<?> login(String username, String password) {
@ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataTypeClass = String.class)
})
public Result login(String username, String password) {
return authenticationService.login(username, password);
}
@OperateLog(name = "退出登录")
@RequestMapping(value = "/logout", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/logout", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "认证登出", notes = "登出系统")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)
})
public Result logout() {
return authenticationService.logout();
}

View File

@@ -33,7 +33,9 @@ public class DatabaseConnectionDAO {
}
public DatabaseConnectionEntity getByName(String name) {
return databaseConnectionMapper.getByName(name);
DatabaseConnectionEntity record = new DatabaseConnectionEntity();
record.setName(name);
return databaseConnectionMapper.selectOne(record);
}
public List<DatabaseConnectionEntity> listAll(String searchText) {

View File

@@ -10,12 +10,8 @@
package com.gitee.dbswitch.admin.mapper;
import com.gitee.dbswitch.admin.entity.DatabaseConnectionEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;
public interface DatabaseConnectionMapper extends Mapper<DatabaseConnectionEntity> {
@Select("select * from DBSWITCH_DATABASE_CONNECTION where name=#{name} limit 1 ")
DatabaseConnectionEntity getByName(@Param("name") String name);
}

View File

@@ -39,6 +39,7 @@ import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -198,7 +199,7 @@ public class AssignmentService {
String sourceSchema = assignmentConfigEntity.getSourceSchema();
if (assignmentConfigEntity.getExcluded()) {
if (assignmentConfigEntity.getSourceTables().isEmpty()) {
if (CollectionUtils.isEmpty(assignmentConfigEntity.getSourceTables())) {
sourceDataSourceProperties.setSourceExcludes("");
} else {
sourceDataSourceProperties.setSourceExcludes(
@@ -207,7 +208,7 @@ public class AssignmentService {
);
}
} else {
if (assignmentConfigEntity.getSourceTables().isEmpty()) {
if (CollectionUtils.isEmpty(assignmentConfigEntity.getSourceTables())) {
sourceDataSourceProperties.setSourceIncludes("");
} else {
sourceDataSourceProperties.setSourceIncludes(

View File

@@ -6,8 +6,8 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum IncludeExcludeEnum {
INCLUDE("包含"),
EXCLUDE("排除"),
INCLUDE("精确包含"),
EXCLUDE("精确排除"),
;
private String name;

View File

@@ -94,6 +94,10 @@ public abstract class AbstractDatabaseWriter implements IDatabaseWriter {
@Override
public long write(List<String> fieldNames, List<Object[]> recordValues) {
if (recordValues.isEmpty()) {
return 0;
}
String sqlInsert = String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )",
schemaName, tableName,
StringUtils.join(fieldNames, "\",\""),
@@ -113,10 +117,12 @@ public abstract class AbstractDatabaseWriter implements IDatabaseWriter {
TransactionStatus status = transactionManager.getTransaction(definition);
try {
int[] affects = jdbcTemplate.batchUpdate(sqlInsert, recordValues, argTypes);
int affectCount = Arrays.stream(affects).sum();
recordValues.clear();
//int[] affects = jdbcTemplate.batchUpdate(sqlInsert, recordValues, argTypes);
//int affectCount = Arrays.stream(affects).sum();
jdbcTemplate.batchUpdate(sqlInsert, recordValues, argTypes);
transactionManager.commit(status);
int affectCount = recordValues.size();
recordValues.clear();
if (log.isDebugEnabled()) {
log.debug("{} insert data affect count: {}", getDatabaseProductName(), affectCount);
}

View File

@@ -75,7 +75,6 @@ public class MySqlWriterImpl extends AbstractDatabaseWriter implements IDatabase
try {
int[] affects = jdbcTemplate.batchUpdate(sqlInsert, recordValues, argTypes);
int affectCount = Arrays.stream(affects).sum();
recordValues.clear();
if (log.isDebugEnabled()) {
log.debug("{} insert data affect count: {}", getDatabaseProductName(), affectCount);
}

View File

@@ -75,7 +75,8 @@ public class OracleWriterImpl extends AbstractDatabaseWriter implements IDatabas
} catch (SQLException | java.io.IOException e) {
throw new RuntimeException(e);
}
} else if (o instanceof java.sql.Array) {
} else if (o instanceof java.sql.Array
|| o instanceof java.sql.SQLXML) {
/**
* 将java.sql.Array 类型转换为java.lang.String
* <p>
@@ -89,6 +90,9 @@ public class OracleWriterImpl extends AbstractDatabaseWriter implements IDatabas
}
return v;
} else if (o instanceof java.sql.Struct) {
log.warn("Unsupported type for convert {} to java.lang.String", o.getClass().getName());
return null;
} else {
return o;
}

View File

@@ -0,0 +1,861 @@
package com.gitee.dbswitch.dbwriter.util;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public final class ObjectCastUtils {
private ObjectCastUtils() {
}
/**
* 将java.sql.Clob类型转换为java.lang.String类型
*
* @param clob java.sql.Clob类型对象
* @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;
}
}
/**
* 将java.sql.Blob类型转换为byte数组
*
* @param blob java.sql.Blob类型对象
* @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);
}
}
/**
* 将Object对象转换为字节数组
*
* @param obj 对象
* @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);
}
}
/**
* 将任意类型转换为java.lang.String类型
*
* @param in 任意类型的对象实例
* @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.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;
}
/**
* 将任意类型转换为java.lang.Byte类型
*
* @param in 任意类型的对象实例
* @return java.lang.Byte类型
*/
public static Byte castToByte(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).byteValue();
} else if (in instanceof java.util.Date) {
return Long.valueOf(((java.util.Date) in).getTime()).byteValue();
} else if (in instanceof java.lang.String) {
try {
return Byte.parseByte(in.toString());
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Byte类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Character) {
try {
return Byte.parseByte(in.toString());
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.Character类型转换为java.lang.Byte类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
return Byte.parseByte(clob2Str((java.sql.Clob) in));
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Byte类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? (byte) 1 : (byte) 0;
}
return null;
}
/**
* 将任意类型转换为java.lang.Short类型
*
* @param in 任意类型的对象实例
* @return java.lang.Short类型
*/
public static Short castToShort(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).shortValue();
} else if (in instanceof java.lang.Byte) {
return (short) (((byte) in) & 0xff);
} else if (in instanceof java.util.Date) {
return (short) ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return (short) ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return (short) java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return (short) java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Short.valueOf((short) 1);
} else if (s.equalsIgnoreCase("false")) {
return Short.valueOf((short) 0);
} else {
return Short.parseShort(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Short类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Short.valueOf((short) 1);
} else if (s.equalsIgnoreCase("false")) {
return Short.valueOf((short) 0);
} else {
return Short.parseShort(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Short类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? (short) 1 : (short) 0;
}
return null;
}
/**
* 将任意类型转换为java.lang.Integer类型
*
* @param in 任意类型的对象实例
* @return java.lang.Integer类型
*/
public static Integer castToInteger(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).intValue();
} else if (in instanceof java.lang.Byte) {
return (((byte) in) & 0xff);
} else if (in instanceof java.util.Date) {
return (int) ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return (int) ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return (int) java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return (int) java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Integer.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Integer.valueOf(0);
} else {
return Integer.parseInt(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Integer类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Integer.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Integer.valueOf(0);
} else {
return Integer.parseInt(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Integer类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? (int) 1 : (int) 0;
}
return null;
}
/**
* 将任意类型转换为java.lang.Long类型
*
* @param in 任意类型的对象实例
* @return java.lang.Long类型
*/
public static Long castToLong(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).longValue();
} else if (in instanceof java.lang.Byte) {
return (long) (((byte) in) & 0xff);
} else if (in instanceof java.util.Date) {
return ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Long.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Long.valueOf(0);
} else {
return Long.parseLong(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Long类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Long.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Long.valueOf(0);
} else {
return Long.parseLong(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Long类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? (long) 1 : (long) 0;
}
return null;
}
/**
* 将任意类型转换为java.lang.Number类型
*
* @param in 任意类型的对象实例
* @return java.lang.Number类型
*/
public static Number castToNumeric(final Object in) {
if (in instanceof java.lang.Number) {
return (java.lang.Number) in;
} else if (in instanceof java.util.Date) {
return ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Integer.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Integer.valueOf(0);
} else {
return new java.math.BigDecimal(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Number类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Integer.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Integer.valueOf(0);
} else {
return new java.math.BigDecimal(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Number类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? (long) 1 : (long) 0;
}
return null;
}
/**
* 将任意类型转换为java.lang.Float类型
*
* @param in 任意类型的对象实例
* @return java.lang.Float类型
*/
public static Float castToFloat(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).floatValue();
} else if (in instanceof java.util.Date) {
return (float) ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return (float) ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return (float) java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return (float) java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Float.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Float.valueOf(0);
} else {
return Float.parseFloat(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Float类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Float.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Float.valueOf(0);
} else {
return Float.parseFloat(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Float类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? 1f : 0f;
}
return null;
}
/**
* 将任意类型转换为java.lang.Double类型
*
* @param in 任意类型的对象实例
* @return java.lang.Double类型
*/
public static Double castToDouble(final Object in) {
if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).doubleValue();
} else if (in instanceof java.util.Date) {
return (double) ((java.util.Date) in).getTime();
} else if (in instanceof java.util.Calendar) {
return (double) ((java.util.Calendar) in).getTime().getTime();
} else if (in instanceof java.time.LocalDateTime) {
return (double) java.sql.Timestamp.valueOf((java.time.LocalDateTime) in).getTime();
} else if (in instanceof java.time.OffsetDateTime) {
return (double) java.sql.Timestamp.valueOf(((java.time.OffsetDateTime) in).toLocalDateTime())
.getTime();
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
String s = in.toString().trim();
if (s.equalsIgnoreCase("true")) {
return Double.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Double.valueOf(0);
} else {
return Double.parseDouble(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将将java.lang.String类型转换为java.lang.Double类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
String s = clob2Str((java.sql.Clob) in).trim();
if (s.equalsIgnoreCase("true")) {
return Double.valueOf(1);
} else if (s.equalsIgnoreCase("false")) {
return Double.valueOf(0);
} else {
return Double.parseDouble(s);
}
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Double类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in ? 1d : 0d;
}
return null;
}
/**
* 将任意类型转换为java.time.LocalDate类型
*
* @param in 任意类型的对象实例
* @return java.time.LocalDate类型
*/
public static LocalDate castToLocalDate(final Object in) {
if (in instanceof java.sql.Time) {
java.sql.Time date = (java.sql.Time) in;
LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate;
} else if (in instanceof java.sql.Timestamp) {
java.sql.Timestamp t = (java.sql.Timestamp) in;
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalDate();
} else if (in instanceof java.util.Date) {
java.util.Date date = (java.util.Date) in;
LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate;
} else if (in instanceof java.util.Calendar) {
java.sql.Date date = new java.sql.Date(((java.util.Calendar) in).getTime().getTime());
LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate;
} else if (in instanceof java.time.LocalDate) {
return (java.time.LocalDate) in;
} else if (in instanceof java.time.LocalTime) {
return java.time.LocalDate.MIN;
} else if (in instanceof java.time.LocalDateTime) {
return ((java.time.LocalDateTime) in).toLocalDate();
} else if (in instanceof java.time.OffsetDateTime) {
return ((java.time.OffsetDateTime) in).toLocalDate();
} else if (in.getClass().getName().equals("oracle.sql.TIMESTAMP")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("timestampValue");
java.sql.Timestamp date = (java.sql.Timestamp) m.invoke(in);
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in.getClass().getName().equals("microsoft.sql.DateTimeOffset")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("getTimestamp");
java.sql.Timestamp t = (java.sql.Timestamp) m.invoke(in);
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalDate();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
java.sql.Time date = java.sql.Time.valueOf(in.toString());
LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate;
} catch (IllegalArgumentException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.sql.Time类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
java.sql.Time date = java.sql.Time.valueOf(clob2Str((java.sql.Clob) in));
LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate;
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.sql.Time类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Number) {
java.sql.Timestamp t = new java.sql.Timestamp(((java.lang.Number) in).longValue());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalDate();
}
return null;
}
/**
* 将任意类型转换为java.time.LocalTime类型
*
* @param in 任意类型的对象实例
* @return java.time.LocalDate类型
*/
public static LocalTime castToLocalTime(final Object in) {
if (in instanceof java.sql.Time) {
java.sql.Time date = (java.sql.Time) in;
LocalTime localTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault())
.toLocalTime();
return localTime;
} else if (in instanceof java.sql.Timestamp) {
java.sql.Timestamp t = (java.sql.Timestamp) in;
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalTime();
} else if (in instanceof java.util.Date) {
return LocalTime.of(0, 0, 0);
} else if (in instanceof java.util.Calendar) {
java.sql.Date date = new java.sql.Date(((java.util.Calendar) in).getTime().getTime());
LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
return localDateTime.toLocalTime();
} else if (in instanceof java.time.LocalDate) {
return LocalTime.of(0, 0, 0);
} else if (in instanceof java.time.LocalTime) {
return (java.time.LocalTime) in;
} else if (in instanceof java.time.LocalDateTime) {
return ((java.time.LocalDateTime) in).toLocalTime();
} else if (in instanceof java.time.OffsetDateTime) {
return ((java.time.OffsetDateTime) in).toLocalTime();
} else if (in.getClass().getName().equals("oracle.sql.TIMESTAMP")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("timestampValue");
java.sql.Timestamp date = (java.sql.Timestamp) m.invoke(in);
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault())
.toLocalDateTime();
return localDateTime.toLocalTime();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in.getClass().getName().equals("microsoft.sql.DateTimeOffset")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("getTimestamp");
java.sql.Timestamp t = (java.sql.Timestamp) m.invoke(in);
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalTime();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
java.sql.Time date = java.sql.Time.valueOf(in.toString());
return LocalTime.ofSecondOfDay(date.getTime());
} catch (IllegalArgumentException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.sql.Time类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
java.sql.Time date = java.sql.Time.valueOf(clob2Str((java.sql.Clob) in));
return LocalTime.ofSecondOfDay(date.getTime());
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.sql.Time类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Number) {
java.sql.Timestamp t = new java.sql.Timestamp(((java.lang.Number) in).longValue());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime.toLocalTime();
}
return null;
}
/**
* 将任意类型转换为java.time.LocalDateTime类型
*
* @param in 任意类型的对象实例
* @return java.time.LocalDateTime类型
*/
public static LocalDateTime castToLocalDateTime(final Object in) {
if (in instanceof java.sql.Timestamp) {
java.sql.Timestamp t = (java.sql.Timestamp) in;
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} else if (in instanceof java.sql.Date) {
java.sql.Date date = (java.sql.Date) in;
LocalDate localDate = date.toLocalDate();
LocalTime localTime = LocalTime.of(0, 0, 0);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
return localDateTime;
} else if (in instanceof java.sql.Time) {
java.sql.Time date = (java.sql.Time) in;
java.sql.Timestamp t = new java.sql.Timestamp(date.getTime());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} else if (in instanceof java.util.Date) {
java.sql.Timestamp t = new java.sql.Timestamp(((java.util.Date) in).getTime());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} else if (in instanceof java.util.Calendar) {
java.sql.Timestamp t = new java.sql.Timestamp(((java.util.Calendar) in).getTime().getTime());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} else if (in instanceof java.time.LocalDate) {
LocalDate localDate = (java.time.LocalDate) in;
LocalTime localTime = LocalTime.of(0, 0, 0);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
return localDateTime;
} else if (in instanceof java.time.LocalTime) {
LocalDate localDate = java.time.LocalDate.MIN;
LocalTime localTime = (java.time.LocalTime) in;
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
return localDateTime;
} else if (in instanceof java.time.LocalDateTime) {
return (java.time.LocalDateTime) in;
} else if (in instanceof java.time.OffsetDateTime) {
return ((java.time.OffsetDateTime) in).toLocalDateTime();
} else if (in.getClass().getName().equals("oracle.sql.TIMESTAMP")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("timestampValue");
java.sql.Timestamp t = (java.sql.Timestamp) m.invoke(in);
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in.getClass().getName().equals("microsoft.sql.DateTimeOffset")) {
Class<?> clz = in.getClass();
try {
Method m = clz.getMethod("getTimestamp");
java.sql.Timestamp t = (java.sql.Timestamp) m.invoke(in);
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
java.sql.Timestamp t = java.sql.Timestamp.valueOf(in.toString());
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} catch (IllegalArgumentException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.sql.TimeStamp类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
java.sql.Timestamp t = java.sql.Timestamp.valueOf(clob2Str((java.sql.Clob) in));
LocalDateTime localDateTime = LocalDateTime
.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.sql.TimeStamp类型:%s", e.getMessage()));
}
} else if (in instanceof java.lang.Number) {
java.sql.Timestamp t = new java.sql.Timestamp(((java.lang.Number) in).longValue());
LocalDateTime localDateTime = LocalDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault());
return localDateTime;
}
return null;
}
/**
* 将任意类型转换为byte[]类型
*
* @param in 任意类型的对象实例
* @return byte[]类型
*/
public static byte[] castToByteArray(final Object in) {
if (in instanceof byte[]) {
return (byte[]) in;
} else if (in instanceof java.util.Date) {
return in.toString().getBytes();
} else if (in instanceof java.sql.Blob) {
return blob2Bytes((java.sql.Blob) in);
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
return in.toString().getBytes();
} else if (in instanceof java.sql.Clob) {
return clob2Str((java.sql.Clob) in).toString().getBytes();
} else {
return toByteArray(in);
}
}
/**
* 将任意类型转换为Boolean类型
*
* @param in 任意类型的对象实例
* @return Boolean类型
*/
public static Boolean castToBoolean(final Object in) {
if (in instanceof java.lang.Boolean) {
return (java.lang.Boolean) in;
} else if (in instanceof java.lang.Number) {
return ((java.lang.Number) in).intValue() != 0;
} else if (in instanceof java.lang.String || in instanceof java.lang.Character) {
try {
return Boolean.parseBoolean(in.toString());
} catch (IllegalArgumentException e) {
throw new RuntimeException(
String.format("无法将java.lang.String类型转换为java.lang.Boolean类型:%s", e.getMessage()));
}
} else if (in instanceof java.sql.Clob) {
try {
return Boolean.parseBoolean(clob2Str((java.sql.Clob) in));
} catch (NumberFormatException e) {
throw new RuntimeException(
String.format("无法将java.sql.Clob类型转换为java.lang.Boolean类型:%s", e.getMessage()));
}
}
return null;
}
}