1
0
穿云 4 сар өмнө
parent
commit
846bc54c91

+ 5 - 0
dbsyncer-connector/dbsyncer-connector-mysql/pom.xml

@@ -26,6 +26,11 @@
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.vividsolutions</groupId>
+            <artifactId>jts</artifactId>
+        </dependency>
+
         <!-- binlog -->
         <dependency>
             <groupId>com.zendesk</groupId>

+ 33 - 15
dbsyncer-connector/dbsyncer-connector-mysql/src/main/java/org/dbsyncer/connector/mysql/schema/support/MySQLStringType.java

@@ -3,9 +3,14 @@
  */
 package org.dbsyncer.connector.mysql.schema.support;
 
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.io.*;
+import org.dbsyncer.connector.mysql.MySQLException;
 import org.dbsyncer.sdk.model.Field;
 import org.dbsyncer.sdk.schema.support.StringType;
 
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Set;
@@ -26,7 +31,7 @@ public final class MySQLStringType extends StringType {
         MEDIUMTEXT, // 可变长度,最多2的24次方-1个字符,16M
         LONGTEXT, // 可变长度,最多2的32次方-1个字符,4GB
         ENUM, // 2字节,最大可达65535个不同的枚举值
-        JSON, GEOMETRY, POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION;
+        JSON, GEOMETRY; // POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION
     }
 
     @Override
@@ -36,31 +41,44 @@ public final class MySQLStringType extends StringType {
 
     @Override
     protected String merge(Object val, Field field) {
+        if (TypeEnum.valueOf(field.getTypeName()) == TypeEnum.GEOMETRY && val instanceof byte[]) {
+            try {
+                System.out.printf("%s %s %n", field.getName(), Arrays.toString((byte[]) val));
+                return deserializeGeometry((byte[]) val);
+            } catch (ParseException e) {
+                throw new MySQLException(e);
+            }
+        }
         if (val instanceof byte[]) {
             return new String((byte[]) val, StandardCharsets.UTF_8);
         }
-        switch (TypeEnum.valueOf(field.getTypeName())) {
-            case CHAR:
-            case VARCHAR:
-            case TINYTEXT:
-            case TEXT:
-            case MEDIUMTEXT:
-            case LONGTEXT:
-            case ENUM:
-            case JSON:
-                return String.valueOf(val);
-
-            default:
-                return throwUnsupportedException(val, field);
-        }
+        return throwUnsupportedException(val, field);
     }
 
     @Override
     protected Object convert(Object val, Field field) {
+        if (TypeEnum.valueOf(field.getTypeName()) == TypeEnum.GEOMETRY && val instanceof String) {
+            try {
+                return serializeGeometry((String) val);
+            } catch (ParseException e) {
+                throw new MySQLException(e);
+            }
+        }
         if (val instanceof String) {
             return val;
         }
         return throwUnsupportedException(val, field);
     }
 
+    private String deserializeGeometry(byte[] bytes) throws ParseException {
+        byte[] geometryBytes = ByteBuffer.allocate(bytes.length - 4).order(ByteOrder.LITTLE_ENDIAN).put(bytes, 4, bytes.length - 4).array();
+        WKBReader reader = new WKBReader();
+        return reader.read(geometryBytes).toText();
+    }
+
+    private byte[] serializeGeometry(String wellKnownText) throws ParseException {
+        Geometry geometry = new WKTReader().read(wellKnownText);
+        byte[] bytes = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN).write(geometry);
+        return ByteBuffer.allocate(bytes.length + 4).order(ByteOrder.LITTLE_ENDIAN).putInt(geometry.getSRID()).put(bytes).array();
+    }
 }

+ 68 - 0
dbsyncer-connector/dbsyncer-connector-mysql/src/test/java/GeometryTest.java

