package SampleDatabase;
import ModSQL.*;
import java.sql.*;

/* SampleDatabase/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>A sample DatabaseManager implementation for ModSQL.  This
 * class will automatically register itself with ModSQL.MetaManager
 * when it is loaded.  This class the table named 'tablename_Table'
 * when the table 'tablename' is requested.  A list of valid tables
 * is found in this class.  All table classes are assumed to be
 * included in the SampleDatabase package.  
 *
 * <p>The purpose of this database is to provided a strictly in
 * memory set of tables for testing purposes.  This database will
 * also support insert, update, delete, and create table.  All
 * changes to this database will occur only in memory and will be
 * lost when the database connection is closed.  
 *
 * @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 = "SampleDatabase";

  private static final String tables[] = {
    "Antiques",
    "AntiqueOwners",
    "EmployeeAddressTable",
    "EmployeeStatisticsTable",
    "Orders"
  };

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

  public boolean hasTable(String name) {
    for (int i=0; i<tables.length; ++i)
      if (name.equalsIgnoreCase(tables[i]))
	return true;
    return false;
  }
    
  public DatabaseTable openTable(String name) throws DatabaseException {
    return openTable(name,true);
  }
    
  public DatabaseTable openTable(String name, boolean readonly) 
    throws DatabaseException {
    for (int i=0; i<tables.length; ++i)
      if (name.equalsIgnoreCase(tables[i])) {
	if (!readonly)
	  throw new DatabaseException(pkg+" must be opened read-only");
	try {
	  return (DatabaseTable)Class.
	    forName(pkg+"."+tables[i]+"_Table").newInstance();
	}
	catch (Exception e) {
	  throw new DatabaseException("table "+name+
				      " could not be loaded (was "+
				      e.toString()+")");
	}
      }
    return null;
  }
    
  public DatabaseTable createTable(String name, boolean temporary) 
    throws DatabaseException {
    if (hasTable(name))
      throw new DatabaseException("table '"+name+"' already exists");
    return null;
  }

  public boolean createIndex(String name, String column) {
    return false;
  }
  public void dropTable(String name) throws DatabaseException {
    if (!hasTable(name))
      throw new DatabaseException("table '"+name+"' not found");
    throw new DatabaseException("cannot drop table '"+name+"'");
  }
  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;
  }

    
};


  
