Browse Source

重新整理代码,增加对POSTGIS geometry和UUID两种数据类型的支持

王翔 2 years ago
parent
commit
ba0af98893

+ 7 - 0
dbsyncer-connector/src/main/java/org/dbsyncer/connector/postgresql/PostgreSQLConnector.java

@@ -8,9 +8,16 @@ import org.dbsyncer.connector.database.AbstractDatabaseConnector;
 import org.dbsyncer.connector.enums.TableTypeEnum;
 import org.dbsyncer.connector.model.PageSql;
 import org.dbsyncer.connector.model.Table;
+import org.dbsyncer.connector.schema.GeometryValueMapper;
+import org.dbsyncer.connector.schema.PGOtherValueMapper;
+
+import java.sql.Types;
 
 public final class PostgreSQLConnector extends AbstractDatabaseConnector {
 
+    static {
+        valueMappers.put(Types.OTHER, new PGOtherValueMapper());
+    }
     @Override
     protected String buildSqlWithQuotation() {
         return "\"";

+ 21 - 38
dbsyncer-connector/src/main/java/org/dbsyncer/connector/schema/OtherValueMapper.java

@@ -1,13 +1,14 @@
 package org.dbsyncer.connector.schema;
 
+import com.microsoft.sqlserver.jdbc.Geometry;
+import oracle.jdbc.OracleConnection;
+//import oracle.spatial.geometry.JGeometry;
 import org.dbsyncer.common.spi.ConnectorMapper;
 import org.dbsyncer.connector.AbstractValueMapper;
 import org.dbsyncer.connector.ConnectorException;
-import org.postgis.Geometry;
-import org.postgis.PGgeometry;
-import org.postgis.binary.BinaryParser;
-import org.postgis.binary.BinaryWriter;
+import org.dbsyncer.connector.database.ds.SimpleConnection;
 
+import java.sql.Connection;
 import java.sql.Struct;
 
 /**
@@ -15,48 +16,30 @@ import java.sql.Struct;
  * @version 1.0.0
  * @date 2022/9/16 16:54
  */
-public class OtherValueMapper extends AbstractValueMapper<Object> {
+public class OtherValueMapper extends AbstractValueMapper<Struct> {
 
     @Override
     protected boolean skipConvert(Object val) {
-        return val instanceof oracle.sql.STRUCT;
+        return val instanceof oracle.sql.STRUCT || val instanceof String;
     }
 
     @Override
-    protected Object convert(ConnectorMapper connectorMapper, Object val) throws Exception {
-
-        if (val instanceof oracle.sql.STRUCT) {
-            return (Struct) val;
-        }
-
-        //PostGIS Geometry的情况
-        if (val instanceof String)
-        {
-            //测试下Geometry能不能用起来
-            try
-            {
-                BinaryParser parser= new BinaryParser();
-                org.postgis.Geometry geo = parser.parse((String) val);
-                BinaryWriter bw = new BinaryWriter();
-                return bw.writeBinary(geo);
-            }
-            catch (Exception ex) {
-                System.out.println(val);
-                return val;
+    protected Struct convert(ConnectorMapper connectorMapper, Object val) throws Exception {
+        // SqlServer Geometry
+        if (val instanceof byte[]) {
+            Object connection = connectorMapper.getConnection();
+            if (connection instanceof Connection) {
+                SimpleConnection simpleConnection = (SimpleConnection) connection;
+                if (simpleConnection instanceof OracleConnection) {
+                    OracleConnection conn = simpleConnection.unwrap(OracleConnection.class);
+                    Geometry geometry = Geometry.deserialize((byte[]) val);
+                    Double x = geometry.getX();
+                    Double y = geometry.getY();
+//                    JGeometry jGeometry = new JGeometry(x, y, 0);
+//                    return JGeometry.store(jGeometry, conn);
+                }
             }
         }
-        if (val instanceof Geometry)
-        {
-            return val;
-
-        }
-
-        if (val instanceof PGgeometry)
-            return val;
-
-        // SqlServer Geometry
-
         throw new ConnectorException(String.format("%s can not find type [%s], val [%s]", getClass().getSimpleName(), val.getClass(), val));
     }
-
 }

+ 33 - 0
dbsyncer-connector/src/main/java/org/dbsyncer/connector/schema/PGOtherValueMapper.java

@@ -0,0 +1,33 @@
+package org.dbsyncer.connector.schema;
+
+import org.dbsyncer.common.spi.ConnectorMapper;
+import org.dbsyncer.connector.AbstractValueMapper;
+import org.dbsyncer.connector.ConnectorException;
+import org.postgis.Geometry;
+import org.postgis.PGgeometry;
+import org.postgis.binary.BinaryParser;
+import org.postgis.binary.BinaryWriter;
+
+public class PGOtherValueMapper extends AbstractValueMapper<Object> {
+    @Override
+    protected Object convert(ConnectorMapper connectorMapper, Object val) throws Exception {
+        if (val instanceof String) {
+            //测试下Geometry能不能用起来
+            try {
+                BinaryParser parser = new BinaryParser();
+                org.postgis.Geometry geo = parser.parse((String) val);
+                BinaryWriter bw = new BinaryWriter();
+                return bw.writeBinary(geo);
+            } catch (Exception ex) {
+                return val;
+            }
+        } else if (val instanceof PGgeometry)
+            return val;
+        else if (val instanceof Geometry) {
+            return val;
+        } else if (val instanceof java.util.UUID) {
+            return val;
+        }
+        throw new ConnectorException(String.format("%s can not find type [%s], val [%s]", getClass().getSimpleName(), val.getClass(), val));
+    }
+}