/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.util;

import java.sql.*;

/**
 * A class containing miscellaneous public methods useful when working with SQL databases.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class SQLUtils
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * SQLUtils instances should NOT be constructed in standard programming.
   * Instead, the class should be used as <code>SQLUtils.shutdown(statement);</code>.
   * This constructor is public to permit tools that require a JavaBean instance
   * to operate.
   */
  public SQLUtils()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Closes a <CODE>ResultSet</CODE> cleanly, without throwing an exception.
   *
   * @param rs The <CODE>ResultSet</CODE> to be closed.
   * @see java.sql.ResultSet#close()
   */
  public static final void shutdown(ResultSet rs)
  {
    try
    { // close the ResultSet
      if (rs!=null)
	rs.close();

    } // end try
    catch (SQLException e)
    { // ignore any SQL errors
    } // end catch

  } // end shutdown

  /**
   * Closes a <CODE>Statement</CODE> cleanly, without throwing an exception.
   *
   * @param rs The <CODE>Statement</CODE> to be closed.
   * @see java.sql.Statement#close()
   */
  public static final void shutdown(Statement stmt)
  {
    try
    { // close the Statement
      if (stmt!=null)
	stmt.close();

    } // end try
    catch (SQLException e)
    { // ignore any SQL errors
    } // end catch

  } // end shutdown

  /**
   * Closes a <CODE>Connection</CODE> cleanly, without throwing an exception.
   *
   * @param rs The <CODE>Connection</CODE> to be closed.
   * @see java.sql.Connection#close()
   */
  public static final void shutdown(Connection conn)
  {
    try
    { // close the Connection
      if (conn!=null)
	conn.close();

    } // end try
    catch (SQLException e)
    { // ignore any SQL errors
    } // end catch

  } // end shutdown

  /**
   * Fetches the return count from a query such as "SELECT COUNT(*) FROM ...".  All such queries will always
   * return at least one row, with the count.
   *
   * @param rs The <CODE>ResultSet</CODE> returned from the query execution.
   * @param column The column index in which the return value may be found.
   * @return The value of that column (usually, the count you were querying for).
   * @exception java.sql.SQLException If there was no returned row, or there was a data type mismatch on the column.
   */
  public static final int getReturnCountInt(ResultSet rs, int column) throws SQLException
  {
    if (!(rs.next()))
      throw new SQLException("expected return count row, not present");
    return rs.getInt(column);

  } // end getReturnCountInt

} // end class SQLUtils
