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.SQLException;
  21. /**
  22. * Interface that abstracts potentially database-specific creation of large binary
  23. * fields and large text fields. Does not work with java.sql.Blob and java.sql.Clob
  24. * instances in the API, as some JDBC drivers do not support these types as such.
  25. *
  26. * <p>A LobCreator represents a session for creating BLOBs: It is <i>not</i>
  27. * thread-safe and needs to be instantiated for each statement execution or for
  28. * each transaction. Each LobCreator needs to be closed after completion.
  29. *
  30. * <p>For convenient working with a PreparedStatement and a LobCreator, consider
  31. * JdbcTemplate with a AbstractLobCreatingPreparedStatementCallback implementation.
  32. * See the latter's javadoc for details.
  33. *
  34. * @author Juergen Hoeller
  35. * @since 04.12.2003
  36. * @see #close
  37. * @see LobHandler#getLobCreator
  38. * @see org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback
  39. * @see DefaultLobHandler.DefaultLobCreator
  40. * @see OracleLobHandler.OracleLobCreator
  41. * @see java.sql.PreparedStatement#setBlob
  42. * @see java.sql.PreparedStatement#setBinaryStream
  43. * @see java.sql.PreparedStatement#setString
  44. * @see java.sql.PreparedStatement#setAsciiStream
  45. * @see java.sql.PreparedStatement#setCharacterStream
  46. */
  47. public interface LobCreator {
  48. /**
  49. * Set the given content as bytes on the given statement, using the given
  50. * parameter index. Might simply invoke PreparedStatement.setBytes
  51. * or create a Blob instance for it, depending on the database and driver.
  52. * @param ps the PreparedStatement to the set the content on
  53. * @param parameterIndex the parameter index to use
  54. * @param content the content as byte array
  55. * @throws SQLException if thrown by JDBC methods
  56. * @see java.sql.PreparedStatement#setBytes
  57. */
  58. void setBlobAsBytes(PreparedStatement ps, int parameterIndex, byte[] content)
  59. throws SQLException;
  60. /**
  61. * Set the given content as binary stream on the given statement, using the
  62. * given parameter index. Might simply invoke PreparedStatement.setBinaryStream
  63. * or create a Blob instance for it, depending on the database and driver.
  64. * @param ps the PreparedStatement to the set the content on
  65. * @param parameterIndex the parameter index to use
  66. * @param contentStream the content as InputStream
  67. * @throws SQLException if thrown by JDBC methods
  68. * @see java.sql.PreparedStatement#setBinaryStream
  69. */
  70. void setBlobAsBinaryStream(PreparedStatement ps, int parameterIndex, InputStream contentStream,
  71. int contentLength)
  72. throws SQLException;
  73. /**
  74. * Set the given content as String on the given statement, using the given
  75. * parameter index. Might simply invoke PreparedStatement.setString
  76. * or create a Clob instance for it, depending on the database and driver.
  77. * @param ps the PreparedStatement to the set the content on
  78. * @param parameterIndex the parameter index to use
  79. * @param content the content as byte array
  80. * @throws SQLException if thrown by JDBC methods
  81. * @see java.sql.PreparedStatement#setBytes
  82. */
  83. void setClobAsString(PreparedStatement ps, int parameterIndex, String content)
  84. throws SQLException;
  85. /**
  86. * Set the given content as ASCII stream on the given statement, using the
  87. * given parameter index. Might simply invoke PreparedStatement.setAsciiStream
  88. * or create a Clob instance for it, depending on the database and driver.
  89. * @param ps the PreparedStatement to the set the content on
  90. * @param parameterIndex the parameter index to use
  91. * @param asciiStream the content as InputStream
  92. * @throws SQLException if thrown by JDBC methods
  93. * @see java.sql.PreparedStatement#setBinaryStream
  94. */
  95. void setClobAsAsciiStream(PreparedStatement ps, int parameterIndex, InputStream asciiStream,
  96. int contentLength)
  97. throws SQLException;
  98. /**
  99. * Set the given content as character stream on the given statement, using the
  100. * given parameter index. Might simply invoke PreparedStatement.setCharacterStream
  101. * or create a Clob instance for it, depending on the database and driver.
  102. * @param ps the PreparedStatement to the set the content on
  103. * @param parameterIndex the parameter index to use
  104. * @param characterStream the content as InputStream
  105. * @throws SQLException if thrown by JDBC methods
  106. * @see java.sql.PreparedStatement#setBinaryStream
  107. */
  108. void setClobAsCharacterStream(PreparedStatement ps, int parameterIndex, Reader characterStream,
  109. int contentLength)
  110. throws SQLException;
  111. /**
  112. * Close this LobCreator session and free its temporarily created BLOBs and CLOBs.
  113. * Will not need to do anything if using PreparedStatement's standard methods,
  114. * but might be necessary to free database resources if using proprietary means.
  115. * <p><b>NOTE</b>: Needs to be invoked after the involved PreparedStatements have
  116. * been executed respectively the affected O/R mapping sessions have been flushed.
  117. * Else, the database resources for the temporary BLOBs might stay allocated.
  118. */
  119. void close();
  120. }