Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class DecoderRegistry(charset: Charset) {
this.stringDecoder
}
}
ColumnTypes.FIELD_TYPE_BIT -> ByteArrayDecoder
ColumnTypes.FIELD_TYPE_BIT,
ColumnTypes.FIELD_TYPE_GEOMETRY -> ByteArrayDecoder
ColumnTypes.FIELD_TYPE_LONGLONG -> LongDecoder
ColumnTypes.FIELD_TYPE_LONG, ColumnTypes.FIELD_TYPE_INT24 -> IntegerDecoder
ColumnTypes.FIELD_TYPE_YEAR, ColumnTypes.FIELD_TYPE_SHORT -> ShortDecoder
Expand Down Expand Up @@ -92,8 +93,12 @@ class DecoderRegistry(charset: Charset) {
ColumnTypes.FIELD_TYPE_VARCHAR,
ColumnTypes.FIELD_TYPE_ENUM -> StringEncoderDecoder
ColumnTypes.FIELD_TYPE_YEAR -> ShortEncoderDecoder
ColumnTypes.FIELD_TYPE_BIT -> ByteArrayColumnDecoder
ColumnTypes.FIELD_TYPE_BIT,
ColumnTypes.FIELD_TYPE_GEOMETRY -> ByteArrayColumnDecoder
ColumnTypes.FIELD_TYPE_BLOB,
ColumnTypes.FIELD_TYPE_LONG_BLOB,
ColumnTypes.FIELD_TYPE_MEDIUM_BLOB,
ColumnTypes.FIELD_TYPE_TINY_BLOB,
ColumnTypes.FIELD_TYPE_VAR_STRING,
ColumnTypes.FIELD_TYPE_STRING -> {
if (charsetCode == CharsetMapper.Binary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import io.netty.util.CharsetUtil
import org.junit.Assert.assertArrayEquals
import org.junit.Test
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.UUID
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class BinaryColumnsSpec : ConnectionHelper() {

Expand Down Expand Up @@ -132,6 +134,46 @@ class BinaryColumnsSpec : ConnectionHelper() {
}
}

@Test
fun `support GEOMETRY and GEOMETRYCOLLECTION types`() {
val create = """CREATE TEMPORARY TABLE geom_test (
id INT NOT NULL PRIMARY KEY,
pt POINT NOT NULL,
gc GEOMETRYCOLLECTION NOT NULL
)"""
val insert = """INSERT INTO geom_test (id, pt, gc) VALUES (
1,
ST_GeomFromText('POINT(1 2)', 4326),
ST_GeomFromText('GEOMETRYCOLLECTION(POINT(1 2), LINESTRING(0 0, 1 1))', 4326)
)"""
val select =
"SELECT id, pt, gc, ST_AsBinary(pt, 'axis-order=long-lat') AS pt_wkb, ST_SRID(pt) AS pt_srid FROM geom_test WHERE id = 1"

withConnection { connection ->
executeQuery(connection, create)
executeQuery(connection, insert)

val textRow = assertNotNull(executeQuery(connection, select).rows[0])
val ptBytes = textRow["pt"] as ByteArray
val gcBytes = textRow["gc"] as ByteArray
val expectedWkb = textRow["pt_wkb"] as ByteArray
val expectedSrid = (textRow["pt_srid"] as Number).toInt()

val expectedInternalPt = ByteBuffer.allocate(4 + expectedWkb.size)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(expectedSrid)
.put(expectedWkb)
.array()

assertArrayEquals(expectedInternalPt, ptBytes)
assertTrue(gcBytes.size > 4)

val binaryRow = assertNotNull(executePreparedStatement(connection, select).rows[0])
assertArrayEquals(ptBytes, binaryRow["pt"] as ByteArray)
assertArrayEquals(gcBytes, binaryRow["gc"] as ByteArray)
}
}

fun compareBytes(row: RowData, column: String, expected: String) {
assertArrayEquals(row[column] as ByteArray, expected.toByteArray(CharsetUtil.UTF_8))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.jasync.sql.db.mysql.codec

import com.github.jasync.sql.db.column.StringEncoderDecoder
import com.github.jasync.sql.db.mysql.binary.decoder.ByteArrayDecoder
import com.github.jasync.sql.db.mysql.column.ByteArrayColumnDecoder
import com.github.jasync.sql.db.mysql.column.ColumnTypes
import com.github.jasync.sql.db.mysql.util.CharsetMapper
import io.netty.util.CharsetUtil
import org.junit.Test
import kotlin.test.assertSame

class DecoderRegistrySpec {

@Test
fun `map FIELD_TYPE_GEOMETRY and binary BLOB variants to byte array decoders`() {
val registry = DecoderRegistry(CharsetUtil.UTF_8)

assertSame(
ByteArrayDecoder,
registry.binaryDecoderFor(ColumnTypes.FIELD_TYPE_GEOMETRY, CharsetMapper.Binary)
)
assertSame(
ByteArrayColumnDecoder,
registry.textDecoderFor(ColumnTypes.FIELD_TYPE_GEOMETRY, CharsetMapper.Binary)
)

val utf8Code = CharsetMapper.Instance.toInt(CharsetUtil.UTF_8)
listOf(
ColumnTypes.FIELD_TYPE_BLOB,
ColumnTypes.FIELD_TYPE_LONG_BLOB,
ColumnTypes.FIELD_TYPE_MEDIUM_BLOB,
ColumnTypes.FIELD_TYPE_TINY_BLOB
).forEach { blobType ->
assertSame(ByteArrayColumnDecoder, registry.textDecoderFor(blobType, CharsetMapper.Binary))
assertSame(StringEncoderDecoder, registry.textDecoderFor(blobType, utf8Code))
}
}
}
Loading