//============================================================================== // SqlObjectResultSet.java //============================================================================== package tribble.sql; import java.lang.IllegalAccessException; import java.lang.InstantiationException; import java.lang.Object; import java.lang.String; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; /******************************************************************************* * SQL query result set. * See {@link SqlObjectQuery} for details. * * *
*
Source code:
*
Available at: * http://david.tribble.com/src/java/tribble/sql/SqlObjectResultSet.java *
*
Documentation:
*
Available at: * http://david.tribble.com/docs/tribble/sql/SqlObjectResultSet.html *
*
* * @version @(#)$Revision: 1.7 $ $Date: 2008/09/13 21:24:37 $ * @since 2008-02-14 * @author David R. Tribble (david@tribble.com) *

* Copyright ©2008 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by * by the United States Department of State as a terrorist, or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. * * @see SqlObjectQuery */ public class SqlObjectResultSet { static final String REV = "@(#)tribble/sql/SqlObjectResultSet.java $Revision: 1.7 $ $Date: 2008/09/13 21:24:37 $\n"; // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Variables /** Query result object type. */ private Class m_type; /** Constructor (default, no-arg) for the result object type. */ private Constructor m_ctor; /** List of mappings from the selected fields to result class members. */ private List/**/ m_mappings; /** Parent SQL query that produced this result set. */ private SqlObjectQuery m_query; /** JDBC query statement for this result set. */ private Statement m_stmt; /** JDBC query result set. */ private ResultSet m_results; /** Has a result row. */ private boolean m_hasRow; // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Constructors /*************************************************************************** * Constructor. * * @since 1.1, 2008-02-14 */ SqlObjectResultSet(SqlObjectQuery query, Class type, Constructor ctor, List mappings, ResultSet results, Statement stmt) { // Initialize m_query = query; m_type = type; m_ctor = ctor; m_mappings = mappings; m_results = results; m_stmt = stmt; } // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Methods /*************************************************************************** * Close this result set, releasing all object storage associated with it. * *

* Note that this method never throws SQLException, even if such an * exception results during the closing of the result set. * * @since 1.1, 2008-02-24 */ public void close() { // Close the result set if (m_results != null) { try { m_results.close(); } catch (SQLException ex) { } m_results = null; } // Close the query statement if (m_stmt != null) { try { m_stmt.close(); } catch (SQLException ex) { } m_stmt = null; } } /*************************************************************************** * Determines if there is a next row in the results of the SQL query, and * advances to that row. * * @throws SQLException * Thrown if an error occurs when the SQL result set is advanced to the next * result row. * * @since 1.1, 2008-02-24 */ public boolean next() throws SQLException { SQLException err = null; // Sanity check if (m_results == null) throw new SQLException("Result set is no longer active"); // Advance the result set pointer to the next result row try { m_hasRow = m_results.next(); } catch (SQLException ex) { err = ex; m_hasRow = false; } if (!m_hasRow) { // No more result rows in the result set, clean up close(); } if (err != null) throw err; return m_hasRow; } /*************************************************************************** * Retrieve the next row from the results of the SQL query. * * @return * An object of the result class type, populated with the selected columns of * the current row of the query result set. * * @throws SQLException * Thrown if an error occurs while retrieving field (column) values from the * SQL query result set. * * @throws InstantiationException (unchecked) * Thrown if an object of the result type could not be created. * * @since 1.1, 2008-02-14 */ public Object getResult() throws SQLException { Object res; Exception err = null; // Sanity check if (m_results == null) throw new SQLException("Result set is no longer active"); try { // Create a new object for the current result row res = m_ctor.newInstance(new Class[0]); // Retrieve the selected fields from the result row, // and populate the return object with them for (int i = 0; i < m_mappings.size(); i++) { FieldTypeMap map; map = (FieldTypeMap) m_mappings.get(i); if (map != null) map.mapResultField(res, i+1, m_results); } return res; } catch (IllegalAccessException ex) { err = ex; } catch (InvocationTargetException ex) { err = ex; } catch (Exception ex) { err = ex; } // Handle failures if (err != null) { SQLException ex; ex = new SQLException("Cannot create result object: " + m_type.getName()); ex.initCause(err); throw ex; } return null; // Should never occur } } // End SqlObjectResultSet.java