/*
 * 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.*;
import org.apache.log4j.Logger;

/**
 * A class containing miscellaneous public methods useful when working with MySQL databases.  All
 * public methods of {@link com.silverwrist.util.SQLUtils SQLUtils} are also accessible through this class.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class MySQLUtils extends SQLUtils
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(MySQLUtils.class);

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

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

  } // end constructor

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

  /**
   * Unlocks all database tables that were previously locked.  Executes the MySQL statement "UNLOCK TABLES;".
   *
   * @param conn The connection on which one or more tables were previously locked.
   */
  public static final void unlockTables(Connection conn)
  {
    Statement stmt = null;
    try
    { // do the update
      stmt = conn.createStatement();
      stmt.executeUpdate("UNLOCK TABLES;");

    } // end try
    catch (SQLException e)
    { // warn if there was an error here
      logger.warn("DB error in unlockTables()",e);

    } // end catch
    finally
    { // shutdown the statement before we go
      shutdown(stmt);

    } // end finally

  } // end unlockTables

  /**
   * Gets the ID of the most recent insert made to a table with an AUTO_INCREMENT column.  This assumes that the
   * column is of integer type.  Executes the MySQL statement "SELECT LAST_INSERT_ID();" and returns the value
   * that that statement returns.
   *
   * @param conn Database connection on which to perform the operation.
   * @return The value of the last inserted ID on this connection.
   * @exception java.sql.SQLException If an error occurred in the execution, or if the SELECT statement returned
   *            no rows (which it should not do).
   */
  public static final int getLastInsertInt(Connection conn) throws SQLException
  {
    Statement stmt = null;
    ResultSet rs = null;
    try
    { // perform the operation
      stmt = conn.createStatement();
      rs = stmt.executeQuery("SELECT LAST_INSERT_ID();");
      if (!(rs.next()))
	throw new SQLException("internal error - getLastInsertInt SELECT should have returned OK");
      return rs.getInt(1);

    } // end try
    finally
    { // shut down the objects before we go
      shutdown(rs);
      shutdown(stmt);

    } // end finally

  } // end getLastInsertInt

} // end class MySQLUtils
