KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > sql > rowset > serial > SQLOutputImpl


1 /*
2  * @(#)SQLOutputImpl.java 1.5 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.sql.rowset.serial;
9
10 import java.sql.*;
11 import javax.sql.*;
12 import java.io.*;
13 import java.lang.String JavaDoc;
14 import java.math.*;
15 import java.util.Map JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 /**
19  * The output stream for writing the attributes of a
20  * custom-mapped user-defined type (UDT) back to the database.
21  * The driver uses this interface internally, and its
22  * methods are never directly invoked by an application programmer.
23  * <p>
24  * When an application calls the
25  * method <code>PreparedStatement.setObject</code>, the driver
26  * checks to see whether the value to be written is a UDT with
27  * a custom mapping. If it is, there will be an entry in a
28  * type map containing the <code>Class</code> object for the
29  * class that implements <code>SQLData</code> for this UDT.
30  * If the value to be written is an instance of <code>SQLData</code>,
31  * the driver will create an instance of <code>SQLOutputImpl</code>
32  * and pass it to the method <code>SQLData.writeSQL</code>.
33  * The method <code>writeSQL</code> in turn calls the
34  * appropriate <code>SQLOutputImpl.writeXXX</code> methods
35  * to write data from the <code>SQLData</code> object to
36  * the <code>SQLOutputImpl</code> output stream as the
37  * representation of an SQL user-defined type.
38  */

