瀏覽代碼

增加对POSTGIS geometry的支持

王翔 2 年之前
父節點
當前提交
2317a6f0cd
共有 1 個文件被更改,包括 33 次插入20 次删除
  1. 33 20
      dbsyncer-connector/src/main/java/org/dbsyncer/connector/schema/OtherValueMapper.java

+ 33 - 20
dbsyncer-connector/src/main/java/org/dbsyncer/connector/schema/OtherValueMapper.java

@@ -1,14 +1,12 @@
 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.dbsyncer.connector.database.ds.SimpleConnection;
+import org.postgis.Geometry;
+import org.postgis.binary.BinaryParser;
+import org.postgis.binary.BinaryWriter;
 
-import java.sql.Connection;
 import java.sql.Struct;
 
 /**
@@ -16,7 +14,7 @@ import java.sql.Struct;
  * @version 1.0.0
  * @date 2022/9/16 16:54
  */
-public class OtherValueMapper extends AbstractValueMapper<Struct> {
+public class OtherValueMapper extends AbstractValueMapper<Object> {
 
     @Override
     protected boolean skipConvert(Object val) {
@@ -24,22 +22,37 @@ public class OtherValueMapper extends AbstractValueMapper<Struct> {
     }
 
     @Override
-    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);
-                }
+    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;
             }
         }
+        if (val instanceof Geometry)
+        {
+            return val;
+
+        }
+
+        // SqlServer Geometry
+
         throw new ConnectorException(String.format("%s can not find type [%s], val [%s]", getClass().getSimpleName(), val.getClass(), val));
     }
+
 }