1. /*
  2. * @(#)SerialStruct.java 1.6 04/05/29
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sql.rowset.serial;
  8. import java.sql.*;
  9. import javax.sql.*;
  10. import java.io.*;
  11. import java.math.*;
  12. import java.util.Map;
  13. import java.util.Vector;
  14. import javax.sql.rowset.*;
  15. /**
  16. * A serialized mapping in the Java programming language of an SQL
  17. * structured type. Each attribute that is not already serialized
  18. * is mapped to a serialized form, and if an attribute is itself
  19. * a structured type, each of its attributes that is not already
  20. * serialized is mapped to a serialized form.
  21. * <P>
  22. * In addition, the structured type is custom mapped to a class in the
  23. * Java programming language if there is such a mapping, as are
  24. * its attributes, if appropriate.
  25. * <P>
  26. * The <code>SerialStruct</code> class provides a constructor for creating
  27. * an instance from a <code>Struct</code> object, a method for retrieving
  28. * the SQL type name of the SQL structured type in the database, and methods
  29. * for retrieving its attribute values.
  30. */
  31. public class SerialStruct implements Struct, Serializable, Cloneable {
  32. /**
  33. * The SQL type name for the structured type that this
  34. * <code>SerialStruct</code> object represents. This is the name
  35. * used in the SQL definition of the SQL structured type.
  36. *
  37. * @serial
  38. */
  39. private String SQLTypeName;
  40. /**
  41. * An array of <code>Object</code> instances in which each
  42. * element is an attribute of the SQL structured type that this
  43. * <code>SerialStruct</code> object represents. The attributes are
  44. * ordered according to their order in the definition of the
  45. * SQL structured type.
  46. *
  47. * @serial
  48. */
  49. private Object attribs[];
  50. /**
  51. * Constructs a <code>SerialStruct</code> object from the given
  52. * <code>Struct</code> object, using the given <code>java.util.Map</code>
  53. * object for custom mapping the SQL structured type or any of its
  54. * attributes that are SQL structured types.
  55. *
  56. * @param map a <code>java.util.Map</code> object in which
  57. * each entry consists of 1) a <code>String</code> object
  58. * giving the fully qualified name of a UDT and 2) the
  59. * <code>Class</code> object for the <code>SQLData</code> implementation
  60. * that defines how the UDT is to be mapped
  61. * @throws SerialException if an error occurs
  62. * @see java.sql.Struct
  63. */
  64. public SerialStruct(Struct in, Map<String,Class<?>> map)
  65. throws SerialException
  66. {
  67. try {
  68. // get the type name
  69. SQLTypeName = new String(in.getSQLTypeName());
  70. System.out.println("SQLTypeName: " + SQLTypeName);
  71. // get the attributes of the struct
  72. attribs = in.getAttributes(map);
  73. /*
  74. * the array may contain further Structs
  75. * and/or classes that have been mapped,
  76. * other types that we have to serialize
  77. */
  78. mapToSerial(map);
  79. } catch (SQLException e) {
  80. throw new SerialException(e.getMessage());
  81. }
  82. }
  83. /**
  84. * Constructs a <code>SerialStruct</code> object from the
  85. * given <code>SQLData</code> object, using the given type
  86. * map to custom map it to a class in the Java programming
  87. * language. The type map gives the SQL type and the class
  88. * to which it is mapped. The <code>SQLData</code> object
  89. * defines the class to which the SQL type will be mapped.
  90. *
  91. * @param in an instance of the <code>SQLData</code> class
  92. * that defines the mapping of the SQL structured
  93. * type to one or more objects in the Java programming language
  94. * @param map a <code>java.util.Map</code> object in which
  95. * each entry consists of 1) a <code>String</code> object
  96. * giving the fully qualified name of a UDT and 2) the
  97. * <code>Class</code> object for the <code>SQLData</code> implementation
  98. * that defines how the UDT is to be mapped
  99. * @throws SerialException if an error occurs
  100. */
  101. public SerialStruct(SQLData in, Map<String,Class<?>> map)
  102. throws SerialException
  103. {
  104. try {
  105. //set the type name
  106. SQLTypeName = new String(in.getSQLTypeName());
  107. Vector tmp = new Vector();
  108. in.writeSQL(new SQLOutputImpl(tmp, map));
  109. attribs = tmp.toArray();
  110. } catch (SQLException e) {
  111. throw new SerialException(e.getMessage());
  112. }
  113. }
  114. /**
  115. * Retrieves the SQL type name for this <code>SerialStruct</code>
  116. * object. This is the name used in the SQL definition of the
  117. * structured type
  118. *
  119. * @return a <code>String</code> object representing the SQL
  120. * type name for the SQL structured type that this
  121. * <code>SerialStruct</code> object represents
  122. * @throws SerialException if an error occurs
  123. */
  124. public String getSQLTypeName() throws SerialException {
  125. return SQLTypeName;
  126. }
  127. /**
  128. * Retrieves an array of <code>Object</code> values containing the
  129. * attributes of the SQL structured type that this
  130. * <code>SerialStruct</code> object represents.
  131. *
  132. * @return an array of <code>Object</code> values, with each
  133. * element being an attribute of the SQL structured type
  134. * that this <code>SerialStruct</code> object represents
  135. * @throws SerialException if an error occurs
  136. */
  137. public Object[] getAttributes() throws SerialException {
  138. return attribs;
  139. }
  140. /**
  141. * Retrieves the attributes for the SQL structured type that
  142. * this <code>SerialStruct</code> represents as an array of
  143. * <code>Object</code> values, using the given type map for
  144. * custom mapping if appropriate.
  145. *
  146. * @param map a <code>java.util.Map</code> object in which
  147. * each entry consists of 1) a <code>String</code> object
  148. * giving the fully qualified name of a UDT and 2) the
  149. * <code>Class</code> object for the <code>SQLData</code> implementation
  150. * that defines how the UDT is to be mapped
  151. * @return an array of <code>Object</code> values, with each
  152. * element being an attribute of the SQL structured
  153. * type that this <code>SerialStruct</code> object
  154. * represents
  155. * @throws SerialException if an error occurs
  156. */
  157. public Object[] getAttributes(Map<String,Class<?>> map)
  158. throws SerialException
  159. {
  160. return attribs;
  161. }
  162. /**
  163. * Maps attributes of an SQL structured type that are not
  164. * serialized to a serialized form, using the given type map
  165. * for custom mapping when appropriate. The following types
  166. * in the Java programming language are mapped to their
  167. * serialized forms: <code>Struct</code>, <code>SQLData</code>,
  168. * <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and
  169. * <code>Array</code>.
  170. * <P>
  171. * This method is called internally and is not used by an
  172. * application programmer.
  173. *
  174. * @param map a <code>java.util.Map</code> object in which
  175. * each entry consists of 1) a <code>String</code> object
  176. * giving the fully qualified name of a UDT and 2) the
  177. * <code>Class</code> object for the <code>SQLData</code> implementation
  178. * that defines how the UDT is to be mapped
  179. * @throws SerialException if an error occurs
  180. */
  181. private void mapToSerial(Map map) throws SerialException {
  182. try {
  183. for (int i = 0; i < attribs.length; i++) {
  184. if (attribs[i] instanceof Struct) {
  185. attribs[i] = new SerialStruct((Struct)attribs[i], map);
  186. } else if (attribs[i] instanceof SQLData) {
  187. attribs[i] = new SerialStruct((SQLData)attribs[i], map);
  188. } else if (attribs[i] instanceof Blob) {
  189. attribs[i] = new SerialBlob((Blob)attribs[i]);
  190. } else if (attribs[i] instanceof Clob) {
  191. attribs[i] = new SerialClob((Clob)attribs[i]);
  192. } else if (attribs[i] instanceof Ref) {
  193. attribs[i] = new SerialRef((Ref)attribs[i]);
  194. } else if (attribs[i] instanceof java.sql.Array) {
  195. attribs[i] = new SerialArray((java.sql.Array)attribs[i], map);
  196. }
  197. }
  198. } catch (SQLException e) {
  199. throw new SerialException(e.getMessage());
  200. }
  201. return;
  202. }
  203. /**
  204. * The identifier that assists in the serialization of this
  205. * <code>SerialStruct</code> object.
  206. */
  207. static final long serialVersionUID = -8322445504027483372L;
  208. }