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.ResultSet;
  20. import java.sql.SQLException;
  21. /**
  22. * Abstraction for handling large binary fields and large text fields in
  23. * specific databases, no matter if represented as simple types or Large OBjects.
  24. * Its main purpose is to isolate Oracle's peculiar handling of LOBs in
  25. * OracleLobHandler; most other databases should work with DefaultLobHandler.
  26. *
  27. * <p>Provides accessor methods for BLOBs and CLOBs, and acts as factory for
  28. * LobCreator instances, to be used as sessions for creating BLOBs or CLOBs.
  29. * LobCreators are typically instantiated for each statement execution or for
  30. * each transaction. They are not thread-safe because they might track
  31. * allocated database resources to be able to free them after execution.
  32. *
  33. * <p>Most databases/drivers should be able to work with DefaultLobHandler, which
  34. * simply delegates to JDBC's direct accessor methods, avoiding java.sql.Blob and
  35. * java.sql.Clob completely. Unfortunately, Oracle just accepts Blob/Clob instances
  36. * created via its own proprietary BLOB/CLOB API, and additionally doesn't accept
  37. * large streams for PreparedStatement's corresponding setter methods. Therefore,
  38. * you need to use OracleLobHandler there, which uses Oracle's BLOB/CLOB API
  39. * for both all access.
  40. *
  41. * <p>Of course, you need to declare different field types for each database.
  42. * In Oracle, any binary content needs to go into a BLOB, and all character content
  43. * beyond 4000 bytes needs to go into a CLOB. In MySQL, there is no notion of a
  44. * CLOB type but rather a LONGTEXT type that behaves like a VARCHAR. For complete
  45. * portability, just use a LobHandler for fields that might typically require LOBs
  46. * on some database because of their size (take Oracle's numbers as a guideline).
  47. *
  48. * @author Juergen Hoeller
  49. * @since 23.12.2003
  50. * @see DefaultLobHandler
  51. * @see OracleLobHandler
  52. * @see java.sql.ResultSet#getBytes
  53. * @see java.sql.ResultSet#getBinaryStream
  54. * @see java.sql.ResultSet#getString
  55. * @see java.sql.ResultSet#getAsciiStream
  56. * @see java.sql.ResultSet#getCharacterStream
  57. */
  58. public interface LobHandler {
  59. /**
  60. * Retrieve the given column as bytes from the given ResultSet.
  61. * Might simply invoke ResultSet.getBytes or work with
  62. * ResultSet.getBlob, depending on the database and driver.
  63. * @param rs the ResultSet to retrieve the content from
  64. * @param columnIndex the column index to use
  65. * @return the content as byte array
  66. * @throws SQLException if thrown by JDBC methods
  67. */
  68. byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException;
  69. /**
  70. * Retrieve the given column as binary stream from the given ResultSet.
  71. * Might simply invoke ResultSet.getBinaryStream or work with
  72. * ResultSet.getBlob, depending on the database and driver.
  73. * @param rs the ResultSet to retrieve the content from
  74. * @param columnIndex the column index to use
  75. * @return the content as binary stream
  76. * @throws SQLException if thrown by JDBC methods
  77. */
  78. InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException;
  79. /**
  80. * Retrieve the given column as String from the given ResultSet.
  81. * Might simply invoke ResultSet.getString or work with
  82. * ResultSet.getClob, depending on the database and driver.
  83. * @param rs the ResultSet to retrieve the content from
  84. * @param columnIndex the column index to use
  85. * @return the content as String
  86. * @throws SQLException if thrown by JDBC methods
  87. */
  88. String getClobAsString(ResultSet rs, int columnIndex) throws SQLException;
  89. /**
  90. * Retrieve the given column as ASCII stream from the given ResultSet.
  91. * Might simply invoke ResultSet.getAsciiStream or work with
  92. * ResultSet.getClob, depending on the database and driver.
  93. * @param rs the ResultSet to retrieve the content from
  94. * @param columnIndex the column index to use
  95. * @return the content as ASCII stream
  96. * @throws SQLException if thrown by JDBC methods
  97. */
  98. InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException;
  99. /**
  100. * Retrieve the given column as character stream from the given ResultSet.
  101. * Might simply invoke ResultSet.getCharacterStream or work with
  102. * ResultSet.getClob, depending on the database and driver.
  103. * @param rs the ResultSet to retrieve the content from
  104. * @param columnIndex the column index to use
  105. * @return the content as character stream
  106. * @throws SQLException if thrown by JDBC methods
  107. */
  108. Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException;
  109. /**
  110. * Create a new LobCreator instance, i.e. a session for creating BLOBs
  111. * and CLOBs. Needs to be closed after the created LOBs are not needed
  112. * anymore, i.e. after statement execution or transaction completion.
  113. * @return the new LobCreator instance
  114. * @see LobCreator#close
  115. */
  116. LobCreator getLobCreator();
  117. }