FileDocCategorySizeDatePackage
TableConstraintEnumeration.javaAPI DocExample1749Tue Dec 08 01:21:00 GMT 1998oisoft.togetherx.scripts.SQL.impl

TableConstraintEnumeration.java

package oisoft.togetherx.scripts.SQL.impl;

import java.util.Enumeration;

class TableConstraintEnumeration implements Enumeration{
  private TableImpl myTable;
  //private ForeignKeyEnumeration myForeignKeys = null;
  private Enumeration myPrimaryKeyFields = null;
  private boolean myPrimaryKeyPassed = false;
  private Enumeration myForeignKeys = null;

  private Object myCurrent;

  public TableConstraintEnumeration(TableImpl table){
    myTable = table;
    advance();
  }
  public boolean hasMoreElements(){
    return myCurrent != null;
  }
  public Object nextElement(){
    Object result = myCurrent;
    advance();
    return result;
  }

  private void advance(){
    myCurrent = null;
    if( hasPrimaryKey() ){
      myPrimaryKeyPassed = true;
      Enumeration primaryKeyFields = getPrimaryKeyFields();
      if( primaryKeyFields.hasMoreElements() ){
        myCurrent = new PrimaryKeyImpl(myTable,primaryKeyFields);
      }
    }
    while(myCurrent == null && getForeignKeys().hasMoreElements() ){
      ForeignKeyImpl tmp = (ForeignKeyImpl)getForeignKeys().nextElement();
      if(tmp.getColumns().hasMoreElements()){
        myCurrent = tmp;
      }
    }
  }
  private boolean hasPrimaryKey(){
    return !myPrimaryKeyPassed && getPrimaryKeyFields().hasMoreElements();
  }
  private Enumeration getPrimaryKeyFields(){
    if(myPrimaryKeyFields == null){
      myPrimaryKeyFields = myTable.getPrimaryKeyColumns();
    }
    return myPrimaryKeyFields;
  }
  private Enumeration getForeignKeys(){
    if(myForeignKeys == null){
      myForeignKeys = new ForeignKeyEnumeration(myTable,(SQLModelImpl)myTable.getModel());
    }
    return myForeignKeys;
  }
}