mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 18:53:12 +08:00
Optimize code
This commit is contained in:
@ -1,11 +1,10 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.value.factory.MysqlValueProcessorFactory;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
||||
import ai.chat2db.spi.model.SQLDataValue;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -16,31 +15,6 @@ import java.util.Set;
|
||||
* attribute: [zerofill] example tinyint[5] zerofill 34->00034
|
||||
*/
|
||||
public class MysqlValueProcessor extends DefaultValueProcessor {
|
||||
|
||||
private static final Set<String> GEOMETRY_TYPE = Set.of(MysqlColumnTypeEnum.GEOMETRY.name()
|
||||
, MysqlColumnTypeEnum.POINT.name()
|
||||
, MysqlColumnTypeEnum.LINESTRING.name()
|
||||
, MysqlColumnTypeEnum.POLYGON.name()
|
||||
, MysqlColumnTypeEnum.MULTIPOINT.name()
|
||||
, MysqlColumnTypeEnum.MULTILINESTRING.name()
|
||||
, MysqlColumnTypeEnum.MULTIPOLYGON.name()
|
||||
, MysqlColumnTypeEnum.GEOMETRYCOLLECTION.name());
|
||||
|
||||
public static final Set<String> BINARY_TYPE = Set.of(MysqlColumnTypeEnum.VARBINARY.name()
|
||||
, MysqlColumnTypeEnum.BLOB.name()
|
||||
, MysqlColumnTypeEnum.LONGBLOB.name()
|
||||
, MysqlColumnTypeEnum.TINYBLOB.name()
|
||||
, MysqlColumnTypeEnum.MEDIUMBLOB.name());
|
||||
|
||||
public static final Set<String> DATE_TYPE = Set.of(MysqlColumnTypeEnum.TIMESTAMP.name(),
|
||||
MysqlColumnTypeEnum.DATETIME.name());
|
||||
|
||||
private static final Map<String, DefaultValueProcessor> PROCESSOR_MAP = Map.of(
|
||||
MysqlColumnTypeEnum.BIT.name(), new MysqlBitProcessor(),
|
||||
MysqlColumnTypeEnum.YEAR.name(), new MysqlYearProcessor(),
|
||||
MysqlColumnTypeEnum.DECIMAL.name(), new MysqlDecimalProcessor(),
|
||||
MysqlColumnTypeEnum.BINARY.name(), new MysqlBinaryProcessor()
|
||||
);
|
||||
public static final Set<String> FUNCTION_SET = Set.of("now()", "default");
|
||||
|
||||
@Override
|
||||
@ -48,46 +22,16 @@ public class MysqlValueProcessor extends DefaultValueProcessor {
|
||||
if (FUNCTION_SET.contains(dataValue.getValue().toLowerCase())) {
|
||||
return dataValue.getValue();
|
||||
}
|
||||
String dataType = dataValue.getDateTypeName();
|
||||
if (GEOMETRY_TYPE.contains(dataType.toUpperCase())) {
|
||||
return new MysqlGeometryProcessor().convertSQLValueByType(dataValue);
|
||||
}
|
||||
if (BINARY_TYPE.contains(dataType)) {
|
||||
return new MysqlVarBinaryProcessor().convertSQLValueByType(dataValue);
|
||||
}
|
||||
if (DATE_TYPE.contains(dataType)) {
|
||||
return new MysqlTimestampProcessor().convertSQLValueByType(dataValue);
|
||||
}
|
||||
return PROCESSOR_MAP.getOrDefault(dataType, new DefaultValueProcessor()).convertSQLValueByType(dataValue);
|
||||
return MysqlValueProcessorFactory.getValueProcessor(dataValue.getDateTypeName()).convertSQLValueByType(dataValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
String dataType = dataValue.getType();
|
||||
if (GEOMETRY_TYPE.contains(dataType.toUpperCase())) {
|
||||
return new MysqlGeometryProcessor().convertJDBCValueByType(dataValue);
|
||||
}
|
||||
if (BINARY_TYPE.contains(dataType)) {
|
||||
return new MysqlVarBinaryProcessor().convertJDBCValueByType(dataValue);
|
||||
}
|
||||
if (DATE_TYPE.contains(dataType)) {
|
||||
return new MysqlTimestampProcessor().convertJDBCValueByType(dataValue);
|
||||
}
|
||||
return PROCESSOR_MAP.getOrDefault(dataType, new DefaultValueProcessor()).convertJDBCValueByType(dataValue);
|
||||
return MysqlValueProcessorFactory.getValueProcessor(dataValue.getType()).convertJDBCValueByType(dataValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
String dataType = dataValue.getType();
|
||||
if (GEOMETRY_TYPE.contains(dataType.toUpperCase())) {
|
||||
return new MysqlGeometryProcessor().convertJDBCValueStrByType(dataValue);
|
||||
}
|
||||
if (BINARY_TYPE.contains(dataType)) {
|
||||
return new MysqlVarBinaryProcessor().convertJDBCValueStrByType(dataValue);
|
||||
}
|
||||
if (DATE_TYPE.contains(dataType)) {
|
||||
return new MysqlTimestampProcessor().convertJDBCValueStrByType(dataValue);
|
||||
}
|
||||
return PROCESSOR_MAP.getOrDefault(dataType, new DefaultValueProcessor()).convertJDBCValueStrByType(dataValue);
|
||||
return MysqlValueProcessorFactory.getValueProcessor(dataValue.getType()).convertJDBCValueStrByType(dataValue);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
package ai.chat2db.plugin.mysql.value.factory;
|
||||
|
||||
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.value.sub.*;
|
||||
import ai.chat2db.plugin.mysql.value.template.sub.*;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
* @date: 2024年06月03日 23:16
|
||||
*/
|
||||
public class MysqlValueProcessorFactory {
|
||||
|
||||
private static final Map<String, DefaultValueProcessor> PROCESSOR_MAP;
|
||||
|
||||
static {
|
||||
MysqlGeometryProcessor mysqlGeometryProcessor = new MysqlGeometryProcessor();
|
||||
MysqlVarBinaryProcessor mysqlVarBinaryProcessor = new MysqlVarBinaryProcessor();
|
||||
MysqlTimestampProcessor mysqlTimestampProcessor = new MysqlTimestampProcessor();
|
||||
PROCESSOR_MAP = Map.ofEntries(
|
||||
// geometry
|
||||
Map.entry(MysqlColumnTypeEnum.GEOMETRY.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.POINT.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.LINESTRING.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.POLYGON.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.MULTIPOINT.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.MULTILINESTRING.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.MULTIPOLYGON.name(), mysqlGeometryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.GEOMETRYCOLLECTION.name(), mysqlGeometryProcessor),
|
||||
// binary
|
||||
Map.entry(MysqlColumnTypeEnum.BLOB.name(), mysqlVarBinaryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.LONGBLOB.name(), mysqlVarBinaryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.TINYBLOB.name(), mysqlVarBinaryProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.MEDIUMBLOB.name(), mysqlVarBinaryProcessor),
|
||||
// timestamp
|
||||
Map.entry(MysqlColumnTypeEnum.TIMESTAMP.name(), mysqlTimestampProcessor),
|
||||
Map.entry(MysqlColumnTypeEnum.DATETIME.name(), mysqlTimestampProcessor),
|
||||
//others
|
||||
Map.entry(MysqlColumnTypeEnum.BIT.name(), new MysqlBitProcessor()),
|
||||
Map.entry(MysqlColumnTypeEnum.YEAR.name(), new MysqlYearProcessor()),
|
||||
Map.entry(MysqlColumnTypeEnum.DECIMAL.name(), new MysqlDecimalProcessor()),
|
||||
Map.entry(MysqlColumnTypeEnum.BINARY.name(), new MysqlBinaryProcessor())
|
||||
);
|
||||
}
|
||||
|
||||
public static DefaultValueProcessor getValueProcessor(String type) {
|
||||
DefaultValueProcessor processor = PROCESSOR_MAP.get(type);
|
||||
return processor==null?new DefaultValueProcessor():processor;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.server.tools.common.util.EasyStringUtils;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
@ -1,4 +1,4 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
Reference in New Issue
Block a user