我正在尝试在使用jOOQ(3.9.x)时在MySQL POLYGON类型和Jts Polygon之间进行无缝转换。理想情况下,我想将数据库中的WKT(众所周知的文本)解析为Jts类型。但是,生成的查询不起作用,我看到它们通过转换为文本的函数得到简单的引号,使其只呈现文本。这是我正在使用的转换器和绑定。我这样做了吗?我如何通过ST_AsWKT和ST_GeomFromText进行转换?
public class PolygonConverter implements Converter<Object, Polygon> {
/**
* Convert WK string into Polygon
*/
@Override
public Polygon from(Object databaseObject) {
if (databaseObject == null) {
return null;
}
String wkString = databaseObject.toString();
WKTReader reader = JtsSpatialContext.GEO.getWktShapeParser();
try {
Polygon poly = (Polygon)reader.parse(wkString);
return poly;
} catch (java.text.ParseException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public Object to(Polygon userObject) {
if (userObject == null) {
return null;
}
return userObject.toString();
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<Polygon> toType() {
return Polygon.class;
}
}
绑定:
public class MySQLPolygonBinding implements Binding<Object, Polygon> {
@Override
public Converter<Object, Polygon> converter() {
return new PolygonConverter();
}
@Override
public void sql(BindingSQLContext<Polygon> ctx) throws SQLException {
ctx.render().visit(DSL.sql("ST_AsWKT(?)"));
}
@Override
public void register(BindingRegisterContext<Polygon> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetStatementContext<Polygon> ctx) throws SQLException {
String resultStr = null;
Object obj = ctx.convert(converter()).value();
if (obj != null) {
resultStr = String.format("ST_GeomFromText('%s')", obj,toString() );
}
ctx.statement().setObject(ctx.index(), resultStr);
}
@Override
public void set(BindingSetSQLOutputContext<Polygon> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(BindingGetResultSetContext<Polygon> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
@Override
public void get(BindingGetStatementContext<Polygon> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
@Override
public void get(BindingGetSQLInputContext<Polygon> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
答案 0 :(得分:3)
根据我的理解,你想要自动发生两件事:
"_id" : ObjectId("592595a1bca1350220027666"),
"user_account" : "5-5XX4XX-X06",
"Xns" : {
"Xn" : [
{
"date" : "2015-09-01",
"narration" : "ATM/CASH WDL/01-01-10/17:48:45/0",
"amount" : "-2500.00",
"category" : "Others",
"balance" : "110578.04",
"_id" : ObjectId("592595a1bca13502200276ca")
},
{
"date" : "2015-09-04",
"narration" : "EBA//20100104081107",
"amount" : "-7206.12",
"category" : "Others",
"balance" : "103371.92",
"_id" : Object Id("592595a1bca13502200276c9")
}
函数这是“简单”的部分,你几乎做对了:
ST_GeomFromText()
@Override
public void sql(BindingSQLContext<Polygon> ctx) throws SQLException {
ctx.render()
.sql("ST_GeomFromText(")
// This will use your converter to convert from a Polygon to "Object"
// prior to binding the variable
.visit(DSL.val(ctx.convert(converter()).value()))
.sql(")");
}
@Override
public void set(BindingSetStatementContext<Polygon> ctx) throws SQLException {
// No wrapping of the bind variable in functions can be done here!
ctx.statement().setString(ctx.index(), ctx.convert(converter()).value());
}
函数您(可能)不必在绑定内部执行任何特定操作,因为绑定不会影响任何不涉及绑定变量的表达式(例如,普通的列表达式)。
相反,您可能需要调整转换器(绑定中的转换器)才能读取从JDBC驱动程序返回的任何类型并将其转换为ST_AsWKT()