1. /*
  2. * Copyright 2002-2004 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. package org.springframework.jdbc.support.lob;
  17. import java.io.InputStream;
  18. import java.io.Reader;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. /**
  25. * Default implementation of the LobHandler interface. Invokes the direct accessor
  26. * methods that java.sql.ResultSet and java.sql.PreparedStatement offer.
  27. *
  28. * <p>This LobHandler should work for any JDBC driver that is JDBC compliant
  29. * in terms of the spec's suggestions regarding simple BLOB and CLOB handling.
  30. *
  31. * @author Juergen Hoeller
  32. * @since 04.12.2003
  33. * @see java.sql.ResultSet#getBytes
  34. * @see java.sql.ResultSet#getBinaryStream
  35. * @see java.sql.ResultSet#getString
  36. * @see java.sql.ResultSet#getAsciiStream
  37. * @see java.sql.ResultSet#getCharacterStream
  38. * @see java.sql.PreparedStatement#setBytes
  39. * @see java.sql.PreparedStatement#setBinaryStream
  40. * @see java.sql.PreparedStatement#setString
  41. * @see java.sql.PreparedStatement#setAsciiStream
  42. * @see java.sql.PreparedStatement#setCharacterStream
  43. */
  44. public class DefaultLobHandler implements LobHandler {
  45. protected final Log logger = LogFactory.getLog(getClass());
  46. public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException {
  47. logger.debug("Returning BLOB as bytes");
  48. return rs.getBytes(columnIndex);
  49. }
  50. public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
  51. logger.debug("Returning BLOB as binary stream");
  52. return rs.getBinaryStream(columnIndex);
  53. }
  54. public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException {
  55. logger.debug("Returning CLOB as string");
  56. return rs.getString(columnIndex);
  57. }
  58. public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException {
  59. logger.debug("Returning CLOB as ASCII stream");
  60. return rs.getAsciiStream(columnIndex);
  61. }
  62. public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
  63. logger.debug("Returning CLOB as character stream");
  64. return rs.getCharacterStream(columnIndex);
  65. }
  66. public LobCreator getLobCreator() {
  67. return new DefaultLobCreator();
  68. }
  69. protected class DefaultLobCreator implements LobCreator {
  70. public void setBlobAsBytes(PreparedStatement ps, int parameterIndex, byte[] content)
  71. throws SQLException {
  72. ps.setBytes(parameterIndex, content);
  73. logger.debug(content != null ?
  74. "Set bytes for BLOB with length " + content.length :
  75. "Set BLOB to null");
  76. }
  77. public void setBlobAsBinaryStream(PreparedStatement ps, int parameterIndex, InputStream binaryStream,
  78. int contentLength)
  79. throws SQLException {
  80. ps.setBinaryStream(parameterIndex, binaryStream, contentLength);
  81. logger.debug(binaryStream != null ?
  82. "Set binary stream for BLOB with length " + contentLength :
  83. "Set BLOB to null");
  84. }
  85. public void setClobAsString(PreparedStatement ps, int parameterIndex, String content)
  86. throws SQLException {
  87. ps.setString(parameterIndex, content);
  88. logger.debug(content != null ?
  89. "Set string for CLOB with length " + content.length() :
  90. "Set CLOB to null");
  91. }
  92. public void setClobAsAsciiStream(PreparedStatement ps, int parameterIndex, InputStream asciiStream,
  93. int contentLength)
  94. throws SQLException {
  95. ps.setAsciiStream(parameterIndex, asciiStream, contentLength);
  96. logger.debug(asciiStream != null ?
  97. "Set ASCII stream for CLOB with length " + contentLength :
  98. "Set CLOB to null");
  99. }
  100. public void setClobAsCharacterStream(PreparedStatement ps, int parameterIndex, Reader characterStream,
  101. int contentLength)
  102. throws SQLException {
  103. ps.setCharacterStream(parameterIndex, characterStream, contentLength);
  104. logger.debug(characterStream != null ?
  105. "Set character stream for CLOB with length " + contentLength :
  106. "Set CLOB to null");
  107. }
  108. public void close() {
  109. // nothing to do here
  110. }
  111. }
  112. }