/*
 * 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) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.index;

import java.sql.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class IndexManagerOps_mysql extends IndexManagerOps
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public IndexManagerOps_mysql(DBConnectionPool pool)
  {
    super(pool);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class IndexManagerOps
   *--------------------------------------------------------------------------------
   */

  protected IndexOps createIndexOps(DBConnectionPool pool)
  {
    return new IndexOps_mysql(pool);

  } // end createIndexOps

  int getIndexID(int nsid, String name) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // create and execute the statement
      stmt = conn.prepareStatement("SELECT ndxid FROM ndx_main WHERE nsid = ? AND name = ?;");
      stmt.setInt(1,nsid);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getInt(1);
      else
	return -1;

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end getIndexID

  int createIndexID(int nsid, String name, String analyzer) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // lock the tables
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES ndx_main WRITE;");

      // look to see if it's already in the list
      stmt = conn.prepareStatement("SELECT ndxid FROM ndx_main WHERE nsid = ? AND name = ?;");
      stmt.setInt(1,nsid);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return -1;  // already present
      rs.close();
      rs = null;
      stmt.close();

      // Now insert it!
      stmt = conn.prepareStatement("INSERT INTO ndx_main (nsid, name, analyzer) VALUES (?, ?, ?);");
      stmt.setInt(1,nsid);
      stmt.setString(2,name);
      stmt.setString(3,analyzer);
      stmt.executeUpdate();
      return MySQLUtils.getLastInsertInt(conn);

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      MySQLUtils.unlockTables(conn);
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(stmt2);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end createIndexID

  void deleteIndex(int ndx) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    try
    { // get a connection
      conn = getConnection();

      // lock the tables
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES ndx_main WRITE, ndx_files WRITE, ndx_locks WRITE;");

      // Do the deletes.
      stmt = conn.prepareStatement("DELETE FROM ndx_main WHERE ndxid = ?;");
      stmt.setInt(1,ndx);
      stmt.executeUpdate();
      stmt.close();
      stmt = conn.prepareStatement("DELETE FROM ndx_files WHERE ndxid = ?;");
      stmt.setInt(1,ndx);
      stmt.executeUpdate();
      stmt.close();
      stmt = conn.prepareStatement("DELETE FROM ndx_locks WHERE ndxid = ?;");
      stmt.setInt(1,ndx);
      stmt.executeUpdate();

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      MySQLUtils.unlockTables(conn);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(stmt2);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end deleteIndex

} // end class IndexManagerOps_mysql
