Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SQLOutputImpl.java
40948 views
/*1* Copyright (c) 2003, 2013, 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 javax.sql.rowset.serial;2627import java.io.BufferedReader;28import java.io.IOException;29import java.io.InputStreamReader;30import java.sql.*;31import java.util.Map;32import java.util.Vector;3334/**35* The output stream for writing the attributes of a36* custom-mapped user-defined type (UDT) back to the database.37* The driver uses this interface internally, and its38* methods are never directly invoked by an application programmer.39* <p>40* When an application calls the41* method <code>PreparedStatement.setObject</code>, the driver42* checks to see whether the value to be written is a UDT with43* a custom mapping. If it is, there will be an entry in a44* type map containing the <code>Class</code> object for the45* class that implements <code>SQLData</code> for this UDT.46* If the value to be written is an instance of <code>SQLData</code>,47* the driver will create an instance of <code>SQLOutputImpl</code>48* and pass it to the method <code>SQLData.writeSQL</code>.49* The method <code>writeSQL</code> in turn calls the50* appropriate <code>SQLOutputImpl.writeXXX</code> methods51* to write data from the <code>SQLData</code> object to52* the <code>SQLOutputImpl</code> output stream as the53* representation of an SQL user-defined type.54*55* @since 1.556*/57public class SQLOutputImpl implements SQLOutput {5859/**60* A reference to an existing vector that61* contains the attributes of a <code>Struct</code> object.62*/63@SuppressWarnings("rawtypes")64private Vector attribs;6566/**67* The type map the driver supplies to a newly created68* <code>SQLOutputImpl</code> object. This type map69* indicates the <code>SQLData</code> class whose70* <code>writeSQL</code> method will be called. This71* method will in turn call the appropriate72* <code>SQLOutputImpl</code> writer methods.73*/74@SuppressWarnings("rawtypes")75private Map map;7677/**78* Creates a new <code>SQLOutputImpl</code> object79* initialized with the given vector of attributes and80* type map. The driver will use the type map to determine81* which <code>SQLData.writeSQL</code> method to invoke.82* This method will then call the appropriate83* <code>SQLOutputImpl</code> writer methods in order and84* thereby write the attributes to the new output stream.85*86* @param attributes a <code>Vector</code> object containing the attributes of87* the UDT to be mapped to one or more objects in the Java88* programming language89*90* @param map a <code>java.util.Map</code> object containing zero or91* more entries, with each entry consisting of 1) a <code>String</code>92* giving the fully qualified name of a UDT and 2) the93* <code>Class</code> object for the <code>SQLData</code> implementation94* that defines how the UDT is to be mapped95* @throws SQLException if the <code>attributes</code> or the <code>map</code>96* is a <code>null</code> value97*/98public SQLOutputImpl(Vector<?> attributes, Map<String,?> map)99throws SQLException100{101if ((attributes == null) || (map == null)) {102throw new SQLException("Cannot instantiate a SQLOutputImpl " +103"instance with null parameters");104}105this.attribs = attributes;106this.map = map;107}108109//================================================================110// Methods for writing attributes to the stream of SQL data.111// These methods correspond to the column-accessor methods of112// java.sql.ResultSet.113//================================================================114115/**116* Writes a <code>String</code> in the Java programming language117* to this <code>SQLOutputImpl</code> object. The driver converts118* it to an SQL <code>CHAR</code>, <code>VARCHAR</code>, or119* <code>LONGVARCHAR</code> before returning it to the database.120*121* @param x the value to pass to the database122* @throws SQLException if the <code>SQLOutputImpl</code> object is in123* use by a <code>SQLData</code> object attempting to write the attribute124* values of a UDT to the database.125*/126@SuppressWarnings("unchecked")127public void writeString(String x) throws SQLException {128//System.out.println("Adding :"+x);129attribs.add(x);130}131132/**133* Writes a <code>boolean</code> in the Java programming language134* to this <code>SQLOutputImpl</code> object. The driver converts135* it to an SQL <code>BIT</code> before returning it to the database.136*137* @param x the value to pass to the database138* @throws SQLException if the <code>SQLOutputImpl</code> object is in139* use by a <code>SQLData</code> object attempting to write the attribute140* values of a UDT to the database.141*/142@SuppressWarnings("unchecked")143public void writeBoolean(boolean x) throws SQLException {144attribs.add(Boolean.valueOf(x));145}146147/**148* Writes a <code>byte</code> in the Java programming language149* to this <code>SQLOutputImpl</code> object. The driver converts150* it to an SQL <code>BIT</code> before returning it to the database.151*152* @param x the value to pass to the database153* @throws SQLException if the <code>SQLOutputImpl</code> object is in154* use by a <code>SQLData</code> object attempting to write the attribute155* values of a UDT to the database.156*/157@SuppressWarnings("unchecked")158public void writeByte(byte x) throws SQLException {159attribs.add(Byte.valueOf(x));160}161162/**163* Writes a <code>short</code> in the Java programming language164* to this <code>SQLOutputImpl</code> object. The driver converts165* it to an SQL <code>SMALLINT</code> before returning it to the database.166*167* @param x the value to pass to the database168* @throws SQLException if the <code>SQLOutputImpl</code> object is in169* use by a <code>SQLData</code> object attempting to write the attribute170* values of a UDT to the database.171*/172@SuppressWarnings("unchecked")173public void writeShort(short x) throws SQLException {174attribs.add(Short.valueOf(x));175}176177/**178* Writes an <code>int</code> in the Java programming language179* to this <code>SQLOutputImpl</code> object. The driver converts180* it to an SQL <code>INTEGER</code> before returning it to the database.181*182* @param x the value to pass to the database183* @throws SQLException if the <code>SQLOutputImpl</code> object is in184* use by a <code>SQLData</code> object attempting to write the attribute185* values of a UDT to the database.186*/187@SuppressWarnings("unchecked")188public void writeInt(int x) throws SQLException {189attribs.add(Integer.valueOf(x));190}191192/**193* Writes a <code>long</code> in the Java programming language194* to this <code>SQLOutputImpl</code> object. The driver converts195* it to an SQL <code>BIGINT</code> before returning it to the database.196*197* @param x the value to pass to the database198* @throws SQLException if the <code>SQLOutputImpl</code> object is in199* use by a <code>SQLData</code> object attempting to write the attribute200* values of a UDT to the database.201*/202@SuppressWarnings("unchecked")203public void writeLong(long x) throws SQLException {204attribs.add(Long.valueOf(x));205}206207/**208* Writes a <code>float</code> in the Java programming language209* to this <code>SQLOutputImpl</code> object. The driver converts210* it to an SQL <code>REAL</code> before returning it to the database.211*212* @param x the value to pass to the database213* @throws SQLException if the <code>SQLOutputImpl</code> object is in214* use by a <code>SQLData</code> object attempting to write the attribute215* values of a UDT to the database.216*/217@SuppressWarnings("unchecked")218public void writeFloat(float x) throws SQLException {219attribs.add(Float.valueOf(x));220}221222/**223* Writes a <code>double</code> in the Java programming language224* to this <code>SQLOutputImpl</code> object. The driver converts225* it to an SQL <code>DOUBLE</code> before returning it to the database.226*227* @param x the value to pass to the database228* @throws SQLException if the <code>SQLOutputImpl</code> object is in229* use by a <code>SQLData</code> object attempting to write the attribute230* values of a UDT to the database.231*/232@SuppressWarnings("unchecked")233public void writeDouble(double x) throws SQLException{234attribs.add(Double.valueOf(x));235}236237/**238* Writes a <code>java.math.BigDecimal</code> object in the Java programming239* language to this <code>SQLOutputImpl</code> object. The driver converts240* it to an SQL <code>NUMERIC</code> before returning it to the database.241*242* @param x the value to pass to the database243* @throws SQLException if the <code>SQLOutputImpl</code> object is in244* use by a <code>SQLData</code> object attempting to write the attribute245* values of a UDT to the database.246*/247@SuppressWarnings("unchecked")248public void writeBigDecimal(java.math.BigDecimal x) throws SQLException{249attribs.add(x);250}251252/**253* Writes an array of <code>bytes</code> in the Java programming language254* to this <code>SQLOutputImpl</code> object. The driver converts255* it to an SQL <code>VARBINARY</code> or <code>LONGVARBINARY</code>256* before returning it to the database.257*258* @param x the value to pass to the database259* @throws SQLException if the <code>SQLOutputImpl</code> object is in260* use by a <code>SQLData</code> object attempting to write the attribute261* values of a UDT to the database.262*/263@SuppressWarnings("unchecked")264public void writeBytes(byte[] x) throws SQLException {265attribs.add(x);266}267268/**269* Writes a <code>java.sql.Date</code> object in the Java programming270* language to this <code>SQLOutputImpl</code> object. The driver converts271* it to an SQL <code>DATE</code> before returning it to the database.272*273* @param x the value to pass to the database274* @throws SQLException if the <code>SQLOutputImpl</code> object is in275* use by a <code>SQLData</code> object attempting to write the attribute276* values of a UDT to the database.277*/278@SuppressWarnings("unchecked")279public void writeDate(java.sql.Date x) throws SQLException {280attribs.add(x);281}282283/**284* Writes a <code>java.sql.Time</code> object in the Java programming285* language to this <code>SQLOutputImpl</code> object. The driver converts286* it to an SQL <code>TIME</code> before returning it to the database.287*288* @param x the value to pass to the database289* @throws SQLException if the <code>SQLOutputImpl</code> object is in290* use by a <code>SQLData</code> object attempting to write the attribute291* values of a UDT to the database.292*/293@SuppressWarnings("unchecked")294public void writeTime(java.sql.Time x) throws SQLException {295attribs.add(x);296}297298/**299* Writes a <code>java.sql.Timestamp</code> object in the Java programming300* language to this <code>SQLOutputImpl</code> object. The driver converts301* it to an SQL <code>TIMESTAMP</code> before returning it to the database.302*303* @param x the value to pass to the database304* @throws SQLException if the <code>SQLOutputImpl</code> object is in305* use by a <code>SQLData</code> object attempting to write the attribute306* values of a UDT to the database.307*/308@SuppressWarnings("unchecked")309public void writeTimestamp(java.sql.Timestamp x) throws SQLException {310attribs.add(x);311}312313/**314* Writes a stream of Unicode characters to this315* <code>SQLOutputImpl</code> object. The driver will do any necessary316* conversion from Unicode to the database <code>CHAR</code> format.317*318* @param x the value to pass to the database319* @throws SQLException if the <code>SQLOutputImpl</code> object is in320* use by a <code>SQLData</code> object attempting to write the attribute321* values of a UDT to the database.322*/323@SuppressWarnings("unchecked")324public void writeCharacterStream(java.io.Reader x) throws SQLException {325BufferedReader bufReader = new BufferedReader(x);326try {327int i;328while( (i = bufReader.read()) != -1 ) {329char ch = (char)i;330StringBuffer strBuf = new StringBuffer();331strBuf.append(ch);332333String str = new String(strBuf);334String strLine = bufReader.readLine();335336writeString(str.concat(strLine));337}338} catch(IOException ioe) {339340}341}342343/**344* Writes a stream of ASCII characters to this345* <code>SQLOutputImpl</code> object. The driver will do any necessary346* conversion from ASCII to the database <code>CHAR</code> format.347*348* @param x the value to pass to the database349* @throws SQLException if the <code>SQLOutputImpl</code> object is in350* use by a <code>SQLData</code> object attempting to write the attribute351* values of a UDT to the database.352*/353@SuppressWarnings("unchecked")354public void writeAsciiStream(java.io.InputStream x) throws SQLException {355BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));356try {357int i;358while( (i=bufReader.read()) != -1 ) {359char ch = (char)i;360361StringBuffer strBuf = new StringBuffer();362strBuf.append(ch);363364String str = new String(strBuf);365String strLine = bufReader.readLine();366367writeString(str.concat(strLine));368}369}catch(IOException ioe) {370throw new SQLException(ioe.getMessage());371}372}373374/**375* Writes a stream of uninterpreted bytes to this <code>SQLOutputImpl</code>376* object.377*378* @param x the value to pass to the database379* @throws SQLException if the <code>SQLOutputImpl</code> object is in380* use by a <code>SQLData</code> object attempting to write the attribute381* values of a UDT to the database.382*/383@SuppressWarnings("unchecked")384public void writeBinaryStream(java.io.InputStream x) throws SQLException {385BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));386try {387int i;388while( (i=bufReader.read()) != -1 ) {389char ch = (char)i;390391StringBuffer strBuf = new StringBuffer();392strBuf.append(ch);393394String str = new String(strBuf);395String strLine = bufReader.readLine();396397writeString(str.concat(strLine));398}399} catch(IOException ioe) {400throw new SQLException(ioe.getMessage());401}402}403404//================================================================405// Methods for writing items of SQL user-defined types to the stream.406// These methods pass objects to the database as values of SQL407// Structured Types, Distinct Types, Constructed Types, and Locator408// Types. They decompose the Java object(s) and write leaf data409// items using the methods above.410//================================================================411412/**413* Writes to the stream the data contained in the given414* <code>SQLData</code> object.415* When the <code>SQLData</code> object is <code>null</code>, this416* method writes an SQL <code>NULL</code> to the stream.417* Otherwise, it calls the <code>SQLData.writeSQL</code>418* method of the given object, which419* writes the object's attributes to the stream.420* <P>421* The implementation of the method <code>SQLData.writeSQ</code>422* calls the appropriate <code>SQLOutputImpl.writeXXX</code> method(s)423* for writing each of the object's attributes in order.424* The attributes must be read from an <code>SQLInput</code>425* input stream and written to an <code>SQLOutputImpl</code>426* output stream in the same order in which they were427* listed in the SQL definition of the user-defined type.428*429* @param x the object representing data of an SQL structured or430* distinct type431* @throws SQLException if the <code>SQLOutputImpl</code> object is in432* use by a <code>SQLData</code> object attempting to write the attribute433* values of a UDT to the database.434*/435@SuppressWarnings("unchecked")436public void writeObject(SQLData x) throws SQLException {437438/*439* Except for the types that are passed as objects440* this seems to be the only way for an object to441* get a null value for a field in a structure.442*443* Note: this means that the class defining SQLData444* will need to track if a field is SQL null for itself445*/446if (x == null) {447attribs.add(null);448} else {449/*450* We have to write out a SerialStruct that contains451* the name of this class otherwise we don't know452* what to re-instantiate during readSQL()453*/454attribs.add(new SerialStruct(x, map));455}456}457458/**459* Writes a <code>Ref</code> object in the Java programming language460* to this <code>SQLOutputImpl</code> object. The driver converts461* it to a serializable <code>SerialRef</code> SQL <code>REF</code> value462* before returning it to the database.463*464* @param x an object representing an SQL <code>REF</code> value465* @throws SQLException if the <code>SQLOutputImpl</code> object is in466* use by a <code>SQLData</code> object attempting to write the attribute467* values of a UDT to the database.468*/469@SuppressWarnings("unchecked")470public void writeRef(Ref x) throws SQLException {471if (x == null) {472attribs.add(null);473} else {474attribs.add(new SerialRef(x));475}476}477478/**479* Writes a <code>Blob</code> object in the Java programming language480* to this <code>SQLOutputImpl</code> object. The driver converts481* it to a serializable <code>SerialBlob</code> SQL <code>BLOB</code> value482* before returning it to the database.483*484* @param x an object representing an SQL <code>BLOB</code> value485* @throws SQLException if the <code>SQLOutputImpl</code> object is in486* use by a <code>SQLData</code> object attempting to write the attribute487* values of a UDT to the database.488*/489@SuppressWarnings("unchecked")490public void writeBlob(Blob x) throws SQLException {491if (x == null) {492attribs.add(null);493} else {494attribs.add(new SerialBlob(x));495}496}497498/**499* Writes a <code>Clob</code> object in the Java programming language500* to this <code>SQLOutputImpl</code> object. The driver converts501* it to a serializable <code>SerialClob</code> SQL <code>CLOB</code> value502* before returning it to the database.503*504* @param x an object representing an SQL <code>CLOB</code> value505* @throws SQLException if the <code>SQLOutputImpl</code> object is in506* use by a <code>SQLData</code> object attempting to write the attribute507* values of a UDT to the database.508*/509@SuppressWarnings("unchecked")510public void writeClob(Clob x) throws SQLException {511if (x == null) {512attribs.add(null);513} else {514attribs.add(new SerialClob(x));515}516}517518/**519* Writes a <code>Struct</code> object in the Java520* programming language to this <code>SQLOutputImpl</code>521* object. The driver converts this value to an SQL structured type522* before returning it to the database.523* <P>524* This method should be used when an SQL structured type has been525* mapped to a <code>Struct</code> object in the Java programming526* language (the standard mapping). The method527* <code>writeObject</code> should be used if an SQL structured type528* has been custom mapped to a class in the Java programming language.529*530* @param x an object representing the attributes of an SQL structured type531* @throws SQLException if the <code>SQLOutputImpl</code> object is in532* use by a <code>SQLData</code> object attempting to write the attribute533* values of a UDT to the database.534*/535@SuppressWarnings("unchecked")536public void writeStruct(Struct x) throws SQLException {537SerialStruct s = new SerialStruct(x,map);;538attribs.add(s);539}540541/**542* Writes an <code>Array</code> object in the Java543* programming language to this <code>SQLOutputImpl</code>544* object. The driver converts this value to a serializable545* <code>SerialArray</code> SQL <code>ARRAY</code>546* value before returning it to the database.547*548* @param x an object representing an SQL <code>ARRAY</code> value549* @throws SQLException if the <code>SQLOutputImpl</code> object is in550* use by a <code>SQLData</code> object attempting to write the attribute551* values of a UDT to the database.552*/553@SuppressWarnings("unchecked")554public void writeArray(Array x) throws SQLException {555if (x == null) {556attribs.add(null);557} else {558attribs.add(new SerialArray(x, map));559}560}561562/**563* Writes an <code>java.sql.Type.DATALINK</code> object in the Java564* programming language to this <code>SQLOutputImpl</code> object. The565* driver converts this value to a serializable <code>SerialDatalink</code>566* SQL <code>DATALINK</code> value before return it to the database.567*568* @param url an object representing a SQL <code>DATALINK</code> value569* @throws SQLException if the <code>SQLOutputImpl</code> object is in570* use by a <code>SQLData</code> object attempting to write the attribute571* values of a UDT to the database.572*/573@SuppressWarnings("unchecked")574public void writeURL(java.net.URL url) throws SQLException {575if (url == null) {576attribs.add(null);577} else {578attribs.add(new SerialDatalink(url));579}580}581582583/**584* Writes the next attribute to the stream as a <code>String</code>585* in the Java programming language. The driver converts this to a586* SQL <code>NCHAR</code> or587* <code>NVARCHAR</code> or <code>LONGNVARCHAR</code> value588* (depending on the argument's589* size relative to the driver's limits on <code>NVARCHAR</code> values)590* when it sends it to the stream.591*592* @param x the value to pass to the database593* @exception SQLException if a database access error occurs594* @since 1.6595*/596@SuppressWarnings("unchecked")597public void writeNString(String x) throws SQLException {598attribs.add(x);599}600601/**602* Writes an SQL <code>NCLOB</code> value to the stream.603*604* @param x a <code>NClob</code> object representing data of an SQL605* <code>NCLOB</code> value606*607* @exception SQLException if a database access error occurs608* @since 1.6609*/610@SuppressWarnings("unchecked")611public void writeNClob(NClob x) throws SQLException {612attribs.add(x);613}614615616/**617* Writes an SQL <code>ROWID</code> value to the stream.618*619* @param x a <code>RowId</code> object representing data of an SQL620* <code>ROWID</code> value621*622* @exception SQLException if a database access error occurs623* @since 1.6624*/625@SuppressWarnings("unchecked")626public void writeRowId(RowId x) throws SQLException {627attribs.add(x);628}629630631/**632* Writes an SQL <code>XML</code> value to the stream.633*634* @param x a <code>SQLXML</code> object representing data of an SQL635* <code>XML</code> value636*637* @exception SQLException if a database access error occurs638* @since 1.6639*/640@SuppressWarnings("unchecked")641public void writeSQLXML(SQLXML x) throws SQLException {642attribs.add(x);643}644645}646647648