Path: blob/master/src/java.sql/share/classes/java/sql/SQLData.java
41153 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627/**28* The interface used for the custom mapping of an SQL user-defined type (UDT) to29* a class in the Java programming language. The class object for a class30* implementing the {@code SQLData} interface will be entered in the31* appropriate {@code Connection} object's type map along with the SQL32* name of the UDT for which it is a custom mapping.33* <P>34* Typically, a {@code SQLData} implementation35* will define a field for each attribute of an SQL structured type or a36* single field for an SQL {@code DISTINCT} type. When the UDT is37* retrieved from a data source with the {@code ResultSet.getObject}38* method, it will be mapped as an instance of this class. A programmer39* can operate on this class instance just as on any other object in the40* Java programming language and then store any changes made to it by41* calling the {@code PreparedStatement.setObject} method,42* which will map it back to the SQL type.43* <p>44* It is expected that the implementation of the class for a custom45* mapping will be done by a tool. In a typical implementation, the46* programmer would simply supply the name of the SQL UDT, the name of47* the class to which it is being mapped, and the names of the fields to48* which each of the attributes of the UDT is to be mapped. The tool will use49* this information to implement the {@code SQLData.readSQL} and50* {@code SQLData.writeSQL} methods. The {@code readSQL} method51* calls the appropriate {@code SQLInput} methods to read52* each attribute from an {@code SQLInput} object, and the53* {@code writeSQL} method calls {@code SQLOutput} methods54* to write each attribute back to the data source via an55* {@code SQLOutput} object.56* <P>57* An application programmer will not normally call {@code SQLData} methods58* directly, and the {@code SQLInput} and {@code SQLOutput} methods59* are called internally by {@code SQLData} methods, not by application code.60*61* @since 1.262*/63public interface SQLData {6465/**66* Returns the fully-qualified67* name of the SQL user-defined type that this object represents.68* This method is called by the JDBC driver to get the name of the69* UDT instance that is being mapped to this instance of70* {@code SQLData}.71*72* @return the type name that was passed to the method {@code readSQL}73* when this object was constructed and populated74* @throws SQLException if there is a database access error75* @throws SQLFeatureNotSupportedException if the JDBC driver does not support76* this method77* @since 1.278*/79String getSQLTypeName() throws SQLException;8081/**82* Populates this object with data read from the database.83* The implementation of the method must follow this protocol:84* <UL>85* <LI>It must read each of the attributes or elements of the SQL86* type from the given input stream. This is done87* by calling a method of the input stream to read each88* item, in the order that they appear in the SQL definition89* of the type.90* <LI>The method {@code readSQL} then91* assigns the data to appropriate fields or92* elements (of this or other objects).93* Specifically, it must call the appropriate <i>reader</i> method94* ({@code SQLInput.readString}, {@code SQLInput.readBigDecimal},95* and so on) method(s) to do the following:96* for a distinct type, read its single data element;97* for a structured type, read a value for each attribute of the SQL type.98* </UL>99* The JDBC driver initializes the input stream with a type map100* before calling this method, which is used by the appropriate101* {@code SQLInput} reader method on the stream.102*103* @param stream the {@code SQLInput} object from which to read the data for104* the value that is being custom mapped105* @param typeName the SQL type name of the value on the data stream106* @throws SQLException if there is a database access error107* @throws SQLFeatureNotSupportedException if the JDBC driver does not support108* this method109* @see SQLInput110* @since 1.2111*/112void readSQL (SQLInput stream, String typeName) throws SQLException;113114/**115* Writes this object to the given SQL data stream, converting it back to116* its SQL value in the data source.117* The implementation of the method must follow this protocol:<BR>118* It must write each of the attributes of the SQL type119* to the given output stream. This is done by calling a120* method of the output stream to write each item, in the order that121* they appear in the SQL definition of the type.122* Specifically, it must call the appropriate {@code SQLOutput} writer123* method(s) ({@code writeInt}, {@code writeString}, and so on)124* to do the following: for a Distinct Type, write its single data element;125* for a Structured Type, write a value for each attribute of the SQL type.126*127* @param stream the {@code SQLOutput} object to which to write the data for128* the value that was custom mapped129* @throws SQLException if there is a database access error130* @throws SQLFeatureNotSupportedException if the JDBC driver does not support131* this method132* @see SQLOutput133* @since 1.2134*/135void writeSQL (SQLOutput stream) throws SQLException;136}137138139