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.DriverWraps 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
DriverWrapperinstead 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 callregisterDataTypes(Connection)on yourConnection.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 Summary
Fields Modifier and Type Field Description static StringPOSTGIS_PROTOCOLPostGIS custom JDBC protocol.static StringPOSTGRES_PROTOCOLPostgreSQL JDBC protocol.
-
Constructor Summary
Constructors Constructor Description DriverWrapper()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptsURL(String url)Check whether the driver thinks he can handle the given URL.Connectionconnect(String url, Properties info)Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes()LoggergetParentLogger()static voidregisterDataTypes(Connection conn)Registers all datatypes on the given connection, supports wrapped connections unlikeregisterDataTypes(PGConnection).static voidregisterDataTypes(org.postgresql.PGConnection pgconn)Registers all datatypes on the given connection.static voidregisterDataTypesForBinaryTransfer(Connection conn)Registers all datatypes for binary transfer on the given connection, supports wrapped connections.
-
-
-
Field Detail
-
POSTGIS_PROTOCOL
public static final String POSTGIS_PROTOCOL
PostGIS custom JDBC protocol.- See Also:
- Constant Field Values
-
POSTGRES_PROTOCOL
public static final String POSTGRES_PROTOCOL
PostgreSQL JDBC protocol.- See Also:
- Constant Field Values
-
-
Method Detail
-
registerDataTypes
public static void registerDataTypes(Connection conn) throws SQLException
Registers all datatypes on the given connection, supports wrapped connections unlikeregisterDataTypes(PGConnection).- Parameters:
conn-Connection- Throws:
SQLException- if theConnectionis neither anPGConnection, nor aConnectionwrapped around anPGConnection.
-
registerDataTypes
public static void registerDataTypes(org.postgresql.PGConnection pgconn) throws SQLExceptionRegisters all datatypes on the given connection.- Parameters:
pgconn-PGConnection- Throws:
SQLException
-
registerDataTypesForBinaryTransfer
public static void registerDataTypesForBinaryTransfer(Connection conn) throws SQLException
Registers all datatypes for binary transfer on the given connection, supports wrapped connections.NOTE: this is experimental and only necessary until PostgreSQL JDBC driver is able to register types for binary transfer.
- Parameters:
conn-Connection- Throws:
SQLException- if theConnectionis noBaseConnection.
-
acceptsURL
public boolean acceptsURL(@Nonnull String url)Check whether the driver thinks he can handle the given URL.- Specified by:
acceptsURLin interfaceDriver- Overrides:
acceptsURLin classorg.postgresql.Driver- Parameters:
url- the URL of the driver- Returns:
- true if this driver accepts the given URL
- See Also:
Driver.acceptsURL(java.lang.String)
-
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:
connectin interfaceDriver- Overrides:
connectin classorg.postgresql.Driver- Parameters:
url- the URL of the database to connect toinfo- 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:
Driver.connect(java.lang.String, java.util.Properties),Driver
-
getParentLogger
public Logger getParentLogger()
- Specified by:
getParentLoggerin interfaceDriver- Overrides:
getParentLoggerin classorg.postgresql.Driver
-
-