39 public class SQLOutputImpl implements SQLOutput {
40
41     /**
42      * A reference to an existing vector that
43      * contains the attributes of a <code>Struct</code> object.
44      */

45     private Vector JavaDoc attribs;
46
47     /**
48      * The type map the driver supplies to a newly created
49      * <code>SQLOutputImpl</code> object. This type map
50      * indicates the <code>SQLData</code> class whose
51      * <code>writeSQL</code> method will be called. This
52      * method will in turn call the appropriate
53      * <code>SQLOutputImpl/code> writer methods.
54      */

55     private Map JavaDoc map;
56
57     /**
58      * Creates a new <code>SQLOutputImpl</code> object
59      * initialized with the given vector of attributes and
60      * type map. The driver will use the type map to determine
61      * which <code>SQLData.writeSQL</code> method to invoke.
62      * This method will then call the appropriate
63      * <code>SQLOutputImpl</code> writer methods in order and
64      * thereby write the attributes to the new output stream.
65      *
66      * @param attributes a <code>Vector</code> object containing the attributes of
67      * the UDT to be mapped to one or more objects in the Java
68      * programming language
69      *
70      * @param map a <code>java.util.Map</code> object containing zero or
71      * more entries, with each entry consisting of 1) a <code>String</code>
72      * giving the fully qualified name of a UDT and 2) the
73      * <code>Class</code> object for the <code>SQLData</code> implementation
74      * that defines how the UDT is to be mapped
75      * @throws SQLException if the <code>attributes</code> or the <code>map</code>
76      * is a <code>null</code> value
77      */

78     public SQLOutputImpl(Vector JavaDoc<?> attributes, Map JavaDoc<String JavaDoc,?> map)
79     throws SQLException
80     {
81         if ((attributes == null) || (map == null)) {
82             throw new SQLException("Cannot instantiate a SQLOutputImpl " +
83             "instance with null parameters");
84         }
85         this.attribs = attributes;
86         this.map = map;
87     }
88
89     //================================================================
90
// Methods for writing attributes to the stream of SQL data.
91
// These methods correspond to the column-accessor methods of
92
// java.sql.ResultSet.
93
//================================================================
94

95     /**
96      * Writes a <code>String</code> in the Java programming language
97      * to this <code>SQLOutputImpl</code> object. The driver converts
98      * it to an SQL <code>CHAR</code>, <code>VARCHAR</code>, or
99      * <code>LONGVARCHAR</code> before returning it to the database.
100      *
101      * @param x the value to pass to the database
102      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
103      * use by a <code>SQLData</code> object attempting to write the attribute
104      * values of a UDT to the database.
105      */

106     public void writeString(String JavaDoc x) throws SQLException {
107         //System.out.println("Adding :"+x);
108
attribs.add(x);
109     }
110
111     /**
112      * Writes a <code>boolean</code> in the Java programming language
113      * to this <code>SQLOutputImpl</code> object. The driver converts
114      * it to an SQL <code>BIT</code> before returning it to the database.
115      *
116      * @param x the value to pass to the database
117      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
118      * use by a <code>SQLData</code> object attempting to write the attribute
119      * values of a UDT to the database.
120      */

121     public void writeBoolean(boolean x) throws SQLException {
122         attribs.add(new Boolean JavaDoc(x));
123     }
124
125     /**
126      * Writes a <code>byte</code> in the Java programming language
127      * to this <code>SQLOutputImpl</code> object. The driver converts
128      * it to an SQL <code>BIT</code> before returning it to the database.
129      *
130      * @param x the value to pass to the database
131      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
132      * use by a <code>SQLData</code> object attempting to write the attribute
133      * values of a UDT to the database.
134      */

135     public void writeByte(byte x) throws SQLException {
136         attribs.add(new Byte JavaDoc(x));
137     }
138
139     /**
140      * Writes a <code>short</code> in the Java programming language
141      * to this <code>SQLOutputImpl</code> object. The driver converts
142      * it to an SQL <code>SMALLINT</code> before returning it to the database.
143      *
144      * @param x the value to pass to the database
145      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
146      * use by a <code>SQLData</code> object attempting to write the attribute
147      * values of a UDT to the database.
148      */

149     public void writeShort(short x) throws SQLException {
150         attribs.add(new Short JavaDoc(x));
151     }
152
153     /**
154      * Writes an <code>int</code> in the Java programming language
155      * to this <code>SQLOutputImpl</code> object. The driver converts
156      * it to an SQL <code>INTEGER</code> before returning it to the database.
157      *
158      * @param x the value to pass to the database
159      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
160      * use by a <code>SQLData</code> object attempting to write the attribute
161      * values of a UDT to the database.
162      */

163     public void writeInt(int x) throws SQLException {
164         attribs.add(new Integer JavaDoc(x));
165     }
166
167     /**
168      * Writes a <code>long</code> in the Java programming language
169      * to this <code>SQLOutputImpl</code> object. The driver converts
170      * it to an SQL <code>BIGINT</code> before returning it to the database.
171      *
172      * @param x the value to pass to the database
173      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
174      * use by a <code>SQLData</code> object attempting to write the attribute
175      * values of a UDT to the database.
176      */

177     public void writeLong(long x) throws SQLException {
178         attribs.add(new Long JavaDoc(x));
179     }
180
181     /**
182      * Writes a <code>float</code> in the Java programming language
183      * to this <code><code>SQLOutputImpl</code></code> object. The driver converts
184      * it to an SQL <code>REAL</code> before returning it to the database.
185      *
186      * @param x the value to pass to the database
187      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
188      * use by a <code>SQLData</code> object attempting to write the attribute
189      * values of a UDT to the database.
190      */

191     public void writeFloat(float x) throws SQLException {
192         attribs.add(new Float JavaDoc(x));
193     }
194
195     /**
196      * Writes a <code>double</code> in the Java programming language
197      * to this <code>SQLOutputImpl</code> object. The driver converts
198      * it to an SQL <code>DOUBLE</code> before returning it to the database.
199      *
200      * @param x the value to pass to the database
201      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
202      * use by a <code>SQLData</code> object attempting to write the attribute
203      * values of a UDT to the database.
204      */

205     public void writeDouble(double x) throws SQLException{
206         attribs.add(new Double JavaDoc(x));
207     }
208
209     /**
210      * Writes a <code>java.math.BigDecimal</code> object in the Java programming
211      * language to this <code>SQLOutputImpl</code> object. The driver converts
212      * it to an SQL <code>NUMERIC</code> before returning it to the database.
213      *
214      * @param x the value to pass to the database
215      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
216      * use by a <code>SQLData</code> object attempting to write the attribute
217      * values of a UDT to the database.
218      */

219     public void writeBigDecimal(java.math.BigDecimal JavaDoc x) throws SQLException{
220         attribs.add(x);
221     }
222
223     /**
224      * Writes an array of <code>bytes</code> in the Java programming language
225      * to this <code>SQLOutputImpl</code> object. The driver converts
226      * it to an SQL <code>VARBINARY</code> or <code>LONGVARBINARY</code>
227      * before returning it to the database.
228      *
229      * @param x the value to pass to the database
230      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
231      * use by a <code>SQLData</code> object attempting to write the attribute
232      * values of a UDT to the database.
233      */

234     public void writeBytes(byte[] x) throws SQLException {
235         attribs.add(x);
236     }
237
238     /**
239      * Writes a <code>java.sql.Date</code> object in the Java programming
240      * language to this <code>SQLOutputImpl</code> object. The driver converts
241      * it to an SQL <code>DATE</code> before returning it to the database.
242      *
243      * @param x the value to pass to the database
244      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
245      * use by a <code>SQLData</code> object attempting to write the attribute
246      * values of a UDT to the database.
247      */

248     public void writeDate(java.sql.Date JavaDoc x) throws SQLException {
249         attribs.add(x);
250     }
251
252     /**
253      * Writes a <code>java.sql.Time</code> object in the Java programming
254      * language to this <code>SQLOutputImpl</code> object. The driver converts
255      * it to an SQL <code>TIME</code> before returning it to the database.
256      *
257      * @param x the value to pass to the database
258      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
259      * use by a <code>SQLData</code> object attempting to write the attribute
260      * values of a UDT to the database.
261      */

262     public void writeTime(java.sql.Time JavaDoc x) throws SQLException {
263         attribs.add(x);
264     }
265
266     /**
267      * Writes a <code>java.sql.Timestamp</code> object in the Java programming
268      * language to this <code>SQLOutputImpl</code> object. The driver converts
269      * it to an SQL <code>TIMESTAMP</code> before returning it to the database.
270      *
271      * @param x the value to pass to the database
272      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
273      * use by a <code>SQLData</code> object attempting to write the attribute
274      * values of a UDT to the database.
275      */

276     public void writeTimestamp(java.sql.Timestamp JavaDoc x) throws SQLException {
277         attribs.add(x);
278     }
279
280     /**
281      * Writes a stream of Unicode characters to this
282      * <code>SQLOutputImpl</code> object. The driver will do any necessary
283      * conversion from Unicode to the database <code>CHAR</code> format.
284      *
285      * @param x the value to pass to the database
286      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
287      * use by a <code>SQLData</code> object attempting to write the attribute
288      * values of a UDT to the database.
289      */

290     public void writeCharacterStream(java.io.Reader JavaDoc x) throws SQLException {
291          BufferedReader bufReader = new BufferedReader(x);
292          try {
293              int i;
294              while( (i = bufReader.read()) != -1 ) {
295                 char ch = (char)i;
296         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
297         strBuf.append(ch);
298         
299         String JavaDoc str = new String JavaDoc(strBuf);
300                 String JavaDoc strLine = bufReader.readLine();
301                 
302                 writeString(str.concat(strLine));
303              }
304          } catch(IOException ioe) {
305          
306          }
307     }
308
309     /**
310      * Writes a stream of ASCII characters to this
311      * <code>SQLOutputImpl</code> object. The driver will do any necessary
312      * conversion from ASCII to the database <code>CHAR</code> format.
313      *
314      * @param x the value to pass to the database
315      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
316      * use by a <code>SQLData</code> object attempting to write the attribute
317      * values of a UDT to the database.
318      */

319     public void writeAsciiStream(java.io.InputStream JavaDoc x) throws SQLException {
320          BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));
321          try {
322                int i;
323                while( (i=bufReader.read()) != -1 ) {
324                 char ch = (char)i;
325                
326         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
327         strBuf.append(ch);
328         
329         String JavaDoc str = new String JavaDoc(strBuf);
330                 String JavaDoc strLine = bufReader.readLine();
331                 
332                 writeString(str.concat(strLine));
333             }
334           }catch(IOException ioe) {
335             throw new SQLException(ioe.getMessage());
336         }
337     }
338
339     /**
340      * Writes a stream of uninterpreted bytes to this <code>SQLOutputImpl</code>
341      * object.
342      *
343      * @param x the value to pass to the database
344      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
345      * use by a <code>SQLData</code> object attempting to write the attribute
346      * values of a UDT to the database.
347      */

