1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  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.apache.commons.beanutils;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. /**
  22. * <p>Implementation of <code>DynaClass</code> for DynaBeans that wrap the
  23. * <code>java.sql.Row</code> objects of a <code>java.sql.ResultSet</code>.
  24. * The normal usage pattern is something like:</p>
  25. * <pre>
  26. * ResultSet rs = ...;
  27. * ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
  28. * Iterator rows = rsdc.iterator();
  29. * while (rows.hasNext()) {
  30. * DynaBean row = (DynaBean) rows.next();
  31. * ... process this row ...
  32. * }
  33. * rs.close();
  34. * </pre>
  35. *
  36. * <p>Each column in the result set will be represented as a DynaBean
  37. * property of the corresponding name (optionally forced to lower case
  38. * for portability).</p>
  39. *
  40. * <p><strong>WARNING</strong> - Any {@link DynaBean} instance returned by
  41. * this class, or from the <code>Iterator</code> returned by the
  42. * <code>iterator()</code> method, is directly linked to the row that the
  43. * underlying result set is currently positioned at. This has the following
  44. * implications:</p>
  45. * <ul>
  46. * <li>Once you retrieve a different {@link DynaBean} instance, you should
  47. * no longer use any previous instance.</li>
  48. * <li>Changing the position of the underlying result set will change the
  49. * data that the {@link DynaBean} references.</li>
  50. * <li>Once the underlying result set is closed, the {@link DynaBean}
  51. * instance may no longer be used.</li>
  52. * </ul>
  53. *
  54. * <p>Any database data that you wish to utilize outside the context of the
  55. * current row of an open result set must be copied. For example, you could
  56. * use the following code to create standalone copies of the information in
  57. * a result set:</p>
  58. * <pre>
  59. * ArrayList results = new ArrayList(); // To hold copied list
  60. * ResultSetDynaClass rsdc = ...;
  61. * DynaProperty properties[] = rsdc.getDynaProperties();
  62. * BasicDynaClass bdc =
  63. * new BasicDynaClass("foo", BasicDynaBean.class,
  64. * rsdc.getDynaProperties());
  65. * Iterator rows = rsdc.iterator();
  66. * while (rows.hasNext()) {
  67. * DynaBean oldRow = (DynaBean) rows.next();
  68. * DynaBean newRow = bdc.newInstance();
  69. * PropertyUtils.copyProperties(newRow, oldRow);
  70. * results.add(newRow);
  71. * }
  72. * </pre>
  73. *
  74. * @author Craig R. McClanahan
  75. * @version $Revision: 1.15 $ $Date: 2004/02/28 13:18:33 $
  76. */
  77. public class ResultSetDynaClass extends JDBCDynaClass implements DynaClass {
  78. // ----------------------------------------------------------- Constructors
  79. /**
  80. * <p>Construct a new ResultSetDynaClass for the specified
  81. * <code>ResultSet</code>. The property names corresponding
  82. * to column names in the result set will be lower cased.</p>
  83. *
  84. * @param resultSet The result set to be wrapped
  85. *
  86. * @exception NullPointerException if <code>resultSet</code>
  87. * is <code>null</code>
  88. * @exception SQLException if the metadata for this result set
  89. * cannot be introspected
  90. */
  91. public ResultSetDynaClass(ResultSet resultSet) throws SQLException {
  92. this(resultSet, true);
  93. }
  94. /**
  95. * <p>Construct a new ResultSetDynaClass for the specified
  96. * <code>ResultSet</code>. The property names corresponding
  97. * to the column names in the result set will be lower cased or not,
  98. * depending on the specified <code>lowerCase</code> value.</p>
  99. *
  100. * <p><strong>WARNING</strong> - If you specify <code>false</code>
  101. * for <code>lowerCase</code>, the returned property names will
  102. * exactly match the column names returned by your JDBC driver.
  103. * Because different drivers might return column names in different
  104. * cases, the property names seen by your application will vary
  105. * depending on which JDBC driver you are using.</p>
  106. *
  107. * @param resultSet The result set to be wrapped
  108. * @param lowerCase Should property names be lower cased?
  109. *
  110. * @exception NullPointerException if <code>resultSet</code>
  111. * is <code>null</code>
  112. * @exception SQLException if the metadata for this result set
  113. * cannot be introspected
  114. */
  115. public ResultSetDynaClass(ResultSet resultSet, boolean lowerCase)
  116. throws SQLException {
  117. if (resultSet == null) {
  118. throw new NullPointerException();
  119. }
  120. this.resultSet = resultSet;
  121. this.lowerCase = lowerCase;
  122. introspect(resultSet);
  123. }
  124. // ----------------------------------------------------- Instance Variables
  125. /**
  126. * <p>The <code>ResultSet</code> we are wrapping.</p>
  127. */
  128. protected ResultSet resultSet = null;
  129. // --------------------------------------------------------- Public Methods
  130. /**
  131. * <p>Return an <code>Iterator</code> of {@link DynaBean} instances for
  132. * each row of the wrapped <code>ResultSet</code>, in "forward" order.
  133. * Unless the underlying result set supports scrolling, this method
  134. * should be called only once.</p>
  135. */
  136. public Iterator iterator() {
  137. return (new ResultSetIterator(this));
  138. }
  139. // -------------------------------------------------------- Package Methods
  140. /**
  141. * <p>Return the result set we are wrapping.</p>
  142. */
  143. ResultSet getResultSet() {
  144. return (this.resultSet);
  145. }
  146. // ------------------------------------------------------ Protected Methods
  147. /**
  148. * <p>Loads the class of the given name which by default uses the class loader used
  149. * to load this library.
  150. * Dervations of this class could implement alternative class loading policies such as
  151. * using custom ClassLoader or using the Threads's context class loader etc.
  152. * </p>
  153. */
  154. protected Class loadClass(String className) throws SQLException {
  155. try {
  156. return getClass().getClassLoader().loadClass(className);
  157. }
  158. catch (Exception e) {
  159. throw new SQLException("Cannot load column class '" +
  160. className + "': " + e);
  161. }
  162. }
  163. }