KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > JdbcTypesConverter


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.util.IOUtils;
19
20 import java.io.Closeable JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.sql.Blob JavaDoc;
25 import java.sql.Clob JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Time JavaDoc;
30 import java.sql.Timestamp JavaDoc;
31 import java.sql.Types JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Calendar JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * Represents a converter for prepared statement parameters and result set columns.
39  * <p>This class defines a strategy for handling specific parameters like {@link URL}
40  * and by default provides a generic behaviour for any objects.
41  * <p>Configuration by exception is the general philosophy of this class, i.e.
42  * most of the conversions must be performed by a provided resultset/preparedstatement and
43  * custom conversions are applied only in rare cases. One of these cases is BLOB/CLOB handling.
44  * <p>Specific adapters of JDBC drivers may provide a subclass of this class to
45  * allow custom conversion conforming with the general contract of this class.
46  *
47  * @author Fyodor Kupolov
48  * @version 1.0
49  */

50 class JdbcTypesConverter implements Closeable JavaDoc {
51     private List JavaDoc<Closeable JavaDoc> resources;
52
53     /**
54      * Gets the value of the designated column in the current row of this ResultSet
55      * object as an Object in the Java programming language.
56      *
57      * @param rs
58      * @param index column index.
59      * @param jdbcType column {@link java.sql.Types JDBC type}
60      * @return
61      * @throws SQLException
62      * @see ResultSet#getObject(int)
63      */

64     public Object JavaDoc getObject(final ResultSet JavaDoc rs, final int index, final int jdbcType) throws SQLException JavaDoc {
65         switch(jdbcType) {
66             case Types.DATE: //For date/timestamp use getTimestamp to keep hh,mm,ss if possible
67
case Types.TIMESTAMP:
68                 return rs.getTimestamp(index);
69             case Types.TIME:
70                 return rs.getTime(index);
71             case Types.BLOB:
72                 return rs.getBlob(index);
73             case Types.CLOB:
74                 return rs.getClob(index);
75             case Types.LONGVARBINARY:
76                 InputStream JavaDoc is = rs.getBinaryStream(index);
77                 return is==null?null:toBlob(is);
78             case Types.LONGVARCHAR:
79                 Reader JavaDoc reader = rs.getCharacterStream(index);
80                 return reader==null?null:toClob(reader);
81         }
82         Object JavaDoc res = rs.getObject(index);
83         if (res==null) {
84             return null;
85         }
86         return res;
87     }
88
89
90     /**
91      * Sets the value of the designated parameter using the given object.
92      * <p>Depending on the value type the concrete subclass of JdbcTypesConverter is chosen.
93      *
94      * @param preparedStatement prepared statement to set object.
95      * @param index he first parameter is 1, the second is 2, ...
96      * @param value the object containing the input parameter value
97      * @throws SQLException
98      */

99     public void setObject(final PreparedStatement JavaDoc preparedStatement, final int index, final Object JavaDoc value) throws SQLException JavaDoc {
100         //Choosing a setter strategy
101
if (value instanceof InputStream JavaDoc) {
102             setBlob(preparedStatement, index, toBlob((InputStream JavaDoc) value));
103         } else if (value instanceof Reader JavaDoc) {
104             setClob(preparedStatement, index, toClob((Reader JavaDoc) value));
105         //For BLOBs/CLOBs use JDBC 1.0 methods for compatibility
106
} else if (value instanceof Blob JavaDoc) {
107             setBlob(preparedStatement, index, (Blob JavaDoc) value);
108         } else if (value instanceof Clob JavaDoc) {
109             setClob(preparedStatement, index, (Clob JavaDoc) value);
110         } else if (value instanceof Date JavaDoc) {
111             setDateObject(preparedStatement, index, (Date JavaDoc) value);
112         } else if (value instanceof Calendar JavaDoc) {
113             preparedStatement.setTimestamp(index, new Timestamp JavaDoc(((Calendar JavaDoc)value).getTimeInMillis()), (Calendar JavaDoc) value);
114         } else {
115             preparedStatement.setObject(index, value);
116         }
117     }
118
119     protected Blob JavaDoc toBlob(InputStream JavaDoc is) {
120         Blob JavaDoc blob = Lobs.newBlob(is);
121         if (blob instanceof Closeable JavaDoc) {
122             registerResource((Closeable JavaDoc) blob);
123         }
124         return blob;
125     }
126
127     protected Clob JavaDoc toClob(Reader JavaDoc reader) {
128         Clob JavaDoc clob = Lobs.newClob(reader);
129         if (clob instanceof Closeable JavaDoc) {
130             registerResource((Closeable JavaDoc) clob);
131         }
132         return clob;
133     }
134
135     protected void setBlob(final PreparedStatement JavaDoc ps, final int index, final Blob JavaDoc blob) throws SQLException JavaDoc {
136         ps.setBinaryStream(index, blob.getBinaryStream(), (int) blob.length());
137     }
138
139     protected void setClob(final PreparedStatement JavaDoc ps, final int index, final Clob JavaDoc clob) throws SQLException JavaDoc {
140         ps.setCharacterStream(index, clob.getCharacterStream(), (int) clob.length());
141     }
142
143     /**
144      * Sets the {@link java.util.Date} or its descendant as a statement parameter.
145      */

146     protected void setDateObject(final PreparedStatement JavaDoc ps, final int index, final Date JavaDoc date) throws SQLException JavaDoc {
147         if (date instanceof Timestamp JavaDoc) {
148             ps.setTimestamp(index, (Timestamp JavaDoc) date);
149         } else if (date instanceof java.sql.Date JavaDoc) {
150             ps.setDate(index, (java.sql.Date JavaDoc) date);
151         } else if (date instanceof Time JavaDoc) {
152             ps.setTime(index, (Time JavaDoc) date);
153         } else {
154             ps.setTimestamp(index, new Timestamp JavaDoc(date.getTime()));
155         }
156     }
157
158     protected void registerResource(Closeable JavaDoc resource) {
159         if (resources==null) {
160             resources=new ArrayList JavaDoc<Closeable JavaDoc>();
161         }
162         resources.add(resource);
163     }
164
165     /**
166      * Closes any resources opened during this object lifecycle.
167      */

168     public void close() {
169         if (resources != null) {
170             IOUtils.closeSilently(resources);
171             resources = null;
172         }
173     }
174
175
176 }
177
Popular Tags