-
Notifications
You must be signed in to change notification settings - Fork 219
/
TimestampCodec.java
384 lines (347 loc) · 12.2 KB
/
TimestampCodec.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab
package org.mariadb.jdbc.plugin.codec;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLDataException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.Calendar;
import java.util.EnumSet;
import java.util.TimeZone;
import org.mariadb.jdbc.client.Column;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.DataType;
import org.mariadb.jdbc.client.ReadableByteBuf;
import org.mariadb.jdbc.client.socket.Writer;
import org.mariadb.jdbc.plugin.Codec;
/** Timestamp codec */
public class TimestampCodec implements Codec<Timestamp> {
/** default instance */
public static final TimestampCodec INSTANCE = new TimestampCodec();
private static final EnumSet<DataType> COMPATIBLE_TYPES =
EnumSet.of(
DataType.DATE,
DataType.NEWDATE,
DataType.DATETIME,
DataType.TIMESTAMP,
DataType.YEAR,
DataType.VARSTRING,
DataType.VARCHAR,
DataType.STRING,
DataType.TIME,
DataType.BLOB,
DataType.TINYBLOB,
DataType.MEDIUMBLOB,
DataType.LONGBLOB);
public String className() {
return Timestamp.class.getName();
}
public boolean canDecode(Column column, Class<?> type) {
return COMPATIBLE_TYPES.contains(column.getType()) && type.isAssignableFrom(Timestamp.class);
}
public boolean canEncode(Object value) {
return value instanceof Timestamp;
}
@Override
@SuppressWarnings("fallthrough")
public Timestamp decodeText(ReadableByteBuf buf, int length, Column column, Calendar calParam)
throws SQLDataException {
switch (column.getType()) {
case TIME:
int[] parts = LocalTimeCodec.parseTime(buf, length, column);
Timestamp t;
// specific case for TIME, to handle value not in 00:00:00-23:59:59
Calendar cal = calParam == null ? Calendar.getInstance() : calParam;
synchronized (cal) {
cal.clear();
cal.setLenient(true);
if (parts[0] == -1) {
cal.set(
1970,
Calendar.JANUARY,
1,
parts[0] * parts[1],
parts[0] * parts[2],
parts[0] * parts[3] - 1);
t = new Timestamp(cal.getTimeInMillis());
t.setNanos(1_000_000_000 - parts[4]);
} else {
cal.set(1970, Calendar.JANUARY, 1, parts[1], parts[2], parts[3]);
t = new Timestamp(cal.getTimeInMillis());
t.setNanos(parts[4]);
}
}
return t;
case YEAR:
Calendar cal1 = calParam == null ? Calendar.getInstance() : calParam;
int year = Integer.parseInt(buf.readAscii(length));
if (column.getLength() <= 2) year += year >= 70 ? 1900 : 2000;
synchronized (cal1) {
cal1.clear();
cal1.set(year, Calendar.JANUARY, 1);
return new Timestamp(cal1.getTimeInMillis());
}
case DATE:
if (calParam == null || calParam.getTimeZone().equals(TimeZone.getDefault())) {
String s = buf.readAscii(length);
if ("0000-00-00".equals(s)) return null;
return new Timestamp(Date.valueOf(s).getTime());
}
String[] datePart = buf.readAscii(length).split("-");
synchronized (calParam) {
calParam.clear();
calParam.set(
Integer.parseInt(datePart[0]),
Integer.parseInt(datePart[1]) - 1,
Integer.parseInt(datePart[2]));
return new Timestamp(calParam.getTimeInMillis());
}
case BLOB:
case TINYBLOB:
case MEDIUMBLOB:
case LONGBLOB:
if (column.isBinary()) {
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Timestamp", column.getType()));
}
// expected fallthrough
// BLOB is considered as String if it has a collation (this is TEXT column)
case STRING:
case VARCHAR:
case VARSTRING:
case TIMESTAMP:
case DATETIME:
int pos = buf.pos();
int nanoBegin = -1;
int[] timestampsPart = new int[] {0, 0, 0, 0, 0, 0, 0};
int partIdx = 0;
for (int begin = 0; begin < length; begin++) {
byte b = buf.readByte();
if (b == '-' || b == ' ' || b == ':') {
partIdx++;
continue;
}
if (b == '.') {
partIdx++;
nanoBegin = begin;
continue;
}
if (b < '0' || b > '9') {
buf.pos(pos);
throw new SQLDataException(
String.format(
"value '%s' (%s) cannot be decoded as Timestamp",
buf.readString(length), column.getType()));
}
timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48;
}
if (timestampsPart[0] == 0
&& timestampsPart[1] == 0
&& timestampsPart[2] == 0
&& timestampsPart[3] == 0
&& timestampsPart[4] == 0
&& timestampsPart[5] == 0
&& timestampsPart[6] == 0) {
return null;
}
// fix non-leading tray for nanoseconds
if (nanoBegin > 0) {
for (int begin = 0; begin < 6 - (length - nanoBegin - 1); begin++) {
timestampsPart[6] = timestampsPart[6] * 10;
}
}
Timestamp timestamp;
if (calParam == null) {
Calendar c = Calendar.getInstance();
c.set(
timestampsPart[0],
timestampsPart[1] - 1,
timestampsPart[2],
timestampsPart[3],
timestampsPart[4],
timestampsPart[5]);
timestamp = new Timestamp(c.getTime().getTime());
timestamp.setNanos(timestampsPart[6] * 1000);
} else {
synchronized (calParam) {
calParam.clear();
calParam.set(
timestampsPart[0],
timestampsPart[1] - 1,
timestampsPart[2],
timestampsPart[3],
timestampsPart[4],
timestampsPart[5]);
timestamp = new Timestamp(calParam.getTime().getTime());
timestamp.setNanos(timestampsPart[6] * 1000);
}
}
return timestamp;
default:
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Timestamp", column.getType()));
}
}
@Override
@SuppressWarnings("fallthrough")
public Timestamp decodeBinary(ReadableByteBuf buf, int length, Column column, Calendar calParam)
throws SQLDataException {
Calendar cal = calParam == null ? Calendar.getInstance() : calParam;
int year;
int month = 1;
long dayOfMonth = 1;
int hour = 0;
int minutes = 0;
int seconds = 0;
long microseconds = 0;
switch (column.getType()) {
case TIME:
// specific case for TIME, to handle value not in 00:00:00-23:59:59
boolean negate = buf.readByte() == 1;
dayOfMonth = buf.readUnsignedInt();
hour = buf.readByte();
minutes = buf.readByte();
seconds = buf.readByte();
if (length > 8) {
microseconds = buf.readUnsignedInt();
}
int offset = cal.getTimeZone().getOffset(0);
long timeInMillis =
((24 * dayOfMonth + hour) * 3_600_000
+ minutes * 60_000
+ seconds * 1_000
+ microseconds / 1_000)
* (negate ? -1 : 1)
- offset;
return new Timestamp(timeInMillis);
case BLOB:
case TINYBLOB:
case MEDIUMBLOB:
case LONGBLOB:
if (column.isBinary()) {
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Timestamp", column.getType()));
}
// expected fallthrough
// BLOB is considered as String if it has a collation (this is TEXT column)
case STRING:
case VARCHAR:
case VARSTRING:
String val = buf.readString(length);
try {
int[] parts = LocalDateTimeCodec.parseTimestamp(val);
if (parts == null) return null;
year = parts[0];
month = parts[1];
dayOfMonth = parts[2];
hour = parts[3];
minutes = parts[4];
seconds = parts[5];
microseconds = parts[6] / 1000;
break;
} catch (DateTimeException dte) {
throw new SQLDataException(
String.format(
"value '%s' (%s) cannot be decoded as Timestamp", val, column.getType()));
}
case DATE:
case TIMESTAMP:
case DATETIME:
if (length == 0) return null;
year = buf.readUnsignedShort();
month = buf.readByte();
dayOfMonth = buf.readByte();
if (length > 4) {
hour = buf.readByte();
minutes = buf.readByte();
seconds = buf.readByte();
if (length > 7) {
microseconds = buf.readUnsignedInt();
}
}
// xpand workaround https://jira.mariadb.org/browse/XPT-274
if (year == 0
&& month == 0
&& dayOfMonth == 0
&& hour == 0
&& minutes == 0
&& seconds == 0
&& microseconds == 0) return null;
break;
case YEAR:
year = buf.readUnsignedShort();
if (column.getLength() <= 2) year += year >= 70 ? 1900 : 2000;
break;
default:
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Timestamp", column.getType()));
}
Timestamp timestamp;
synchronized (cal) {
cal.clear();
cal.set(year, month - 1, (int) dayOfMonth, hour, minutes, seconds);
timestamp = new Timestamp(cal.getTimeInMillis());
}
timestamp.setNanos((int) (microseconds * 1000));
return timestamp;
}
@Override
public void encodeText(
Writer encoder, Context context, Object val, Calendar providedCal, Long maxLen)
throws IOException {
Timestamp ts = (Timestamp) val;
Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(cal.getTimeZone());
String dateString = sdf.format(ts);
encoder.writeByte('\'');
encoder.writeAscii(dateString);
int microseconds = ts.getNanos() / 1000;
if (microseconds > 0) {
if (microseconds % 1000 == 0) {
encoder.writeAscii("." + Integer.toString(microseconds / 1000 + 1000).substring(1));
} else {
encoder.writeAscii("." + Integer.toString(microseconds + 1000000).substring(1));
}
}
encoder.writeByte('\'');
}
@Override
public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength)
throws IOException {
Timestamp ts = (Timestamp) value;
Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal;
synchronized (cal) {
cal.clear();
cal.setTimeInMillis(ts.getTime());
if (ts.getNanos() == 0) {
encoder.writeByte(7); // length
encoder.writeShort((short) cal.get(Calendar.YEAR));
encoder.writeByte((cal.get(Calendar.MONTH) + 1));
encoder.writeByte(cal.get(Calendar.DAY_OF_MONTH));
encoder.writeByte(cal.get(Calendar.HOUR_OF_DAY));
encoder.writeByte(cal.get(Calendar.MINUTE));
encoder.writeByte(cal.get(Calendar.SECOND));
} else {
encoder.writeByte(11); // length
encoder.writeShort((short) cal.get(Calendar.YEAR));
encoder.writeByte((cal.get(Calendar.MONTH) + 1));
encoder.writeByte(cal.get(Calendar.DAY_OF_MONTH));
encoder.writeByte(cal.get(Calendar.HOUR_OF_DAY));
encoder.writeByte(cal.get(Calendar.MINUTE));
encoder.writeByte(cal.get(Calendar.SECOND));
encoder.writeInt(ts.getNanos() / 1000);
}
}
}
public int getBinaryEncodeType() {
return DataType.DATETIME.get();
}
}