File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcArray.java.html

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