-
Notifications
You must be signed in to change notification settings - Fork 219
/
DateCodec.java
231 lines (201 loc) · 7.08 KB
/
DateCodec.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
// 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.util.Calendar;
import java.util.EnumSet;
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;
/** Date codec */
public class DateCodec implements Codec<Date> {
/** default instance */
public static final DateCodec INSTANCE = new DateCodec();
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.BLOB,
DataType.TINYBLOB,
DataType.MEDIUMBLOB,
DataType.LONGBLOB);
public String className() {
return Date.class.getName();
}
public boolean canDecode(Column column, Class<?> type) {
return COMPATIBLE_TYPES.contains(column.getType()) && type.isAssignableFrom(Date.class);
}
public boolean canEncode(Object value) {
return value instanceof Date || java.util.Date.class.equals(value.getClass());
}
@Override
@SuppressWarnings("fallthrough")
public Date decodeText(ReadableByteBuf buf, int length, Column column, Calendar cal)
throws SQLDataException {
switch (column.getType()) {
case YEAR:
short y = (short) buf.atoi(length);
if (column.getLength() == 2) {
// YEAR(2) - deprecated
if (y <= 69) {
y += 2000;
} else {
y += 1900;
}
}
return Date.valueOf(y + "-01-01");
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 Date", column.getType()));
}
// expected fallthrough
// BLOB is considered as String if it has a collation (this is TEXT column)
case VARCHAR:
case VARSTRING:
case STRING:
case DATE:
String val = buf.readString(length);
if ("0000-00-00".equals(val)) return null;
String[] stDatePart = val.split("[- ]");
if (stDatePart.length < 3) {
throw new SQLDataException(
String.format("value '%s' (%s) cannot be decoded as Date", val, column.getType()));
}
return getDate(column, cal, val, stDatePart);
case TIMESTAMP:
case DATETIME:
Timestamp lt = TimestampCodec.INSTANCE.decodeText(buf, length, column, cal);
String st = lt.toString();
return Date.valueOf(st.substring(0, 10));
default:
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Date", column.getType()));
}
}
private Date getDate(Column column, Calendar cal, String val, String[] stDatePart)
throws SQLDataException {
try {
int year = Integer.parseInt(stDatePart[0]);
int month = Integer.parseInt(stDatePart[1]);
int dayOfMonth = Integer.parseInt(stDatePart[2]);
Calendar c = cal == null ? Calendar.getInstance() : cal;
synchronized (c) {
c.clear();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
return new Date(c.getTimeInMillis());
}
} catch (NumberFormatException nfe) {
throw new SQLDataException(
String.format("value '%s' (%s) cannot be decoded as Date", val, column.getType()));
}
}
@Override
@SuppressWarnings("fallthrough")
public Date decodeBinary(ReadableByteBuf buf, int length, Column column, Calendar cal)
throws SQLDataException {
switch (column.getType()) {
case YEAR:
int v = buf.readShort();
if (column.getLength() == 2) {
// YEAR(2) - deprecated
if (v <= 69) {
v += 2000;
} else {
v += 1900;
}
}
return Date.valueOf(v + "-01-01");
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 Date", column.getType()));
}
// expected fallthrough
// BLOB is considered as String if it has a collation (this is TEXT column)
case VARCHAR:
case VARSTRING:
case STRING:
String val = buf.readString(length);
String[] stDatePart = val.split("[- ]");
if (stDatePart.length < 3) {
throw new SQLDataException(
String.format("value '%s' (%s) cannot be decoded as Date", val, column.getType()));
}
return getDate(column, cal, val, stDatePart);
case DATE:
if (length == 0) return null;
Calendar c = cal == null ? Calendar.getInstance() : cal;
synchronized (c) {
c.clear();
c.set(Calendar.YEAR, buf.readShort());
c.set(Calendar.MONTH, buf.readByte() - 1);
c.set(Calendar.DAY_OF_MONTH, buf.readByte());
return new Date(c.getTimeInMillis());
}
case TIMESTAMP:
case DATETIME:
Timestamp lt = TimestampCodec.INSTANCE.decodeBinary(buf, length, column, cal);
if (lt == null) return null;
String st = lt.toString();
return Date.valueOf(st.substring(0, 10));
default:
buf.skip(length);
throw new SQLDataException(
String.format("Data type %s cannot be decoded as Date", column.getType()));
}
}
@Override
public void encodeText(
Writer encoder, Context context, Object val, Calendar providedCal, Long maxLen)
throws IOException {
Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(cal.getTimeZone());
String dateString = sdf.format(val);
encoder.writeByte('\'');
encoder.writeAscii(dateString);
encoder.writeByte('\'');
}
@Override
public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength)
throws IOException {
Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal;
synchronized (cal) {
cal.clear();
cal.setTimeInMillis(((java.util.Date) value).getTime());
encoder.writeByte(4); // length
encoder.writeShort((short) cal.get(Calendar.YEAR));
encoder.writeByte(((cal.get(Calendar.MONTH) + 1) & 0xff));
encoder.writeByte((cal.get(Calendar.DAY_OF_MONTH) & 0xff));
}
}
public int getBinaryEncodeType() {
return DataType.DATE.get();
}
}