Revert "optimize MysqlValueProcessor & OracleValueProcessor"

This reverts commit 0d80a36a6fc11c6b360387c7a998f602ed2a0ab4.
This commit is contained in:
zgq
2024-06-28 22:27:05 +08:00
parent 0d80a36a6f
commit c4955bc72b
29 changed files with 347 additions and 415 deletions

View File

@ -45,6 +45,7 @@ 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())
);

View File

@ -1,6 +1,5 @@
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;
@ -13,28 +12,18 @@ public class MysqlBinaryProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
String value = dataValue.getValue();
if (value.startsWith("0x")) {
return value;
}
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
return dataValue.getBlobHexString();
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
byte[] bytes = dataValue.getBytes();
if (bytes.length == 1) {
if (bytes[0] >= 32 && bytes[0] <= 126) {
return new String(bytes);
}
}
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
return dataValue.getBlobHexString();
}
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
return dataValue.getBlobHexString();
}
}

View File

@ -8,7 +8,6 @@ import ai.chat2db.spi.model.SQLDataValue;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
import java.util.function.Function;
/**
* @author: zgq
@ -24,16 +23,23 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return getValue(dataValue, s -> s);
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);
}
@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) {
@ -45,7 +51,7 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
return String.valueOf(dataValue.getInt());
}
//bit(m) m: 2~64
return function.apply(EasyStringUtils.getBitString(bytes, precision));
return wrap(EasyStringUtils.getBitString(bytes, precision));
}
public String getString(String value) {
@ -59,10 +65,10 @@ public class MysqlBitProcessor extends DefaultValueProcessor {
if (StringUtils.isBlank(value)) {
return "NULL";
}
return MysqlDmlValueTemplate.wrapBit(value);
return wrap(value);
}
private String wrap(String value) {
return MysqlDmlValueTemplate.wrapBit(value);
return String.format(MysqlDmlValueTemplate.BIT_TEMPLATE, value);
}
}

View File

@ -12,7 +12,7 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
return dataValue.getValue();
return super.convertSQLValueByType(dataValue);
}
@ -24,6 +24,6 @@ public class MysqlDecimalProcessor extends DefaultValueProcessor {
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return dataValue.getBigDecimalString();
return convertJDBCValueByType(dataValue);
}
}

View File

@ -6,8 +6,6 @@ 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;
@ -19,11 +17,9 @@ 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 MysqlDmlValueTemplate.wrapGeometry(dataValue.getValue());
return wrap(dataValue.getValue());
}
@Override
@ -79,15 +75,18 @@ public class MysqlGeometryProcessor extends DefaultValueProcessor {
}
return dbGeometry != null ? dbGeometry.toString() : null;
} catch (Exception e) {
log.warn("Error converting database geometry", e);
return dataValue.getStringValue();
return super.getJdbcValue(dataValue);
}
}
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return MysqlDmlValueTemplate.wrapGeometry(convertJDBCValueByType(dataValue));
return wrap(convertJDBCValueByType(dataValue));
}
private String wrap(String value) {
return String.format(MysqlDmlValueTemplate.GEOMETRY_TEMPLATE, value);
}
}

View File

@ -4,8 +4,11 @@ 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
@ -16,19 +19,31 @@ public class MysqlTextProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
return EasyStringUtils.escapeAndQuoteString(dataValue.getValue());
return wrap(dataValue.getValue());
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return dataValue.getClobString();
return getClobString(dataValue, super::convertJDBCValueByType);
}
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return EasyStringUtils.escapeAndQuoteString(dataValue.getClobString());
return wrap(getClobString(dataValue, super::convertJDBCValueStrByType));
}
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);
}
}

View File

@ -1,6 +1,6 @@
package ai.chat2db.plugin.mysql.value.sub;
import ai.chat2db.server.tools.common.util.EasyStringUtils;
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;
@ -13,18 +13,28 @@ public class MysqlTimestampProcessor 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 isValidTimestamp(dataValue) ? new String(dataValue.getBytes()) : "0000-00-00 00:00:00";
}
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 EasyStringUtils.quoteString(new String(dataValue.getBytes()));
return String.format(MysqlDmlValueTemplate.COMMON_TEMPLATE, convertJDBCValueByType(dataValue));
}
}

View File

@ -1,9 +1,10 @@
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;
/**
@ -15,24 +16,26 @@ public class MysqlVarBinaryProcessor extends DefaultValueProcessor {
@Override
public String convertSQLValueByType(SQLDataValue dataValue) {
String value = dataValue.getValue();
if (value.startsWith("0x")) {
return value;
}
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
// TODO: insert file
return super.convertSQLValueByType(dataValue);
}
@Override
public String convertJDBCValueByType(JDBCDataValue dataValue) {
return dataValue.getBlobString();
try {
return dataValue.getBlobString();
} catch (Exception e) {
log.warn("convertJDBCValue error database: {} , error dataType: {} ",
Chat2DBContext.getDBConfig().getDbType(), dataValue.getType(), e);
return super.convertJDBCValueByType(dataValue);
}
}
@Override
public String convertJDBCValueStrByType(JDBCDataValue dataValue) {
return MysqlDmlValueTemplate.wrapHex(dataValue.getBlobHexString());
return dataValue.getBlobHexString();
}
}

View File

@ -0,0 +1,62 @@
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);
}
}

View File

@ -6,20 +6,7 @@ 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);
}
}