/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.empire.db;

import org.apache.empire.exceptions.InvalidArgumentException;

/**
 * This class allows to create an expression based index
 */
public class DBExpressionIndex extends DBIndex
{
    // *Deprecated* private static final long serialVersionUID = 1L;
    
    private DBColumnExpr[] columnExpressions;
    
    /**
     * Constructs a DBExpresionIndex
     * 
     * @param name the index name
     * @param type the index type
     * @param columnExpressions an array of one or more column expressions of the index
     */
    public DBExpressionIndex(String name, DBIndexType type, DBColumnExpr... columnExpressions)
    {
        super(name, type, null);
        // columnExpressions
        if (columnExpressions==null || columnExpressions.length==0)
            throw new InvalidArgumentException("columnExpressions", columnExpressions);
        // set expression
        this.columnExpressions = columnExpressions;
    }

    /**
     * Constructs a DBExpresionIndex
     * @param name the index name
     * @param unique flag whether the index is unique
     * @param columnExpressions the index columns
     * Overload for convenience
     */
    public DBExpressionIndex(String name, boolean unique, DBColumnExpr... columnExpressions)
    {
        this(name, (unique ? DBIndexType.UNIQUE : DBIndexType.STANDARD), columnExpressions);
    }
    
    @Override
	public final DBDatabase getDatabase()
    {
        return columnExpressions[0].getDatabase();
    }

    /**
     * Returns the columnExpressions belonging to this index.
     * 
     * @return the columnExpressions belonging to this index
     */
    @Override
    public DBColumnExpr[] getExpressions()
    {
        return columnExpressions;
    }

    /**
     * Checks if this index contains the column col  
     * 
     * @param col the column
     * 
     * @return true if this index contains this column
     */
    @Override
    public boolean contains(DBColumn col)
    {
        for (DBColumnExpr columnExpression : columnExpressions)
        {
            if (col.equals(columnExpression.getUpdateColumn()))
                return true;
        }    
        return false;
    }

    /**
     * Gets the position of a specified DBColumn object.
     * 
     * @param col the column 
     * 
     * @return the position or -1 if the column was not found
     */
    @Override
    public int getColumnPos(DBColumn col)
    {
        return -1;
    }

}

