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.object;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.ArrayList;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import javax.sql.DataSource;
  24. import org.springframework.jdbc.core.ResultReader;
  25. /**
  26. * Reusable RDBMS query in which concrete subclasses must implement
  27. * the abstract mapRow(ResultSet, int) method to map each row of
  28. * the JDBC ResultSet into an object.
  29. *
  30. * <p>Such manual mapping is usually preferable to "automatic"
  31. * mapping using reflection, which can become complex in non-trivial
  32. * cases. For example, the present class allows different objects
  33. * to be used for different rows (for example, if a subclass is indicated).
  34. * It allows computed fields to be set. And there's no need for
  35. * ResultSet columns to have the same names as bean properties.
  36. * The Pareto Principle in action: going the extra mile to automate
  37. * the extraction process makes the framework much more complex
  38. * and delivers little real benefit.
  39. *
  40. * <p>Subclasses can be constructed providing SQL, parameter types
  41. * and a DataSource. SQL will often vary between subclasses.
  42. *
  43. * @author Rod Johnson
  44. * @author Thomas Risberg
  45. * @author Jean-Pierre Pawlak
  46. * @see org.springframework.jdbc.object.MappingSqlQuery
  47. * @see org.springframework.jdbc.object.SqlQuery
  48. */
  49. public abstract class MappingSqlQueryWithParameters extends SqlQuery {
  50. /**
  51. * Constructor to allow use as a JavaBean
  52. */
  53. public MappingSqlQueryWithParameters() {
  54. }
  55. /**
  56. * Convenient constructor with DataSource and SQL string.
  57. * @param ds DataSource to use to get connections
  58. * @param sql SQL to run
  59. */
  60. public MappingSqlQueryWithParameters(DataSource ds, String sql) {
  61. super(ds, sql);
  62. }
  63. /**
  64. * Implementation of protected abstract method. This invokes the subclass's
  65. * implementation of the mapRow() method.
  66. */
  67. protected ResultReader newResultReader(int rowsExpected, Object[] parameters, Map context) {
  68. return new ResultReaderImpl(rowsExpected, parameters, context);
  69. }
  70. /**
  71. * Subclasses must implement this method to convert each row
  72. * of the ResultSet into an object of the result type.
  73. * @param rs ResultSet we're working through
  74. * @param rowNum row number (from 0) we're up to
  75. * @param parameters to the query (passed to the execute() method).
  76. * Subclasses are rarely interested in these.
  77. * It can be null if there are no parameters.
  78. * @param context passed to the execute() method.
  79. * It can be null if no contextual information is need.
  80. * @return an object of the result type
  81. * @throws SQLException if there's an error extracting data.
  82. * Subclasses can simply not catch SQLExceptions, relying on the
  83. * framework to clean up.
  84. */
  85. protected abstract Object mapRow(ResultSet rs, int rowNum, Object[] parameters, Map context)
  86. throws SQLException;
  87. /**
  88. * Implementation of ResultReader that calls the enclosing
  89. * class's mapRow() method for each row.
  90. */
  91. protected class ResultReaderImpl implements ResultReader {
  92. /** List to save results in */
  93. private final List results;
  94. private final Object[] params;
  95. private final Map context;
  96. private int rowNum = 0;
  97. /**
  98. * Use an array results. More efficient if we know how many results to expect.
  99. */
  100. public ResultReaderImpl(int rowsExpected, Object[] parameters, Map context) {
  101. // use the more efficient collection if we know how many rows to expect
  102. this.results = (rowsExpected > 0) ? (List) new ArrayList(rowsExpected) : (List) new LinkedList();
  103. this.params = parameters;
  104. this.context = context;
  105. }
  106. public void processRow(ResultSet rs) throws SQLException {
  107. this.results.add(mapRow(rs, this.rowNum++, this.params, this.context));
  108. }
  109. public List getResults() {
  110. return this.results;
  111. }
  112. }
  113. }