8000 move decoder to jts module · jasync-sql/jasync-sql@3171717 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3171717

Browse files
committed
move decoder to jts module
1 parent bfd96d9 commit 3171717

File tree

5 files changed

+82
-71
lines changed

5 files changed

+82
-71
lines changed

postgis-jasync/src/main/java/com/github/jasync/sql/db/postgis/BinaryParser.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.jasync.sql.db.postgis
22

3-
import net.postgis.jdbc.geometry.binary.ByteGetter
4-
import net.postgis.jdbc.geometry.binary.ValueGetter
53
import org.locationtech.jts.geom.Coordinate
64
import org.locationtech.jts.geom.CoordinateSequence
75
import org.locationtech.jts.geom.Geometry
@@ -39,7 +37,7 @@ class JtsBinaryParser {
3937
* @param value String containing the hex data to be parsed
4038
* @return the resulting parsed geometry
4139
*/
42-
fun parse(value: String?): Geometry {
40+
fun parse(value: String): Geometry {
4341
val bytes = ByteGetter.StringByteGetter(value)
4442
return parseGeometry(valueGetterForEndian(bytes))
4543
}
@@ -49,7 +47,7 @@ class JtsBinaryParser {
4947
* @param value byte array containing the binary encoded geometru
5048
* @return the resulting parsed geometry
5149
*/
52-
fun parse(value: ByteArray?): Geometry {
50+
fun parse(value: ByteArray): Geometry {
5351
val bytes = ByteGetter.BinaryByteGetter(value)
5452
return parseGeometry(valueGetterForEndian(bytes))
5553
}
@@ -213,6 +211,8 @@ class JtsBinaryParser {
213211

214212
object Geom {
215213

214+
const val GeometryColumnType = 18011
215+
216216
// OpenGIS Geometry types as defined in the OGC WKB Spec
217217
// (May we replace this with an ENUM as soon as JDK 1.5
218218
// has gained widespread usage?)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.jasync.sql.db.postgis
2+
3+
import com.github.jasync.sql.db.column.ColumnDecoder
4+
import org.locationtech.jts.geom.Geometry
5+
6+
class JtsColumnDecoder : ColumnDecoder {
7+
8+
private val parser = JtsBinaryParser()
9+
10+
override fun decode(value: String): Geometry {
11+
return parser.parse(value)
12+
}
13+
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
package com.github.aysnc.sql.db.integration
22

3-
import com.github.jasync.sql.db.postgis.JtsBinaryParser
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
46
import org.assertj.core.api.Assertions.assertThat
57
import org.junit.Test
8+
import org.locationtech.jts.geom.GeometryFactory
9+
import org.locationtech.jts.geom.PrecisionModel
10+
import org.locationtech.jts.io.WKTReader
611

712
class PostgisSpec : DatabaseTestHelper() {
813

14+
init {
15+
PostgreSQLColumnDecoderRegistry.Instance.registerType(Geom.GeometryColumnType, JtsColumnDecoder())
16+
}
17+
918
@Test
1019
fun `simple query`() {
11-
1220
withHandler { handler ->
1321
val res1 = executeQuery(handler, "SELECT postgis_full_version()")
1422
assertThat(res1.rows[0][0].toString()).contains("POSTGIS=")
1523
// val res2 = executeQuery(handler, "SELECT ST_GeomFromText('POINT(1 2)',4326)")
1624
val res2 = executeQuery(handler, "SELECT ST_GeomFromText('LINESTRING(1 2, 3 4)',4326)")
17-
// can try to parse it similar to https://github.com/postgis/postgis-java/blob/main/postgis-jdbc-geometry/src/main/java/net/postgis/jdbc/geometry/binary/BinaryParser.java
18-
assertThat(JtsBinaryParser().parse(res2.rows[0][0] as String)).isEqualTo("POSTGIS=")
19-
}
20-
}
21-
22-
private val HEX_ARRAY = "0123456789ABCDEF".toCharArray()
23-
fun bytesToHex(bytes: ByteArray): String {
24-
val hexChars = CharArray(bytes.size * 2)
25-
for (j in bytes.indices) {
26-
val v = bytes[j].toInt() and 0xFF
27-
hexChars[j * 2] = HEX_ARRAY[v ushr 4]
28-
hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F]
25+
assertThat(res2.rows[0][0]).isEqualTo(WKTReader(GeometryFactory(PrecisionModel(), 4326)).read("LINESTRING(1 2, 3 4)"))
2926
}
30-
return String(hexChars)
3127
}
3228
}

postgresql-async/src/main/java/com/github/jasync/sql/db/postgresql/column/ColumnTypes.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ object ColumnTypes {
5454

5555
const val Inet = 869
5656
const val InetArray = 1041
57-
const val Geometry = 18011
5857
}
5958

6059
/*

postgresql-async/src/main/java/com/github/jasync/sql/db/postgresql/column/PostgreSQLColumnDecoderRegistry.kt

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -42,79 +42,82 @@ class PostgreSQLColumnDecoderRegistry(val charset: Charset = CharsetUtil.UTF_8)
4242
private val uuidArrayDecoder = ArrayDecoder(UUIDEncoderDecoder)
4343
private val inetAddressArrayDecoder = ArrayDecoder(InetAddressEncoderDecoder)
4444

45-
override fun decode(kind: ColumnData, value: ByteBuf, charset: Charset): Any {
46-
return decoderFor(kind.dataType()).decode(kind, value, charset)!!
45+
private val registry: MutableMap<Int, ColumnDecoder> = defaultRegistry()
46+
47+
fun registerType(type: Int, decoder: ColumnDecoder) {
48+
registry[type] = decoder
4749
}
4850

49-
private fun decoderFor(kind: Int): ColumnDecoder {
50-
return when (kind) {
51+
override fun decode(kind: ColumnData, value: ByteBuf, charset: Charset): Any {
52+
return registry.getOrDefault(kind.dataType(), StringEncoderDecoder).decode(kind, value, charset)!!
53+
}
5154

52-
ColumnTypes.Boolean -> BooleanEncoderDecoder
53-
ColumnTypes.BooleanArray -> this.booleanArrayDecoder
55+
private fun defaultRegistry(): MutableMap<Int, ColumnDecoder> {
56+
val res = mutableMapOf<Int, ColumnDecoder>()
57+
res[ColumnTypes.Boolean] = BooleanEncoderDecoder
58+
res[ColumnTypes.BooleanArray] = this.booleanArrayDecoder
5459

55-
ColumnTypes.Char -> CharEncoderDecoder
56-
ColumnTypes.CharArray -> this.charArrayDecoder
60+
res[ColumnTypes.Char] = CharEncoderDecoder
61+
res[ColumnTypes.CharArray] = this.charArrayDecoder
5762

58-
ColumnTypes.Bigserial -> LongEncoderDecoder
59-
ColumnTypes.BigserialArray -> this.longArrayDecoder
63+
res[ColumnTypes.Bigserial] = LongEncoderDecoder
64+
res[ColumnTypes.BigserialArray] = this.longArrayDecoder
6065

61-
ColumnTypes.Smallint -> ShortEncoderDecoder
62-
ColumnTypes.SmallintArray -> this.shortArrayDecoder
66+
res[ColumnTypes.Smallint] = ShortEncoderDecoder
67+
res[ColumnTypes.SmallintArray] = this.shortArrayDecoder
6368

64-
ColumnTypes.Integer -> IntegerEncoderDecoder
65-
ColumnTypes.IntegerArray -> this.integerArrayDecoder
69+
res[ColumnTypes.Integer] = IntegerEncoderDecoder
70+
res[ColumnTypes.IntegerArray] = this.integerArrayDecoder
6671

67-
ColumnTypes.OID -> LongEncoderDecoder
68-
ColumnTypes.OIDArray -> this.longArrayDecoder
72+
res[ColumnTypes.OID] = LongEncoderDecoder
73+
res[ColumnTypes.OIDArray] = this.longArrayDecoder
6974

70-
ColumnTypes.Numeric -> BigDecimalEncoderDecoder
71-
ColumnTypes.NumericArray -> this.bigDecimalArrayDecoder
75+
res[ColumnTypes.Numeric] = BigDecimalEncoderDecoder
76+
res[ColumnTypes.NumericArray] = this.bigDecimalArrayDecoder
7277

73-
ColumnTypes.Real -> FloatEncoderDecoder
74-
ColumnTypes.RealArray -> this.floatArrayDecoder
78+
res[ColumnTypes.Real] = FloatEncoderDecoder
79+
res[ColumnTypes.RealArray] = this.floatArrayDecoder
7580

76-
ColumnTypes.Double -> DoubleEncoderDecoder
77-
ColumnTypes.DoubleArray -> this.doubleArrayDecoder
81+
res[ColumnTypes.Double] = DoubleEncoderDecoder
82+
res[ColumnTypes.DoubleArray] = this.doubleArrayDecoder
7883

79-
ColumnTypes.Text -> StringEncoderDecoder
80-
ColumnTypes.TextArray -> this.stringArrayDecoder
84+
res[ColumnTypes.Text] = StringEncoderDecoder
85+
res[ColumnTypes.TextArray] = this.stringArrayDecoder
8186

82-
ColumnTypes.Varchar -> StringEncoderDecoder
83-
ColumnTypes.VarcharArray -> this.stringArrayDecoder
87+
res[ColumnTypes.Varchar] = StringEncoderDecoder
88+
res[ColumnTypes.VarcharArray] = this.stringArrayDecoder
8489

85-
ColumnTypes.Bpchar -> StringEncoderDecoder
86-
ColumnTypes.BpcharArray -> this.stringArrayDecoder
90+
res[ColumnTypes.Bpchar] = StringEncoderDecoder
91+
res[ColumnTypes.BpcharArray] = this.stringArrayDecoder
8792

88-
ColumnTypes.Timestamp -> PostgreSQLTimestampEncoderDecoder
89-
ColumnTypes.TimestampArray -> this.timestampArrayDecoder
93+
res[ColumnTypes.Timestamp] = PostgreSQLTimestampEncoderDecoder
94+
res[ColumnTypes.TimestampArray] = this.timestampArrayDecoder
9095

91-
ColumnTypes.TimestampWithTimezone -> PostgreSQLTimestampEncoderDecoder
92-
ColumnTypes.TimestampWithTimezoneArray -> this.timestampWithTimezoneArrayDecoder
96+
res[ColumnTypes.TimestampWithTimezone] = PostgreSQLTimestampEncoderDecoder
97+
res[ColumnTypes.TimestampWithTimezoneArray] = this.timestampWithTimezoneArrayDecoder
9398

94-
ColumnTypes.Date -> DateEncoderDecoder
95-
ColumnTypes.DateArray -> this.dateArrayDecoder
99+
res[ColumnTypes.Date] = DateEncoderDecoder
100+
res[ColumnTypes.DateArray] = this.dateArrayDecoder
96101

97-
ColumnTypes.Time -> TimeEncoderDecoder.Instance
98-
ColumnTypes.TimeArray -> this.timeArrayDecoder
102+
res[ColumnTypes.Time] = TimeEncoderDecoder.Instance
103+
res[ColumnTypes.TimeArray] = this.timeArrayDecoder
99104

100-
ColumnTypes.TimeWithTimezone -> TimeWithTimezoneEncoderDecoder
101-
ColumnTypes.TimeWithTimezoneArray -> this.timeWithTimestampArrayDecoder
105+
res[ColumnTypes.TimeWithTimezone] = TimeWithTimezoneEncoderDecoder
106+
res[ColumnTypes.TimeWithTimezoneArray] = this.timeWithTimestampArrayDecoder
102107

103-
ColumnTypes.Interval -> PostgreSQLIntervalEncoderDecoder
104-
ColumnTypes.IntervalArray -> this.intervalArrayDecoder
108+
res[ColumnTypes.Interval] = PostgreSQLIntervalEncoderDecoder
109+
res[ColumnTypes.IntervalArray] = this.intervalArrayDecoder
105110

106-
ColumnTypes.MoneyArray -> this.stringArrayDecoder
107-
ColumnTypes.NameArray -> this.stringArrayDecoder
108-
ColumnTypes.UUID -> UUIDEncoderDecoder
109-
ColumnTypes.UUIDArray -> this.uuidArrayDecoder
110-
ColumnTypes.XMLArray -> this.stringArrayDecoder
111-
ColumnTypes.ByteA -> ByteArrayEncoderDecoder
111+
res[ColumnTypes.MoneyArray] = this.stringArrayDecoder
112+
res[ColumnTypes.NameArray] = this.stringArrayDecoder
113+
res[ColumnTypes.UUID] = UUIDEncoderDecoder
114+
res[ColumnTypes.UUIDArray] = this.uuidArrayDecoder
115+
res[ColumnTypes.XMLArray] = this.stringArrayDecoder
116+
res[ColumnTypes.ByteA] = ByteArrayEncoderDecoder
112117

113-
ColumnTypes.Inet -> InetAddressEncoderDecoder
114-
ColumnTypes.InetArray -> this.inetAddressArrayDecoder
115-
ColumnTypes.Geometry -> StringEncoderDecoder
118+
res[ColumnTypes.Inet] = InetAddressEncoderDecoder
119+
res[ColumnTypes.InetArray] = this.inetAddressArrayDecoder
116120

117-
else -> StringEncoderDecoder
118-
}
121+
return res
119122
}
120123
}

0 commit comments

Comments
 (0)
0