Skip to content

Commit da5a689

Browse files
committed
adding support for DayOfWeek data type - pgjdbcgh-591
1 parent f77d357 commit da5a689

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
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

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
244244
new BigIntegerCodec(byteBufAllocator),
245245
new BooleanCodec(byteBufAllocator),
246246
new CharacterCodec(byteBufAllocator),
247+
new DayOfWeekCodec(byteBufAllocator),
247248
new DoubleCodec(byteBufAllocator),
248249
new FloatCodec(byteBufAllocator),
249250
new InetAddressCodec(byteBufAllocator),
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.r2dbc.postgresql.client.EncodedParameter;
4+
import io.r2dbc.postgresql.client.ParameterAssert;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.time.DayOfWeek;
8+
import java.util.Arrays;
9+
import java.util.function.Consumer;
10+
11+
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
12+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
13+
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
14+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
15+
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
18+
19+
class DayOfWeekCodecTest {
20+
21+
@Test
22+
void constructorNoByteBufAllocator() {
23+
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(null))
24+
.withMessage("byteBufAllocator must not be null");
25+
}
26+
27+
@Test
28+
void decode() {
29+
forEveryDayOfWeek(d ->
30+
assertThat(new DayOfWeekCodec(TEST).decode(TEST.buffer().writeInt(d.getValue()), INT4, FORMAT_BINARY, DayOfWeek.class)).isEqualTo(d));
31+
}
32+
33+
@Test
34+
void decodeNoByteBuf() {
35+
assertThat(new DayOfWeekCodec(TEST).decode(null, INT4.getObjectId(), FORMAT_BINARY, DayOfWeek.class)).isNull();
36+
}
37+
38+
@Test
39+
void doCanDecode() {
40+
DayOfWeekCodec codec = new DayOfWeekCodec(TEST);
41+
42+
assertThat(codec.doCanDecode(INT4, FORMAT_BINARY)).isTrue();
43+
assertThat(codec.doCanDecode(INT2, FORMAT_BINARY)).isTrue();
44+
assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue();
45+
assertThat(codec.doCanDecode(NUMERIC, FORMAT_TEXT)).isTrue();
46+
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse();
47+
}
48+
49+
@Test
50+
void doCanDecodeNoType() {
51+
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).doCanDecode(null, FORMAT_BINARY))
52+
.withMessage("type must not be null");
53+
}
54+
55+
@Test
56+
void doEncodeInt() {
57+
58+
forEveryDayOfWeek(d -> {
59+
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d))
60+
.hasFormat(FORMAT_BINARY)
61+
.hasType(INT4.getObjectId())
62+
.hasValue(TEST.buffer().writeInt(d.getValue()));
63+
});
64+
}
65+
66+
@Test
67+
void doEncodeShort() {
68+
forEveryDayOfWeek(d -> {
69+
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d, INT2))
70+
.hasFormat(FORMAT_BINARY)
71+
.hasType(INT2.getObjectId())
72+
.hasValue(TEST.buffer().writeShort(d.getValue()));
73+
});
74+
}
75+
76+
@Test
77+
void doEncodeLong() {
78+
79+
forEveryDayOfWeek(d -> {
80+
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).doEncode(d, INT8))
81+
.hasFormat(FORMAT_BINARY)
82+
.hasType(INT8.getObjectId())
83+
.hasValue(TEST.buffer().writeLong(d.getValue()));
84+
});
85+
}
86+
87+
@Test
88+
void doEncodeNoValue() {
89+
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).doEncode(null))
90+
.withMessage("value must not be null");
91+
}
92+
93+
@Test
94+
void encodeItemNoValue() {
95+
assertThatIllegalArgumentException().isThrownBy(() -> new DayOfWeekCodec(TEST).encode(null))
96+
.withMessage("value must not be null");
97+
}
98+
99+
@Test
100+
void encodeNull() {
101+
ParameterAssert.assertThat(new DayOfWeekCodec(TEST).encodeNull())
102+
.isEqualTo(new EncodedParameter(FORMAT_BINARY, INT4.getObjectId(), NULL_VALUE));
103+
}
104+
105+
private void forEveryDayOfWeek(Consumer<DayOfWeek> assertion) {
106+
Arrays.stream(DayOfWeek.values()).forEach(assertion);
107+
}
108+
}

0 commit comments

Comments
 (0)