Debian Codegraph @

src/main/org/h2/jdbc/JdbcArray.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.jdbc;
7
8 import java.sql.Array;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.Map;
12
13 import org.h2.api.ErrorCode;
14 import org.h2.message.DbException;
15 import org.h2.message.TraceObject;
16 import org.h2.result.SimpleResult;
17 import org.h2.value.DataType;
18 import org.h2.value.TypeInfo;
19 import org.h2.value.Value;
20 import org.h2.value.ValueArray;
21 import org.h2.value.ValueBigint;
22 import org.h2.value.ValueToObjectConverter;
23
24 /**
25 * Represents an ARRAY value.
26 */
27 public final class JdbcArray extends TraceObject implements Array {
28
29 private ValueArray value;
30 private final JdbcConnection conn;
31
32 /**
33 * INTERNAL
34 * @param conn it belongs to
35 * @param value of
36 * @param id of the trace object
37 */
38 public JdbcArray(JdbcConnection conn, Value value, int id) {
39 setTrace(conn.getSession().getTrace(), TraceObject.ARRAY, id);
40 this.conn = conn;
41 this.value = value.convertToAnyArray(conn);
42 }
43
44 /**
45 * Returns the value as a Java array.
46 * This method always returns an Object[].
47 *
48 * @return the Object array
49 */
50 @Override
51 public Object getArray() throws SQLException {
52 try {
53 debugCodeCall("getArray");
54 checkClosed();
55 return get();
56 } catch (Exception e) {
57 throw logAndConvert(e);
58 }
59 }
60
61 /**
62 * Returns the value as a Java array.
63 * This method always returns an Object[].
64 *
65 * @param map is ignored. Only empty or null maps are supported
66 * @return the Object array
67 */
68 @Override
69 public Object getArray(Map<String, Class<?>> map) throws SQLException {
70 try {
71 if (isDebugEnabled()) {
72 debugCode("getArray(" + quoteMap(map) + ')');
73 }
74 JdbcConnection.checkMap(map);
75 checkClosed();
76 return get();
77 } catch (Exception e) {
78 throw logAndConvert(e);
79 }
80 }
81
82 /**
83 * Returns the value as a Java array. A subset of the array is returned,
84 * starting from the index (1 meaning the first element) and up to the given
85 * object count. This method always returns an Object[].
86 *
87 * @param index the start index of the subset (starting with 1)
88 * @param count the maximum number of values
89 * @return the Object array
90 */
91 @Override
92 public Object getArray(long index, int count) throws SQLException {
93 try {
94 if (isDebugEnabled()) {
95 debugCode("getArray(" + index + ", " + count + ')');
96 }
97 checkClosed();
98 return get(index, count);
99 } catch (Exception e) {
100 throw logAndConvert(e);
101 }
102 }
103
104 /**
105 * Returns the value as a Java array. A subset of the array is returned,
106 * starting from the index (1 meaning the first element) and up to the given
107 * object count. This method always returns an Object[].
108 *
109 * @param index the start index of the subset (starting with 1)
110 * @param count the maximum number of values
111 * @param map is ignored. Only empty or null maps are supported
112 * @return the Object array
113 */
114 @Override
115 public Object getArray(long index, int count, Map<String, Class<?>> map)
116 throws SQLException {
117 try {
118 if (isDebugEnabled()) {
119 debugCode("getArray(" + index + ", " + count + ", " + quoteMap(map) + ')');
120 }
121 checkClosed();
122 JdbcConnection.checkMap(map);
123 return get(index, count);
124 } catch (Exception e) {
125 throw logAndConvert(e);
126 }
127 }
128
129 /**
130 * Returns the base type of the array.
131 *
132 * @return the base type or Types.NULL
133 */
134 @Override
135 public int getBaseType() throws SQLException {
136 try {
137 debugCodeCall("getBaseType");
138 checkClosed();
139 return DataType.convertTypeToSQLType(value.getComponentType());
140 } catch (Exception e) {
141 throw logAndConvert(e);
142 }
143 }
144
145 /**
146 * Returns the base type name of the array. This database does support mixed
147 * type arrays and therefore there is no base type.
148 *
149 * @return the base type name or "NULL"
150 */
151 @Override
152 public String getBaseTypeName() throws SQLException {
153 try {
154 debugCodeCall("getBaseTypeName");
155 checkClosed();
156 return value.getComponentType().getDeclaredTypeName();
157 } catch (Exception e) {
158 throw logAndConvert(e);
159 }
160 }
161
162 /**
163 * Returns the value as a result set.
164 * The first column contains the index
165 * (starting with 1) and the second column the value.
166 *
167 * @return the result set
168 */
169 @Override
170 public ResultSet getResultSet() throws SQLException {
171 try {
172 debugCodeCall("getResultSet");
173 checkClosed();
174 return getResultSetImpl(1L, Integer.MAX_VALUE);
175 } catch (Exception e) {
176 throw logAndConvert(e);
177 }
178 }
179
180 /**
181 * Returns the value as a result set. The first column contains the index
182 * (starting with 1) and the second column the value.
183 *
184 * @param map is ignored. Only empty or null maps are supported
185 * @return the result set
186 */
187 @Override
188 public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
189 try {
190 if (isDebugEnabled()) {
191 debugCode("getResultSet(" + quoteMap(map) + ')');
192 }
193 checkClosed();
194 JdbcConnection.checkMap(map);
195 return getResultSetImpl(1L, Integer.MAX_VALUE);
196 } catch (Exception e) {
197 throw logAndConvert(e);
198 }
199 }
200
201 /**
202 * Returns the value as a result set. The first column contains the index
203 * (starting with 1) and the second column the value. A subset of the array
204 * is returned, starting from the index (1 meaning the first element) and
205 * up to the given object count.
206 *
207 * @param index the start index of the subset (starting with 1)
208 * @param count the maximum number of values
209 * @return the result set
210 */
211 @Override
212 public ResultSet getResultSet(long index, int count) throws SQLException {
213 try {
214 if (isDebugEnabled()) {
215 debugCode("getResultSet(" + index + ", " + count + ')');
216 }
217 checkClosed();
218 return getResultSetImpl(index, count);
219 } catch (Exception e) {
220 throw logAndConvert(e);
221 }
222 }
223
224 /**
225 * Returns the value as a result set.
226 * The first column contains the index
227 * (starting with 1) and the second column the value.
228 * A subset of the array is returned, starting from the index
229 * (1 meaning the first element) and up to the given object count.
230 *
231 * @param index the start index of the subset (starting with 1)
232 * @param count the maximum number of values
233 * @param map is ignored. Only empty or null maps are supported
234 * @return the result set
235 */
236 @Override
237 public ResultSet getResultSet(long index, int count,
238 Map<String, Class<?>> map) throws SQLException {
239 try {
240 if (isDebugEnabled()) {
241 debugCode("getResultSet(" + index + ", " + count + ", " + quoteMap(map) + ')');
242 }
243 checkClosed();
244 JdbcConnection.checkMap(map);
245 return getResultSetImpl(index, count);
246 } catch (Exception e) {
247 throw logAndConvert(e);
248 }
249 }
250
251 /**
252 * Release all resources of this object.
253 */
254 @Override
255 public void free() {
256 debugCodeCall("free");
257 value = null;
258 }
259
260 private ResultSet getResultSetImpl(long index, int count) {
261 int id = getNextId(TraceObject.RESULT_SET);
262 SimpleResult rs = new SimpleResult();
263 rs.addColumn("INDEX", TypeInfo.TYPE_BIGINT);
264 rs.addColumn("VALUE", value.getComponentType());
265 Value[] values = value.getList();
266 count = checkRange(index, count, values.length);
267 for (int i = (int) index; i < index + count; i++) {
268 rs.addRow(ValueBigint.get(i), values[i - 1]);
269 }
270 return new JdbcResultSet(conn, null, null, rs, id, true, false, false);
271 }
272
273 private void checkClosed() {
274 conn.checkClosed();
275 if (value == null) {
276 throw DbException.get(ErrorCode.OBJECT_CLOSED);
277 }
278 }
279
280 private Object get() {
281 return ValueToObjectConverter.valueToDefaultArray(value, conn, true);
282 }
283
284 private Object get(long index, int count) {
285 Value[] values = value.getList();
286 count = checkRange(index, count, values.length);
287 Object[] a = new Object[count];
288 for (int i = 0, j = (int) index - 1; i < count; i++, j++) {
289 a[i] = ValueToObjectConverter.valueToDefaultObject(values[j], conn, true);
290 }
291 return a;
292 }
293
294 private static int checkRange(long index, int count, int len) {
295 if (index < 1 || (index != 1 && index > len)) {
296 throw DbException.getInvalidValueException("index (1.." + len + ')', index);
297 }
298 int rem = len - (int) index + 1;
299 if (count < 0) {
300 throw DbException.getInvalidValueException("count (0.." + rem + ')', count);
301 }
302 return Math.min(rem, count);
303 }
304
305 /**
306 * INTERNAL
307 */
308 @Override
309 public String toString() {
310 return value == null ? "null" :
311 (getTraceObjectName() + ": " + value.getTraceSQL());
312 }
313 }