Skip to content

Add support for point type #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator) {
new ShortArrayCodec(byteBufAllocator),
new StringArrayCodec(byteBufAllocator),
new IntegerArrayCodec(byteBufAllocator),
new LongArrayCodec(byteBufAllocator)
new LongArrayCodec(byteBufAllocator),

new PointCodec(byteBufAllocator)
));
}

Expand Down
119 changes: 119 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2017-2020 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;

/**
* <p>It maps to the point datatype in org.postgresql.</p>
*
* <p> It uses double to represent the coordinates.</p>
*/
public class Point {

private final double x;

private final double y;

/**
* @param x coordinate
* @param y coordinate
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

/**
* Translate the point by the supplied amount.
*
* @param x integer amount to add on the x axis
* @param y integer amount to add on the y axis
* @return - new point with translated values
*/
public Point translate(int x, int y) {
return translate((double) x, (double) y);
}

/**
* Translate the point by the supplied amount.
*
* @param x double amount to add on the x axis
* @param y double amount to add on the y axis
* @return - new point with translated values
*/
public Point translate(double x, double y) {
return new Point(this.x + x, this.y + y);
}

/**
* Moves the point to the supplied coordinates.
*
* @param x integer coordinate
* @param y integer coordinate
*/
public Point move(int x, int y) {
return setLocation(x, y);
}

/**
* Moves the point to the supplied coordinates.
*
* @param x double coordinate
* @param y double coordinate
* @return - new point with provided coordinates
*/
public Point move(double x, double y) {
return new Point(x, y);
}

/**
* Moves the point to the supplied coordinates.
*
* @param x integer coordinate
* @param y integer coordinate
* @return - return new Point with these coordinates
*/
public Point setLocation(int x, int y) {
return move((double) x, (double) y);
}

public boolean equals(Object obj) {
if (obj instanceof Point) {
Point p = (Point) obj;
return x == p.x && y == p.y;
}
return false;
}

public int hashCode() {
long v1 = Double.doubleToLongBits(x);
long v2 = Double.doubleToLongBits(y);
return (int) (v1 ^ v2 ^ (v1 >>> 32) ^ (v2 >>> 32));
}

public String toString() {
return "(" + x + "," + y + ")";
}

}
86 changes: 86 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/PointCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2017-2020 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;
import io.netty.buffer.ByteBufAllocator;
import io.r2dbc.postgresql.client.Parameter;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.type.PostgresqlObjectId;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.ByteBufUtils;
import reactor.util.annotation.Nullable;

import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;

public class PointCodec extends AbstractCodec<Point> {

private final ByteBufAllocator byteBufAllocator;

PointCodec(ByteBufAllocator byteBufAllocator) {
super(Point.class);
this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
}

@Override
boolean doCanDecode(PostgresqlObjectId type, @Nullable Format format) {
Assert.requireNonNull(type, "type must not be null");

return POINT == type;
}

@Override
Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class<? extends Point> type) {
Assert.requireNonNull(buffer, "byteBuf must not be null");
Assert.requireNonNull(type, "type must not be null");
Assert.requireNonNull(format, "format must not be null");

if (format == FORMAT_BINARY) {
double x = buffer.readDouble();
double y = buffer.readDouble();
return new Point(x, y);
} else {
String decodedAsString = ByteBufUtils.decode(buffer);
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
String[] coordinatesAsString = parenRemovedVal.split(",");
double x = Double.parseDouble(coordinatesAsString[0]);
double y = Double.parseDouble(coordinatesAsString[1]);
return new Point(x, y);
}
}

/**
* @param value the {@code value}.
* @return Point in String format as understood by postgresql - (x,y)
*/
@Override
Parameter doEncode(Point value) {
Assert.requireNonNull(value, "value must not be null");
return create(POINT, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes()).writeDouble(value.getX()).writeDouble(value.getY()));
}

@Override
public Parameter encodeNull() {
return createNull(POINT, FORMAT_BINARY);
}

public int lengthInBytes() {
return 16;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.r2dbc.postgresql.api.PostgresqlStatement;
import io.r2dbc.postgresql.codec.EnumCodec;
import io.r2dbc.postgresql.codec.Json;
import io.r2dbc.postgresql.codec.Point;
import io.r2dbc.spi.Blob;
import io.r2dbc.spi.Clob;
import io.r2dbc.spi.Connection;
Expand Down Expand Up @@ -360,6 +361,11 @@ void offsetDateTime() {
testCodec(OffsetDateTime.class, OffsetDateTime.now(), (actual, expected) -> assertThat(actual.isEqual(expected)).isTrue(), "TIMESTAMP WITH TIME ZONE");
}

@Test
void point() {
testCodec(Point.class, new Point(1.12, 2.12), "POINT");
}

@Test
void shortArray() {
testCodec(Short[].class, new Short[]{100, 200, 300}, "INT2[]");
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/io/r2dbc/postgresql/codec/PointCodecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2017-2020 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;
import io.r2dbc.postgresql.client.Parameter;
import io.r2dbc.postgresql.client.ParameterAssert;
import org.junit.jupiter.api.Test;

import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR;
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class PointCodecTest {

@Test
void constructorNoByteBufAllocator() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(null))
.withMessage("byteBufAllocator must not be null");
}

@Test
void doCanDecodeNoType() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doCanDecode(null, FORMAT_BINARY))
.withMessage("type must not be null");
}

@Test
void doCanDecode() {
PointCodec codec = new PointCodec(TEST);

assertThat(codec.doCanDecode(VARCHAR, FORMAT_BINARY)).isFalse();
assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isTrue();
assertThat(codec.doCanDecode(POINT, FORMAT_BINARY)).isTrue();
}

@Test
void doDecodeNoByteBuf() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(null, POINT, FORMAT_BINARY, Point.class))
.withMessage("byteBuf must not be null");
}

@Test
void doDecodeNoType() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(TEST.buffer(), POINT, FORMAT_BINARY, null))
.withMessage("type must not be null");
}

@Test
void doDecodeNoFormat() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(TEST.buffer(), POINT, null, Point.class))
.withMessage("format must not be null");
}

@Test
void doDecode() {
PointCodec codec = new PointCodec(TEST);
Point point = new Point(1.12, 2.12);
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);
assertThat(codec.doDecode(pointAsBinary, POINT, FORMAT_BINARY, Point.class)).isEqualTo(point);
}

@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doEncode(null))
.withMessage("value must not be null");
}

@Test
void doEncode() {
PointCodec codec = new PointCodec(TEST);
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);

ParameterAssert.assertThat(codec.doEncode(new Point(1.12, 2.12)))
.hasFormat(FORMAT_BINARY)
.hasType(POINT.getObjectId())
.hasValue(pointAsBinary);
}

@Test
void encodeNull() {
ParameterAssert.assertThat(new PointCodec(TEST).encodeNull())
.isEqualTo(new Parameter(FORMAT_BINARY, POINT.getObjectId(), NULL_VALUE));
}
}