348     public void writeBinaryStream(java.io.InputStream JavaDoc x) throws SQLException {
349          BufferedReader bufReader = new BufferedReader(new InputStreamReader(x));
350          try {
351                int i;
352              while( (i=bufReader.read()) != -1 ) {
353                 char ch = (char)i;
354                
355         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
356         strBuf.append(ch);
357         
358         String JavaDoc str = new String JavaDoc(strBuf);
359                 String JavaDoc strLine = bufReader.readLine();
360                 
361                 writeString(str.concat(strLine));
362              }
363         } catch(IOException ioe) {
364             throw new SQLException(ioe.getMessage());
365         }
366     }
367     
368     //================================================================
369
// Methods for writing items of SQL user-defined types to the stream.
370
// These methods pass objects to the database as values of SQL
371
// Structured Types, Distinct Types, Constructed Types, and Locator
372
// Types. They decompose the Java object(s) and write leaf data
373
// items using the methods above.
374
//================================================================
375

376     /**
377      * Writes to the stream the data contained in the given
378      * <code>SQLData</code> object.
379      * When the <code>SQLData</code> object is <code>null</code>, this
380      * method writes an SQL <code>NULL</code> to the stream.
381      * Otherwise, it calls the <code>SQLData.writeSQL</code>
382      * method of the given object, which
383      * writes the object's attributes to the stream.
384      * <P>
385      * The implementation of the method <code>SQLData.writeSQ</code>
386      * calls the appropriate <code>SQLOutputImpl.writeXXX</code> method(s)
387      * for writing each of the object's attributes in order.
388      * The attributes must be read from an <code>SQLInput</code>
389      * input stream and written to an <code>SQLOutputImpl</code>
390      * output stream in the same order in which they were
391      * listed in the SQL definition of the user-defined type.
392      *
393      * @param x the object representing data of an SQL structured or
394      * distinct type
395      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
396      * use by a <code>SQLData</code> object attempting to write the attribute
397      * values of a UDT to the database.
398      */

