Skip to content

Commit 43bf9d9

Browse files
steverigneymp911de
authored andcommitted
Add codecs for DayOfWeek, Month, MonthDay, Period, Year, YearMonth.
[resolves #591][#608]
1 parent a5d3b06 commit 43bf9d9

File tree

17 files changed

+1029
-109
lines changed

17 files changed

+1029
-109
lines changed

README.md

Lines changed: 86 additions & 78 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.DayOfWeek;
22+
23+
final class DayOfWeekCodec extends IntegerCodecDelegate<DayOfWeek> {
24+
25+
DayOfWeekCodec(ByteBufAllocator byteBufAllocator) {
26+
super(DayOfWeek.class, byteBufAllocator, DayOfWeek::getValue, DayOfWeek::of);
27+
}
28+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
128128
new UrlCodec(byteBufAllocator),
129129
new UuidCodec(byteBufAllocator),
130130
new ZoneIdCodec(byteBufAllocator),
131+
new DayOfWeekCodec(byteBufAllocator),
132+
new MonthCodec(byteBufAllocator),
133+
new MonthDayCodec(byteBufAllocator),
134+
new PeriodCodec(byteBufAllocator),
135+
new YearCodec(byteBufAllocator),
136+
new YearMonthCodec(byteBufAllocator),
131137

132138
// JSON
133139
new JsonCodec(byteBufAllocator, preferAttachedBuffers),
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.r2dbc.postgresql.client.EncodedParameter;
22+
import io.r2dbc.postgresql.message.Format;
23+
import io.r2dbc.postgresql.util.Assert;
24+
25+
import java.util.function.Function;
26+
27+
class IntegerCodecDelegate<T> extends AbstractCodec<T> {
28+
29+
private final IntegerCodec delegate;
30+
private final Function<T, Integer> toIntegerConverter;
31+
private final Function<Integer, T> fromIntegerConverter;
32+
33+
IntegerCodecDelegate(Class<T> type, ByteBufAllocator byteBufAllocator, Function<T,Integer> toIntegerConverter, Function<Integer, T> fromIntegerConverter) {
34+
super(type);
35+
this.delegate = new IntegerCodec(byteBufAllocator);
36+
this.toIntegerConverter = toIntegerConverter;
37+
this.fromIntegerConverter = fromIntegerConverter;
38+
}
39+
40+
@Override
41+
boolean doCanDecode(PostgresqlObjectId type, Format format) {
42+
return delegate.doCanDecode(type, format);
43+
}
44+
45+
@Override
46+
T doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format, Class<? extends T> type) {
47+
final Integer number = delegate.doDecode(buffer, dataType, format, Integer.TYPE);
48+
return fromIntegerConverter.apply(number);
49+
}
50+
51+
@Override
52+
EncodedParameter doEncode(T value) {
53+
Assert.requireNonNull(value, "value must not be null");
54+
return delegate.doEncode(toIntegerConverter.apply(value));
55+
}
56+
57+
@Override
58+
EncodedParameter doEncode(T value, PostgresTypeIdentifier dataType) {
59+
Assert.requireNonNull(value, "value must not be null");
60+
Assert.requireNonNull(dataType, "dataType must not be null");
61+
return delegate.doEncode(toIntegerConverter.apply(value), dataType);
62+
}
63+
64+
@Override
65+
public Iterable<? extends PostgresTypeIdentifier> getDataTypes() {
66+
return delegate.getDataTypes();
67+
}
68+
69+
@Override
70+
public EncodedParameter encodeNull() {
71+
return delegate.encodeNull();
72+
}
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.Month;
22+
23+
final class MonthCodec extends IntegerCodecDelegate<Month> {
24+
25+
MonthCodec(ByteBufAllocator byteBufAllocator) {
26+
super(Month.class, byteBufAllocator, Month::getValue, Month::of);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.MonthDay;
22+
23+
final class MonthDayCodec extends StringCodecDelegate<MonthDay> {
24+
25+
MonthDayCodec(ByteBufAllocator byteBufAllocator) {
26+
super(MonthDay.class, byteBufAllocator, MonthDay::toString, MonthDay::parse);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
23+
final class PeriodCodec extends StringCodecDelegate<Period> {
24+
25+
PeriodCodec(ByteBufAllocator byteBufAllocator) {
26+
super(Period.class, byteBufAllocator, Period::toString, Period::parse);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
23+
final class YearCodec extends IntegerCodecDelegate<Year> {
24+
25+
YearCodec(ByteBufAllocator byteBufAllocator) {
26+
super(Year.class, byteBufAllocator, Year::getValue, Year::of);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.YearMonth;
22+
23+
final class YearMonthCodec extends StringCodecDelegate<YearMonth> {
24+
25+
YearMonthCodec(ByteBufAllocator byteBufAllocator) {
26+
super(YearMonth.class, byteBufAllocator, YearMonth::toString, YearMonth::parse);
27+
}
28+
}

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

Lines changed: 12 additions & 0 deletions
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

0 commit comments

Comments
 (0)