001: /*
002: * Copyright 2002-2005 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 LobHandler interface. Invokes the
030: * direct accessor methods that <code>java.sql.ResultSet</code> and
031: * <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 OracleLobHandler for accessing Oracle BLOBs/CLOBs.
037: *
038: * @author Juergen Hoeller
039: * @since 04.12.2003
040: * @see OracleLobHandler
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: public byte[] getBlobAsBytes(ResultSet rs, int columnIndex)
057: throws SQLException {
058: logger.debug("Returning BLOB as bytes");
059: return rs.getBytes(columnIndex);
060: }
061:
062: public InputStream getBlobAsBinaryStream(ResultSet rs,
063: int columnIndex) throws SQLException {
064: logger.debug("Returning BLOB as binary stream");
065: return rs.getBinaryStream(columnIndex);
066: }
067:
068: public String getClobAsString(ResultSet rs, int columnIndex)
069: throws SQLException {
070: logger.debug("Returning CLOB as string");
071: return rs.getString(columnIndex);
072: }
073:
074: public InputStream getClobAsAsciiStream(ResultSet rs,
075: int columnIndex) throws SQLException {
076: logger.debug("Returning CLOB as ASCII stream");
077: return rs.getAsciiStream(columnIndex);
078: }
079:
080: public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex)
081: throws SQLException {
082: logger.debug("Returning CLOB as character stream");
083: return rs.getCharacterStream(columnIndex);
084: }
085:
086: public LobCreator getLobCreator() {
087: logger.debug("Creating new DefaultLobCreator");
088: return new DefaultLobCreator();
089: }
090:
091: protected class DefaultLobCreator implements LobCreator {
092:
093: public void setBlobAsBytes(PreparedStatement ps,
094: int paramIndex, byte[] content) throws SQLException {
095:
096: ps.setBytes(paramIndex, content);
097: if (logger.isDebugEnabled()) {
098: logger
099: .debug(content != null ? "Set bytes for BLOB with length "
100: + content.length
101: : "Set BLOB to null");
102: }
103: }
104:
105: public void setBlobAsBinaryStream(PreparedStatement ps,
106: int paramIndex, InputStream binaryStream,
107: int contentLength) throws SQLException {
108:
109: ps.setBinaryStream(paramIndex, binaryStream, contentLength);
110: if (logger.isDebugEnabled()) {
111: logger
112: .debug(binaryStream != null ? "Set binary stream for BLOB with length "
113: + contentLength
114: : "Set BLOB to null");
115: }
116: }
117:
118: public void setClobAsString(PreparedStatement ps,
119: int paramIndex, String content) throws SQLException {
120:
121: ps.setString(paramIndex, content);
122: if (logger.isDebugEnabled()) {
123: logger
124: .debug(content != null ? "Set string for CLOB with length "
125: + content.length()
126: : "Set CLOB to null");
127: }
128: }
129:
130: public void setClobAsAsciiStream(PreparedStatement ps,
131: int paramIndex, InputStream asciiStream,
132: int contentLength) throws SQLException {
133:
134: ps.setAsciiStream(paramIndex, asciiStream, contentLength);
135: if (logger.isDebugEnabled()) {
136: logger
137: .debug(asciiStream != null ? "Set ASCII stream for CLOB with length "
138: + contentLength
139: : "Set CLOB to null");
140: }
141: }
142:
143: public void setClobAsCharacterStream(PreparedStatement ps,
144: int paramIndex, Reader characterStream,
145: int contentLength) throws SQLException {
146:
147: ps.setCharacterStream(paramIndex, characterStream,
148: contentLength);
149: if (logger.isDebugEnabled()) {
150: logger
151: .debug(characterStream != null ? "Set character stream for CLOB with length "
152: + contentLength
153: : "Set CLOB to null");
154: }
155: }
156:
157: public void close() {
158: logger.debug("Closing DefaultLobCreator");
159: // nothing to do here
160: }
161: }
162:
163: }
|