author | lancea |
Thu, 26 Jan 2012 19:41:35 -0500 | |
changeset 11683 | 5e02efd89af6 |
parent 11129 | f9ad1aadf3fa |
child 14409 | d879c92507ec |
permissions | -rw-r--r-- |
2 | 1 |
/* |
11683
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.sql.rowset.serial; |
|
27 |
||
28 |
import java.sql.*; |
|
29 |
import javax.sql.*; |
|
30 |
import java.io.*; |
|
31 |
import java.math.*; |
|
11683
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
32 |
import java.util.Arrays; |
2 | 33 |
import java.util.Map; |
34 |
import java.util.Vector; |
|
35 |
||
36 |
import javax.sql.rowset.*; |
|
37 |
||
38 |
/** |
|
39 |
* A serialized mapping in the Java programming language of an SQL |
|
40 |
* structured type. Each attribute that is not already serialized |
|
41 |
* is mapped to a serialized form, and if an attribute is itself |
|
42 |
* a structured type, each of its attributes that is not already |
|
43 |
* serialized is mapped to a serialized form. |
|
44 |
* <P> |
|
45 |
* In addition, the structured type is custom mapped to a class in the |
|
46 |
* Java programming language if there is such a mapping, as are |
|
47 |
* its attributes, if appropriate. |
|
48 |
* <P> |
|
49 |
* The <code>SerialStruct</code> class provides a constructor for creating |
|
50 |
* an instance from a <code>Struct</code> object, a method for retrieving |
|
51 |
* the SQL type name of the SQL structured type in the database, and methods |
|
52 |
* for retrieving its attribute values. |
|
53 |
*/ |
|
54 |
public class SerialStruct implements Struct, Serializable, Cloneable { |
|
55 |
||
56 |
||
57 |
/** |
|
58 |
* The SQL type name for the structured type that this |
|
59 |
* <code>SerialStruct</code> object represents. This is the name |
|
60 |
* used in the SQL definition of the SQL structured type. |
|
61 |
* |
|
62 |
* @serial |
|
63 |
*/ |
|
64 |
private String SQLTypeName; |
|
65 |
||
66 |
/** |
|
67 |
* An array of <code>Object</code> instances in which each |
|
68 |
* element is an attribute of the SQL structured type that this |
|
69 |
* <code>SerialStruct</code> object represents. The attributes are |
|
70 |
* ordered according to their order in the definition of the |
|
71 |
* SQL structured type. |
|
72 |
* |
|
73 |
* @serial |
|
74 |
*/ |
|
75 |
private Object attribs[]; |
|
76 |
||
77 |
/** |
|
78 |
* Constructs a <code>SerialStruct</code> object from the given |
|
79 |
* <code>Struct</code> object, using the given <code>java.util.Map</code> |
|
80 |
* object for custom mapping the SQL structured type or any of its |
|
81 |
* attributes that are SQL structured types. |
|
82 |
* |
|
83 |
* @param map a <code>java.util.Map</code> object in which |
|
84 |
* each entry consists of 1) a <code>String</code> object |
|
85 |
* giving the fully qualified name of a UDT and 2) the |
|
86 |
* <code>Class</code> object for the <code>SQLData</code> implementation |
|
87 |
* that defines how the UDT is to be mapped |
|
88 |
* @throws SerialException if an error occurs |
|
89 |
* @see java.sql.Struct |
|
90 |
*/ |
|
91 |
public SerialStruct(Struct in, Map<String,Class<?>> map) |
|
92 |
throws SerialException |
|
93 |
{ |
|
94 |
||
95 |
try { |
|
96 |
||
97 |
// get the type name |
|
6692
aef4e294026f
6988993: Address Findbugs warnings for the use of String Constructor
lancea
parents:
5506
diff
changeset
|
98 |
SQLTypeName = in.getSQLTypeName(); |
2 | 99 |
System.out.println("SQLTypeName: " + SQLTypeName); |
100 |
||
101 |
// get the attributes of the struct |
|
102 |
attribs = in.getAttributes(map); |
|
103 |
||
104 |
/* |
|
105 |
* the array may contain further Structs |
|
106 |
* and/or classes that have been mapped, |
|
107 |
* other types that we have to serialize |
|
108 |
*/ |
|
109 |
mapToSerial(map); |
|
110 |
||
111 |
} catch (SQLException e) { |
|
112 |
throw new SerialException(e.getMessage()); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Constructs a <code>SerialStruct</code> object from the |
|
118 |
* given <code>SQLData</code> object, using the given type |
|
119 |
* map to custom map it to a class in the Java programming |
|
120 |
* language. The type map gives the SQL type and the class |
|
121 |
* to which it is mapped. The <code>SQLData</code> object |
|
122 |
* defines the class to which the SQL type will be mapped. |
|
123 |
* |
|
124 |
* @param in an instance of the <code>SQLData</code> class |
|
125 |
* that defines the mapping of the SQL structured |
|
126 |
* type to one or more objects in the Java programming language |
|
127 |
* @param map a <code>java.util.Map</code> object in which |
|
128 |
* each entry consists of 1) a <code>String</code> object |
|
129 |
* giving the fully qualified name of a UDT and 2) the |
|
130 |
* <code>Class</code> object for the <code>SQLData</code> implementation |
|
131 |
* that defines how the UDT is to be mapped |
|
132 |
* @throws SerialException if an error occurs |
|
133 |
*/ |
|
134 |
public SerialStruct(SQLData in, Map<String,Class<?>> map) |
|
135 |
throws SerialException |
|
136 |
{ |
|
137 |
||
138 |
try { |
|
139 |
||
140 |
//set the type name |
|
6692
aef4e294026f
6988993: Address Findbugs warnings for the use of String Constructor
lancea
parents:
5506
diff
changeset
|
141 |
SQLTypeName = in.getSQLTypeName(); |
2 | 142 |
|
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
6692
diff
changeset
|
143 |
Vector<Object> tmp = new Vector<>(); |
2 | 144 |
in.writeSQL(new SQLOutputImpl(tmp, map)); |
145 |
attribs = tmp.toArray(); |
|
146 |
||
147 |
} catch (SQLException e) { |
|
148 |
throw new SerialException(e.getMessage()); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
||
153 |
/** |
|
154 |
* Retrieves the SQL type name for this <code>SerialStruct</code> |
|
155 |
* object. This is the name used in the SQL definition of the |
|
156 |
* structured type |
|
157 |
* |
|
158 |
* @return a <code>String</code> object representing the SQL |
|
159 |
* type name for the SQL structured type that this |
|
160 |
* <code>SerialStruct</code> object represents |
|
161 |
* @throws SerialException if an error occurs |
|
162 |
*/ |
|
163 |
public String getSQLTypeName() throws SerialException { |
|
164 |
return SQLTypeName; |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Retrieves an array of <code>Object</code> values containing the |
|
169 |
* attributes of the SQL structured type that this |
|
170 |
* <code>SerialStruct</code> object represents. |
|
171 |
* |
|
172 |
* @return an array of <code>Object</code> values, with each |
|
173 |
* element being an attribute of the SQL structured type |
|
174 |
* that this <code>SerialStruct</code> object represents |
|
175 |
* @throws SerialException if an error occurs |
|
176 |
*/ |
|
177 |
public Object[] getAttributes() throws SerialException { |
|
11683
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
178 |
Object[] val = this.attribs; |
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
179 |
return (val == null) ? null : Arrays.copyOf(val, val.length); |
2 | 180 |
} |
181 |
||
182 |
/** |
|
183 |
* Retrieves the attributes for the SQL structured type that |
|
184 |
* this <code>SerialStruct</code> represents as an array of |
|
185 |
* <code>Object</code> values, using the given type map for |
|
186 |
* custom mapping if appropriate. |
|
187 |
* |
|
188 |
* @param map a <code>java.util.Map</code> object in which |
|
189 |
* each entry consists of 1) a <code>String</code> object |
|
190 |
* giving the fully qualified name of a UDT and 2) the |
|
191 |
* <code>Class</code> object for the <code>SQLData</code> implementation |
|
192 |
* that defines how the UDT is to be mapped |
|
193 |
* @return an array of <code>Object</code> values, with each |
|
194 |
* element being an attribute of the SQL structured |
|
195 |
* type that this <code>SerialStruct</code> object |
|
196 |
* represents |
|
197 |
* @throws SerialException if an error occurs |
|
198 |
*/ |
|
199 |
public Object[] getAttributes(Map<String,Class<?>> map) |
|
200 |
throws SerialException |
|
201 |
{ |
|
11683
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
202 |
Object[] val = this.attribs; |
5e02efd89af6
7133815: address the findbug errors in CachedRowSetImpl, SerialStruct, BaseRow, SerialInputImpl, SerialOutputImpl
lancea
parents:
11129
diff
changeset
|
203 |
return (val == null) ? null : Arrays.copyOf(val, val.length); |
2 | 204 |
} |
205 |
||
206 |
||
207 |
/** |
|
208 |
* Maps attributes of an SQL structured type that are not |
|
209 |
* serialized to a serialized form, using the given type map |
|
210 |
* for custom mapping when appropriate. The following types |
|
211 |
* in the Java programming language are mapped to their |
|
212 |
* serialized forms: <code>Struct</code>, <code>SQLData</code>, |
|
213 |
* <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and |
|
214 |
* <code>Array</code>. |
|
215 |
* <P> |
|
216 |
* This method is called internally and is not used by an |
|
217 |
* application programmer. |
|
218 |
* |
|
219 |
* @param map a <code>java.util.Map</code> object in which |
|
220 |
* each entry consists of 1) a <code>String</code> object |
|
221 |
* giving the fully qualified name of a UDT and 2) the |
|
222 |
* <code>Class</code> object for the <code>SQLData</code> implementation |
|
223 |
* that defines how the UDT is to be mapped |
|
224 |
* @throws SerialException if an error occurs |
|
225 |
*/ |
|
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
6692
diff
changeset
|
226 |
private void mapToSerial(Map<String,Class<?>> map) throws SerialException { |
2 | 227 |
|
228 |
try { |
|
229 |
||
230 |
for (int i = 0; i < attribs.length; i++) { |
|
231 |
if (attribs[i] instanceof Struct) { |
|
232 |
attribs[i] = new SerialStruct((Struct)attribs[i], map); |
|
233 |
} else if (attribs[i] instanceof SQLData) { |
|
234 |
attribs[i] = new SerialStruct((SQLData)attribs[i], map); |
|
235 |
} else if (attribs[i] instanceof Blob) { |
|
236 |
attribs[i] = new SerialBlob((Blob)attribs[i]); |
|
237 |
} else if (attribs[i] instanceof Clob) { |
|
238 |
attribs[i] = new SerialClob((Clob)attribs[i]); |
|
239 |
} else if (attribs[i] instanceof Ref) { |
|
240 |
attribs[i] = new SerialRef((Ref)attribs[i]); |
|
241 |
} else if (attribs[i] instanceof java.sql.Array) { |
|
242 |
attribs[i] = new SerialArray((java.sql.Array)attribs[i], map); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
} catch (SQLException e) { |
|
247 |
throw new SerialException(e.getMessage()); |
|
248 |
} |
|
249 |
return; |
|
250 |
} |
|
251 |
||
252 |
/** |
|
6692
aef4e294026f
6988993: Address Findbugs warnings for the use of String Constructor
lancea
parents:
5506
diff
changeset
|
253 |
* The identifier that assists in the serialization of this |
2 | 254 |
* <code>SerialStruct</code> object. |
255 |
*/ |
|
256 |
static final long serialVersionUID = -8322445504027483372L; |
|
257 |
} |