提交 f806d7ed 编写于 作者: S serge-rider

#5333 PostGIS support improvement


Former-commit-id: 7fa232ae
上级 27a43733
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.data.gis.handlers;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKTReader;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCAbstractValueHandler;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import java.sql.SQLException;
/**
* GIS geometry handler
*/
public class GISGeometryTest {
public static void main(String[] args) throws Exception {
WKTReader wktReader = new WKTReader();
Geometry g = wktReader.read("MultiPointZ((1 2 3))");
System.out.println(g);
}
}
......@@ -68,6 +68,7 @@ public class PostgreConstants {
public static final String PG_OBJECT_CLASS = "org.postgresql.util.PGobject";
public static final String PG_ARRAY_CLASS = "org.postgresql.jdbc.PgArray";
public static final String PG_INTERVAL_CLASS = "org.postgresql.util.PGInterval";
public static final String PG_GEOMETRY_CLASS = "org.postgis.PGgeometry";
public static final DBDPseudoAttribute PSEUDO_ATTR_OID = new DBDPseudoAttribute(DBDPseudoAttributeType.ROWID, "oid",
"oid", "oid", "Row identifier", false);
......
......@@ -17,15 +17,13 @@
package org.jkiss.dbeaver.ext.postgresql.model.data;
import com.vividsolutions.jts.geom.Geometry;
import org.jkiss.dbeaver.data.gis.handlers.GISGeometryValueHandler;
import org.jkiss.dbeaver.data.gis.handlers.GeometryConverter;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.formatters.BinaryFormatterHex;
import org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCAbstractValueHandler;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.BeanUtils;
......@@ -44,7 +42,7 @@ public class PostgreGeometryValueHandler extends JDBCAbstractValueHandler {
@Override
protected Object fetchColumnValue(DBCSession session, JDBCResultSet resultSet, DBSTypedObject type, int index) throws DBCException, SQLException {
return getValueFromObject(session, type,
resultSet.getString(index),
resultSet.getObject(index),
false);
}
......@@ -54,8 +52,10 @@ public class PostgreGeometryValueHandler extends JDBCAbstractValueHandler {
statement.setNull(paramIndex, paramType.getTypeID());
} else if (value instanceof Geometry) {
statement.setObject(paramIndex, getStringFromGeometry(session, (Geometry)value), Types.OTHER);
} else if (value.getClass().getName().equals(PostgreConstants.PG_GEOMETRY_CLASS)) {
statement.setObject(paramIndex, value, Types.OTHER);
} else {
throw new DBCException("Invalid geometry object: " + value);
statement.setObject(paramIndex, value.toString(), Types.OTHER);
}
}
......@@ -72,17 +72,27 @@ public class PostgreGeometryValueHandler extends JDBCAbstractValueHandler {
return object;
} else if (object instanceof String) {
return makeGeometryFromString(session, (String) object);
} else if (object.getClass().getName().equals(PostgreConstants.PG_GEOMETRY_CLASS)) {
return makeGeometryFromPGGeometry(session, object);
} else {
return makeGeometryFromString(session, object.toString());
}
}
private Object makeGeometryFromPGGeometry(DBCSession session, Object pgGeometry) throws DBCException {
try {
return BeanUtils.invokeObjectMethod(pgGeometry, "getGeometry", null, null);
} catch (Throwable e) {
throw new DBCException(e, session.getDataSource());
}
}
private Object makeGeometryFromString(DBCSession session, String object) throws DBCException {
if (CommonUtils.isEmpty(object)) {
return null;
}
try {
Class<?> jtsGeometry = DBUtils.getDriverClass(session.getDataSource(), "org.postgis.jts.JtsGeometry");
Class<?> jtsGeometry = DBUtils.getDriverClass(session.getDataSource(), PostgreConstants.PG_GEOMETRY_CLASS);
return BeanUtils.invokeStaticMethod(
jtsGeometry, "geomFromString", new Class[] { String.class }, new Object[] { object }
);
......@@ -93,7 +103,7 @@ public class PostgreGeometryValueHandler extends JDBCAbstractValueHandler {
private String getStringFromGeometry(JDBCSession session, Geometry geometry) throws DBCException {
try {
Class<?> jtsGeometry = DBUtils.getDriverClass(session.getDataSource(), "org.postgis.jts.JtsGeometry");
Class<?> jtsGeometry = DBUtils.getDriverClass(session.getDataSource(), PostgreConstants.PG_GEOMETRY_CLASS);
Object jtsg = jtsGeometry.getConstructor(Geometry.class).newInstance(geometry);
return (String)BeanUtils.invokeObjectMethod(
jtsg, "getValue", null, null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册