package com.akiban.cserver.itests.alter;

import java.util.ArrayList;
import java.util.List;

import com.akiban.ais.model.AkibaInformationSchema;
import com.akiban.ais.model.Column;
import com.akiban.ais.model.Index;
import com.akiban.ais.model.IndexColumn;
import com.akiban.ais.model.Table;
import com.akiban.ais.model.TableName;
import com.akiban.ais.model.UserTable;
import com.akiban.cserver.api.common.NoSuchTableException;
import com.akiban.cserver.api.common.ResolutionException;
import com.akiban.cserver.api.common.TableId;
import com.akiban.cserver.itests.ApiTestBase;

public class AlterTestBase extends ApiTestBase {
    public AkibaInformationSchema createAISWithTable(TableId id) throws NoSuchTableException, ResolutionException {
        ddl().resolveTableId(id);
        Integer tableId = id.getTableId(null);
        TableName tname = ddl().getTableName(id);
        String schemaName = tname.getSchemaName();
        String tableName = tname.getTableName();

        AkibaInformationSchema ais = new AkibaInformationSchema();
        UserTable.create(ais, schemaName, tableName, tableId);
        
        return ais;
    }

    public Index addIndexToAIS(AkibaInformationSchema ais, String sname, String tname, String iname, 
            String[] refColumns, boolean isUnique) {
        Table table = ais.getTable(sname, tname);
        Table curTable = ddl().getAIS(session).getTable(sname, tname);
        Index index = Index.create(ais, table, iname, -1, isUnique, isUnique ? "UNIQUE" : "KEY");

        if(refColumns != null) {
            int pos = 0;
            for (String colName : refColumns) {
                Column col = curTable.getColumn(colName);
                Column refCol = Column.create(table, col.getName(), col.getPosition(), col.getType());
                refCol.setTypeParameter1(col.getTypeParameter1());
                refCol.setTypeParameter2(col.getTypeParameter2());
                Integer indexedLen = col.getMaxStorageSize().intValue();
                index.addColumn(new IndexColumn(index, refCol, pos++, true, indexedLen));
            }
        }
        
        return index;
    }
    
    public List<Index> getAllIndexes(AkibaInformationSchema ais)
    {
        ArrayList<Index> indexes = new ArrayList<Index>();
        for(UserTable tbl : ais.getUserTables().values()) {
            indexes.addAll(tbl.getIndexes());
        }
        return indexes;
    }
}

@@ -1,8 +1,665 @@
+/**%0A * Copyright (C) 2011 Akiban Technologies Inc.%0A * This program is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Affero General Public License, version 3,%0A * as published by the Free Software Foundation.%0A *%0A * This program is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with this program.  If not, see http://www.gnu.org/licenses.%0A */%0A%0A
 package 

Add missing license header on new file.