Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Use this. for instance field access. Align license headers. Simplify classpath detection.

[#483][closes #491]

Signed-off-by: Mark Paluch <mpaluch@vmware.com>
  • Loading branch information
mp911de committed Feb 16, 2022
1 parent fb8917d commit 8f2c18f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
| [`date`][psql-date-ref] | [`LocalDate`][java-ld-ref]|
| [`double precision`][psql-floating-point-ref] | [**`Double`**][java-double-ref], [`Float`][java-float-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Integer`][java-integer-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|
| [enumerated types][psql-enum-ref] | Client code `Enum` types through `EnumCodec`|
| [`geometry`][postgis-ref] | **`org.locationtech.jts.geom.Geometry`**|
| [`hstore`][psql-hstore-ref] | [**`Map`**][java-map-ref]|
| [`inet`][psql-inet-ref] | [**`InetAddress`**][java-inet-ref]|
| [`integer`][psql-integer-ref] | [**`Integer`**][java-integer-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|
Expand Down Expand Up @@ -493,9 +494,10 @@ Support for the following single-dimensional arrays (read and write):
[psql-uuid-ref]: https://www.postgresql.org/docs/current/datatype-uuid.html
[psql-xml-ref]: https://www.postgresql.org/docs/current/datatype-xml.html
[psql-runtime-config]: https://www.postgresql.org/docs/current/runtime-config-client.html
[postgis-ref]: http://postgis.net/workshops/postgis-intro/geometries.html

[r2dbc-blob-ref]: https://r2dbc.io/spec/0.9.0.M1/api/io/r2dbc/spi/Blob.html
[r2dbc-clob-ref]: https://r2dbc.io/spec/0.9.0.M1/api/io/r2dbc/spi/Clob.html
[r2dbc-blob-ref]: https://r2dbc.io/spec/0.9.0.RELEASE/api/io/r2dbc/spi/Blob.html
[r2dbc-clob-ref]: https://r2dbc.io/spec/0.9.0.RELEASE/api/io/r2dbc/spi/Clob.html

[java-bigdecimal-ref]: https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html
[java-biginteger-ref]: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html
Expand Down Expand Up @@ -529,7 +531,7 @@ This driver accepts the following extensions:

Extensions can be registered programmatically using `PostgresConnectionConfiguration` or discovered using Java's `ServiceLoader` mechanism (from `META-INF/services/io.r2dbc.postgresql.extension.Extension`).

The driver ships with built-in dynamic codecs (e.g. `hstore`) that are registered during the connection handshake depending on their availability while connecting. Note that Postgres extensions registered after a connection was established require a reconnect to initialize the codec.
The driver ships with built-in dynamic codecs (e.g. `hstore`, PostGIS `geometry`) that are registered during the connection handshake depending on their availability while connecting. Note that Postgres extensions registered after a connection was established require a reconnect to initialize the codec.

## Logging
If SL4J is on the classpath, it will be used. Otherwise, there are two possible fallbacks: Console or `java.util.logging.Logger`). By default, the Console fallback is used. To use the JDK loggers, set the `reactor.logging.fallback` System property to `JDK`.
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/io/r2dbc/postgresql/codec/BuiltinDynamicCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@ public class BuiltinDynamicCodecs implements CodecRegistrar {

private static final Object EMPTY = new Object();

private interface CodecSupport {
default boolean isSupported() {
return true;
}
}

enum BuiltinCodec implements CodecSupport {
enum BuiltinCodec {

HSTORE("hstore"),
POSTGIS_GEOMETRY("geometry") {

private final boolean jtsPresent = isPresent(BuiltinDynamicCodecs.class.getClassLoader(), "org.locationtech.jts.geom.Geometry");

@Override
public boolean isSupported() {
String className = "org.locationtech.jts.geom.Geometry";
ClassLoader classLoader = getClass().getClassLoader();

return isPresent(classLoader, className);
return this.jtsPresent;
}
};

Expand All @@ -74,6 +69,10 @@ public String getName() {
return this.name;
}

boolean isSupported() {
return true;
}

static BuiltinCodec lookup(@Nullable String name) {

for (BuiltinCodec codec : values()) {
Expand Down Expand Up @@ -115,9 +114,9 @@ private static String getPlaceholders() {
return Arrays.stream(BuiltinCodec.values()).map(s -> "'" + s.getName() + "'").collect(Collectors.joining(","));
}

private static boolean isPresent(ClassLoader classLoader, String fullyQualifiedClassName) {
private static boolean isPresent(ClassLoader classLoader, String name) {
try {
classLoader.loadClass(fullyQualifiedClassName);
Class.forName(name, false, classLoader);
return true;
} catch (ClassNotFoundException e) {
return false;
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/io/r2dbc/postgresql/codec/PostgisGeometryCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.ByteBufUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKTWriter;
import reactor.core.publisher.Mono;

import javax.annotation.Nullable;
Expand All @@ -34,12 +36,17 @@
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;

/**
* PostGIS codec using {@link WKBReader} and {@link WKTWriter}.
*/
final class PostgisGeometryCodec implements Codec<Geometry>, CodecMetadata {

private static final Class<Geometry> TYPE = Geometry.class;

private final ByteBufAllocator byteBufAllocator;

private final GeometryFactory geometryFactory = new GeometryFactory();

private final int oid;

/**
Expand Down Expand Up @@ -83,7 +90,7 @@ public Geometry decode(@Nullable ByteBuf buffer, int dataType, Format format, Cl
Assert.isTrue(format == FORMAT_TEXT, "format must be FORMAT_TEXT");

try {
return new WKBReader().read(WKBReader.hexToBytes(ByteBufUtils.decode(buffer)));
return new WKBReader(this.geometryFactory).read(WKBReader.hexToBytes(ByteBufUtils.decode(buffer)));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
Expand All @@ -94,8 +101,8 @@ public EncodedParameter encode(Object value) {
Assert.requireType(value, Geometry.class, "value must be Geometry type");
Geometry geometry = (Geometry) value;

return new EncodedParameter(Format.FORMAT_TEXT, oid, Mono.fromSupplier(
() -> ByteBufUtils.encode(byteBufAllocator, geometry.toText())
return new EncodedParameter(Format.FORMAT_TEXT, this.oid, Mono.fromSupplier(
() -> ByteBufUtils.encode(this.byteBufAllocator, geometry.toText())
));
}

Expand All @@ -106,7 +113,7 @@ public EncodedParameter encode(Object value, int dataType) {

@Override
public EncodedParameter encodeNull() {
return new EncodedParameter(FORMAT_BINARY, oid, NULL_VALUE);
return new EncodedParameter(FORMAT_BINARY, this.oid, NULL_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2022 the original author or authors.
*
* 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
*
* https://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 io.r2dbc.postgresql.codec;

import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -30,7 +46,7 @@
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Unit tests for {@link PostgisGeometryCodec }.
* Unit tests for {@link PostgisGeometryCodec}.
*/
final class PostgisGeometryCodecUnitTests {

Expand Down

0 comments on commit 8f2c18f

Please sign in to comment.