chore: optimize SqlUtil.java

This commit is contained in:
Michael Yang 2024-10-05 15:08:52 +08:00
parent 539eeb16b8
commit 9f142276da
3 changed files with 41 additions and 3 deletions

View File

@ -98,14 +98,15 @@ public class EnumWrapper<E extends Enum<E>> {
* @param object * @param object
* @return * @return
*/ */
public Object getEnumValue(E object) { public Object getEnumValue(Object object) {
try { try {
if (getterMethod != null) { if (getterMethod != null) {
return getterMethod.invoke(object); return getterMethod.invoke(object);
} else if(property != null){ } else if(property != null){
return property.get(object); return property.get(object);
} else { } else {
return object.name(); //noinspection unchecked
return ((E)object).name();
} }
} catch (Exception e) { } catch (Exception e) {
throw FlexExceptions.wrap(e); throw FlexExceptions.wrap(e);

View File

@ -15,7 +15,13 @@
*/ */
package com.mybatisflex.core.util; package com.mybatisflex.core.util;
import com.mybatisflex.core.audit.AuditMessage;
import com.mybatisflex.core.mybatis.TypeHandlerObject;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
@ -163,6 +169,26 @@ public class SqlUtil {
} }
return joiner.toString(); return joiner.toString();
} }
// TypeHandlerObject
else if (value instanceof TypeHandlerObject) {
TypeHandlerObject handlerObject = (TypeHandlerObject) value;
String[] paramArray = new String[1];
PreparedStatement preparedStatement = createPreparedStatement(paramArray);
try {
handlerObject.setParameter(preparedStatement, 0);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return paramArray[0];
}
// enum
else if (value.getClass().isEnum()) {
EnumWrapper<?> ew = EnumWrapper.of(value.getClass());
Object enumValue = ew.getEnumValue(value);
return enumValue.toString();
}
// other // other
else { else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -192,4 +218,15 @@ public class SqlUtil {
return sb.toString(); return sb.toString();
} }
private static PreparedStatement createPreparedStatement(String[] params) {
return (PreparedStatement) Proxy.newProxyInstance(
AuditMessage.class.getClassLoader(),
new Class[]{PreparedStatement.class}, (proxy, method, args) -> {
if (args != null && (args.length == 2 || args.length == 3)) {
params[0] = args[1].toString();
}
return null;
});
}
} }

View File

@ -51,7 +51,7 @@ public class EnumTypeHandler<E extends Enum<E> & BaseEnum> extends BaseTypeHandl
* @param value 枚举值 * @param value 枚举值
* @return 枚举对象 * @return 枚举对象
*/ */
private E getEnumByValue(int value) { public E getEnumByValue(int value) {
return Stream.of(enumClass.getEnumConstants()).filter(e -> e.getValue() == value).findAny().orElse(null); return Stream.of(enumClass.getEnumConstants()).filter(e -> e.getValue() == value).findAny().orElse(null);
} }
} }