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.core;
  17. /**
  18. * Common base class for ResultSet-supporting SqlParameters like
  19. * SqlOutParameter and SqlReturnResultSet.
  20. * @author Juergen Hoeller
  21. * @since 26.05.2004
  22. * @see SqlOutParameter
  23. * @see SqlReturnResultSet
  24. */
  25. public class ResultSetSupportingSqlParameter extends SqlParameter {
  26. private ResultSetExtractor resultSetExtractor = null;
  27. private RowCallbackHandler rowCallbackHandler = null;
  28. private RowMapper rowMapper = null;
  29. private int rowsExpected = 0;
  30. /**
  31. * Create a new ResultSetSupportingSqlParameter, supplying name and SQL type.
  32. * @param name name of the parameter, as used in input and output maps
  33. * @param type SQL type of the parameter according to java.sql.Types
  34. */
  35. public ResultSetSupportingSqlParameter(String name, int type) {
  36. super(name, type);
  37. }
  38. public ResultSetSupportingSqlParameter(String name, int type, String typeName) {
  39. super(name, type, typeName);
  40. }
  41. public ResultSetSupportingSqlParameter(String name, int type, ResultSetExtractor resultSetExtractor) {
  42. super(name, type);
  43. this.resultSetExtractor = resultSetExtractor;
  44. }
  45. public ResultSetSupportingSqlParameter(String name, int type, RowCallbackHandler rch) {
  46. super(name, type);
  47. this.rowCallbackHandler = rch;
  48. }
  49. public ResultSetSupportingSqlParameter(String name, int type, RowMapper rm) {
  50. super(name, type);
  51. this.rowMapper = rm;
  52. }
  53. public ResultSetSupportingSqlParameter(String name, int type, RowMapper rm, int rowsExpected) {
  54. super(name, type);
  55. this.rowMapper = rm;
  56. this.rowsExpected = rowsExpected;
  57. }
  58. public boolean isResultSetSupported() {
  59. return (this.resultSetExtractor != null || this.rowCallbackHandler != null || this.rowMapper != null);
  60. }
  61. protected boolean isRowCallbackHandlerSupported() {
  62. return (this.rowCallbackHandler != null || this.rowMapper != null);
  63. }
  64. protected ResultSetExtractor getResultSetExtractor() {
  65. return resultSetExtractor;
  66. }
  67. /**
  68. * Return a new instance of the implementation of a RowCallbackHandler,
  69. * usable for returned ResultSets. This implementation invokes a given
  70. * RowMapper via the RowMapperResultReader adapter, respectively returns
  71. * a given RowCallbackHandler directly.
  72. * @see RowMapperResultReader
  73. */
  74. protected RowCallbackHandler getRowCallbackHandler() {
  75. if (this.rowMapper != null) {
  76. return new RowMapperResultReader(this.rowMapper, this.rowsExpected);
  77. }
  78. else {
  79. return this.rowCallbackHandler;
  80. }
  81. }
  82. }