Hi,
I'm using jOOQ to create new database based on existing one. Existing Postgis DB has a table with Geometry (Point,4326) type. When I'm using jOOQ to generate code, it switch this geometry type to SQLDataType.GEOMETRY. I am able to create new DB but dataType is incorrect, when I'm trying to insert anything I've receiving message:
The out of the box binding for geometry is available in the commercial jOOQ distribution only. Alternatively, you can implement your own custom binding.
How can I create custom binding to solve this issue? I've tried something like that:
import org.jooq.*;
import org.jooq.impl.DSL;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import javax.validation.constraints.NotNull;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
public class JooqGeometryToJtsGeometryBinding implements Binding<Geometry, org.locationtech.jts.geom.Geometry> {
private final WKTReader reader = new WKTReader();
@Override
public @NotNull Converter<Geometry, org.locationtech.jts.geom.Geometry> converter() {
return new Converter<>() {
@Override
public org.locationtech.jts.geom.Geometry from(Geometry geometry) {
try {
return geometry == null ? null : reader.read(geometry.data());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@Override
public Geometry to(org.locationtech.jts.geom.Geometry geometry) {
return Geometry.geometry(geometry.toString());
}
@Override
public @NotNull Class<Geometry> fromType() {
return Geometry.class;
}
@Override
public @NotNull Class<org.locationtech.jts.geom.Geometry> toType() {
return org.locationtech.jts.geom.Geometry.class;
}
};
}
@Override
public void sql(BindingSQLContext<org.locationtech.jts.geom.Geometry> bindingSQLContext) throws SQLException {
bindingSQLContext.render().visit(DSL.sql("?::geometry"));
}
@Override
public void register(BindingRegisterContext<org.locationtech.jts.geom.Geometry> bindingRegisterContext) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetStatementContext<org.locationtech.jts.geom.Geometry> bindingSetStatementContext) throws SQLException {
bindingSetStatementContext.statement()
.setObject(bindingSetStatementContext.index(), bindingSetStatementContext.convert(converter())
.value());
}
@Override
public void set(BindingSetSQLOutputContext<org.locationtech.jts.geom.Geometry> bindingSetSQLOutputContext) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(BindingGetResultSetContext<org.locationtech.jts.geom.Geometry> bindingGetResultSetContext) throws SQLException {
bindingGetResultSetContext.convert(converter()).value((Geometry) bindingGetResultSetContext.resultSet()
.getObject(bindingGetResultSetContext.index()));
}
@Override
public void get(BindingGetStatementContext<org.locationtech.jts.geom.Geometry> bindingGetStatementContext) throws SQLException {
bindingGetStatementContext.convert(converter())
.value((Geometry) bindingGetStatementContext.statement()
.getObject(bindingGetStatementContext.index()));
}
@Override
public void get(BindingGetSQLInputContext<org.locationtech.jts.geom.Geometry> bindingGetSQLInputContext) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
with this xml changes in pom.xml :
<forcedTypes>
<forcedType>
<!-- Specify the Java type of your custom type. This corresponds to the Binding's <U> type. -->
<userType>org.jooq.impl.Geometry</userType>
<!-- Associate that custom type with your binding. -->
<binding>my.custom.package.JooqGeometryToJtsGeometryBinding</binding>
</forcedType>
</forcedTypes>
But compiling doesn't work due to issues with:
import org.jooq.impl.Geometry; cannot resolve symbol 'Geometry'
in generated class≥