-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathDB2Row.java
More file actions
186 lines (167 loc) · 5.31 KB
/
Copy pathDB2Row.java
File metadata and controls
186 lines (167 loc) · 5.31 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
/*
* Copyright (C) 2019,2020 IBM Corporation
*
* 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
*
* http://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.vertx.db2client.impl;
import java.sql.RowId;
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.UUID;
import io.vertx.core.buffer.Buffer;
import io.vertx.db2client.impl.drda.DB2RowId;
import io.vertx.sqlclient.data.Numeric;
import io.vertx.sqlclient.impl.RowBase;
import io.vertx.sqlclient.internal.RowDescriptorBase;
public class DB2Row extends RowBase {
public DB2Row(RowDescriptorBase rowDescriptor) {
super(rowDescriptor);
}
@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 == LocalTime.class) {
return type.cast(getLocalTime(position));
} else if (type == Duration.class) {
return type.cast(getDuration(position));
} else if (type == RowId.class || type == DB2RowId.class) {
return type.cast(getRowId(position));
} else if (type == UUID.class) {
return type.cast(getUUID(position));
} else if (type.isEnum()) {
return type.cast(getEnum(type, position));
} else {
throw new UnsupportedOperationException("Unsupported type " + type.getName());
}
}
@Override
public int getColumnIndex(String name) {
int idx = super.getColumnIndex(name);
if (idx >= 0) {
return idx;
}
// Unless the column is renamed in the SQL query, the column name will be uppercase
return super.getColumnIndex(name.toUpperCase());
}
@Override
public Boolean getBoolean(int pos) {
// DB2 stores booleans as TINYINT
Object val = getValue(pos);
if (val instanceof Boolean) {
return (Boolean) val;
} else if (val instanceof Short) {
return (Short) val != 0;
}
return null;
}
public RowId getRowId(int pos) {
Object val = getValue(pos);
if (val instanceof RowId) {
return (RowId) val;
} else {
return null;
}
}
public RowId getRowId(String name) {
int pos = getColumnIndex(name);
return pos == -1 ? null : getRowId(pos);
}
@Override
public Temporal getTemporal(int pos) {
throw new UnsupportedOperationException();
}
@Override
public OffsetTime getOffsetTime(int pos) {
throw new UnsupportedOperationException();
}
@Override
public OffsetDateTime getOffsetDateTime(int pos) {
throw new UnsupportedOperationException();
}
@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();
}
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 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];
}
}
} else if (val == null) {
return null;
}
throw new ClassCastException();
}
}