KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > SqlLobValue


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.jdbc.core.support;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 import org.springframework.jdbc.core.DisposableSqlTypeValue;
26 import org.springframework.jdbc.support.lob.DefaultLobHandler;
27 import org.springframework.jdbc.support.lob.LobCreator;
28 import org.springframework.jdbc.support.lob.LobHandler;
29
30 /**
31  * Object to represent an SQL BLOB/CLOB value parameter. BLOBs can either be an
32  * InputStream or a byte array. CLOBs can be in the form of a Reader, InputStream
33  * or String. Each CLOB/BLOB value will be stored together with its length.
34  * The type is based on which constructor is used. Objects of this class are
35  * immutable except for the LobCreator reference. Use them and discard them.
36  *
37  * <p>This class holds a reference to a LocCreator that must be closed after the
38  * update has completed. This is done via a call to the closeLobCreator method.
39  * All handling of the LobCreator is done by the framework classes that use it -
40  * no need to set or close the LobCreator for end users of this class.
41  *
42  * <p>A usage example:
43  *
44  * <pre class="code">JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
45  * LobHandler lobHandler = new DefaultLobHandler(); // reusable object
46  *
47  * jdbcTemplate.update(
48  * "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
49  * new Object[] {
50  * name,
51  * new SqlLobValue(contentStream, contentLength, lobHandler),
52  * new SqlLobValue(description, lobHandler)
53  * },
54  * new int[] {Types.VARCHAR, Types.BLOB, Types.CLOB});
55  * </pre>
56  *
57  * @author Thomas Risberg
58  * @author Juergen Hoeller
59  * @since 1.1
60  * @see org.springframework.jdbc.support.lob.LobHandler
61  * @see org.springframework.jdbc.support.lob.LobCreator
62  * @see org.springframework.jdbc.core.JdbcTemplate#update(String, Object[], int[])
63  * @see org.springframework.jdbc.object.SqlUpdate#update(Object[])
64  * @see org.springframework.jdbc.object.StoredProcedure#execute(java.util.Map)
65  */

