Skip to content
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

New codecs for java 8 time types - issue 591 #608

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
163 changes: 85 additions & 78 deletions README.md

Large diffs are not rendered by default.

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

import java.time.DayOfWeek;

final class DayOfWeekCodec extends IntegerCodecDelegate<DayOfWeek> {

DayOfWeekCodec(ByteBufAllocator byteBufAllocator) {
super(DayOfWeek.class, byteBufAllocator, DayOfWeek::getValue, DayOfWeek::of);
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
new UrlCodec(byteBufAllocator),
new UuidCodec(byteBufAllocator),
new ZoneIdCodec(byteBufAllocator),
new DayOfWeekCodec(byteBufAllocator),
new MonthCodec(byteBufAllocator),
new MonthDayCodec(byteBufAllocator),
new PeriodCodec(byteBufAllocator),
new YearCodec(byteBufAllocator),
new YearMonthCodec(byteBufAllocator),

// JSON
new JsonCodec(byteBufAllocator, preferAttachedBuffers),
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/IntegerCodecDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017 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.EncodedParameter;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.util.Assert;

import java.util.function.Function;

class IntegerCodecDelegate<T> extends AbstractCodec<T> {

private final IntegerCodec delegate;
private final Function<T, Integer> toIntegerConverter;
private final Function<Integer, T> fromIntegerConverter;

IntegerCodecDelegate(Class<T> type, ByteBufAllocator byteBufAllocator, Function<T,Integer> toIntegerConverter, Function<Integer, T> fromIntegerConverter) {
super(type);
this.delegate = new IntegerCodec(byteBufAllocator);
this.toIntegerConverter = toIntegerConverter;
this.fromIntegerConverter = fromIntegerConverter;
}

@Override
boolean doCanDecode(PostgresqlObjectId type, Format format) {
return delegate.doCanDecode(type, format);
}

@Override
T doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format, Class<? extends T> type) {
final Integer number = delegate.doDecode(buffer, dataType, format, Integer.TYPE);
return fromIntegerConverter.apply(number);
}

@Override
EncodedParameter doEncode(T value) {
Assert.requireNonNull(value, "value must not be null");
return delegate.doEncode(toIntegerConverter.apply(value));
}

@Override
EncodedParameter doEncode(T value, PostgresTypeIdentifier dataType) {
Assert.requireNonNull(value, "value must not be null");
Assert.requireNonNull(dataType, "dataType must not be null");
return delegate.doEncode(toIntegerConverter.apply(value), dataType);
}

@Override
public Iterable<? extends PostgresTypeIdentifier> getDataTypes() {
return delegate.getDataTypes();
}

@Override
public EncodedParameter encodeNull() {
return delegate.encodeNull();
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/MonthCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 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.ByteBufAllocator;

import java.time.Month;

final class MonthCodec extends IntegerCodecDelegate<Month> {

MonthCodec(ByteBufAllocator byteBufAllocator) {
super(Month.class, byteBufAllocator, Month::getValue, Month::of);
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/MonthDayCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 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.ByteBufAllocator;

import java.time.MonthDay;

final class MonthDayCodec extends StringCodecDelegate<MonthDay> {

MonthDayCodec(ByteBufAllocator byteBufAllocator) {
super(MonthDay.class, byteBufAllocator, MonthDay::toString, MonthDay::parse);
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/PeriodCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 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.ByteBufAllocator;

import java.time.Period;

final class PeriodCodec extends StringCodecDelegate<Period> {

PeriodCodec(ByteBufAllocator byteBufAllocator) {
super(Period.class, byteBufAllocator, Period::toString, Period::parse);
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/YearCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 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.ByteBufAllocator;

import java.time.Year;

final class YearCodec extends IntegerCodecDelegate<Year> {

YearCodec(ByteBufAllocator byteBufAllocator) {
super(Year.class, byteBufAllocator, Year::getValue, Year::of);
}
}
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/YearMonthCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 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.ByteBufAllocator;

import java.time.YearMonth;

final class YearMonthCodec extends StringCodecDelegate<YearMonth> {

YearMonthCodec(ByteBufAllocator byteBufAllocator) {
super(YearMonth.class, byteBufAllocator, YearMonth::toString, YearMonth::parse);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
[
{
"name": "[Ljava.time.Period;",
"unsafeAllocated": true
},
{
"name": "[Ljava.time.MonthDay;",
"unsafeAllocated": true
},
{
"name": "[Ljava.time.YearMonth;",
"unsafeAllocated": true
},
{
"name": "[Lio.r2dbc.postgresql.codec.Box;",
"unsafeAllocated": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,8 @@
import io.netty.buffer.Unpooled;
import io.r2dbc.postgresql.api.PostgresqlResult;
import io.r2dbc.postgresql.api.PostgresqlStatement;
import io.r2dbc.postgresql.codec.Box;
import io.r2dbc.postgresql.codec.Circle;
import io.r2dbc.postgresql.codec.EnumCodec;
import io.r2dbc.postgresql.codec.Json;
import io.r2dbc.postgresql.codec.Line;
import io.r2dbc.postgresql.codec.Lseg;
import io.r2dbc.postgresql.codec.Path;
import io.r2dbc.postgresql.codec.Point;
import io.r2dbc.postgresql.codec.Polygon;
import io.r2dbc.postgresql.codec.PostgresqlObjectId;
import io.r2dbc.spi.Blob;
import io.r2dbc.spi.Clob;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.Parameters;
import io.r2dbc.spi.R2dbcType;
import io.r2dbc.spi.Type;
import io.r2dbc.postgresql.codec.*;
import io.r2dbc.spi.*;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
Expand All @@ -51,22 +37,10 @@
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.LinkedHashMap;
Expand All @@ -75,6 +49,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;

import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -578,6 +553,31 @@ void uuid() {
testCodec(UUID.class, UUID.randomUUID(), "UUID");
}

@Test
void year() {
testCodec(Year.class, Year.now(), "INT4");
}

@Test
void month() {
IntStream.rangeClosed(1, 12).forEach(month -> testCodec(Month.class, Month.of(month), "INT4"));
}

@Test
void dayOfWeek() {
IntStream.rangeClosed(1, 7).forEach(month -> testCodec(DayOfWeek.class, DayOfWeek.of(month), "INT4"));
}

@Test
void monthDay() {
testCodec(MonthDay.class, MonthDay.now(), "VARCHAR");
}

@Test
void yearMonth() {
testCodec(YearMonth.class, YearMonth.now(), "VARCHAR");
}

@Test
void uuidArray() {
testCodec(UUID[].class, new UUID[]{UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()}, "UUID[]");
Expand Down