-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathMySQLRowImpl.java
More file actions
276 lines (245 loc) · 8.25 KB
/
MySQLRowImpl.java
File metadata and controls
276 lines (245 loc) · 8.25 KB
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
/*
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.mysqlclient.impl;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.mysqlclient.data.spatial.*;
import io.vertx.mysqlclient.impl.protocol.ColumnDefinition;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.data.Numeric;
import io.vertx.sqlclient.impl.ArrayTuple;
import io.vertx.core.buffer.Buffer;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.temporal.Temporal;
import java.util.List;
import java.util.UUID;
public class MySQLRowImpl extends ArrayTuple implements Row {
private final MySQLRowDesc rowDesc;
public MySQLRowImpl(MySQLRowDesc rowDesc) {
super(rowDesc.columnNames().size());
this.rowDesc = rowDesc;
}
@Override
public <T> T get(Class<T> type, int position) {
if (type == Boolean.class) {
return type.cast(getBoolean(position));
} else if (type == Byte.class) {
return type.cast(getByte(position));
} else if (type == Short.class) {
return type.cast(getShort(position));
} else if (type == Integer.class) {
return type.cast(getInteger(position));
} else if (type == Long.class) {
return type.cast(getLong(position));
} else if (type == Float.class) {
return type.cast(getFloat(position));
} else if (type == Double.class) {
return type.cast(getDouble(position));
} else if (type == Numeric.class) {
return type.cast(getNumeric(position));
} else if (type == String.class) {
return type.cast(getString(position));
} else if (type == Buffer.class) {
return type.cast(getBuffer(position));
} else if (type == LocalDate.class) {
return type.cast(getLocalDate(position));
} else if (type == LocalDateTime.class) {
return type.cast(getLocalDateTime(position));
} else if (type == Duration.class) {
return type.cast(getDuration(position));
} else if (type == JsonObject.class) {
return type.cast(getJsonObject(position));
} else if (type == JsonArray.class) {
return type.cast(getJsonArray(position));
} else if (type == Geometry.class) {
return type.cast(getGeometry(position));
} else if (type == Point.class) {
return type.cast(getPoint(position));
} else if (type == LineString.class) {
return type.cast(getLineString(position));
} else if (type == Polygon.class) {
return type.cast(getPolygon(position));
} else if (type == MultiPoint.class) {
return type.cast(getMultiPoint(position));
} else if (type == MultiLineString.class) {
return type.cast(getMultiLineString(position));
} else if (type == MultiPolygon.class) {
return type.cast(getMultiPolygon(position));
} else if (type == GeometryCollection.class) {
return type.cast(getGeometryCollection(position));
} else if (type.isEnum()) {
return type.cast(getEnum(type, position));
} else {
throw new UnsupportedOperationException("Unsupported type " + type.getName());
}
}
@Override
public String getColumnName(int pos) {
List<String> columnNames = rowDesc.columnNames();
return pos < 0 || columnNames.size() - 1 < pos ? null : columnNames.get(pos);
}
@Override
public int getColumnIndex(String name) {
if (name == null) {
throw new NullPointerException();
}
return rowDesc.columnNames().indexOf(name);
}
@Override
public Temporal getTemporal(int pos) {
throw new UnsupportedOperationException(buildIllegalAccessMessage(getValue(pos), getColumnName(pos), Temporal.class));
}
@Override
public OffsetTime getOffsetTime(int pos) {
throw new UnsupportedOperationException(buildIllegalAccessMessage(getValue(pos), getColumnName(pos), OffsetTime.class));
}
@Override
public OffsetDateTime getOffsetDateTime(int pos) {
throw new UnsupportedOperationException(buildIllegalAccessMessage(getValue(pos), getColumnName(pos), OffsetDateTime.class));
}
@Override
public UUID getUUID(int pos) {
throw new UnsupportedOperationException(buildIllegalAccessMessage(getValue(pos), getColumnName(pos), UUID.class));
}
@Override
public LocalDateTime[] getArrayOfLocalDateTimes(int pos) {
throw new UnsupportedOperationException();
}
public OffsetDateTime[] getArrayOfOffsetDateTimes(int pos) {
throw new UnsupportedOperationException();
}
@Override
public Buffer[] getArrayOfBuffers(String column) {
throw new UnsupportedOperationException();
}
@Override
public UUID[] getArrayOfUUIDs(String column) {
throw new UnsupportedOperationException();
}
@Override
public Boolean getBoolean(int pos) {
// in MySQL BOOLEAN type is mapped to TINYINT
Object val = getValue(pos);
if (val instanceof Boolean) {
return (Boolean) val;
} else if (val instanceof Number) {
return ((Number) val).byteValue() != 0;
}
return null;
}
private Byte getByte(int pos) {
Object val = getValue(pos);
if (val instanceof Byte) {
return (Byte) val;
} else if (val instanceof Number) {
return ((Number) val).byteValue();
}
return null;
}
private Duration getDuration(int pos) {
Object val = getValue(pos);
if (val instanceof Duration) {
return (Duration) val;
}
return null;
}
private Geometry getGeometry(int pos) {
Object val = getValue(pos);
if (val instanceof Geometry) {
return (Geometry) val;
}
return null;
}
private Point getPoint(int pos) {
Object val = getValue(pos);
if (val instanceof Point) {
return (Point) val;
}
return null;
}
private LineString getLineString(int pos) {
Object val = getValue(pos);
if (val instanceof LineString) {
return (LineString) val;
}
return null;
}
private Polygon getPolygon(int pos) {
Object val = getValue(pos);
if (val instanceof Polygon) {
return (Polygon) val;
}
return null;
}
private MultiPoint getMultiPoint(int pos) {
Object val = getValue(pos);
if (val instanceof MultiPoint) {
return (MultiPoint) val;
}
return null;
}
private MultiLineString getMultiLineString(int pos) {
Object val = getValue(pos);
if (val instanceof MultiLineString) {
return (MultiLineString) val;
}
return null;
}
private MultiPolygon getMultiPolygon(int pos) {
Object val = getValue(pos);
if (val instanceof MultiPolygon) {
return (MultiPolygon) val;
}
return null;
}
private GeometryCollection getGeometryCollection(int pos) {
Object val = getValue(pos);
if (val instanceof GeometryCollection) {
return (GeometryCollection) val;
}
return null;
}
@Override
public LocalTime getLocalTime(int pos) {
ColumnDefinition columnDefinition = rowDesc.columnDefinitions()[pos];
Object val = getValue(pos);
if (columnDefinition.type() == ColumnDefinition.ColumnType.MYSQL_TYPE_TIME && val instanceof Duration) {
// map MySQL TIME data type to java.time.LocalTime
Duration duration = (Duration) val;
return LocalTime.ofNanoOfDay(duration.toNanos());
} else {
return super.getLocalTime(pos);
}
}
private Object getEnum(Class enumType, int position) {
Object val = getValue(position);
if (val instanceof String) {
return Enum.valueOf(enumType, (String) val);
} else if (val instanceof Number) {
int ordinal = ((Number) val).intValue();
if (ordinal >= 0) {
Object[] constants = enumType.getEnumConstants();
if (ordinal < constants.length) {
return constants[ordinal];
}
}
}
return null;
}
private <T> String buildIllegalAccessMessage(Object value, String columnName, Class<T> clazz) {
return String.format("Can not retrieve row value[%s] as class[%s], columnName=[%s]", value.toString(), clazz.getName(), columnName);
}
}