diff --git a/README.md b/README.md index de1e338..0b67c1f 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ jooq-postgis-spatial Geometry org.locationtech.jts.geom.Geometry - net.dmitry.jooq.postgis.spatial.binding.PostgisGeometryBinding + net.dmitry.jooq.postgis.spatial.jts.PostgisGeometryBinding diff --git a/pom.xml b/pom.xml index fa92074..7143f5e 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ ${compileSource} + org.jetbrains.kotlin kotlin-maven-plugin @@ -70,7 +71,6 @@ compile - compile compile @@ -98,12 +98,12 @@ - 1.8 + 16 1.5.21 - 9.4-1201-jdbc4 - 3.7.3 - 1.18.1 - 2.2.0 + 42.2.14 + 3.15.1 + 1.18.2 + 2.5.1 diff --git a/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryBinding.java b/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryBinding.java new file mode 100644 index 0000000..0f95aa7 --- /dev/null +++ b/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryBinding.java @@ -0,0 +1,54 @@ +package net.dmitry.jooq.postgis.spatial.jts; + +import org.jooq.*; +import org.jooq.impl.DSL; +import org.locationtech.jts.geom.Geometry; +//import org.postgis.Geometry; + +import java.sql.SQLException; + +public class PostgisGeometryBinding implements Binding { + private final PostgisGeometryConverter geometryConverter = new PostgisGeometryConverter(); + + + public Converter converter() { + return geometryConverter; + } + + @Override + public void sql(BindingSQLContext ctx) throws SQLException { + ctx.render().visit(DSL.sql("?::geometry")); + } + + @Override + public void register(BindingRegisterContext ctx) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void set(BindingSetStatementContext ctx) throws SQLException { + ctx.statement().setObject(ctx.index(), ctx.convert(converter()).value()); + } + + @Override + public void set(BindingSetSQLOutputContext ctx) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void get(BindingGetResultSetContext ctx) throws SQLException { + ctx.convert(converter()).value(ctx.resultSet().getObject(ctx.index())); + } + + @Override + public void get(BindingGetStatementContext ctx) throws SQLException { + ctx.convert(converter()).value(ctx.statement().getObject(ctx.index())); + } + + @Override + public void get(BindingGetSQLInputContext ctx) throws SQLException { + throw new UnsupportedOperationException(); + } + + +} diff --git a/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryConverter.java b/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryConverter.java new file mode 100644 index 0000000..b189ecc --- /dev/null +++ b/src/main/java/net/dmitry/jooq/postgis/spatial/jts/PostgisGeometryConverter.java @@ -0,0 +1,56 @@ +package net.dmitry.jooq.postgis.spatial.jts; + +import net.dmitry.jooq.postgis.spatial.converter.JTSGeometryConverter; +import org.jetbrains.annotations.NotNull; +import org.jooq.Converter; +import org.locationtech.jts.geom.Geometry; +import org.postgis.PGgeometry; +import org.postgresql.util.PGobject; + +import java.sql.SQLException; + +public class PostgisGeometryConverter implements Converter { + @Override + public Geometry from(Object o) { + if (o == null) { + return null; + } else { + try { + org.postgis.Geometry g = new PGgeometry(o.toString()).getGeometry(); + return new JTSGeometryConverter().toJTS(g); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return null; + } + } + } + + @Override + public Object to(Geometry geom) { + if (geom == null) { + return null; + } else { + PGobject p = new PGobject(); + p.setType("Geometry"); + try { + p.setValue(geom.toText()); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return null; + } + return p; + } + } + + @Override + public @NotNull + Class fromType() { + return Object.class; + } + + @Override + public @NotNull + Class toType() { + return Geometry.class; + } +} diff --git a/src/main/java/net/dmitry/jooq/postgis/spatial/jts/package-info.java b/src/main/java/net/dmitry/jooq/postgis/spatial/jts/package-info.java deleted file mode 100644 index 88357c1..0000000 --- a/src/main/java/net/dmitry/jooq/postgis/spatial/jts/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Copied from Hibernate Spatial 4.3 project - */ -package net.dmitry.jooq.postgis.spatial.jts; \ No newline at end of file diff --git a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/binding/PostgisGeometryBinding.kt b/src/main/kotlin/net/dmitry/jooq/postgis/spatial/binding/PostgisGeometryBinding.kt deleted file mode 100644 index 7d1327d..0000000 --- a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/binding/PostgisGeometryBinding.kt +++ /dev/null @@ -1,40 +0,0 @@ -package net.dmitry.jooq.postgis.spatial.binding - -import net.dmitry.jooq.postgis.spatial.converter.PostgisGeometryConverter -import org.jooq.* -import org.jooq.impl.DSL -import org.postgis.Geometry - -/** - * @author Dmitry Zhuravlev - * Date: 07.03.16 - */ -class PostgisGeometryBinding : Binding { - - private val geometryConverter = PostgisGeometryConverter(); - - override fun converter(): Converter? = geometryConverter - - override fun set(ctx: BindingSetStatementContext?) { - ctx?.statement()?.setObject(ctx.index(), ctx.convert(converter()).value()) - } - - override fun get(ctx: BindingGetStatementContext?) { - ctx?.convert(converter())?.value(ctx.statement().getObject(ctx.index())); - } - - override fun get(ctx: BindingGetResultSetContext?) { - ctx?.convert(converter())?.value(ctx.resultSet().getObject(ctx.index())); - } - - override fun sql(ctx: BindingSQLContext?) { - ctx?.render()?.visit(DSL.sql("?::geometry")) - } - - override fun get(ctx: BindingGetSQLInputContext?) = throw UnsupportedOperationException() - - override fun set(ctx: BindingSetSQLOutputContext?) = throw UnsupportedOperationException() - - override fun register(ctx: BindingRegisterContext?) = throw UnsupportedOperationException() - -} \ No newline at end of file diff --git a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverter.kt b/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverter.kt index 1842ee5..78b108c 100644 --- a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverter.kt +++ b/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverter.kt @@ -4,6 +4,7 @@ import org.locationtech.jts.geom.Coordinate import org.locationtech.jts.geom.Geometry import org.locationtech.jts.geom.GeometryFactory import net.dmitry.jooq.postgis.spatial.jts.JTS +import net.dmitry.jooq.postgis.spatial.jts.PostgisGeometryConverter import net.dmitry.jooq.postgis.spatial.jts.mgeom.MCoordinate import net.dmitry.jooq.postgis.spatial.jts.mgeom.MGeometry import net.dmitry.jooq.postgis.spatial.jts.mgeom.MGeometryFactory @@ -17,7 +18,8 @@ import org.postgis.* */ class JTSGeometryConverter : Converter { - private val postgisGeometryConverter = PostgisGeometryConverter() + private val postgisGeometryConverter = + PostgisGeometryConverter() override fun from(obj: Any?): Geometry? = toJTS(postgisGeometryConverter.from(obj)) @@ -33,7 +35,7 @@ class JTSGeometryConverter : Converter { } - fun toJTS(obj: Any?): Geometry? { + public fun toJTS(obj: Any?): Geometry? { var objNotNull = obj ?: return null // in some cases, Postgis returns not PGgeometry objects // but org.postgis.Geometry instances. @@ -222,7 +224,7 @@ class JTSGeometryConverter : Converter { * * * @return native database geometry object corresponding to jtsGeom. */ - protected fun toNative(jtsGeom: Geometry): PGgeometry { + public fun toNative(jtsGeom: Geometry): PGgeometry { val jtsGeomNotNull = forceEmptyToGeometryCollection(jtsGeom) val geom: org.postgis.Geometry? = when (jtsGeomNotNull) { is org.locationtech.jts.geom.Point -> { diff --git a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/PostgisGeometryConverter.kt b/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/PostgisGeometryConverter.kt deleted file mode 100644 index 668dbb0..0000000 --- a/src/main/kotlin/net/dmitry/jooq/postgis/spatial/converter/PostgisGeometryConverter.kt +++ /dev/null @@ -1,32 +0,0 @@ -package net.dmitry.jooq.postgis.spatial.converter - -import org.jooq.Converter -import org.postgis.Geometry -import org.postgis.PGgeometry -import org.postgresql.util.PGobject - -/** - * @author Dmitry Zhuravlev - * Date: 07.03.16 - */ -class PostgisGeometryConverter : Converter { - - override fun from(obj: Any?): Geometry? = - if (obj == null) { - null - } else PGgeometry.geomFromString(obj.toString()) - - - override fun to(geom: Geometry?): Any? = - if (geom == null) { - null - } else - PGobject().apply { - type = geom.typeString - value = geom.value - } - - override fun toType(): Class? = Geometry::class.java - - override fun fromType(): Class? = Any::class.java -} \ No newline at end of file diff --git a/src/test/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverterTest.kt b/src/test/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverterTest.kt deleted file mode 100644 index 48107a5..0000000 --- a/src/test/kotlin/net/dmitry/jooq/postgis/spatial/converter/JTSGeometryConverterTest.kt +++ /dev/null @@ -1,37 +0,0 @@ -package net.dmitry.jooq.postgis.spatial.converter - -import org.locationtech.jts.geom.Coordinate -import org.locationtech.jts.geom.Geometry -import net.dmitry.jooq.postgis.spatial.jts.JTS -import org.junit.Assert.assertTrue -import org.junit.Test -import org.postgresql.util.PGobject - -/** - * @author Dmitry Zhuravlev - * * Date: 08.03.16 - */ -class JTSGeometryConverterTest { - - val jtsGeometryConverter = JTSGeometryConverter() - - @Test - fun testFrom() { - val pGobject = PGobject().apply { - type = "geography" - value = "0101000020E6100000304CA60A460D4140BE9F1A2FDD0C4E40" - } - val converted = jtsGeometryConverter.from(pGobject) - assertTrue(converted is Geometry) - } - - @Test - fun testTo() { - val x = 34.1037 - val y = 60.1005 - val jtsPoint = JTS.getDefaultGeomFactory().createPoint(Coordinate(x, y)) - val convertedBack = jtsGeometryConverter.to(jtsPoint) - assertTrue(convertedBack is org.postgis.PGgeometry - && convertedBack.geometry.getPoint(0).x == x && convertedBack.geometry.getPoint(0).y == y) - } -} \ No newline at end of file