-
Notifications
You must be signed in to change notification settings - Fork 176
/
AbstractTemporalCodec.java
171 lines (146 loc) · 6.74 KB
/
AbstractTemporalCodec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2019 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.message.Format;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.ByteBufUtils;
import reactor.util.annotation.Nullable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.Function;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.DATE;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TIME;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TIMESTAMP;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TIMESTAMPTZ;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TIMETZ;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
/**
* Codec to decode all known temporal types.
*
* @param <T> the type that is handled by this {@link Codec}
*/
abstract class AbstractTemporalCodec<T extends Temporal> extends BuiltinCodecSupport<T> {
private static final Set<PostgresqlObjectId> SUPPORTED_TYPES = EnumSet.of(DATE, TIMESTAMP, TIMESTAMPTZ, TIME, TIMETZ);
private final PostgresqlObjectId postgresType;
/**
* Create a new {@link AbstractTemporalCodec}.
*
* @param type the type handled by this codec
*/
AbstractTemporalCodec(Class<T> type, ByteBufAllocator byteBufAllocator, PostgresqlObjectId postgresType, PostgresqlObjectId postgresArrayType, Function<T, String> toTextEncoder) {
super(type, byteBufAllocator, postgresType, postgresArrayType, toTextEncoder);
this.postgresType = postgresType;
}
@Override
public boolean canDecode(int dataType, Format format, Class<?> type) {
Assert.requireNonNull(format, "format must not be null");
Assert.requireNonNull(type, "type must not be null");
if (type == Object.class) {
if (PostgresqlObjectId.isValid(dataType) && PostgresqlObjectId.valueOf(dataType) != getDefaultType()) {
return false;
}
}
return super.canDecode(dataType, format, type);
}
@Override
final boolean doCanDecode(PostgresqlObjectId type, Format format) {
Assert.requireNonNull(format, "format must not be null");
Assert.requireNonNull(type, "type must not be null");
return SUPPORTED_TYPES.contains(type);
}
/**
* Decode {@code buffer} to {@link Temporal} and potentially convert it to {@link Class expectedType} using {@link Function converter} if the decoded type does not match {@code expectedType}.
*
* @param buffer the buffer
* @param dataType the Postgres type
* @param format value format
* @param expectedType expected result type
* @param converter converter to convert from {@link Temporal} into {@code expectedType}
* @return the decoded value
*/
T decodeTemporal(ByteBuf buffer, PostgresTypeIdentifier dataType, @Nullable Format format, Class<T> expectedType, Function<Temporal, T> converter) {
Temporal number = decodeTemporal(buffer, PostgresqlObjectId.from(dataType), format);
return potentiallyConvert(number, expectedType, converter);
}
/**
* Decode {@code buffer} to {@link Temporal} according to {@link PostgresqlObjectId}.
*
* @param buffer the buffer
* @param dataType the Postgres type
* @param format value format
* @return the decoded value
*/
private Temporal decodeTemporal(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format) {
Assert.requireNonNull(buffer, "byteBuf must not be null");
switch (dataType) {
case TIMESTAMP:
case TIMESTAMP_ARRAY:
if (FORMAT_BINARY == format) {
return EpochTime.fromLong(buffer.readLong()).toLocalDateTime();
}
return PostgresqlDateTimeFormatter.parse(ByteBufUtils.decode(buffer), LocalDateTime::from);
case DATE:
case DATE_ARRAY:
if (FORMAT_BINARY == format) {
return LocalDate.ofEpochDay(EpochTime.fromInt(buffer.readInt()).getJavaDays());
}
return LocalDate.parse(ByteBufUtils.decode(buffer));
case TIME:
case TIME_ARRAY:
if (FORMAT_BINARY == format) {
return LocalTime.ofNanoOfDay(buffer.readLong() * 1000);
}
return LocalTime.parse(ByteBufUtils.decode(buffer));
case TIMESTAMPTZ:
case TIMESTAMPTZ_ARRAY:
if (FORMAT_BINARY == format) {
return EpochTime.fromLong(buffer.readLong()).toInstant().atOffset(OffsetDateTime.now().getOffset());
}
return PostgresqlDateTimeFormatter.parse(ByteBufUtils.decode(buffer), ZonedDateTime::from);
case TIMETZ:
case TIMETZ_ARRAY:
if (FORMAT_BINARY == format) {
long timeNano = buffer.readLong() * 1000;
int offsetSec = -buffer.readInt();
return OffsetTime.of(LocalTime.ofNanoOfDay(timeNano), ZoneOffset.ofTotalSeconds(offsetSec));
}
return PostgresqlTimeFormatter.parse(ByteBufUtils.decode(buffer), OffsetTime::from);
}
throw new UnsupportedOperationException(String.format("Cannot decode value for type %s, format %s", dataType, format));
}
/**
* Returns the {@link PostgresqlObjectId} for to identify whether this codec is the default codec.
*
* @return the {@link PostgresqlObjectId} for to identify whether this codec is the default codec
*/
@Nullable
PostgresqlObjectId getDefaultType() {
return this.postgresType;
}
static <T> T potentiallyConvert(Temporal temporal, Class<T> expectedType, Function<Temporal, T> converter) {
return expectedType.isInstance(temporal) ? expectedType.cast(temporal) : converter.apply(temporal);
}
}