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

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

public class NamespaceCacheOps_mysql extends NamespaceCacheOps
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

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

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class NamespaceCacheOps
   *--------------------------------------------------------------------------------
   */

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

      // lock the table while we do this
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES namespaces WRITE;");

      // try the initial SELECT to see if it works
      stmt = conn.prepareStatement("SELECT nsid FROM namespaces WHERE namespace = ?;");
      stmt.setString(1,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getInt(1);

      // it wasn't present - try adding it
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      stmt = conn.prepareStatement("INSERT INTO namespaces (namespace) VALUES (?);");
      stmt.setString(1,name);
      stmt.executeUpdate();
      return MySQLUtils.getLastInsertInt(conn);

    } // end try
    catch (SQLException e)
    { // translate this to a 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 namespaceNameToId

  String namespaceIdToName(int id) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // lock the table while we do this
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES namespaces READ;");

      // try the SELECT to see if it works
      stmt = conn.prepareStatement("SELECT namespace FROM namespaces WHERE nsid = ?;");
      stmt.setInt(1,id);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getString(1);
      else
	return null;

    } // end try
    catch (SQLException e)
    { // translate the exception and rethrow
      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 namespaceIdToName

} // end class NamespaceCacheOps_mysql
