001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jdbc.support.lob;
018:
019: import java.io.InputStream;
020: import java.io.Reader;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: * Default implementation of the {@link LobHandler} interface. Invokes
030: * the direct accessor methods that <code>java.sql.ResultSet</code>
031: * and <code>java.sql.PreparedStatement</code> offer.
032: *
033: * <p>This LobHandler should work for any JDBC driver that is JDBC compliant
034: * in terms of the spec's suggestions regarding simple BLOB and CLOB handling.
035: * This does not apply to Oracle 9i, and only to a limited degree to Oracle 10g!
036: * As a consequence, use {@link OracleLobHandler} for accessing Oracle BLOBs/CLOBs.
037: *
038: * @author Juergen Hoeller
039: * @since 04.12.2003
040: * @see #setStreamAsLob
041: * @see java.sql.ResultSet#getBytes
042: * @see java.sql.ResultSet#getBinaryStream
043: * @see java.sql.ResultSet#getString
044: * @see java.sql.ResultSet#getAsciiStream
045: * @see java.sql.ResultSet#getCharacterStream
046: * @see java.sql.PreparedStatement#setBytes
047: * @see java.sql.PreparedStatement#setBinaryStream
048: * @see java.sql.PreparedStatement#setString
049: * @see java.sql.PreparedStatement#setAsciiStream
050: * @see java.sql.PreparedStatement#setCharacterStream
051: */
052: public class DefaultLobHandler extends AbstractLobHandler {
053:
054: protected final Log logger = LogFactory.getLog(getClass());
055:
056: private boolean streamAsLob = false;
057:
058: /**
059: * Specify whether a submitted binary stream / character stream
060: * should be passed to the JDBC driver as explicit LOB content,
061: * using the JDBC 4.0 <code>setBlob</code> / <code>setClob</code>
062: * method with a stream argument.
063: * <p>Default is "false", using the common JDBC 2.0 <code>setBinaryStream</code>
064: * / <code>setCharacterStream</code> method for setting the content.
065: * Switch this to "true" for explicit JDBC 4.0 usage, provided that
066: * your JDBC driver actually supports those JDBC 4.0 operations.
067: * @see java.sql.PreparedStatement#setBlob(int, java.io.InputStream, long)
068: * @see java.sql.PreparedStatement#setClob(int, java.io.Reader, long)
069: * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)
070: * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)
071: */
072: public void setStreamAsLob(boolean streamAsLob) {
073: this .streamAsLob = streamAsLob;
074: }
075:
076: public byte[] getBlobAsBytes(ResultSet rs, int columnIndex)
077: throws SQLException {
078: logger.debug("Returning BLOB as bytes");
079: return rs.getBytes(columnIndex);
080: }
081:
082: public InputStream getBlobAsBinaryStream(ResultSet rs,
083: int columnIndex) throws SQLException {
084: logger.debug("Returning BLOB as binary stream");
085: return rs.getBinaryStream(columnIndex);
086: }
087:
088: public String getClobAsString(ResultSet rs, int columnIndex)
089: throws SQLException {
090: logger.debug("Returning CLOB as string");
091: return rs.getString(columnIndex);
092: }
093:
094: public InputStream getClobAsAsciiStream(ResultSet rs,
095: int columnIndex) throws SQLException {
096: logger.debug("Returning CLOB as ASCII stream");
097: return rs.getAsciiStream(columnIndex);
098: }
099:
100: public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex)
101: throws SQLException {
102: logger.debug("Returning CLOB as character stream");
103: return rs.getCharacterStream(columnIndex);
104: }
105:
106: public LobCreator getLobCreator() {
107: logger.debug("Creating new DefaultLobCreator");
108: return new DefaultLobCreator();
109: }
110:
111: protected class DefaultLobCreator implements LobCreator {
112:
113: public void setBlobAsBytes(PreparedStatement ps,
114: int paramIndex, byte[] content) throws SQLException {
115:
116: ps.setBytes(paramIndex, content);
117: if (logger.isDebugEnabled()) {
118: logger
119: .debug(content != null ? "Set bytes for BLOB with length "
120: + content.length
121: : "Set BLOB to null");
122: }
123: }
124:
125: public void setBlobAsBinaryStream(PreparedStatement ps,
126: int paramIndex, InputStream binaryStream,
127: int contentLength) throws SQLException {
128:
129: if (streamAsLob) {
130: ps.setBlob(paramIndex, binaryStream, contentLength);
131: } else {
132: ps.setBinaryStream(paramIndex, binaryStream,
133: contentLength);
134: }
135: if (logger.isDebugEnabled()) {
136: logger
137: .debug(binaryStream != null ? "Set binary stream for BLOB with length "
138: + contentLength
139: : "Set BLOB to null");
140: }
141: }
142:
143: public void setClobAsString(PreparedStatement ps,
144: int paramIndex, String content) throws SQLException {
145:
146: ps.setString(paramIndex, content);
147: if (logger.isDebugEnabled()) {
148: logger
149: .debug(content != null ? "Set string for CLOB with length "
150: + content.length()
151: : "Set CLOB to null");
152: }
153: }
154:
155: public void setClobAsAsciiStream(PreparedStatement ps,
156: int paramIndex, InputStream asciiStream,
157: int contentLength) throws SQLException {
158:
159: ps.setAsciiStream(paramIndex, asciiStream, contentLength);
160: if (logger.isDebugEnabled()) {
161: logger
162: .debug(asciiStream != null ? "Set ASCII stream for CLOB with length "
163: + contentLength
164: : "Set CLOB to null");
165: }
166: }
167:
168: public void setClobAsCharacterStream(PreparedStatement ps,
169: int paramIndex, Reader characterStream,
170: int contentLength) throws SQLException {
171:
172: if (streamAsLob) {
173: ps.setClob(paramIndex, characterStream, contentLength);
174: } else {
175: ps.setCharacterStream(paramIndex, characterStream,
176: contentLength);
177: }
178: if (logger.isDebugEnabled()) {
179: logger
180: .debug(characterStream != null ? "Set character stream for CLOB with length "
181: + contentLength
182: : "Set CLOB to null");
183: }
184: }
185:
186: public void close() {
187: logger.debug("Closing DefaultLobCreator");
188: // nothing to do here
189: }
190: }
191:
192: }
|