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.Map;
  20. import javax.sql.DataSource;
  21. /**
  22. * Reusable query in which concrete subclasses must implement the abstract
  23. * mapRow(ResultSet, int) method to convert each row of the JDBC ResultSet
  24. * into an object.
  25. *
  26. * <p>Simplifies MappingSqlQueryWithParameters API by dropping parameters and
  27. * context. Most subclasses won't care about parameters. If you don't use
  28. * contextual information, subclass this instead of MappingSqlQueryWithParameters.
  29. *
  30. * @author Rod Johnson
  31. * @author Thomas Risberg
  32. * @author Jean-Pierre Pawlak
  33. * @see MappingSqlQueryWithParameters
  34. */
  35. public abstract class MappingSqlQuery extends MappingSqlQueryWithParameters {
  36. /**
  37. * Constructor that allows use as a JavaBean.
  38. */
  39. public MappingSqlQuery() {
  40. }
  41. /**
  42. * Convenient constructor with DataSource and SQL string.
  43. * @param ds DataSource to use to obtain connections
  44. * @param sql SQL to run
  45. */
  46. public MappingSqlQuery(DataSource ds, String sql) {
  47. super(ds, sql);
  48. }
  49. /**
  50. * This method is implemented to invoke the simpler mapRow
  51. * template method, ignoring parameters.
  52. * @see #mapRow(ResultSet, int)
  53. */
  54. protected final Object mapRow(ResultSet rs, int rowNum, Object[] parameters, Map context) throws SQLException {
  55. return mapRow(rs, rowNum);
  56. }
  57. /**
  58. * Subclasses must implement this method to convert each row of the
  59. * ResultSet into an object of the result type.
  60. * <p>Subclasses of this class, as opposed to direct subclasses of
  61. * MappingSqlQueryWithParameters, don't need to concern themselves
  62. * with the parameters to the execute method of the query object.
  63. * @param rs ResultSet we're working through
  64. * @param rowNum row number (from 0) we're up to
  65. * @return an object of the result type
  66. * @throws SQLException if there's an error extracting data.
  67. * Subclasses can simply not catch SQLExceptions, relying on the
  68. * framework to clean up.
  69. */
  70. protected abstract Object mapRow(ResultSet rs, int rowNum) throws SQLException;
  71. }