Skip to content

Commit a340c02

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

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
264264
new UrlCodec(byteBufAllocator),
265265
new UuidCodec(byteBufAllocator),
266266
new YearCodec(byteBufAllocator),
267+
new YearMonthCodec(byteBufAllocator),
267268
new ZoneIdCodec(byteBufAllocator),
268269

269270
// JSON
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.Year;
22+
import java.time.YearMonth;
23+
24+
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
25+
26+
final class YearMonthCodec extends StringCodecDelegate<YearMonth> {
27+
28+
YearMonthCodec(ByteBufAllocator byteBufAllocator) {
29+
super(YearMonth.class, byteBufAllocator, YearMonth::toString, YearMonth::parse);
30+
}
31+
}

src/main/resources/META-INF/native-image/org.postgresql/r2dbc-postgresql/reflect-config.json

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
[
2+
{
3+
"name": "[Ljava.time.Period;",
4+
"unsafeAllocated": true
5+
},
6+
{
7+
"name": "[Ljava.time.MonthDay;",
8+
"unsafeAllocated": true
9+
},
10+
{
11+
"name": "[Ljava.time.YearMonth;",
12+
"unsafeAllocated": true
13+
},
214
{
315
"name": "[Lio.r2dbc.postgresql.codec.Box;",
416
"unsafeAllocated": true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.YearMonth;
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 YearMonthCodecTest {
25+
26+
@Test
27+
void constructorNoByteBufAllocator() {
28+
assertThatIllegalArgumentException().isThrownBy(() -> new YearMonthCodec(null))
29+
.withMessage("byteBufAllocator must not be null");
30+
}
31+
32+
@Test
33+
void decode() {
34+
final YearMonth yearMonth = YearMonth.now();
35+
36+
final ByteBuf buffer = TEST.buffer();
37+
38+
final int charsWritten = buffer.writeCharSequence(yearMonth.toString(), Charset.defaultCharset());
39+
assertThat(charsWritten).isEqualTo(yearMonth.toString().length());
40+
41+
assertThat(new YearMonthCodec(TEST)
42+
.decode(buffer, VARCHAR, FORMAT_TEXT, YearMonth.class))
43+
.isEqualTo(yearMonth);
44+
}
45+
46+
@Test
47+
void decodeJunkString() {
48+
final String junkString = "hello world";
49+
final ByteBuf buffer = TEST.buffer();
50+
51+
final int charsWritten = buffer.writeCharSequence(junkString, Charset.defaultCharset());
52+
assertThat(charsWritten).isEqualTo(junkString.length());
53+
54+
assertThatExceptionOfType(DateTimeParseException.class)
55+
.isThrownBy(() -> new YearMonthCodec(TEST).decode(buffer, VARCHAR, FORMAT_TEXT, YearMonth.class));
56+
}
57+
58+
@Test
59+
void decodeNoByteBuf() {
60+
assertThat(new YearMonthCodec(TEST).decode(null, VARCHAR.getObjectId(), FORMAT_TEXT, YearMonth.class)).isNull();
61+
}
62+
63+
@Test
64+
void doCanDecode() {
65+
YearMonthCodec codec = new YearMonthCodec(TEST);
66+
67+
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isTrue();
68+
assertThat(codec.doCanDecode(CHAR, FORMAT_TEXT)).isTrue();
69+
assertThat(codec.doCanDecode(BPCHAR, FORMAT_TEXT)).isTrue();
70+
assertThat(codec.doCanDecode(NAME, FORMAT_TEXT)).isTrue();
71+
assertThat(codec.doCanDecode(TEXT, FORMAT_TEXT)).isTrue();
72+
}
73+
74+
@Test
75+
void doCanDecodeNoType() {
76+
assertThatIllegalArgumentException().isThrownBy(() -> new YearMonthCodec(TEST).doCanDecode(null, FORMAT_TEXT))
77+
.withMessage("type must not be null");
78+
}
79+
80+
@Test
81+
void doEncodeNoValue() {
82+
assertThatIllegalArgumentException().isThrownBy(() -> new YearMonthCodec(TEST).doEncode(null))
83+
.withMessage("value must not be null");
84+
}
85+
86+
@Test
87+
void encodeItemNoValue() {
88+
assertThatIllegalArgumentException().isThrownBy(() -> new YearMonthCodec(TEST).encode(null))
89+
.withMessage("value must not be null");
90+
}
91+
92+
@Test
93+
void encodeNull() {
94+
ParameterAssert.assertThat(new YearMonthCodec(TEST).encodeNull())
95+
.isEqualTo(new EncodedParameter(FORMAT_TEXT, VARCHAR.getObjectId(), NULL_VALUE));
96+
}
97+
98+
}

0 commit comments

Comments
 (0)