mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 18:53:12 +08:00
fix point 乱码问题
This commit is contained in:
@ -14,11 +14,11 @@ import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.type.MysqlIndexTypeEnum;
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.SqlBuilder;
|
||||
import ai.chat2db.spi.ValueHandler;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.model.*;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static ai.chat2db.spi.util.SortUtils.sortDatabase;
|
||||
@ -294,4 +294,9 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
|
||||
public String getMetaDataName(String... names) {
|
||||
return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining("."));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueHandler getValueHandler() {
|
||||
return new MysqlValueHandler();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package ai.chat2db.plugin.mysql;
|
||||
|
||||
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.value.GeometryValueHandler;
|
||||
import ai.chat2db.spi.ValueHandler;
|
||||
import ai.chat2db.spi.jdbc.DefaultValueHandler;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
public class MysqlValueHandler extends DefaultValueHandler {
|
||||
|
||||
private static final Map<String, ValueHandler> VALUE_HANDLER_MAP = Map.of(
|
||||
MysqlColumnTypeEnum.GEOMETRY.name(), new GeometryValueHandler()
|
||||
);
|
||||
|
||||
@Override
|
||||
public String getString(ResultSet rs, int index, boolean limitSize) throws SQLException {
|
||||
Object obj = rs.getObject(index);
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
String columnTypeName = rs.getMetaData().getColumnTypeName(index);
|
||||
if (MysqlColumnTypeEnum.GEOMETRY.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.POINT.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.LINESTRING.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.POLYGON.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.MULTIPOINT.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.MULTILINESTRING.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.MULTIPOLYGON.name().equalsIgnoreCase(columnTypeName)
|
||||
|| MysqlColumnTypeEnum.GEOMETRYCOLLECTION.name().equalsIgnoreCase(columnTypeName)
|
||||
) {
|
||||
ValueHandler handler = VALUE_HANDLER_MAP.get(MysqlColumnTypeEnum.GEOMETRY.name());
|
||||
return handler.getString(rs, index, limitSize);
|
||||
} else {
|
||||
return super.getString(rs, index, limitSize);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
import ai.chat2db.spi.ValueHandler;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.WKBReader;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class GeometryValueHandler implements ValueHandler {
|
||||
@Override
|
||||
public String getString(ResultSet rs, int index, boolean limitSize) throws SQLException {
|
||||
try {
|
||||
InputStream inputStream = rs.getBinaryStream(index);
|
||||
Geometry dbGeometry = null;
|
||||
if (inputStream != null) {
|
||||
|
||||
//convert the stream to a byte[] array
|
||||
//so it can be passed to the WKBReader
|
||||
byte[] buffer = new byte[255];
|
||||
|
||||
int bytesRead = 0;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
byte[] geometryAsBytes = baos.toByteArray();
|
||||
|
||||
if (geometryAsBytes.length < 5) {
|
||||
throw new Exception("Invalid geometry inputStream - less than five bytes");
|
||||
}
|
||||
|
||||
//first four bytes of the geometry are the SRID,
|
||||
//followed by the actual WKB. Determine the SRID
|
||||
//here
|
||||
byte[] sridBytes = new byte[4];
|
||||
System.arraycopy(geometryAsBytes, 0, sridBytes, 0, 4);
|
||||
boolean bigEndian = (geometryAsBytes[4] == 0x00);
|
||||
|
||||
int srid = 0;
|
||||
if (bigEndian) {
|
||||
for (int i = 0; i < sridBytes.length; i++) {
|
||||
srid = (srid << 8) + (sridBytes[i] & 0xff);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < sridBytes.length; i++) {
|
||||
srid += (sridBytes[i] & 0xff) << (8 * i);
|
||||
}
|
||||
}
|
||||
|
||||
//use the JTS WKBReader for WKB parsing
|
||||
WKBReader wkbReader = new WKBReader();
|
||||
|
||||
//copy the byte array, removing the first four
|
||||
//SRID bytes
|
||||
byte[] wkb = new byte[geometryAsBytes.length - 4];
|
||||
System.arraycopy(geometryAsBytes, 4, wkb, 0, wkb.length);
|
||||
dbGeometry = wkbReader.read(wkb);
|
||||
dbGeometry.setSRID(srid);
|
||||
}
|
||||
return dbGeometry.toString();
|
||||
} catch (Exception e) {
|
||||
return rs.getString(index);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user