0N/A/*
2823N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.sql.rowset.serial;
0N/A
0N/Aimport java.sql.*;
0N/Aimport javax.sql.*;
0N/Aimport java.io.*;
0N/Aimport java.math.*;
0N/Aimport java.util.Map;
0N/Aimport java.util.Vector;
0N/A
0N/Aimport javax.sql.rowset.*;
0N/A
0N/A/**
0N/A * A serialized mapping in the Java programming language of an SQL
0N/A * structured type. Each attribute that is not already serialized
0N/A * is mapped to a serialized form, and if an attribute is itself
0N/A * a structured type, each of its attributes that is not already
0N/A * serialized is mapped to a serialized form.
0N/A * <P>
0N/A * In addition, the structured type is custom mapped to a class in the
0N/A * Java programming language if there is such a mapping, as are
0N/A * its attributes, if appropriate.
0N/A * <P>
0N/A * The <code>SerialStruct</code> class provides a constructor for creating
0N/A * an instance from a <code>Struct</code> object, a method for retrieving
0N/A * the SQL type name of the SQL structured type in the database, and methods
0N/A * for retrieving its attribute values.
0N/A */
0N/Apublic class SerialStruct implements Struct, Serializable, Cloneable {
0N/A
0N/A
0N/A /**
0N/A * The SQL type name for the structured type that this
0N/A * <code>SerialStruct</code> object represents. This is the name
0N/A * used in the SQL definition of the SQL structured type.
0N/A *
0N/A * @serial
0N/A */
0N/A private String SQLTypeName;
0N/A
0N/A /**
0N/A * An array of <code>Object</code> instances in which each
0N/A * element is an attribute of the SQL structured type that this
0N/A * <code>SerialStruct</code> object represents. The attributes are
0N/A * ordered according to their order in the definition of the
0N/A * SQL structured type.
0N/A *
0N/A * @serial
0N/A */
0N/A private Object attribs[];
0N/A
0N/A /**
0N/A * Constructs a <code>SerialStruct</code> object from the given
0N/A * <code>Struct</code> object, using the given <code>java.util.Map</code>
0N/A * object for custom mapping the SQL structured type or any of its
0N/A * attributes that are SQL structured types.
0N/A *
0N/A * @param map a <code>java.util.Map</code> object in which
0N/A * each entry consists of 1) a <code>String</code> object
0N/A * giving the fully qualified name of a UDT and 2) the
0N/A * <code>Class</code> object for the <code>SQLData</code> implementation
0N/A * that defines how the UDT is to be mapped
0N/A * @throws SerialException if an error occurs
0N/A * @see java.sql.Struct
0N/A */
0N/A public SerialStruct(Struct in, Map<String,Class<?>> map)
0N/A throws SerialException
0N/A {
0N/A
0N/A try {
0N/A
0N/A // get the type name
2823N/A SQLTypeName = in.getSQLTypeName();
0N/A System.out.println("SQLTypeName: " + SQLTypeName);
0N/A
0N/A // get the attributes of the struct
0N/A attribs = in.getAttributes(map);
0N/A
0N/A /*
0N/A * the array may contain further Structs
0N/A * and/or classes that have been mapped,
0N/A * other types that we have to serialize
0N/A */
0N/A mapToSerial(map);
0N/A
0N/A } catch (SQLException e) {
0N/A throw new SerialException(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>SerialStruct</code> object from the
0N/A * given <code>SQLData</code> object, using the given type
0N/A * map to custom map it to a class in the Java programming
0N/A * language. The type map gives the SQL type and the class
0N/A * to which it is mapped. The <code>SQLData</code> object
0N/A * defines the class to which the SQL type will be mapped.
0N/A *
0N/A * @param in an instance of the <code>SQLData</code> class
0N/A * that defines the mapping of the SQL structured
0N/A * type to one or more objects in the Java programming language
0N/A * @param map a <code>java.util.Map</code> object in which
0N/A * each entry consists of 1) a <code>String</code> object
0N/A * giving the fully qualified name of a UDT and 2) the
0N/A * <code>Class</code> object for the <code>SQLData</code> implementation
0N/A * that defines how the UDT is to be mapped
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public SerialStruct(SQLData in, Map<String,Class<?>> map)
0N/A throws SerialException
0N/A {
0N/A
0N/A try {
0N/A
0N/A //set the type name
2823N/A SQLTypeName = in.getSQLTypeName();
0N/A
0N/A Vector tmp = new Vector();
0N/A in.writeSQL(new SQLOutputImpl(tmp, map));
0N/A attribs = tmp.toArray();
0N/A
0N/A } catch (SQLException e) {
0N/A throw new SerialException(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Retrieves the SQL type name for this <code>SerialStruct</code>
0N/A * object. This is the name used in the SQL definition of the
0N/A * structured type
0N/A *
0N/A * @return a <code>String</code> object representing the SQL
0N/A * type name for the SQL structured type that this
0N/A * <code>SerialStruct</code> object represents
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public String getSQLTypeName() throws SerialException {
0N/A return SQLTypeName;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves an array of <code>Object</code> values containing the
0N/A * attributes of the SQL structured type that this
0N/A * <code>SerialStruct</code> object represents.
0N/A *
0N/A * @return an array of <code>Object</code> values, with each
0N/A * element being an attribute of the SQL structured type
0N/A * that this <code>SerialStruct</code> object represents
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public Object[] getAttributes() throws SerialException {
0N/A return attribs;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the attributes for the SQL structured type that
0N/A * this <code>SerialStruct</code> represents as an array of
0N/A * <code>Object</code> values, using the given type map for
0N/A * custom mapping if appropriate.
0N/A *
0N/A * @param map a <code>java.util.Map</code> object in which
0N/A * each entry consists of 1) a <code>String</code> object
0N/A * giving the fully qualified name of a UDT and 2) the
0N/A * <code>Class</code> object for the <code>SQLData</code> implementation
0N/A * that defines how the UDT is to be mapped
0N/A * @return an array of <code>Object</code> values, with each
0N/A * element being an attribute of the SQL structured
0N/A * type that this <code>SerialStruct</code> object
0N/A * represents
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A public Object[] getAttributes(Map<String,Class<?>> map)
0N/A throws SerialException
0N/A {
0N/A return attribs;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Maps attributes of an SQL structured type that are not
0N/A * serialized to a serialized form, using the given type map
0N/A * for custom mapping when appropriate. The following types
0N/A * in the Java programming language are mapped to their
0N/A * serialized forms: <code>Struct</code>, <code>SQLData</code>,
0N/A * <code>Ref</code>, <code>Blob</code>, <code>Clob</code>, and
0N/A * <code>Array</code>.
0N/A * <P>
0N/A * This method is called internally and is not used by an
0N/A * application programmer.
0N/A *
0N/A * @param map a <code>java.util.Map</code> object in which
0N/A * each entry consists of 1) a <code>String</code> object
0N/A * giving the fully qualified name of a UDT and 2) the
0N/A * <code>Class</code> object for the <code>SQLData</code> implementation
0N/A * that defines how the UDT is to be mapped
0N/A * @throws SerialException if an error occurs
0N/A */
0N/A private void mapToSerial(Map map) throws SerialException {
0N/A
0N/A try {
0N/A
0N/A for (int i = 0; i < attribs.length; i++) {
0N/A if (attribs[i] instanceof Struct) {
0N/A attribs[i] = new SerialStruct((Struct)attribs[i], map);
0N/A } else if (attribs[i] instanceof SQLData) {
0N/A attribs[i] = new SerialStruct((SQLData)attribs[i], map);
0N/A } else if (attribs[i] instanceof Blob) {
0N/A attribs[i] = new SerialBlob((Blob)attribs[i]);
0N/A } else if (attribs[i] instanceof Clob) {
0N/A attribs[i] = new SerialClob((Clob)attribs[i]);
0N/A } else if (attribs[i] instanceof Ref) {
0N/A attribs[i] = new SerialRef((Ref)attribs[i]);
0N/A } else if (attribs[i] instanceof java.sql.Array) {
0N/A attribs[i] = new SerialArray((java.sql.Array)attribs[i], map);
0N/A }
0N/A }
0N/A
0N/A } catch (SQLException e) {
0N/A throw new SerialException(e.getMessage());
0N/A }
0N/A return;
0N/A }
0N/A
0N/A /**
2823N/A * The identifier that assists in the serialization of this
0N/A * <code>SerialStruct</code> object.
0N/A */
0N/A static final long serialVersionUID = -8322445504027483372L;
0N/A}