Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/serial/SQLOutputImpl.java
38918 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*/55public class SQLOutputImpl implements SQLOutput {5657/**58* A reference to an existing vector that59* contains the attributes of a <code>Struct</code> object.60*/61@SuppressWarnings("rawtypes")62private Vector attribs;6364/**65* The type map the driver supplies to a newly created66* <code>SQLOutputImpl</code> object. This type map67* indicates the <code>SQLData</code> class whose68* <code>writeSQL</code> method will be called. This69* method will in turn call the appropriate70* <code>SQLOutputImpl</code> writer methods.71*/72@SuppressWarnings("rawtypes")73private Map map;7475/**76* Creates a new <code>SQLOutputImpl</code> object77* initialized with the given vector of attributes and78* type map. The driver will use the type map to determine79* which <code>SQLData.writeSQL</code> method to invoke.80* This method will then call the appropriate81* <code>SQLOutputImpl</code> writer methods in order and82* thereby write the attributes to the new output stream.83*84* @param attributes a <code>Vector</code> object containing the attributes of85* the UDT to be mapped to one or more objects in the Java86* programming language87*88* @param map a <code>java.util.Map</code> object containing zero or89* more entries, with each entry consisting of 1) a <code>String</code>90* giving the fully qualified name of a UDT and 2) the91* <code>Class</code> object for the <code>SQLData</code> implementation92* that defines how the UDT is to be mapped93* @throws SQLException if the <code>attributes</code> or the <code>map</code>94* is a <code>null</code> value95*/96public SQLOutputImpl(Vector<?> attributes, Map<String,?> map)97throws SQLException98{99if ((attributes == null) || (map == null)) {100throw new SQLException("Cannot instantiate a SQLOutputImpl " +101"instance with null parameters");102}103this.attribs = attributes;104this.map = map;105}106107//================================================================108// Methods for writing attributes to the stream of SQL data.109// These methods correspond to the column-accessor methods of110// java.sql.ResultSet.111//================================================================112113/**114* Writes a <code>String</code> in the Java programming language115* to this <code>SQLOutputImpl</code> object. The driver converts116* it to an SQL <code>CHAR</code>, <code>VARCHAR</code>, or117* <code>LONGVARCHAR</code> before returning it to the database.118*119* @param x the value to pass to the database120* @throws SQLException if the <code>SQLOutputImpl</code> object is in121* use by a <code>SQLData</code> object attempting to write the attribute122* values of a UDT to the database.123*/124@SuppressWarnings("unchecked")125public void writeString(String x) throws SQLException {126//System.out.println("Adding :"+x);127attribs.add(x);128}129130/**131* Writes a <code>boolean</code> in the Java programming language132* to this <code>SQLOutputImpl</code> object. The driver converts133* it to an SQL <code>BIT</code> before returning it to the database.134*135* @param x the value to pass to the database136* @throws SQLException if the <code>SQLOutputImpl</code> object is in137* use by a <code>SQLData</code> object attempting to write the attribute138* values of a UDT to the database.139*/140@SuppressWarnings("unchecked")141public void writeBoolean(boolean x) throws SQLException {142attribs.add(Boolean.valueOf(x));143}144145/**146* Writes a <code>byte</code> in the Java programming language147* to this <code>SQLOutputImpl</code> object. The driver converts148* it to an SQL <code>BIT</code> before returning it to the database.149*150* @param x the value to pass to the database151* @throws SQLException if the <code>SQLOutputImpl</code> object is in152* use by a <code>SQLData</code> object attempting to write the attribute153* values of a UDT to the database.154*/155@SuppressWarnings("unchecked")156public void writeByte(byte x) throws SQLException {157attribs.add(Byte.valueOf(x));158}159160/**161* Writes a <code>short</code> in the Java programming language162* to this <code>SQLOutputImpl</code> object. The driver converts163* it to an SQL <code>SMALLINT</code> before returning it to the database.164*165* @param x the value to pass to the database166* @throws SQLException if the <code>SQLOutputImpl</code> object is in167* use by a <code>SQLData</code> object attempting to write the attribute168* values of a UDT to the database.169*/170@SuppressWarnings("unchecked")171public void writeShort(short x) throws SQLException {172attribs.add(Short.valueOf(x));173}174175/**176* Writes an <code>int</code> in the Java programming language177* to this <code>SQLOutputImpl</code> object. The driver converts178* it to an SQL <code>INTEGER</code> before returning it to the database.179*180* @param x the value to pass to the database181* @throws SQLException if the <code>SQLOutputImpl</code> object is in182* use by a <code>SQLData</code> object attempting to write the attribute183* values of a UDT to the database.184*/185@SuppressWarnings("unchecked")186public void writeInt(int x) throws SQLException {187attribs.add(Integer.valueOf(x));188}189190/**191* Writes a <code>long</code> in the Java programming language192* to this <code>SQLOutputImpl</code> object. The driver converts193* it to an SQL <code>BIGINT</code> before returning it to the database.194*195* @param x the value to pass to the database196* @throws SQLException if the <code>SQLOutputImpl</code> object is in197* use by a <code>SQLData</code> object attempting to write the attribute198* values of a UDT to the database.199*/200@SuppressWarnings("unchecked")201public void writeLong(long x) throws SQLException {202attribs.add(Long.valueOf(x));203}204205/**206* Writes a <code>float</code> in the Java programming language207* to this <code>SQLOutputImpl</code> object. The driver converts208* it to an SQL <code>REAL</code> before returning it to the database.209*210* @param x the value to pass to the database211* @throws SQLException if the <code>SQLOutputImpl</code> object is in212* use by a <code>SQLData</code> object attempting to write the attribute213* values of a UDT to the database.214*/215@SuppressWarnings("unchecked")216public void writeFloat(float x) throws SQLException {217attribs.add(Float.valueOf(x));218}219220/**221* Writes a <code>double</code> in the Java programming language222* to this <code>SQLOutputImpl</code> object. The driver converts223* it to an SQL <code>DOUBLE</code> before returning it to the database.224*225* @param x the value to pass to the database226* @throws SQLException if the <code>SQLOutputImpl</code> object is in227* use by a <code>SQLData</code> object attempting to write the attribute228* values of a UDT to the database.229*/230@SuppressWarnings("unchecked")231public void writeDouble(double x) throws SQLException{232attribs.add(Double.valueOf(x));233}234235/**236* Writes a <code>java.math.BigDecimal</code> object in the Java programming237* language to this <code>SQLOutputImpl</code> object. The driver converts238* it to an SQL <code>NUMERIC</code> before returning it to the database.239*240* @param x the value to pass to the database241* @throws SQLException if the <code>SQLOutputImpl</code> object is in242* use by a <code>SQLData</code> object attempting to write the attribute243* values of a UDT to the database.244*/245@SuppressWarnings("unchecked")246public void writeBigDecimal(java.math.BigDecimal x) throws SQLException{247attribs.add(x);248}249250/**251* Writes an array of <code>bytes</code> in the Java programming language252* to this <code>SQLOutputImpl</code> object. The driver converts253* it to an SQL <code>VARBINARY</code> or <code>LONGVARBINARY</code>254* before returning it to the database.255*256* @param x the value to pass to the database257* @throws SQLException if the <code>SQLOutputImpl</code> object is in258* use by a <code>SQLData</code> object attempting to write the attribute259* values of a UDT to the database.260*/261@SuppressWarnings("unchecked")262public void writeBytes(byte[] x) throws SQLException {263attribs.add(x);264}265266/**267* Writes a <code>java.sql.Date</code> object in the Java programming268* language to this <code>SQLOutputImpl</code> object. The driver converts269* it to an SQL <code>DATE</code> before returning it to the database.270*271* @param x the value to pass to the database272* @throws SQLException if the <code>SQLOutputImpl</code> object is in273* use by a <code>SQLData</code> object attempting to write the attribute274* values of a UDT to the database.275*/276@SuppressWarnings("unchecked")277public void writeDate(java.sql.Date x) throws SQLException {278attribs.add(x);279}280281/**282* Writes a <code>java.sql.Time</code> object in the Java programming283* language to this <code>SQLOutputImpl</code> object. The driver converts284* it to an SQL <code>TIME</code> before returning it to the database.285*286* @param x the value to pass to the database287* @throws SQLException if the <code>SQLOutputImpl</code> object is in288* use by a <code>SQLData</code> object attempting to write the attribute289* values of a UDT to the database.290*/291@SuppressWarnings("unchecked")292public void writeTime(java.sql.Time x) throws SQLException {293attribs.add(x);294}295296/**297* Writes a <code>java.sql.Timestamp</code> object in the Java programming298* language to this <code>SQLOutputImpl</code> object. The driver converts299* it to an SQL <code>TIMESTAMP</code> before returning it to the database.300*301* @param x the value to pass to the database302* @throws SQLException if the <code>SQLOutputImpl</code> object is in303* use by a <code>SQLData</code> object attempting to write the attribute304* values of a UDT to the database.305*/306@SuppressWarnings("unchecked")307public void writeTimestamp(java.sql.Timestamp x) throws SQLException {308attribs.add(x);309}310311/**312* Writes a stream of Unicode characters to this313* <code>SQLOutputImpl</code> object. The driver will do any necessary314* conversion from Unicode to the database <code>CHAR</code> format.315*316* @param x the value to pass to the database317* @throws SQLException if the <code>SQLOutputImpl</code> object is in318* use by a <code>SQLData</code> object attempting to write the attribute319* values of a UDT to the database.320*/321@SuppressWarnings("unchecked")322public void writeCharacterStream(java.io.Reader x) throws SQLException {323BufferedReader bufReader = new BufferedReader(x);324try {325int i;326while( (i = bufReader.read()) != -1 ) {327char ch = (char)i;328StringBuffer strBuf = new StringBuffer();329strBuf.append(ch);330331String str = new String(strBuf);332String strLine = bufReader.readLine();333334writeString(str.concat(strLine));335}336} catch(IOException ioe) {337338}339}340341/**342* Writes a stream of ASCII characters to this343* <code>SQLOutputImpl</code> object. The driver will do any necessary344* conversion from ASCII to the database <code>CHAR</code> format.345*346* @param x the value to pass to the database347* @throws SQLException if the <code>SQLOutputImpl</code> object is in348* use by a <code>SQLData</code> object attempting to write the attribute349* values of a UDT to the database.350*/351@SuppressWarnings("unchecked")352public void writeAsciiStream(java.io.InputStream x) throws SQLException {353BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));354try {355int i;356while( (i=bufReader.read()) != -1 ) {357char ch = (char)i;358359StringBuffer strBuf = new StringBuffer();360strBuf.append(ch);361362String str = new String(strBuf);363String strLine = bufReader.readLine();364365writeString(str.concat(strLine));366}367}catch(IOException ioe) {368throw new SQLException(ioe.getMessage());369}370}371372/**373* Writes a stream of uninterpreted bytes to this <code>SQLOutputImpl</code>374* object.375*376* @param x the value to pass to the database377* @throws SQLException if the <code>SQLOutputImpl</code> object is in378* use by a <code>SQLData</code> object attempting to write the attribute379* values of a UDT to the database.380*/381@SuppressWarnings("unchecked")382public void writeBinaryStream(java.io.InputStream x) throws SQLException {383BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));384try {385int i;386while( (i=bufReader.read()) != -1 ) {387char ch = (char)i;388389StringBuffer strBuf = new StringBuffer();390strBuf.append(ch);391392String str = new String(strBuf);393String strLine = bufReader.readLine();394395writeString(str.concat(strLine));396}397} catch(IOException ioe) {398throw new SQLException(ioe.getMessage());399}400}401402//================================================================403// Methods for writing items of SQL user-defined types to the stream.404// These methods pass objects to the database as values of SQL405// Structured Types, Distinct Types, Constructed Types, and Locator406// Types. They decompose the Java object(s) and write leaf data407// items using the methods above.408//================================================================409410/**411* Writes to the stream the data contained in the given412* <code>SQLData</code> object.413* When the <code>SQLData</code> object is <code>null</code>, this414* method writes an SQL <code>NULL</code> to the stream.415* Otherwise, it calls the <code>SQLData.writeSQL</code>416* method of the given object, which417* writes the object's attributes to the stream.418* <P>419* The implementation of the method <code>SQLData.writeSQ</code>420* calls the appropriate <code>SQLOutputImpl.writeXXX</code> method(s)421* for writing each of the object's attributes in order.422* The attributes must be read from an <code>SQLInput</code>423* input stream and written to an <code>SQLOutputImpl</code>424* output stream in the same order in which they were425* listed in the SQL definition of the user-defined type.426*427* @param x the object representing data of an SQL structured or428* distinct type429* @throws SQLException if the <code>SQLOutputImpl</code> object is in430* use by a <code>SQLData</code> object attempting to write the attribute431* values of a UDT to the database.432*/433@SuppressWarnings("unchecked")434public void writeObject(SQLData x) throws SQLException {435436/*437* Except for the types that are passed as objects438* this seems to be the only way for an object to439* get a null value for a field in a structure.440*441* Note: this means that the class defining SQLData442* will need to track if a field is SQL null for itself443*/444if (x == null) {445attribs.add(null);446} else {447/*448* We have to write out a SerialStruct that contains449* the name of this class otherwise we don't know450* what to re-instantiate during readSQL()451*/452attribs.add(new SerialStruct(x, map));453}454}455456/**457* Writes a <code>Ref</code> object in the Java programming language458* to this <code>SQLOutputImpl</code> object. The driver converts459* it to a serializable <code>SerialRef</code> SQL <code>REF</code> value460* before returning it to the database.461*462* @param x an object representing an SQL <code>REF</code> value463* @throws SQLException if the <code>SQLOutputImpl</code> object is in464* use by a <code>SQLData</code> object attempting to write the attribute465* values of a UDT to the database.466*/467@SuppressWarnings("unchecked")468public void writeRef(Ref x) throws SQLException {469if (x == null) {470attribs.add(null);471} else {472attribs.add(new SerialRef(x));473}474}475476/**477* Writes a <code>Blob</code> object in the Java programming language478* to this <code>SQLOutputImpl</code> object. The driver converts479* it to a serializable <code>SerialBlob</code> SQL <code>BLOB</code> value480* before returning it to the database.481*482* @param x an object representing an SQL <code>BLOB</code> value483* @throws SQLException if the <code>SQLOutputImpl</code> object is in484* use by a <code>SQLData</code> object attempting to write the attribute485* values of a UDT to the database.486*/487@SuppressWarnings("unchecked")488public void writeBlob(Blob x) throws SQLException {489if (x == null) {490attribs.add(null);491} else {492attribs.add(new SerialBlob(x));493}494}495496/**497* Writes a <code>Clob</code> object in the Java programming language498* to this <code>SQLOutputImpl</code> object. The driver converts499* it to a serializable <code>SerialClob</code> SQL <code>CLOB</code> value500* before returning it to the database.501*502* @param x an object representing an SQL <code>CLOB</code> value503* @throws SQLException if the <code>SQLOutputImpl</code> object is in504* use by a <code>SQLData</code> object attempting to write the attribute505* values of a UDT to the database.506*/507@SuppressWarnings("unchecked")508public void writeClob(Clob x) throws SQLException {509if (x == null) {510attribs.add(null);511} else {512attribs.add(new SerialClob(x));513}514}515516/**517* Writes a <code>Struct</code> object in the Java518* programming language to this <code>SQLOutputImpl</code>519* object. The driver converts this value to an SQL structured type520* before returning it to the database.521* <P>522* This method should be used when an SQL structured type has been523* mapped to a <code>Struct</code> object in the Java programming524* language (the standard mapping). The method525* <code>writeObject</code> should be used if an SQL structured type526* has been custom mapped to a class in the Java programming language.527*528* @param x an object representing the attributes of an SQL structured type529* @throws SQLException if the <code>SQLOutputImpl</code> object is in530* use by a <code>SQLData</code> object attempting to write the attribute531* values of a UDT to the database.532*/533@SuppressWarnings("unchecked")534public void writeStruct(Struct x) throws SQLException {535SerialStruct s = new SerialStruct(x,map);;536attribs.add(s);537}538539/**540* Writes an <code>Array</code> object in the Java541* programming language to this <code>SQLOutputImpl</code>542* object. The driver converts this value to a serializable543* <code>SerialArray</code> SQL <code>ARRAY</code>544* value before returning it to the database.545*546* @param x an object representing an SQL <code>ARRAY</code> value547* @throws SQLException if the <code>SQLOutputImpl</code> object is in548* use by a <code>SQLData</code> object attempting to write the attribute549* values of a UDT to the database.550*/551@SuppressWarnings("unchecked")552public void writeArray(Array x) throws SQLException {553if (x == null) {554attribs.add(null);555} else {556attribs.add(new SerialArray(x, map));557}558}559560/**561* Writes an <code>java.sql.Type.DATALINK</code> object in the Java562* programming language to this <code>SQLOutputImpl</code> object. The563* driver converts this value to a serializable <code>SerialDatalink</code>564* SQL <code>DATALINK</code> value before return it to the database.565*566* @param url an object representing a SQL <code>DATALINK</code> value567* @throws SQLException if the <code>SQLOutputImpl</code> object is in568* use by a <code>SQLData</code> object attempting to write the attribute569* values of a UDT to the database.570*/571@SuppressWarnings("unchecked")572public void writeURL(java.net.URL url) throws SQLException {573if (url == null) {574attribs.add(null);575} else {576attribs.add(new SerialDatalink(url));577}578}579580581/**582* Writes the next attribute to the stream as a <code>String</code>583* in the Java programming language. The driver converts this to a584* SQL <code>NCHAR</code> or585* <code>NVARCHAR</code> or <code>LONGNVARCHAR</code> value586* (depending on the argument's587* size relative to the driver's limits on <code>NVARCHAR</code> values)588* when it sends it to the stream.589*590* @param x the value to pass to the database591* @exception SQLException if a database access error occurs592* @since 1.6593*/594@SuppressWarnings("unchecked")595public void writeNString(String x) throws SQLException {596attribs.add(x);597}598599/**600* Writes an SQL <code>NCLOB</code> value to the stream.601*602* @param x a <code>NClob</code> object representing data of an SQL603* <code>NCLOB</code> value604*605* @exception SQLException if a database access error occurs606* @since 1.6607*/608@SuppressWarnings("unchecked")609public void writeNClob(NClob x) throws SQLException {610attribs.add(x);611}612613614/**615* Writes an SQL <code>ROWID</code> value to the stream.616*617* @param x a <code>RowId</code> object representing data of an SQL618* <code>ROWID</code> value619*620* @exception SQLException if a database access error occurs621* @since 1.6622*/623@SuppressWarnings("unchecked")624public void writeRowId(RowId x) throws SQLException {625attribs.add(x);626}627628629/**630* Writes an SQL <code>XML</code> value to the stream.631*632* @param x a <code>SQLXML</code> object representing data of an SQL633* <code>XML</code> value634*635* @exception SQLException if a database access error occurs636* @since 1.6637*/638@SuppressWarnings("unchecked")639public void writeSQLXML(SQLXML x) throws SQLException {640attribs.add(x);641}642643}644645646