Skip to content

Commit

Permalink
PGgeo-PGgeometry-PGgeography class hierarchy (#80)
Browse files Browse the repository at this point in the history
#55

- Renamed test in postgis-geometry test suite

- Introduced new JDBC PGobject extensions class hierarchy which now allows jdbc queries to return geometry or geography data with specific types which callers can use to differentiate which data type was queried

- Cleanup and better documentation for DriverWrapper

- Renamed AutoRegistrationTest to DatatypesAutoRegistration to better reflect what is being tested

- Added geography as one of the castTypes in the EmptyGeometriesTest class

- Added instantiation test for PGgeography to DatatypesTest class

- Added GeographyDatatypeTest to verify new PGgeo/PGgeometry/PGgeography class hierarchy

- Added PGgeography to driverconfig.properties
  • Loading branch information
phillipross authored Mar 29, 2020
1 parent 138bfd6 commit a77061a
Show file tree
Hide file tree
Showing 14 changed files with 887 additions and 246 deletions.
147 changes: 95 additions & 52 deletions jdbc/src/main/java/org/postgis/DriverWrapper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
/*
* DriverWrapper.java
*
* PostGIS extension for PostgreSQL JDBC driver - Wrapper utility class
*
* (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
*
* (C) 2015 Phillip Ross, phillip.w.g.ross@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
Expand All @@ -20,21 +12,25 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
*
* (C) 2015 Phillip Ross, phillip.w.g.ross@gmail.com
*/

package org.postgis;


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.postgresql.Driver;
import org.postgresql.PGConnection;


/**
* DriverWrapper
*
Expand Down Expand Up @@ -73,6 +69,7 @@
*/
public class DriverWrapper extends Driver {

/** The static logger instance. */
protected static final Logger logger = Logger.getLogger("org.postgis.DriverWrapper");

public static final String POSTGRES_PROTOCOL = "jdbc:postgresql:";
Expand All @@ -84,6 +81,17 @@ public class DriverWrapper extends Driver {

protected TypesAdder typesAdder;


static {
try {
// Try to register ourself to the DriverManager
java.sql.DriverManager.registerDriver(new DriverWrapper());
} catch (SQLException e) {
logger.log(Level.WARNING, "Error registering PostGIS Wrapper Driver", e);
}
}


/**
* Default constructor.
*
Expand All @@ -101,7 +109,8 @@ public DriverWrapper() throws SQLException {
}
}

protected static TypesAdder getTypesAdder(Driver d) throws SQLException {

protected static TypesAdder getTypesAdder(final Driver d) throws SQLException {
if (d.getMajorVersion() == 7) {
if (d.getMinorVersion() >= 3) {
if (ta74 == null) {
Expand All @@ -122,7 +131,8 @@ protected static TypesAdder getTypesAdder(Driver d) throws SQLException {
}
}

private static TypesAdder loadTypesAdder(String version) throws SQLException {

private static TypesAdder loadTypesAdder(final String version) throws SQLException {
try {
Class klass = Class.forName("org.postgis.DriverWrapper$TypesAdder" + version);
return (TypesAdder) klass.newInstance();
Expand All @@ -131,53 +141,50 @@ private static TypesAdder loadTypesAdder(String version) throws SQLException {
}
}

static {
try {
// Try to register ourself to the DriverManager
java.sql.DriverManager.registerDriver(new DriverWrapper());
} catch (SQLException e) {
logger.log(Level.WARNING, "Error registering PostGIS Wrapper Driver", e);
}
}

/**
* Creates a postgresql connection, and then adds the PostGIS data types to
* it calling addpgtypes()
* Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes().
*
* A side-effect of this method is that the specified url parameter may be be changed
*
* @param url the URL of the database to connect to
* @param url the URL of the database to connect to (may be changed as a side-effect of this method)
* @param info a list of arbitrary tag/value pairs as connection arguments
* @return a connection to the URL or null if it isnt us
* @exception SQLException if a database access error occurs
*
* @see java.sql.Driver#connect
* @see org.postgresql.Driver
*/
public java.sql.Connection connect(String url, Properties info) throws SQLException {
public java.sql.Connection connect(String url, final Properties info) throws SQLException {
url = mangleURL(url);
Connection result = super.connect(url, info);
typesAdder.addGT(result, useLW(result));
return result;
}


/**
* Do we have HexWKB as well known text representation - to be overridden by
* subclasses.
*
* @param result Connection to check
* @return true if using EWKB, false otherwise
*/
protected boolean useLW(Connection result) {
protected boolean useLW(final Connection result) {
if (result == null) {
throw new IllegalArgumentException("null is no valid parameter");
}
return false;
}


/**
* Check whether the driver thinks he can handle the given URL.
*
* A side-effect of this method is that the specified url parameter may be be changed
*
* @see java.sql.Driver#acceptsURL
* @param url the URL of the driver
* @param url the URL of the driver (may be changed as a side-effect of this method)
* @return true if this driver accepts the given URL
*/
public boolean acceptsURL(String url) {
Expand All @@ -189,6 +196,7 @@ public boolean acceptsURL(String url) {
return super.acceptsURL(url);
}


/**
* Returns our own CVS version plus postgres Version
*
Expand All @@ -198,6 +206,7 @@ public static String getVersion() {
return "PostGisWrapper " + REVISION + ", wrapping " + Driver.getVersion();
}


/*
* Here follows the addGISTypes() stuff. This is a little tricky because the
* pgjdbc people had several, partially incompatible API changes during 7.2
Expand All @@ -214,38 +223,41 @@ public static String getVersion() {
* @throws SQLException when a SQLException occurs
*
*/
public static void addGISTypes(PGConnection pgconn) throws SQLException {
public static void addGISTypes(final PGConnection pgconn) throws SQLException {
loadTypesAdder("74").addGT((Connection) pgconn, false);
}


/**
* adds the JTS/PostGIS Data types to a PG 8.0+ Connection.
*
* @param pgconn The PGConnection object to add the types to
* @throws SQLException when a SQLException occurs
*/
public static void addGISTypes80(PGConnection pgconn) throws SQLException {
public static void addGISTypes80(final PGConnection pgconn) throws SQLException {
loadTypesAdder("80").addGT((Connection) pgconn, false);
}


/**
* adds the JTS/PostGIS Data types to a PG 7.2 Connection.
*
* @param pgconn The PGConnection object to add the types to
* @throws SQLException when a SQLException occurs
*/
public static void addGISTypes72(org.postgresql.PGConnection pgconn) throws SQLException {
public static void addGISTypes72(final org.postgresql.PGConnection pgconn) throws SQLException {
loadTypesAdder("72").addGT((Connection) pgconn, false);
}


/**
* Mangles the PostGIS URL to return the original PostGreSQL URL
*
* @param url String containing the url to be "mangled"
* @return "mangled" string
* @throws SQLException when a SQLException occurs
*/
protected String mangleURL(String url) throws SQLException {
protected String mangleURL(final String url) throws SQLException {
String myProgo = getProtoString();
if (url.startsWith(myProgo)) {
return POSTGRES_PROTOCOL + url.substring(myProgo.length());
Expand All @@ -254,13 +266,15 @@ protected String mangleURL(String url) throws SQLException {
}
}


protected String getProtoString() {
return POSTGIS_PROTOCOL;
}


/** Base class for the three typewrapper implementations */
protected abstract static class TypesAdder {
public final void addGT(java.sql.Connection conn, boolean lw) throws SQLException {
public final void addGT(final java.sql.Connection conn, final boolean lw) throws SQLException {
if (lw) {
addBinaryGeometries(conn);
} else {
Expand All @@ -269,68 +283,88 @@ public final void addGT(java.sql.Connection conn, boolean lw) throws SQLExceptio
addBoxen(conn);
}

public abstract void addGeometries(Connection conn) throws SQLException;
public abstract void addGeometries(final Connection conn) throws SQLException;

public abstract void addBoxen(Connection conn) throws SQLException;
public abstract void addBoxen(final Connection conn) throws SQLException;

public abstract void addBinaryGeometries(Connection conn) throws SQLException;
public abstract void addBinaryGeometries(final Connection conn) throws SQLException;
}


/** addGISTypes for V7.3 and V7.4 pgjdbc */
protected static final class TypesAdder74 extends TypesAdder {
public void addGeometries(Connection conn) throws SQLException {

/** {@inheritDoc} */
@Override
public void addGeometries(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometry.class);
pgconn.addDataType("public.geometry", org.postgis.PGgeometry.class);
pgconn.addDataType("\"public\".\"geometry\"", org.postgis.PGgeometry.class);

pgconn.addDataType("geography", org.postgis.PGgeometry.class);
pgconn.addDataType("public.geography", org.postgis.PGgeometry.class);
pgconn.addDataType("\"public\".\"geography\"", org.postgis.PGgeometry.class);
pgconn.addDataType("geography", org.postgis.PGgeography.class);
pgconn.addDataType("public.geography", org.postgis.PGgeography.class);
pgconn.addDataType("\"public\".\"geography\"", org.postgis.PGgeography.class);
}

public void addBoxen(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBoxen(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
}

public void addBinaryGeometries(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBinaryGeometries(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeographyLW.class);
}
}


/** addGISTypes for V7.2 pgjdbc */
protected static class TypesAdder72 extends TypesAdder {
public void addGeometries(Connection conn) throws SQLException {

/** {@inheritDoc} */
@Override
public void addGeometries(final Connection conn) throws SQLException {
org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometry.class);
pgconn.addDataType("public.geometry", org.postgis.PGgeometry.class);
pgconn.addDataType("\"public\".\"geometry\"", org.postgis.PGgeometry.class);

pgconn.addDataType("geography", org.postgis.PGgeometry.class);
pgconn.addDataType("public.geography", org.postgis.PGgeometry.class);
pgconn.addDataType("\"public\".\"geography\"", org.postgis.PGgeometry.class);
pgconn.addDataType("geography", org.postgis.PGgeography.class);
pgconn.addDataType("public.geography", org.postgis.PGgeography.class);
pgconn.addDataType("\"public\".\"geography\"", org.postgis.PGgeography.class);
}

public void addBoxen(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBoxen(final Connection conn) throws SQLException {
org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
}

public void addBinaryGeometries(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBinaryGeometries(final Connection conn) throws SQLException {
org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeographyLW.class);
}
}


/** addGISTypes for V8.0 (and hopefully newer) pgjdbc */
protected static class TypesAdder80 extends TypesAdder {
public void addGeometries(Connection conn) throws SQLException {

/** {@inheritDoc} */
@Override
public void addGeometries(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometry.class);
pgconn.addDataType("public.geometry", org.postgis.PGgeometry.class);
Expand All @@ -341,20 +375,29 @@ public void addGeometries(Connection conn) throws SQLException {
pgconn.addDataType("\"public\".\"geography\"", org.postgis.PGgeometry.class);
}

public void addBoxen(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBoxen(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
}

public void addBinaryGeometries(Connection conn) throws SQLException {
/** {@inheritDoc} */
@Override
public void addBinaryGeometries(final Connection conn) throws SQLException {
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("geometry", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeometryLW.class);
pgconn.addDataType("geography", org.postgis.PGgeographyLW.class);
}
}


/** {@inheritDoc} */
@Override
public Logger getParentLogger() {
throw new UnsupportedOperationException("Not supported yet.");
}


}
Loading

0 comments on commit a77061a

Please sign in to comment.