Skip to content

Commit 53f74aa

Browse files
committed
adding support for Period data type - pgjdbcgh-591
1 parent da5a689 commit 53f74aa

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
258258
new MonthDayCodec(byteBufAllocator),
259259
new OffsetDateTimeCodec(byteBufAllocator),
260260
new OffsetTimeCodec(byteBufAllocator),
261+
new PeriodCodec(byteBufAllocator),
261262
new ShortCodec(byteBufAllocator),
262263
new UriCodec(byteBufAllocator),
263264
new UrlCodec(byteBufAllocator),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import io.netty.buffer.ByteBufAllocator;
20+
21+
import java.time.Period;
22+
import java.time.YearMonth;
23+
24+
final class PeriodCodec extends StringCodecDelegate<Period> {
25+
26+
PeriodCodec(ByteBufAllocator byteBufAllocator) {
27+
super(Period.class, byteBufAllocator, Period::toString, Period::parse);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.r2dbc.postgresql.client.EncodedParameter;
5+
import io.r2dbc.postgresql.client.ParameterAssert;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.nio.charset.Charset;
9+
import java.time.Period;
10+
import java.time.format.DateTimeParseException;
11+
12+
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
13+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.BPCHAR;
14+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.CHAR;
15+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.NAME;
16+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TEXT;
17+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.VARCHAR;
18+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
19+
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
23+
24+
class PeriodCodecTest {
25+
26+
@Test
27+
void constructorNoByteBufAllocator() {
28+
assertThatIllegalArgumentException().isThrownBy(() -> new PeriodCodec(null))
29+
.withMessage("byteBufAllocator must not be null");
30+
}
31+
32+
@Test
33+
void decodeDays() {
34+
final int days = 4;
35+
36+
final Period period = Period.ofDays(days);
37+
assertDecodeOfPeriod(period);
38+
}
39+
40+
@Test
41+
void decodeJunkString() {
42+
final String junkString = "hello world";
43+
final ByteBuf buffer = TEST.buffer();
44+
45+
final int charsWritten = buffer.writeCharSequence(junkString, Charset.defaultCharset());
46+
assertThat(charsWritten).isEqualTo(junkString.length());
47+
48+
assertThatExceptionOfType(DateTimeParseException.class)
49+
.isThrownBy(() -> new PeriodCodec(TEST).decode(buffer, VARCHAR, FORMAT_TEXT, Period.class));
50+
}
51+
52+
@Test
53+
void decodeMonths() {
54+
final int months = 3;
55+
56+
final Period period = Period.ofMonths(months);
57+
assertDecodeOfPeriod(period);
58+
}
59+
60+
@Test
61+
void decodeNoByteBuf() {
62+
assertThat(new PeriodCodec(TEST).decode(null, VARCHAR.getObjectId(), FORMAT_TEXT, Period.class)).isNull();
63+
}
64+
65+
@Test
66+
void decodeWeeks() {
67+
final int weeks = 7;
68+
69+
final Period period = Period.ofWeeks(weeks);
70+
assertDecodeOfPeriod(period);
71+
}
72+
73+
@Test
74+
void decodeYearsMonthsDays() {
75+
final int years = 5;
76+
final int months = 4;
77+
final int days = 7;
78+
79+
final Period period = Period.of(years, months, days);
80+
assertDecodeOfPeriod(period);
81+
}
82+
83+
@Test
84+
void doCanDecode() {
85+
final PeriodCodec codec = new PeriodCodec(TEST);
86+
87+
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isTrue();
88+
assertThat(codec.doCanDecode(CHAR, FORMAT_TEXT)).isTrue();
89+
assertThat(codec.doCanDecode(BPCHAR, FORMAT_TEXT)).isTrue();
90+
assertThat(codec.doCanDecode(NAME, FORMAT_TEXT)).isTrue();
91+
assertThat(codec.doCanDecode(TEXT, FORMAT_TEXT)).isTrue();
92+
}
93+
94+
@Test
95+
void doCanDecodeNoType() {
96+
assertThatIllegalArgumentException().isThrownBy(() -> new PeriodCodec(TEST).doCanDecode(null, FORMAT_TEXT))
97+
.withMessage("type must not be null");
98+
}
99+
100+
@Test
101+
void doEncodeNoValue() {
102+
assertThatIllegalArgumentException().isThrownBy(() -> new PeriodCodec(TEST).doEncode(null))
103+
.withMessage("value must not be null");
104+
}
105+
106+
@Test
107+
void encodeItemNoValue() {
108+
assertThatIllegalArgumentException().isThrownBy(() -> new PeriodCodec(TEST).encode(null))
109+
.withMessage("value must not be null");
110+
}
111+
112+
@Test
113+
void encodeNull() {
114+
ParameterAssert.assertThat(new PeriodCodec(TEST).encodeNull())
115+
.isEqualTo(new EncodedParameter(FORMAT_TEXT, VARCHAR.getObjectId(), NULL_VALUE));
116+
}
117+
118+
private static void assertDecodeOfPeriod(Period period) {
119+
final ByteBuf buffer = TEST.buffer();
120+
121+
final int charsWritten = buffer.writeCharSequence(period.toString(), Charset.defaultCharset());
122+
assertThat(charsWritten).isEqualTo(period.toString().length());
123+
124+
assertThat(new PeriodCodec(TEST)
125+
.decode(buffer, VARCHAR, FORMAT_TEXT, Period.class))
126+
.isEqualTo(period);
127+
}
128+
129+
}

0 commit comments

Comments
 (0)