mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 10:43:06 +08:00
optimize MysqlValueProcessor & OracleValueProcessor
This commit is contained in:
@ -45,7 +45,6 @@ public class MysqlValueProcessorFactory {
|
||||
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())
|
||||
);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
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;
|
||||
@ -12,18 +13,28 @@ public class MysqlBinaryProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return dataValue.getBlobHexString();
|
||||
String value = dataValue.getValue();
|
||||
if (value.startsWith("0x")) {
|
||||
return value;
|
||||
}
|
||||
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return dataValue.getBlobHexString();
|
||||
byte[] bytes = dataValue.getBytes();
|
||||
if (bytes.length == 1) {
|
||||
if (bytes[0] >= 32 && bytes[0] <= 126) {
|
||||
return new String(bytes);
|
||||
}
|
||||
}
|
||||
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return dataValue.getBlobHexString();
|
||||
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import ai.chat2db.spi.model.SQLDataValue;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
@ -23,23 +24,16 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
int precision = dataValue.getPrecision();
|
||||
byte[] bytes = dataValue.getBytes();
|
||||
if (precision == 1) {
|
||||
//bit(1) [1 -> true] [0 -> false]
|
||||
if (bytes.length == 1 && (bytes[0] == 0 || bytes[0] == 1)) {
|
||||
return String.valueOf(dataValue.getBoolean());
|
||||
}
|
||||
// tinyint(1)
|
||||
return String.valueOf(dataValue.getInt());
|
||||
}
|
||||
//bit(m) m: 1~64
|
||||
return EasyStringUtils.getBitString(bytes, precision);
|
||||
return getValue(dataValue, s -> s);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return getValue(dataValue, this::wrap);
|
||||
}
|
||||
|
||||
private String getValue(JDBCDataValue dataValue, Function<String, String> function) {
|
||||
int precision = dataValue.getPrecision();
|
||||
byte[] bytes = dataValue.getBytes();
|
||||
if (precision == 1) {
|
||||
@ -51,7 +45,7 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
|
||||
return String.valueOf(dataValue.getInt());
|
||||
}
|
||||
//bit(m) m: 2~64
|
||||
return wrap(EasyStringUtils.getBitString(bytes, precision));
|
||||
return function.apply(EasyStringUtils.getBitString(bytes, precision));
|
||||
}
|
||||
|
||||
public String getString(String value) {
|
||||
@ -65,10 +59,10 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return "NULL";
|
||||
}
|
||||
return wrap(value);
|
||||
return MysqlDmlValueTemplate.wrapBit(value);
|
||||
}
|
||||
|
||||
private String wrap(String value) {
|
||||
return String.format(MysqlDmlValueTemplate.BIT_TEMPLATE, value);
|
||||
return MysqlDmlValueTemplate.wrapBit(value);
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return super.convertSQLValueByType(dataValue);
|
||||
return dataValue.getValue();
|
||||
}
|
||||
|
||||
|
||||
@ -24,6 +24,6 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return convertJDBCValueByType(dataValue);
|
||||
return dataValue.getBigDecimalString();
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import ai.chat2db.spi.model.JDBCDataValue;
|
||||
import ai.chat2db.spi.model.SQLDataValue;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.WKBReader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
@ -17,9 +19,11 @@ import java.io.InputStream;
|
||||
public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MysqlGeometryProcessor.class);
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return wrap(dataValue.getValue());
|
||||
return MysqlDmlValueTemplate.wrapGeometry(dataValue.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,18 +79,15 @@ public class MysqlGeometryProcessor extends DefaultValueProcessor {
|
||||
}
|
||||
return dbGeometry != null ? dbGeometry.toString() : null;
|
||||
} catch (Exception e) {
|
||||
return super.getJdbcValue(dataValue);
|
||||
log.warn("Error converting database geometry", e);
|
||||
return dataValue.getStringValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return wrap(convertJDBCValueByType(dataValue));
|
||||
}
|
||||
|
||||
private String wrap(String value) {
|
||||
return String.format(MysqlDmlValueTemplate.GEOMETRY_TEMPLATE, value);
|
||||
return MysqlDmlValueTemplate.wrapGeometry(convertJDBCValueByType(dataValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,11 +4,8 @@ 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
|
||||
@ -19,31 +16,19 @@ public class MysqlTextProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return wrap(dataValue.getValue());
|
||||
return EasyStringUtils.escapeAndQuoteString(dataValue.getValue());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return getClobString(dataValue, super::convertJDBCValueByType);
|
||||
return dataValue.getClobString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return wrap(getClobString(dataValue, super::convertJDBCValueStrByType));
|
||||
return EasyStringUtils.escapeAndQuoteString(dataValue.getClobString());
|
||||
}
|
||||
|
||||
private String getClobString(JDBCDataValue dataValue, Function<JDBCDataValue, String> function) {
|
||||
try {
|
||||
return dataValue.getClobString();
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.plugin.mysql.value.template.MysqlDmlValueTemplate;
|
||||
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;
|
||||
@ -13,28 +13,18 @@ public class MysqlTimestampProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return super.convertSQLValueByType(dataValue);
|
||||
return dataValue.getValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
return isValidTimestamp(dataValue) ? new String(dataValue.getBytes()) : "0000-00-00 00:00:00";
|
||||
return new String(dataValue.getBytes());
|
||||
}
|
||||
|
||||
protected boolean isValidTimestamp(JDBCDataValue data) {
|
||||
byte[] buffer = data.getBytes();
|
||||
String stringValue = new String(buffer);
|
||||
return stringValue.length() <= 0
|
||||
|| stringValue.charAt(0) != '0'
|
||||
|| !"0000-00-00".equals(stringValue)
|
||||
&& !"0000-00-00 00:00:00".equals(stringValue)
|
||||
&& !"00000000000000".equals(stringValue)
|
||||
&& !"0".equals(stringValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return String.format(MysqlDmlValueTemplate.COMMON_TEMPLATE, convertJDBCValueByType(dataValue));
|
||||
return EasyStringUtils.quoteString(new String(dataValue.getBytes()));
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
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 ai.chat2db.spi.sql.Chat2DBContext;
|
||||
import ch.qos.logback.core.model.processor.DefaultProcessor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
@ -16,26 +15,24 @@ public class MysqlVarBinaryProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
// TODO: insert file
|
||||
return super.convertSQLValueByType(dataValue);
|
||||
String value = dataValue.getValue();
|
||||
if (value.startsWith("0x")) {
|
||||
return value;
|
||||
}
|
||||
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
try {
|
||||
return dataValue.getBlobString();
|
||||
} catch (Exception e) {
|
||||
log.warn("convertJDBCValue error database: {} , error dataType: {} ",
|
||||
Chat2DBContext.getDBConfig().getDbType(), dataValue.getType(), e);
|
||||
return super.convertJDBCValueByType(dataValue);
|
||||
}
|
||||
return dataValue.getBlobString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return dataValue.getBlobHexString();
|
||||
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,62 +0,0 @@
|
||||
package ai.chat2db.plugin.mysql.value.sub;
|
||||
|
||||
import ai.chat2db.spi.jdbc.DefaultValueProcessor;
|
||||
import ai.chat2db.spi.model.JDBCDataValue;
|
||||
import ai.chat2db.spi.model.SQLDataValue;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
* 功能描述
|
||||
*
|
||||
* @author: zgq
|
||||
* @date: 2024年06月01日 12:57
|
||||
*/
|
||||
public class MysqlYearProcessor extends DefaultValueProcessor {
|
||||
|
||||
@Override
|
||||
public String convertSQLValueByType(SQLDataValue dataValue) {
|
||||
return dataValue.getValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueByType(JDBCDataValue dataValue) {
|
||||
Date date = dataValue.getDate();
|
||||
if (!isValidYear(dataValue)) {
|
||||
return "0000";
|
||||
}
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
String yStr;
|
||||
String yZerosPadding = "0000";
|
||||
if (year < 1000) {
|
||||
yStr = "" + year;
|
||||
yStr = yZerosPadding.substring(0, (4 - yStr.length())) + yStr;
|
||||
} else {
|
||||
yStr = "" + year;
|
||||
}
|
||||
return yStr;
|
||||
}
|
||||
|
||||
private boolean isValidYear(JDBCDataValue data) {
|
||||
byte[] buffer = data.getBytes();
|
||||
String stringValue = new String(buffer);
|
||||
return stringValue.length() <= 0
|
||||
|| stringValue.charAt(0) != '0'
|
||||
|| !"0000-00-00".equals(stringValue)
|
||||
&& !"0000-00-00 00:00:00".equals(stringValue)
|
||||
&& !"00000000000000".equals(stringValue)
|
||||
&& !"0".equals(stringValue)
|
||||
&& !"00000000".equals(stringValue)
|
||||
&& !"0000".equals(stringValue);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
|
||||
return getJdbcValue(dataValue);
|
||||
}
|
||||
}
|
@ -6,7 +6,20 @@ package ai.chat2db.plugin.mysql.value.template;
|
||||
*/
|
||||
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 HEX_TEMPLATE = "0x%s";
|
||||
|
||||
|
||||
public static String wrapGeometry(String value) {
|
||||
return String.format(GEOMETRY_TEMPLATE, value);
|
||||
}
|
||||
|
||||
public static String wrapBit(String value) {
|
||||
return String.format(BIT_TEMPLATE, value);
|
||||
}
|
||||
|
||||
public static String wrapHex(String value) {
|
||||
return String.format(HEX_TEMPLATE, value);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user