@@ -0,0 +1,68 @@
+/**
+ * DBSyncer Copyright 2020-2025 All Rights Reserved.
+ */
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.io.*;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+
+/**
+ * @Author 穿云
+ * @Version 1.0.0
+ * @Date 2025-01-04 13:22
+ */
+public class GeometryTest {
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Test
+    public void testGeometry() throws Exception {
+        // POINT (121.474103 31.232862)
+        byte[] point = new byte[]{0, 0, 0, 0, 1, 1, 0, 0, 0, -33, -5, 27, -76, 87, 94, 94, 64, 45, 123, 18, -40, -100, 59, 63, 64};
+        testGeometry("point", point);
+
+        // LINESTRING (121.474103 31.232862, 121.472462 31.231339, 121.471984 31.232821)
+        byte[] linestring = new byte[]{0, 0, 0, 0, 1, 2, 0, 0, 0, 3, 0, 0, 0, -33, -5, 27, -76, 87, 94, 94, 64, 45, 123, 18, -40, -100, 59, 63, 64, -109, -90, 65, -47, 60, 94, 94, 64, 18, 74, 95, 8, 57, 59, 63, 64, 15, 15, 97, -4, 52, 94, 94, 64, 112, -46, 52, 40, -102, 59, 63, 64};
+        testGeometry("linestring", linestring);
+
+        // POLYGON ((121.474243 31.234504, 121.471775 31.233348, 121.470724 31.23155, 121.471603 31.230229, 121.472655 31.230357, 121.475777 31.232045, 121.474243 31.234504))
+        byte[] polygon = new byte[]{0, 0, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 0, 7, 0, 0, 0, -40, -42, 79, -1, 89, 94, 94, 64, -4, -57, 66, 116, 8, 60, 63, 64, -127, 4, -59, -113, 49, 94, 94, 64, 70, -106, -52, -79, -68, 59, 63, 64, 79, 92, -114, 87, 32, 94, 94, 64, -120, 99, 93, -36, 70, 59, 63, 64, -125, 108, 89, -66, 46, 94, 94, 64, 62, -105, -87, 73, -16, 58, 63, 64, -98, -46, -63, -6, 63, 94, 94, 64, -103, 103, 37, -83, -8, 58, 63, 64, 24, -52, 95, 33, 115, 94, 94, 64, 72, 51, 22, 77, 103, 59, 63, 64, -40, -42, 79, -1, 89, 94, 94, 64, -4, -57, 66, 116, 8, 60, 63, 64};
+        testGeometry("polygon", polygon);
+
+        // MULTIPOINT ((103 35), (104 34), (105 35))
+        byte[] multipoint = new byte[]{0, 0, 0, 0, 1, 4, 0, 0, 0, 3, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, 0, 65, 64, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64, 90, 64, 0, 0, 0, 0, 0, -128, 65, 64};
+        testGeometry("multipoint", multipoint);
+
+        // MULTILINESTRING ((103 35, 104 35), (105 36, 105 37))
+        byte[] multilinestring = new byte[]{0, 0, 0, 0, 1, 5, 0, 0, 0, 2, 0, 0, 0, 1, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, -128, 65, 64, 1, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 64, 90, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, 64, 90, 64, 0, 0, 0, 0, 0, -128, 66, 64};
+        testGeometry("multilinestring", multilinestring);
+
+        // MULTIPOLYGON (((103 35, 104 35, 104 36, 103 36, 103 35)), ((103 36, 104 36, 104 37, 103 36)))
+        byte[] multipolygon = new byte[]{0, 0, 0, 0, 1, 6, 0, 0, 0, 2, 0, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, -128, 65, 64, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 1, 3, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, 0, 66, 64, 0, 0, 0, 0, 0, 0, 90, 64, 0, 0, 0, 0, 0, -128, 66, 64, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, 0, 66, 64};
+        testGeometry("multipolygon", multipolygon);
+
+        // GEOMETRYCOLLECTION (POINT (103 35), LINESTRING (103 35, 103 37))
+        byte[] geometrycollection = new byte[]{0, 0, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 1, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 65, 64, 0, 0, 0, 0, 0, -64, 89, 64, 0, 0, 0, 0, 0, -128, 66, 64};
+        testGeometry("geometrycollection", geometrycollection);
+    }
+
+    private void testGeometry(String typeName, byte[] bytes) throws ParseException {
+        // 序列化
+        byte[] geometryBytes = ByteBuffer.allocate(bytes.length - 4).order(ByteOrder.LITTLE_ENDIAN).put(bytes, 4, bytes.length - 4).array();
+        WKBReader reader = new WKBReader();
+        String text = reader.read(geometryBytes).toText();
+        logger.info("test {}:{}", typeName, text);
+
+        // 反序列化
+        Geometry geometry = new WKTReader().read(text);
+        byte[] bytesArray = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN).write(geometry);
+        byte[] array = ByteBuffer.allocate(bytesArray.length + 4).order(ByteOrder.LITTLE_ENDIAN).putInt(geometry.getSRID()).put(bytesArray).array();
+        logger.info(Arrays.toString(array));
+        logger.info("test {}:{}", typeName, Arrays.equals(bytes, array));
+        assert Arrays.equals(bytes, array);
+    }
+}

+ 6 - 0
pom.xml

@@ -40,6 +40,7 @@
         <ojdbc8.version>21.6.0.0</ojdbc8.version>
         <!--<mysql.version>5.1.40</mysql.version>-->
         <mysql.version>8.0.21</mysql.version>
+        <jts.version>1.13</jts.version>
         <mysql-binlog.version>0.27.0</mysql-binlog.version>
         <mssql-jdbc.version>7.4.1.jre8</mssql-jdbc.version>
         <antlr4-runtime.version>4.7.2</antlr4-runtime.version>
@@ -139,6 +140,11 @@
                 <artifactId>mysql-connector-java</artifactId>
                 <version>${mysql.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.vividsolutions</groupId>
+                <artifactId>jts</artifactId>
+                <version>${jts.version}</version>
+            </dependency>
 
             <!-- oracle-driver -->
             <dependency>