001 /*
002 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.sql.rowset.serial;
027
028 import java.sql.*;
029 import javax.sql.*;
030 import java.io.*;
031 import java.math.*;
032 import java.util.Map;
033 import java.util.Vector;
034
035 import javax.sql.rowset.*;
036
037 /**
038 * A serialized mapping in the Java programming language of an SQL
039 * structured type. Each attribute that is not already serialized
040 * is mapped to a serialized form, and if an attribute is itself
041 * a structured type, each of its attributes that is not already
042 * serialized is mapped to a serialized form.
043 * <P>
044 * In addition, the structured type is custom mapped to a class in the
045 * Java programming language if there is such a mapping, as are
046 * its attributes, if appropriate.
047 * <P>
048 * The <code>SerialStruct</code> class provides a constructor for creating
049 * an instance from a <code>Struct</code> object, a method for retrieving
050 * the SQL type name of the SQL structured type in the database, and methods
051 * for retrieving its attribute values.
052 */
053 public class SerialStruct implements Struct, Serializable, Cloneable {
054
055 /**
056 * The SQL type name for the structured type that this
057 * <code>SerialStruct</code> object represents. This is the name
058 * used in the SQL definition of the SQL structured type.
059 *
060 * @serial
061 */
062 private String SQLTypeName;
063
064 /**
065 * An array of <code>Object</code> instances in which each
066 * element is an attribute of the SQL structured type that this
067 * <code>SerialStruct</code> object represents. The attributes are
068 * ordered according to their order in the definition of the
069 * SQL structured type.
070 *
071 * @serial
072 */
073 private Object attribs[];
074
075 /**
076 * Constructs a <code>SerialStruct</code> object from the given
077 * <code>Struct</code> object, using the given <code>java.util.Map</code>
078 * object for custom mapping the SQL structured type or any of its
079 * attributes that are SQL structured types.
080 *
081 * @param map a <code>java.util.Map</code> object in which
082 * each entry consists of 1) a <code>String</code> object
083 * giving the fully qualified name of a UDT and 2) the
084 * <code>Class</code> object for the <code>SQLData</code> implementation
085 * that defines how the UDT is to be mapped
086 * @throws SerialException if an error occurs
087 * @see java.sql.Struct
088 */
089 public SerialStruct(Struct in, Map<String, Class<?>> map)
090 throws SerialException {
091
092 try {
093
094 // get the type name
095 SQLTypeName = new String(in.getSQLTypeName());
096 System.out.println("SQLTypeName: " + SQLTypeName);
097
098 // get the attributes of the struct
099 attribs = in.getAttributes(map);
100
101 /*
102 * the array may contain further Structs
103 * and/or classes that have been mapped,
104 * other types that we have to serialize
105 */
106 mapToSerial(map);
107
108 } catch (SQLException e) {
109 throw new SerialException(e.getMessage());
110 }
111 }
112
113 /**
114 * Constructs a <code>SerialStruct</code> object from the
115 * given <code>SQLData</code> object, using the given type
116 * map to custom map it to a class in the Java programming
117 * language. The type map gives the SQL type and the class
118 * to which it is mapped. The <code>SQLData</code> object
119 * defines the class to which the SQL type will be mapped.
120 *
121 * @param in an instance of the <code>SQLData</code> class
122 * that defines the mapping of the SQL structured
123 * type to one or more objects in the Java programming language
124 * @param map a <code>java.util.Map</code> object in which
125 * each entry consists of 1) a <code>String</code> object
126 * giving the fully qualified name of a UDT and 2) the
127 * <code>Class</code> object for the <code>SQLData</code> implementation
128 * that defines how the UDT is to be mapped
129 * @throws SerialException if an error occurs
130 */
131 public SerialStruct(SQLData in, Map<String, Class<?>> map)
132 throws SerialException {
133
134 try {
135
136 //set the type name
137 SQLTypeName = new String(in.getSQLTypeName());
138
139 Vector tmp = new Vector();
140 in.writeSQL(new SQLOutputImpl(tmp, map));
141 attribs = tmp.toArray();
142
143 } catch (SQLException e) {
144 throw new SerialException(e.getMessage());
145 }
146 }
147
148 /**
149 * Retrieves the SQL type name for this <code>SerialStruct</code>
150 * object. This is the name used in the SQL definition of the
151 * structured type
152 *
153 * @return a <code>String</code> object representing the SQL
154 * type name for the SQL structured type that this
155 * <code>SerialStruct</code> object represents
156 * @throws SerialException if an error occurs
157 */
158 public String getSQLTypeName() throws SerialException {
159 return SQLTypeName;
160 }
161
162 /**
163 * Retrieves an array of <code>Object</code> values containing the
164 * attributes of the SQL structured type that this
165 * <code>SerialStruct</code> object represents.
166 *
167 * @return an array of <code>Object</code> values, with each
168 * element being an attribute of the SQL structured type
169 * that this <code>SerialStruct</code> object represents
170 * @throws SerialException if an error occurs
171 */
172 public Object[] getAttributes() throws SerialException {
173 return attribs;
174 }
175
176 /**
177 * Retrieves the attributes for the SQL structured type that
178 * this <code>SerialStruct</code> represents as an array of
179 * <code>Object</code> values, using the given type map for
180 * custom mapping if appropriate.
181 *
182 * @param map a <code>java.util.Map</code> object in which
183 * each entry consists of 1) a <code>String</code> object
184 * giving the fully qualified name of a UDT and 2) the
185 * <code>Class</code> object for the <code>SQLData</code> implementation
186 * that defines how the UDT is to be mapped
187 * @return an array of <code>Object</code> values, with each
188 * element being an attribute of the SQL structured
189 * type that this <code>SerialStruct</code> object
190 * represents
191 * @throws SerialException if an error occurs
192 */
193 public Object[] getAttributes(Map<String, Class<?>> map)
194 throws SerialException {
195 return attribs;
196 }
197
198 /**
199 * Maps attributes of an SQL structured type that are not
200 * serialized to a serialized form, using the given type map
201 * for custom mapping when appropriate. The following types
202 * in the Java programming language are mapped to their
203 * serialized forms: <code>Struct</code>, <code>SQLData</code>,
204 * <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and
205 * <code>Array</code>.
206 * <P>
207 * This method is called internally and is not used by an
208 * application programmer.
209 *
210 * @param map a <code>java.util.Map</code> object in which
211 * each entry consists of 1) a <code>String</code> object
212 * giving the fully qualified name of a UDT and 2) the
213 * <code>Class</code> object for the <code>SQLData</code> implementation
214 * that defines how the UDT is to be mapped
215 * @throws SerialException if an error occurs
216 */
217 private void mapToSerial(Map map) throws SerialException {
218
219 try {
220
221 for (int i = 0; i < attribs.length; i++) {
222 if (attribs[i] instanceof Struct) {
223 attribs[i] = new SerialStruct((Struct) attribs[i],
224 map);
225 } else if (attribs[i] instanceof SQLData) {
226 attribs[i] = new SerialStruct((SQLData) attribs[i],
227 map);
228 } else if (attribs[i] instanceof Blob) {
229 attribs[i] = new SerialBlob((Blob) attribs[i]);
230 } else if (attribs[i] instanceof Clob) {
231 attribs[i] = new SerialClob((Clob) attribs[i]);
232 } else if (attribs[i] instanceof Ref) {
233 attribs[i] = new SerialRef((Ref) attribs[i]);
234 } else if (attribs[i] instanceof java.sql.Array) {
235 attribs[i] = new SerialArray(
236 (java.sql.Array) attribs[i], map);
237 }
238 }
239
240 } catch (SQLException e) {
241 throw new SerialException(e.getMessage());
242 }
243 return;
244 }
245
246 /**
247 * The identifier that assists in the serialization of this
248 * <code>SerialStruct</code> object.
249 */
250 static final long serialVersionUID = -8322445504027483372L;
251 }
|