8000 add dynamic registration (#341) · jasync-sql/jasync-sql@d4c534b · GitHub
[go: up one dir, main page]

Skip to content

Commit d4c534b

Browse files
authored
add dynamic registration (#341)
* change postgis version to 14-3.2 * add dynamic registration to geometry type
1 parent 254c30e commit d4c534b

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.jasync.sql.db.postgis
2+
3+
import com.github.jasync.sql.db.Connection
4+
import com.github.jasync.sql.db.postgresql.column.PostgreSQLColumnDecoderRegistry
5+
import com.github.jasync.sql.db.util.FP
6+
import com.github.jasync.sql.db.util.map
7+
import mu.KotlinLogging
8+
import java.util.concurrent.CompletableFuture
9+
import java.util.concurrent.atomic.AtomicBoolean
10+
11+
private val logger = KotlinLogging.logger {}
12+
13+
/**
14+
* This class is responsible to register the geometry type decoder.
15+
* To use it call init() method and wait for future completion.
16+
* Since the geometry type is part of an extension and has dynamic oid it requires a connection to query the db for
17+
* the actual oid.
18+
*/
19+
object JasyncPostgisRegister {
20+
21+
val geometryRegistered = AtomicBoolean(false)
22+
@JvmStatic
23+
fun init(connection: Connection): CompletableFuture<Unit> {
24+
if (geometryRegistered.get()) {
25+
logger.trace { "init geometry type already registered" }
26+
return FP.successful(Unit)
27+
}
28+
return connection.sendQuery("SELECT 'geometry'::regtype::oid").map { result ->
29+
val key = (result.rows[0][0] as Long).toInt()
30+
logger.info { "init geometry type with id $key" }
31+
PostgreSQLColumnDecoderRegistry.Instance.registerDecoder(key, JtsColumnDecoder())
32+
geometryRegistered.set(true)
33+
}
34+
}
35+
}

postgis-jasync/src/test/java/ContainerHelper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ object ContainerHelper {
8787
}
8888
}
8989

90-
class MyPostgreSQLContainer : PostgreSQLContainer<MyPostgreSQLContainer>(DockerImageName.parse("postgis/postgis").asCompatibleSubstituteFor("postgres"))
90+
class MyPostgreSQLContainer : PostgreSQLContainer<MyPostgreSQLContainer>(
91+
DockerImageName.parse("postgis/postgis:14-3.2").asCompatibleSubstituteFor("postgres")
92+
)

postgis-jasync/src/test/java/PostgisSpec.kt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.aysnc.sql.db.integration
22

3-
import com.github.jasync.sql.db.postgis.Geom
4-
import com.github.jasync.sql.db.postgis.JtsColumnDecoder
5-
import com.github.jasync.sql.db.postgresql.column.PostgreSQLColumnDecoderRegistry
3+
import com.github.jasync.sql.db.postgis.JasyncPostgisRegister
64
import org.assertj.core.api.Assertions.assertThat
75
import org.junit.Test
86
import org.locationtech.jts.geom.GeometryFactory
@@ -11,25 +9,44 @@ import org.locationtech.jts.io.WKTReader
119

1210
class PostgisSpec : DatabaseTestHelper() {
1311

14-
init {
15-
PostgreSQLColumnDecoderRegistry.Instance.registerDecoder(Geom.GeometryColumnType, JtsColumnDecoder())
16-
}
17-
1812
private val lineString = WKTReader(GeometryFactory(PrecisionModel(), 4326)).read("LINESTRING(1 2, 3 4)")
13+
private val pointString = WKTReader(GeometryFactory(PrecisionModel(), 4326)).read("POINT(1 2)")
14+
15+
private fun setup() {
16+
withHandler { connection ->
17+
JasyncPostgisRegister.init(connection).get()
18+
}
19+
}
1920

2021
@Test
21-
fun `simple query`() {
22+
fun `test version`() {
2223
withHandler { handler ->
2324
val res1 = executeQuery(handler, "SELECT postgis_full_version()")
2425
assertThat(res1.rows[0][0].toString()).contains("POSTGIS=")
25-
// val res2 = executeQuery(handler, "SELECT ST_GeomFromText('POINT(1 2)',4326)")
26-
val res2 = executeQuery(handler, "SELECT ST_GeomFromText('LINESTRING(1 2, 3 4)',4326)")
27-
assertThat(res2.rows[0][0]).isEqualTo(lineString)
26+
}
27+
}
28+
29+
@Test
30+
fun `simple line query`() {
31+
setup()
32+
withHandler { handler ->
33+
val res = executeQuery(handler, "SELECT ST_GeomFromText('LINESTRING(1 2, 3 4)',4326)")
34+
assertThat(res.rows[0][0]).isEqualTo(lineString)
35+
}
36+
}
37+
38+
@Test
39+
fun `simple point query`() {
40+
setup()
41+
withHandler { handler ->
42+
val res = executeQuery(handler, "SELECT ST_GeomFromText('POINT(1 2)',4326)")
43+
assertThat(res.rows[0][0]).isEqualTo(pointString)
2844
}
2945
}
3046

3147
@Test
3248
fun `insert and query`() {
49+
setup()
3350
withHandler { handler ->
3451
executeQuery(handler, "DROP TABLE if exists postgis_geom_test")
3552
executeQuery(handler, "CREATE TABLE postgis_geom_test (geom geometry NOT NULL)")

0 commit comments

Comments
 (0)
0