OracleValueProcessor

This commit is contained in:
zgq
2024-06-05 20:17:33 +08:00
parent de9c187a68
commit f0b90cab0d
21 changed files with 638 additions and 74 deletions

View File

@ -18,7 +18,13 @@ public class MysqlValueProcessorFactory {
MysqlGeometryProcessor mysqlGeometryProcessor = new MysqlGeometryProcessor();
MysqlVarBinaryProcessor mysqlVarBinaryProcessor = new MysqlVarBinaryProcessor();
MysqlTimestampProcessor mysqlTimestampProcessor = new MysqlTimestampProcessor();
MysqlTextProcessor mysqlTextProcessor = new MysqlTextProcessor();
PROCESSOR_MAP = Map.ofEntries(
//text
Map.entry(MysqlColumnTypeEnum.TEXT.name(), mysqlTextProcessor),
Map.entry(MysqlColumnTypeEnum.TINYTEXT.name(), mysqlTextProcessor),
Map.entry(MysqlColumnTypeEnum.MEDIUMTEXT.name(), mysqlTextProcessor),
Map.entry(MysqlColumnTypeEnum.LONGTEXT.name(), mysqlTextProcessor),
// geometry
Map.entry(MysqlColumnTypeEnum.GEOMETRY.name(), mysqlGeometryProcessor),
Map.entry(MysqlColumnTypeEnum.POINT.name(), mysqlGeometryProcessor),

View File

@ -14,13 +14,13 @@ public class MysqlBinaryProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
return wrap(BaseEncoding.base16().encode(dataValue.getValue().getBytes()));
return dataValue.getBlobHexString();
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return wrap(BaseEncoding.base16().encode(dataValue.getBytes()));
return dataValue.getBlobHexString();
}
@ -28,8 +28,4 @@ public class MysqlBinaryProcessor extends DefaultValueProcessor {
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return convertJDBCValueByType(dataValue);
}
private String wrap(String value) {
return String.format(MysqlDmlValueTemplate.BINARY_TEMPLATE, value);
}
}

View File

@ -12,13 +12,13 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
return dataValue.getValue();
return super.convertSQLValueByType(dataValue);
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return new String(dataValue.getBytes());
return dataValue.getBigDecimalString();
}

View File

@ -0,0 +1,49 @@
package ai.chat2db.plugin.mysql.value.sub;
import ai.chat2db.server.tools.common.util.EasyStringUtils;
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
import ai.chat2db.spi.model.JDBCDataValue;
import ai.chat2db.spi.model.SQLDataValue;
import ai.chat2db.spi.sql.Chat2DBContext;
import lombok.extern.slf4j.Slf4j;
import java.util.function.Function;
/**
* @author: zgq
* @date: 2024年06月05日 0:11
*/
@Slf4j
public class MysqlTextProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
return wrap(dataValue.getValue());
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return getClobString(dataValue, true, super::convertJDBCValueByType);
}
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return wrap(getClobString(dataValue, false, super::convertJDBCValueStrByType));
}
private String getClobString(JDBCDataValue dataValue, boolean limitSize, Function<JDBCDataValue, String> function) {
try {
return dataValue.getClobString(limitSize);
} catch (Exception e) {
log.warn("convertJDBCValue error database: {} , error dataType: {} ",
Chat2DBContext.getDBConfig().getDbType(), dataValue.getType(), e);
return function.apply(dataValue);
}
}
private String wrap(String value) {
return EasyStringUtils.escapeAndQuoteString(value);
}
}

View File

@ -1,26 +1,16 @@
package ai.chat2db.plugin.mysql.value.sub;
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;
import ai.chat2db.spi.sql.Chat2DBContext;
import lombok.extern.slf4j.Slf4j;
/**
* @author: zgq
* @date: 2024年06月03日 20:48
*/
@Slf4j
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) {
@ -31,44 +21,11 @@ public class MysqlVarBinaryProcessor extends MysqlBinaryProcessor {
@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 dataValue.getBlobString(true);
} catch (Exception e) {
log.warn("convertJDBCValueByType error database: {} , error dataType: {} ",
Chat2DBContext.getDBConfig().getDbType(), dataValue.getType(), e);
return super.convertJDBCValueByType(dataValue);
}
}

View File

@ -9,10 +9,4 @@ 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";
}