package TemporaryDatabase;
import ModSQL.*;
import java.sql.*;
import java.util.*;

/* TemporaryDatabase/Manager.java
 *
 * Copyright (c) 2004 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/**
 * <p>Database for storage of temporary (in memory) tables.
 *
 * @author chris.studholme@utoronto.ca
 */

public class Manager implements DatabaseManager {

  private static final int majorVersion = 0;
  private static final int minorVersion = 40;

  private static final String pkg = "TemporaryDatabase";

  private ArrayList tables; // TableData objects

  public Manager() {
    tables = new ArrayList();
  }

  public int getMajorVersion() {
    return majorVersion;
  }
  public int getMinorVersion() {
    return minorVersion;
  }

  public boolean hasTable(String name) {
    Iterator i = tables.iterator();
    while (i.hasNext()) {
      TableData t = (TableData)i.next();
      if (name.equalsIgnoreCase(t.name))
	return true;
    }
    return false;
  }
        
  public DatabaseTable openTable(String name, boolean readonly) 
    throws DatabaseException {
    Iterator i = tables.iterator();
    while (i.hasNext()) {
      TableData td = (TableData)i.next();
      if (name.equalsIgnoreCase(td.name))
	return new Table(td,readonly);
    }
    return null;
  }
  public DatabaseTable openTable(String name) throws DatabaseException {
    return openTable(name,true);
  }
    
  public DatabaseTable createTable(String name, boolean temporary) 
    throws DatabaseException {
    if (hasTable(name))
      throw new DatabaseException("table '"+name+"' already exists");
    TableData td = new TableData(name);
    tables.add(td);
    return new Table(td,false);
  }

  public void dropTable(String name) throws DatabaseException {
    Iterator i = tables.iterator();
    while (i.hasNext()) {
      TableData t = (TableData)i.next();
      if (name.equalsIgnoreCase(t.name)) {
	i.remove();
	return;
      }
    }
    throw new DatabaseException("table '"+name+"' does not exist");
  }

  public boolean createIndex(String name, String column) {
    return false;
  }
  public boolean dropIndex(String name, String column)
    throws DatabaseException {
    if (!hasTable(name))
      throw new DatabaseException("table '"+name+"' not found");
    return false;
  }
    
  public Function getFunction(String function_name) {
    return null;
  }
};
  
