Class DriverWrapper

java.lang.Object
org.postgresql.Driver
io.github.sebasbaumh.postgis.DriverWrapper
All Implemented Interfaces:
Driver

@NonNullByDefault({PARAMETER,RETURN_TYPE}) public class DriverWrapper extends org.postgresql.Driver
Wraps the PostGreSQL Driver to transparently add the PostGIS Object Classes. This avoids the need of explicit addDataType() calls from the driver users side.

This method currently works with J2EE DataSource implementations, and with DriverManager framework.

Simply replace the "jdbc:postgresql:" with a "jdbc:postgresql_postGIS:" in the jdbc URL.

When using the drivermanager, you need to initialize DriverWrapper instead of (or in addition to) Driver. When using a J2EE DataSource implementation, set the driver class property in the datasource config, the following works for jboss: <driver-class>io.github.sebasbaumh.postgis.DriverWrapper</driver-class>

If you don't like or want to use the DriverWrapper, you can just call registerDataTypes(Connection) on your Connection.

This wrapper always uses EWKB as representation, and thus works against PostGIS servers starting from 2.3.

original author Markus Schaber <[email protected]>

reworked by Sebastian Baumhekel

  • Field Details

  • Constructor Details

    • DriverWrapper

      public DriverWrapper()
      Default constructor.
  • Method Details

    • registerDataTypes

      public static void registerDataTypes(Connection conn) throws SQLException
      Registers all datatypes on the given connection, supports wrapped connections unlike registerDataTypes(PGConnection).
      Parameters:
      conn - Connection
      Throws:
      SQLException - if the Connection is neither an PGConnection, nor a Connection wrapped around an PGConnection.
    • registerDataTypes

      public static void registerDataTypes(org.postgresql.PGConnection pgconn) throws SQLException
      Registers all datatypes on the given connection.
      Parameters:
      pgconn - PGConnection
      Throws:
      SQLException
    • acceptsURL

      public boolean acceptsURL(@Nonnull String url)
      Check whether the driver thinks he can handle the given URL.
      Specified by:
      acceptsURL in interface Driver
      Overrides:
      acceptsURL in class org.postgresql.Driver
      Parameters:
      url - the URL of the driver
      Returns:
      true if this driver accepts the given URL
      See Also:
    • connect

      @Nullable public Connection connect(@Nonnull String url, @Nonnull Properties info) throws SQLException
      Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes()
      Specified by:
      connect in interface Driver
      Overrides:
      connect in class org.postgresql.Driver
      Parameters:
      url - the URL of the database to connect to
      info - a list of arbitrary tag/value pairs as connection arguments
      Returns:
      a connection to the URL or null if it isnt us
      Throws:
      SQLException - if a database access error occurs
      See Also:
    • getParentLogger

      public Logger getParentLogger()
      Specified by:
      getParentLogger in interface Driver
      Overrides:
      getParentLogger in class org.postgresql.Driver