399     public void writeObject(SQLData x) throws SQLException {
400         
401         /*
402          * Except for the types that are passed as objects
403          * this seems to be the only way for an object to
404          * get a null value for a field in a structure.
405          *
406          * Note: this means that the class defining SQLData
407          * will need to track if a field is SQL null for itself
408          */

409         if (x == null) {
410             attribs.add(x);
411             return;
412         }
413         
414         /*
415          * We have to write out a SerialStruct that contains
416          * the name of this class otherwise we don't know
417          * what to re-instantiate during readSQL()
418          */

419         attribs.add(new SerialStruct JavaDoc((SQLData)x, map));
420     }
421
422     /**
423      * Writes a <code>Ref</code> object in the Java programming language
424      * to this <code>SQLOutputImpl</code> object. The driver converts
425      * it to a serializable <code>SerialRef</code> SQL <code>REF</code> value
426      * before returning it to the database.
427      *
428      * @param x an object representing an SQL <code>REF</code> value
429      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
430      * use by a <code>SQLData</code> object attempting to write the attribute
431      * values of a UDT to the database.
432      */

433     public void writeRef(Ref x) throws SQLException {
434         if (x == null) {
435             attribs.add(x);
436             return;
437         }
438         attribs.add(new SerialRef JavaDoc(x));
439     }
440
441     /**
442      * Writes a <code>Blob</code> object in the Java programming language
443      * to this <code>SQLOutputImpl</code> object. The driver converts
444      * it to a serializable <code>SerialBlob</code> SQL <code>BLOB</code> value
445      * before returning it to the database.
446      *
447      * @param x an object representing an SQL <code>BLOB</code> value
448      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
449      * use by a <code>SQLData</code> object attempting to write the attribute
450      * values of a UDT to the database.
451      */

