mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 02:32:33 +08:00
SQLValueProcessor
This commit is contained in:
@ -0,0 +1,71 @@
|
||||
package ai.chat2db.plugin.mysql.type;
|
||||
|
||||
import ai.chat2db.spi.SQLValueProcessor;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.WKBReader;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public enum MysqlValueProcessorEnum implements SQLValueProcessor {
|
||||
GEOMETRY{
|
||||
@Override
|
||||
public String getSqlValueString(ResultSet rs, int index) 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package ai.chat2db.plugin.mysql.value;
|
||||
|
||||
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.type.MysqlValueProcessorEnum;
|
||||
import ai.chat2db.spi.jdbc.DefaultSQLValueProcessor;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @author: zgq
|
||||
* @date: 2024年05月24日 21:02
|
||||
*/
|
||||
public class MysqlValueProcessor extends DefaultSQLValueProcessor {
|
||||
/**
|
||||
* @param rs
|
||||
* @param index
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public String getSqlValueString(ResultSet rs, int index) 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)
|
||||
) {
|
||||
return MysqlValueProcessorEnum.GEOMETRY.getSqlValueString(rs, index);
|
||||
} else {
|
||||
super.getSqlValueString(rs, index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user