/* Table.java - encapsulates a table.  */
import java.util.*;

public class Table {

  String pkey;        // The primary key 
  String name;        // Name of this table 
  Vector columns;     // Columns to include in result set
  Hashtable col_desc; // Description of each column 
  Vector children;    // Child tables 
  Vector constraints; // All constraints 

  /* Constructor for a new, uninitialized table. */
  protected Table() {
    columns     = new Vector();
    children    = new Vector();
    constraints = new Vector();
    col_desc = new Hashtable();
  }

  /* Constructor for a new table. */
  public Table(String name, String pkey) {
    this();
    this.name = name;
    this.pkey = pkey;
  }

  /* Return the primary key. */
  public String getPrimaryKey() { 
    return pkey;
  }

  /* Creates a new constraint, sets its values, and adds 
   * it to this table's list of constraints.
   */
  public void addConstraint(String column, 
                            int op, 
                            String value) 
  {
    Constraint c = new Constraint();
    c.column = column;
    c.op     = op;
    c.value  = value;
    constraints.add(c);
  }

  /* Convenience method for constraining results to
   * a single row.
   */
  public void constrainByPrimaryKey(String value) {
    addConstraint(pkey, Constraint.EQ, "'" + value + "'");
  }

  /* Adds a column. */
  public void addColumn(String column, 
                        String description) 
  {
    columns.add(column);
    col_desc.put(column, description);
  }

  /* Adds a child table, related by a foreign key. */
  public void addChildTable(Table table, String fkey) {
    children.add(table);
    addConstraint(this.pkey, 
                  Constraint.EQ, 
                  table.name + "." + fkey);
  }

  /* Search this table, and (recursively) all child 
   * tables for a column, then return its description.
   */
  public String findColumnDescription(String column) {

    String result = (String) col_desc.get(column);
    if (result != null) { // I found the column.
      return result;
    } else {

      // Search all the children for the column.
      Enumeration e = children.elements();
      while (e.hasMoreElements()) {
        Table child = (Table) e.nextElement();
        result = child.findColumnDescription(column);
        if (result != null) { // Found it!
          return result;
        }
      }
      return null; // Column wasn't found in any children.

    }
  }

  /* Search this table, and (recursively) all child
   * tables to see if column is a primary key.
   */
  public boolean isPrimaryKey(String column) {

    if (pkey.equals(column)) { // I found the column.
      return true;
    } else {

      // Search all the children.
      Enumeration e = children.elements();
      while (e.hasMoreElements()) {
        Table child = (Table) e.nextElement();
        if (child.isPrimaryKey(column)) { // Found it!
          return true;
        }
      }
      return false;
    }
  }

}