452     public void writeBlob(Blob x) throws SQLException {
453         if (x == null) {
454             attribs.add(x);
455             return;
456         }
457         attribs.add(new SerialBlob JavaDoc(x));
458     }
459     
460     /**
461      * Writes a <code>Clob</code> object in the Java programming language
462      * to this <code>SQLOutputImpl</code> object. The driver converts
463      * it to a serializable <code>SerialClob</code> SQL <code>CLOB</code> value
464      * before returning it to the database.
465      *
466      * @param x an object representing an SQL <code>CLOB</code> value
467      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
468      * use by a <code>SQLData</code> object attempting to write the attribute
469      * values of a UDT to the database.
470      */

471     public void writeClob(Clob x) throws SQLException {
472         if (x == null) {
473             attribs.add(x);
474             return;
475         }
476         attribs.add(new SerialClob JavaDoc(x));
477     }
478
479     /**
480      * Writes a <code>Struct</code> object in the Java
481      * programming language to this <code>SQLOutputImpl</code>
482      * object. The driver converts this value to an SQL structured type
483      * before returning it to the database.
484      * <P>
485      * This method should be used when an SQL structured type has been
486      * mapped to a <code>Struct</code> object in the Java programming
487      * language (the standard mapping). The method
488      * <code>writeObject</code> should be used if an SQL structured type
489      * has been custom mapped to a class in the Java programming language.
490      *
491      * @param x an object representing the attributes of an SQL structured type
492      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
493      * use by a <code>SQLData</code> object attempting to write the attribute
494      * values of a UDT to the database.
495      */

496     public void writeStruct(Struct x) throws SQLException {
497         SerialStruct JavaDoc s = new SerialStruct JavaDoc(x,map);;
498         attribs.add(s);
499     }
500     
501     /**
502      * Writes an <code>Array</code> object in the Java
503      * programming language to this <code>SQLOutputImpl</code>
504      * object. The driver converts this value to a serializable
505      * <code>SerialArray</code> SQL <code>ARRAY</code>
506      * value before returning it to the database.
507      *
508      * @param x an object representing an SQL <code>ARRAY</code> value
509      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
510      * use by a <code>SQLData</code> object attempting to write the attribute
511      * values of a UDT to the database.
512      */

513     public void writeArray(Array x) throws SQLException {
514         if (x == null) {
515             attribs.add(x);
516             return;
517         }
518         attribs.add(new SerialArray JavaDoc(x, map));
519     }
520
521     /**
522      * Writes an <code>java.sql.Type.DATALINK</code> object in the Java
523      * programming language to this <code>SQLOutputImpl</code> object. The
524      * driver converts this value to a serializable <code>SerialDatalink</code>
525      * SQL <code>DATALINK</code> value before return it to the database.
526      *
527      * @param url an object representing a SQL <code>DATALINK</code> value
528      * @throws SQLException if the <code>SQLOutputImpl</code> object is in
529      * use by a <code>SQLData</code> object attempting to write the attribute
530      * values of a UDT to the database.
531      */

532     public void writeURL(java.net.URL JavaDoc url) throws SQLException {
533         if (url == null) {
534             attribs.add(url);
535             return;
536         }
537         attribs.add(new SerialDatalink JavaDoc(url));
538         
539     }
540 }
541
542
543
544
545
546
Popular Tags