mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 18:53:12 +08:00
Optimize MysqlValueProcessor
This commit is contained in:
@ -0,0 +1,35 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
||||
import ai.chat2db.spi.model.SQLDataValue;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
* @date: 2024年06月03日 19:43
|
||||
*/
|
||||
public class MysqlBinaryProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return wrap(BaseEncoding.base16().encode(dataValue.getValue().getBytes()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return wrap(BaseEncoding.base16().encode(dataValue.getBytes()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return convertJDBCValueByType(dataValue);
|
||||
}
|
||||
|
||||
private String wrap(String value) {
|
||||
return String.format(MysqlDmlValueTemplate.BINARY_TEMPLATE, value);
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
int precision = dataValue.getPrecision();
|
||||
byte[] bytes = dataValue.getBytes();
|
||||
if (precision == 1) {
|
||||
@ -40,7 +40,7 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return getString((String) convertJDBCValueByType(dataValue));
|
||||
return getString(convertJDBCValueByType(dataValue));
|
||||
}
|
||||
|
||||
public String getString(String value) {
|
||||
|
@ -1,8 +0,0 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
* @date: 2024年06月01日 19:20
|
||||
*/
|
||||
public class MysqlDateTimeProcessor extends MysqlTimestampProcessor{
|
||||
}
|
@ -17,13 +17,13 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return new String(dataValue.getBytes());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return (String) convertJDBCValueByType(dataValue);
|
||||
return convertJDBCValueByType(dataValue);
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import java.io.InputStream;
|
||||
* @author: zgq
|
||||
* @date: 2024年06月01日 12:42
|
||||
*/
|
||||
public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
@Override
|
||||
@ -23,7 +23,7 @@ public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
try {
|
||||
InputStream inputStream = dataValue.getBinaryStream();
|
||||
Geometry dbGeometry = null;
|
||||
@ -82,15 +82,11 @@ public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
Object jdbcValue = getJdbcValue(dataValue);
|
||||
if (jdbcValue == null) {
|
||||
return super.getJdbcValueString(dataValue);
|
||||
}
|
||||
return wrap((String) jdbcValue);
|
||||
return wrap(convertJDBCValueByType(dataValue));
|
||||
}
|
||||
|
||||
private String wrap(String value) {
|
||||
return String.format(MysqlDmlValueTemplate.GEOMETRY_TEMPLATE,value);
|
||||
return String.format(MysqlDmlValueTemplate.GEOMETRY_TEMPLATE, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ public class MysqlTimestampProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return isValidTimestamp(dataValue) ? new String(dataValue.getBytes()) : "0000-00-00 00:00:00";
|
||||
}
|
||||
|
||||
|
@ -26,35 +26,53 @@ public class MysqlValueProcessor extends DefaultValueProcessor {
|
||||
, 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.TIMESTAMP.name(), new MysqlTimestampProcessor(),
|
||||
MysqlColumnTypeEnum.DATETIME.name(), new MysqlDateTimeProcessor()
|
||||
MysqlColumnTypeEnum.BINARY.name(), new MysqlBinaryProcessor()
|
||||
);
|
||||
public static final Set<String> FUNCTION_SET = Set.of("now()");
|
||||
public static final Set<String> FUNCTION_SET = Set.of("now()", "default");
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
if (FUNCTION_SET.contains(dataValue.getValue())) {
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -64,6 +82,12 @@ public class MysqlValueProcessor extends DefaultValueProcessor {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,82 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
||||
import ai.chat2db.spi.model.SQLDataValue;
|
||||
import org.apache.tika.Tika;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
* @date: 2024年06月03日 20:48
|
||||
*/
|
||||
public class MysqlVarBinaryProcessor extends MysqlBinaryProcessor {
|
||||
private final static int KB = 1024;
|
||||
private final static int MB = KB * 1024;
|
||||
private final static int GB = MB * 1024;
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
// TODO: insert file
|
||||
return super.convertSQLValueByType(dataValue);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
//TODO: Identify more file types
|
||||
InputStream binaryStream = dataValue.getBinaryStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = binaryStream.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
baos.flush();
|
||||
|
||||
InputStream copiedStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
|
||||
Tika tika = new Tika();
|
||||
String fileType = tika.detect(copiedStream);
|
||||
|
||||
if ("image/jpeg".equals(fileType)) {
|
||||
BufferedImage bufferedImage = ImageIO.read(copiedStream);
|
||||
copiedStream.reset();
|
||||
long size = baos.size();
|
||||
String unit = "B";
|
||||
if (size > GB) {
|
||||
size /= GB;
|
||||
unit = "GB";
|
||||
} else if (size > MB) {
|
||||
size /= MB;
|
||||
unit = "MB";
|
||||
} else if (size > KB) {
|
||||
size /= KB;
|
||||
unit = "KB";
|
||||
}
|
||||
return String.format(MysqlDmlValueTemplate.IMAGE_TEMPLATE, dataValue.getType(),
|
||||
bufferedImage.getWidth(), bufferedImage.getHeight(), size, unit);
|
||||
} else if ("text/plain".equals(fileType)) {
|
||||
return baos.toString(StandardCharsets.UTF_8);
|
||||
}
|
||||
return super.convertJDBCValueByType(dataValue);
|
||||
} catch (IOException e) {
|
||||
return super.convertJDBCValueByType(dataValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return super.convertJDBCValueStrByType(dataValue);
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public class MysqlYearProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
@Override
|
||||
public Object convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
Date date = dataValue.getDate();
|
||||
if (!isValidYear(dataValue)) {
|
||||
return "0000";
|
||||
@ -57,6 +57,6 @@ public class MysqlYearProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return (String) getJdbcValue(dataValue);
|
||||
return getJdbcValue(dataValue);
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,10 @@ public class MysqlDmlValueTemplate {
|
||||
public static final String COMMON_TEMPLATE = "'%s'";
|
||||
public static final String GEOMETRY_TEMPLATE = "ST_GeomFromText('%s')";
|
||||
public static final String BIT_TEMPLATE = "b'%s'";
|
||||
public static final String BINARY_TEMPLATE = "0x%s";
|
||||
|
||||
/**
|
||||
* example: [VARBINARY] 525x542 JPEG image 34.67 KB
|
||||
*/
|
||||
public static final String IMAGE_TEMPLATE = "[%s] %dx%d JPEG image %d %s";
|
||||
}
|
||||
|
Reference in New Issue
Block a user