History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/javax/sql/rowset/serial/SerialArray.java (1 of 18352 files). Show all

Unified · Split

java/pkg/javax/sql/rowset/serial/SerialArray.java+535−0
@@ -0,0 +1,535 @@
1+/*
2+ * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package javax.sql.rowset.serial;
27+
28+import java.sql.*;
29+import java.io.*;
30+import java.util.Map;
31+import java.net.URL;
32+
33+
34+/**
35+ * A serialized version of an <code>Array</code>
36+ * object, which is the mapping in the Java programming language of an SQL
37+ * <code>ARRAY</code> value.
38+ * <P>
39+ * The <code>SerialArray</code> class provides a constructor for creating
40+ * a <code>SerialArray</code> instance from an <code>Array</code> object,
41+ * methods for getting the base type and the SQL name for the base type, and
42+ * methods for copying all or part of a <code>SerialArray</code> object.
43+ * <P>
44+ * Note: In order for this class to function correctly, a connection to the
45+ * data source
46+ * must be available in order for the SQL <code>Array</code> object to be
47+ * materialized (have all of its elements brought to the client server)
48+ * if necessary. At this time, logical pointers to the data in the data source,
49+ * such as locators, are not currently supported.
50+ */
51+public class SerialArray implements Array, Serializable, Cloneable {
52+
53+ /**
54+ * A serialized array in which each element is an <code>Object</code>
55+ * in the Java programming language that represents an element
56+ * in the SQL <code>ARRAY</code> value.
57+ * @serial
58+ */
59+ private Object[] elements;
60+
61+ /**
62+ * The SQL type of the elements in this <code>SerialArray</code> object. The
63+ * type is expressed as one of the constants from the class
64+ * <code>java.sql.Types</code>.
65+ * @serial
66+ */
67+ private int baseType;
68+
69+ /**
70+ * The type name used by the DBMS for the elements in the SQL <code>ARRAY</code>
71+ * value that this <code>SerialArray</code> object represents.
72+ * @serial
73+ */
74+ private String baseTypeName;
75+
76+ /**
77+ * The number of elements in this <code>SerialArray</code> object, which
78+ * is also the number of elements in the SQL <code>ARRAY</code> value
79+ * that this <code>SerialArray</code> object represents.
80+ * @serial
81+ */
82+ private int len;
83+
84+ /**
85+ * Constructs a new <code>SerialArray</code> object from the given
86+ * <code>Array</code> object, using the given type map for the custom
87+ * mapping of each element when the elements are SQL UDTs.
88+ * <P>
89+ * This method does custom mapping if the array elements are a UDT
90+ * and the given type map has an entry for that UDT.
91+ * Custom mapping is recursive,
92+ * meaning that if, for instance, an element of an SQL structured type
93+ * is an SQL structured type that itself has an element that is an SQL
94+ * structured type, each structured type that has a custom mapping will be
95+ * mapped according to the given type map.
96+ * <P>
97+ * The new <code>SerialArray</code>
98+ * object contains the same elements as the <code>Array</code> object
99+ * from which it is built, except when the base type is the SQL type
100+ * <code>STRUCT</code>, <code>ARRAY</code>, <code>BLOB</code>,
101+ * <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.
102+ * In this case, each element in the new
103+ * <code>SerialArray</code> object is the appropriate serialized form,
104+ * that is, a <code>SerialStruct</code>, <code>SerialArray</code>,
105+ * <code>SerialBlob</code>, <code>SerialClob</code>,
106+ * <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.
107+ * <P>
108+ * Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>
109+ * object is created must have materialized the SQL <code>ARRAY</code> value's
110+ * data on the client before it is passed to the constructor. Otherwise,
111+ * the new <code>SerialArray</code> object will contain no data.
112+ * <p>
113+ * Note: (2) If the <code>Array</code> contains <code>java.sql.Types.JAVA_OBJECT</code>
114+ * types, the <code>SerialJavaObject</code> constructor is called where checks
115+ * are made to ensure this object is serializable.
116+ * <p>
117+ * Note: (3) The <code>Array</code> object supplied to this constructor cannot
118+ * return <code>null</code> for any <code>Array.getArray()</code> methods.
119+ * <code>SerialArray</code> cannot serialize null array values.
120+ *
121+ *
122+ * @param array the <code>Array</code> object to be serialized
123+ * @param map a <code>java.util.Map</code> object in which
124+ * each entry consists of 1) a <code>String</code> object
125+ * giving the fully qualified name of a UDT (an SQL structured type or
126+ * distinct type) and 2) the
127+ * <code>Class</code> object for the <code>SQLData</code> implementation
128+ * that defines how the UDT is to be mapped. The <i>map</i>
129+ * parameter does not have any effect for <code>Blob</code>,
130+ * <code>Clob</code>, <code>DATALINK</code>, or
131+ * <code>JAVA_OBJECT</code> types.
132+ * @throws SerialException if an error occurs serializing the
133+ * <code>Array</code> object
134+ * @throws SQLException if a database access error occurs or if the
135+ * <i>array</i> or the <i>map</i> values are <code>null</code>
136+ */
137+ public SerialArray(Array array, Map<String,Class<?>> map)
138+ throws SerialException, SQLException
139+ {
140+
141+ if ((array == null) || (map == null)) {
142+ throw new SQLException("Cannot instantiate a SerialArray " +
143+ "object with null parameters");
144+ }
145+
146+ if ((elements = (Object[])array.getArray()) == null) {
147+ throw new SQLException("Invalid Array object. Calls to Array.getArray() " +
148+ "return null value which cannot be serialized");
149+ }
150+
151+ elements = (Object[])array.getArray(map);
152+ baseType = array.getBaseType();
153+ baseTypeName = array.getBaseTypeName();
154+ len = elements.length;
155+
156+ switch (baseType) {
157+ case java.sql.Types.STRUCT:
158+ for (int i = 0; i < len; i++) {
159+ elements[i] = new SerialStruct((Struct)elements[i], map);
160+ }
161+ break;
162+
163+ case java.sql.Types.ARRAY:
164+ for (int i = 0; i < len; i++) {
165+ elements[i] = new SerialArray((Array)elements[i], map);
166+ }
167+ break;
168+
169+ case java.sql.Types.BLOB:
170+ for (int i = 0; i < len; i++) {
171+ elements[i] = new SerialBlob((Blob)elements[i]);
172+ }
173+ break;
174+
175+ case java.sql.Types.CLOB:
176+ for (int i = 0; i < len; i++) {
177+ elements[i] = new SerialClob((Clob)elements[i]);
178+ }
179+ break;
180+
181+ case java.sql.Types.DATALINK:
182+ for (int i = 0; i < len; i++) {
183+ elements[i] = new SerialDatalink((URL)elements[i]);
184+ }
185+ break;
186+
187+ case java.sql.Types.JAVA_OBJECT:
188+ for (int i = 0; i < len; i++) {
189+ elements[i] = new SerialJavaObject((Object)elements[i]);
190+ }
191+ default:
192+ ;
193+ }
194+ }
195+
196+ /**
197+ * This method frees the <code>Array</code> object and releases the resources that
198+ * it holds. The object is invalid once the <code>free</code>
199+ * method is called.
200+ *<p>
201+ * After <code>free</code> has been called, any attempt to invoke a
202+ * method other than <code>free</code> will result in a <code>SQLException</code>
203+ * being thrown. If <code>free</code> is called multiple times, the subsequent
204+ * calls to <code>free</code> are treated as a no-op.
205+ *<p>
206+ *
207+ * @throws SQLException if an error occurs releasing
208+ * the Array's resources
209+ * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
210+ * this method
211+ * @since 1.6
212+ */
213+ public void free() throws SQLException {
214+ throw new SQLFeatureNotSupportedException("Feature not supported");
215+ }
216+
217+ /**
218+ * Constructs a new <code>SerialArray</code> object from the given
219+ * <code>Array</code> object.
220+ * <P>
221+ * This constructor does not do custom mapping. If the base type of the array
222+ * is an SQL structured type and custom mapping is desired, the constructor
223+ * <code>SerialArray(Array array, Map map)</code> should be used.
224+ * <P>
225+ * The new <code>SerialArray</code>
226+ * object contains the same elements as the <code>Array</code> object
227+ * from which it is built, except when the base type is the SQL type
228+ * <code>BLOB</code>,
229+ * <code>CLOB</code>, <code>DATALINK</code> or <code>JAVA_OBJECT</code>.
230+ * In this case, each element in the new
231+ * <code>SerialArray</code> object is the appropriate serialized form,
232+ * that is, a <code>SerialBlob</code>, <code>SerialClob</code>,
233+ * <code>SerialDatalink</code>, or <code>SerialJavaObject</code> object.
234+ * <P>
235+ * Note: (1) The <code>Array</code> object from which a <code>SerialArray</code>
236+ * object is created must have materialized the SQL <code>ARRAY</code> value's
237+ * data on the client before it is passed to the constructor. Otherwise,
238+ * the new <code>SerialArray</code> object will contain no data.
239+ * <p>
240+ * Note: (2) The <code>Array</code> object supplied to this constructor cannot
241+ * return <code>null</code> for any <code>Array.getArray()</code> methods.
242+ * <code>SerialArray</code> cannot serialize <code>null</code> array values.
243+ *
244+ * @param array the <code>Array</code> object to be serialized
245+ * @throws SerialException if an error occurs serializing the
246+ * <code>Array</code> object
247+ * @throws SQLException if a database access error occurs or the
248+ * <i>array</i> parameter is <code>null</code>.
249+ */
250+ public SerialArray(Array array) throws SerialException, SQLException {
251+ if (array == null) {
252+ throw new SQLException("Cannot instantiate a SerialArray " +
253+ "object with a null Array object");
254+ }
255+
256+ if ((elements = (Object[])array.getArray()) == null) {
257+ throw new SQLException("Invalid Array object. Calls to Array.getArray() " +
258+ "return null value which cannot be serialized");
259+ }
260+
261+ //elements = (Object[])array.getArray();
262+ baseType = array.getBaseType();
263+ baseTypeName = array.getBaseTypeName();
264+ len = elements.length;
265+
266+ switch (baseType) {
267+
268+ case java.sql.Types.BLOB:
269+ for (int i = 0; i < len; i++) {
270+ elements[i] = new SerialBlob((Blob)elements[i]);
271+ }
272+ break;
273+
274+ case java.sql.Types.CLOB:
275+ for (int i = 0; i < len; i++) {
276+ elements[i] = new SerialClob((Clob)elements[i]);
277+ }
278+ break;
279+
280+ case java.sql.Types.DATALINK:
281+ for (int i = 0; i < len; i++) {
282+ elements[i] = new SerialDatalink((URL)elements[i]);
283+ }
284+ break;
285+
286+ case java.sql.Types.JAVA_OBJECT:
287+ for (int i = 0; i < len; i++) {
288+ elements[i] = new SerialJavaObject((Object)elements[i]);
289+ }
290+
291+ default:
292+ ;
293+ }
294+
295+
296+ }
297+
298+ /**
299+ * Returns a new array that is a copy of this <code>SerialArray</code>
300+ * object.
301+ *
302+ * @return a copy of this <code>SerialArray</code> object as an
303+ * <code>Object</code> in the Java programming language
304+ * @throws SerialException if an error occurs retrieving a copy of
305+ * this <code>SerialArray</code> object
306+ */
307+ public Object getArray() throws SerialException {
308+ Object dst = new Object[len];
309+ System.arraycopy((Object)elements, 0, dst, 0, len);
310+ return dst;
311+ }
312+
313+ //[if an error occurstype map used??]
314+ /**
315+ * Returns a new array that is a copy of this <code>SerialArray</code>
316+ * object, using the given type map for the custom
317+ * mapping of each element when the elements are SQL UDTs.
318+ * <P>
319+ * This method does custom mapping if the array elements are a UDT
320+ * and the given type map has an entry for that UDT.
321+ * Custom mapping is recursive,
322+ * meaning that if, for instance, an element of an SQL structured type
323+ * is an SQL structured type that itself has an element that is an SQL
324+ * structured type, each structured type that has a custom mapping will be
325+ * mapped according to the given type map.
326+ *
327+ * @param map a <code>java.util.Map</code> object in which
328+ * each entry consists of 1) a <code>String</code> object
329+ * giving the fully qualified name of a UDT and 2) the
330+ * <code>Class</code> object for the <code>SQLData</code> implementation
331+ * that defines how the UDT is to be mapped
332+ * @return a copy of this <code>SerialArray</code> object as an
333+ * <code>Object</code> in the Java programming language
334+ * @throws SerialException if an error occurs
335+ */
336+ public Object getArray(Map<String, Class<?>> map) throws SerialException {
337+ Object dst[] = new Object[len];
338+ System.arraycopy((Object)elements, 0, dst, 0, len);
339+ return dst;
340+ }
341+
342+ /**
343+ * Returns a new array that is a copy of a slice
344+ * of this <code>SerialArray</code> object, starting with the
345+ * element at the given index and containing the given number
346+ * of consecutive elements.
347+ *
348+ * @param index the index into this <code>SerialArray</code> object
349+ * of the first element to be copied;
350+ * the index of the first element is <code>0</code>
351+ * @param count the number of consecutive elements to be copied, starting
352+ * at the given index
353+ * @return a copy of the designated elements in this <code>SerialArray</code>
354+ * object as an <code>Object</code> in the Java programming language
355+ * @throws SerialException if an error occurs
356+ */
357+ public Object getArray(long index, int count) throws SerialException {
358+ Object dst = new Object[count];
359+ System.arraycopy((Object)elements, (int)index, dst, 0, count);
360+ return dst;
361+ }
362+
363+ /**
364+ * Returns a new array that is a copy of a slice
365+ * of this <code>SerialArray</code> object, starting with the
366+ * element at the given index and containing the given number
367+ * of consecutive elements.
368+ * <P>
369+ * This method does custom mapping if the array elements are a UDT
370+ * and the given type map has an entry for that UDT.
371+ * Custom mapping is recursive,
372+ * meaning that if, for instance, an element of an SQL structured type
373+ * is an SQL structured type that itself has an element that is an SQL
374+ * structured type, each structured type that has a custom mapping will be
375+ * mapped according to the given type map.
376+ *
377+ * @param index the index into this <code>SerialArray</code> object
378+ * of the first element to be copied; the index of the
379+ * first element in the array is <code>0</code>
380+ * @param count the number of consecutive elements to be copied, starting
381+ * at the given index
382+ * @param map a <code>java.util.Map</code> object in which
383+ * each entry consists of 1) a <code>String</code> object
384+ * giving the fully qualified name of a UDT and 2) the
385+ * <code>Class</code> object for the <code>SQLData</code> implementation
386+ * that defines how the UDT is to be mapped
387+ * @return a copy of the designated elements in this <code>SerialArray</code>
388+ * object as an <code>Object</code> in the Java programming language
389+ * @throws SerialException if an error occurs
390+ */
391+ public Object getArray(long index, int count, Map<String,Class<?>> map)
392+ throws SerialException
393+ {
394+ Object dst = new Object[count];
395+ System.arraycopy((Object)elements, (int)index, dst, 0, count);
396+ return dst;
397+ }
398+
399+ /**
400+ * Retrieves the SQL type of the elements in this <code>SerialArray</code>
401+ * object. The <code>int</code> returned is one of the constants in the class
402+ * <code>java.sql.Types</code>.
403+ *
404+ * @return one of the constants in <code>java.sql.Types</code>, indicating
405+ * the SQL type of the elements in this <code>SerialArray</code> object
406+ * @throws SerialException if an error occurs
407+ */
408+ public int getBaseType() throws SerialException {
409+ return baseType;
410+ }
411+
412+ /**
413+ * Retrieves the DBMS-specific type name for the elements in this
414+ * <code>SerialArray</code> object.
415+ *
416+ * @return the SQL type name used by the DBMS for the base type of this
417+ * <code>SerialArray</code> object
418+ * @throws SerialException if an error occurs
419+ */
420+ public String getBaseTypeName() throws SerialException {
421+ return baseTypeName;
422+ }
423+
424+ /**
425+ * Retrieves a <code>ResultSet</code> object holding the elements of
426+ * the subarray that starts at
427+ * index <i>index</i> and contains up to <i>count</i> successive elements.
428+ * This method uses the connection's type map to map the elements of
429+ * the array if the map contains
430+ * an entry for the base type. Otherwise, the standard mapping is used.
431+ *
432+ * @param index the index into this <code>SerialArray</code> object
433+ * of the first element to be copied; the index of the
434+ * first element in the array is <code>0</code>
435+ * @param count the number of consecutive elements to be copied, starting
436+ * at the given index
437+ * @return a <code>ResultSet</code> object containing the designated
438+ * elements in this <code>SerialArray</code> object, with a
439+ * separate row for each element
440+ * @throws SerialException, which in turn throws an
441+ * <code>UnsupportedOperationException</code>, if this method is called
442+ */
443+ public ResultSet getResultSet(long index, int count) throws SerialException {
444+ throw new UnsupportedOperationException();
445+ }
446+
447+ /**
448+ *
449+ * Retrieves a <code>ResultSet</code> object that contains all of
450+ * the elements of the SQL <code>ARRAY</code>
451+ * value represented by this <code>SerialArray</code> object. This method uses
452+ * the specified map for type map customizations unless the base type of the
453+ * array does not match a user-defined type (UDT) in <i>map</i>, in
454+ * which case it uses the
455+ * standard mapping. This version of the method <code>getResultSet</code>
456+ * uses either the given type map or the standard mapping; it never uses the
457+ * type map associated with the connection.
458+ *
459+ * @param map a <code>java.util.Map</code> object in which
460+ * each entry consists of 1) a <code>String</code> object
461+ * giving the fully qualified name of a UDT and 2) the
462+ * <code>Class</code> object for the <code>SQLData</code> implementation
463+ * that defines how the UDT is to be mapped
464+ * @return a <code>ResultSet</code> object containing all of the
465+ * elements in this <code>SerialArray</code> object, with a
466+ * separate row for each element
467+ * @throws SerialException, which in turn throws an
468+ * <code>UnsupportedOperationException</code>, if this method is called
469+ */
470+ public ResultSet getResultSet(Map<String, Class<?>> map)
471+ throws SerialException
472+ {
473+ throw new UnsupportedOperationException();
474+ }
475+
476+ /**
477+ * Retrieves a <code>ResultSet</code> object that contains all of
478+ * the elements in the <code>ARRAY</code> value that this
479+ * <code>SerialArray</code> object represents.
480+ * If appropriate, the elements of the array are mapped using the connection's
481+ * type map; otherwise, the standard mapping is used.
482+ *
483+ * @return a <code>ResultSet</code> object containing all of the
484+ * elements in this <code>SerialArray</code> object, with a
485+ * separate row for each element
486+ * @throws SerialException if called, which in turn throws an
487+ * <code>UnsupportedOperationException</code>, if this method is called
488+ */
489+ public ResultSet getResultSet() throws SerialException {
490+ throw new UnsupportedOperationException();
491+ }
492+
493+
494+ /**
495+ * Retrieves a result set holding the elements of the subarray that starts at
496+ * Retrieves a <code>ResultSet</code> object that contains a subarray of the
497+ * elements in this <code>SerialArray</code> object, starting at
498+ * index <i>index</i> and containing up to <i>count</i> successive
499+ * elements. This method uses
500+ * the specified map for type map customizations unless the base type of the
501+ * array does not match a user-defined type (UDT) in <i>map</i>, in
502+ * which case it uses the
503+ * standard mapping. This version of the method <code>getResultSet</code> uses
504+ * either the given type map or the standard mapping; it never uses the type
505+ * map associated with the connection.
506+ *
507+ * @param index the index into this <code>SerialArray</code> object
508+ * of the first element to be copied; the index of the
509+ * first element in the array is <code>0</code>
510+ * @param count the number of consecutive elements to be copied, starting
511+ * at the given index
512+ * @param map a <code>java.util.Map</code> object in which
513+ * each entry consists of 1) a <code>String</code> object
514+ * giving the fully qualified name of a UDT and 2) the
515+ * <code>Class</code> object for the <code>SQLData</code> implementation
516+ * that defines how the UDT is to be mapped
517+ * @return a <code>ResultSet</code> object containing the designated
518+ * elements in this <code>SerialArray</code> object, with a
519+ * separate row for each element
520+ * @throws SerialException if called, which in turn throws an
521+ * <code>UnsupportedOperationException</code>
522+ */
523+ public ResultSet getResultSet(long index, int count,
524+ Map<String,Class<?>> map)
525+ throws SerialException
526+ {
527+ throw new UnsupportedOperationException();
528+ }
529+
530+ /**
531+ * The identifier that assists in the serialization of this <code>SerialArray</code>
532+ * object.
533+ */
534+ static final long serialVersionUID = -8466174297270688520L;
535+}