66 public class SqlLobValue implements DisposableSqlTypeValue {
67
68     private final Object JavaDoc content;
69
70     private final int length;
71
72     /**
73      * This contains a reference to the LobCreator - so we can close it
74      * once the update is done.
75      */

76     private final LobCreator lobCreator;
77
78
79     /**
80      * Create a new BLOB value with the given byte array,
81      * using a DefaultLobHandler.
82      * @param bytes the byte array containing the BLOB value
83      * @see org.springframework.jdbc.support.lob.DefaultLobHandler
84      */

85     public SqlLobValue(byte[] bytes) {
86         this(bytes, new DefaultLobHandler());
87     }
88
89     /**
90      * Create a new BLOB value with the given byte array.
91      * @param bytes the byte array containing the BLOB value
92      * @param lobHandler the LobHandler to be used
93      */

94     public SqlLobValue(byte[] bytes, LobHandler lobHandler) {
95         this.content = bytes;
96         this.length = (bytes != null ? bytes.length : 0);
97         this.lobCreator = lobHandler.getLobCreator();
98     }
99
100     /**
101      * Create a new CLOB value with the given content string,
102      * using a DefaultLobHandler.
103      * @param content the String containing the CLOB value
104      * @see org.springframework.jdbc.support.lob.DefaultLobHandler
105      */

106     public SqlLobValue(String JavaDoc content) {
107         this(content, new DefaultLobHandler());
108     }
109
110     /**
111      * Create a new CLOB value with the given content string.
112      * @param content the String containing the CLOB value
113      * @param lobHandler the LobHandler to be used
114      */

115     public SqlLobValue(String JavaDoc content, LobHandler lobHandler) {
116         this.content = content;
117         this.length = (content != null ? content.length() : 0);
118         this.lobCreator = lobHandler.getLobCreator();
119     }
120
121     /**
122      * Create a new BLOB/CLOB value with the given stream,
123      * using a DefaultLobHandler.
124      * @param stream the stream containing the LOB value
125      * @param length the length of the LOB value
126      * @see org.springframework.jdbc.support.lob.DefaultLobHandler
127      */

128     public SqlLobValue(InputStream JavaDoc stream, int length) {
129         this(stream, length, new DefaultLobHandler());
130     }
131
132     /**
133      * Create a new BLOB/CLOB value with the given stream.
134      * @param stream the stream containing the LOB value
135      * @param length the length of the LOB value
136      * @param lobHandler the LobHandler to be used
137      */

138     public SqlLobValue(InputStream JavaDoc stream, int length, LobHandler lobHandler) {
139         this.content = stream;
140         this.length = length;
141         this.lobCreator = lobHandler.getLobCreator();
142     }
143
144     /**
145      * Create a new CLOB value with the given character stream,
146      * using a DefaultLobHandler.
147      * @param reader the character stream containing the CLOB value
148      * @param length the length of the CLOB value
149      * @see org.springframework.jdbc.support.lob.DefaultLobHandler
150      */

151     public SqlLobValue(Reader JavaDoc reader, int length) {
152         this(reader, length, new DefaultLobHandler());
153     }
154
155     /**
156      * Create a new CLOB value with the given character stream.
157      * @param reader the character stream containing the CLOB value
158      * @param length the length of the CLOB value
159      * @param lobHandler the LobHandler to be used
160      */

161     public SqlLobValue(Reader JavaDoc reader, int length, LobHandler lobHandler) {
162         this.content = reader;
163         this.length = length;
164         this.lobCreator = lobHandler.getLobCreator();
165     }
166
167
168     /**
169      * Set the specified content via the LobCreator.
170      */

171     public void setTypeValue(PreparedStatement JavaDoc ps, int paramIndex, int sqlType, String JavaDoc typeName)
172             throws SQLException JavaDoc {
173         if (sqlType == Types.BLOB) {
174             if (this.content instanceof byte[] || this.content == null) {
175                 this.lobCreator.setBlobAsBytes(ps, paramIndex, (byte[]) this.content);
176             }
177             else if (this.content instanceof String JavaDoc) {
178                 this.lobCreator.setBlobAsBytes(ps, paramIndex, ((String JavaDoc) this.content).getBytes());
179             }
180             else if (this.content instanceof InputStream JavaDoc) {
181                 this.lobCreator.setBlobAsBinaryStream(ps, paramIndex, (InputStream JavaDoc) this.content, this.length);
182             }
183             else {
184                 throw new IllegalArgumentException JavaDoc(
185                         "Content type [" + this.content.getClass().getName() + "] not supported for BLOB columns");
186             }
187         }
188         else if (sqlType == Types.CLOB) {
189             if (this.content instanceof String JavaDoc || this.content == null) {
190                 this.lobCreator.setClobAsString(ps, paramIndex, (String JavaDoc) this.content);
191             }
192             else if (this.content instanceof InputStream JavaDoc) {
193                 this.lobCreator.setClobAsAsciiStream(ps, paramIndex, (InputStream JavaDoc) this.content, this.length);
194             }
195             else if (this.content instanceof Reader JavaDoc) {
196                 this.lobCreator.setClobAsCharacterStream(ps, paramIndex, (Reader JavaDoc) this.content, this.length);
197             }
198             else {
199                 throw new IllegalArgumentException JavaDoc(
200                         "Content type [" + this.content.getClass().getName() + "] not supported for CLOB columns");
201             }
202         }
203         else {
204             throw new IllegalArgumentException JavaDoc("SqlLobValue only supports SQL types BLOB and CLOB");
205         }
206     }
207
208     /**
209      * Close the LobCreator, if any.
210      */

211     public void cleanup() {
212         this.lobCreator.close();
213     }
214
215 }
216
Popular Tags