// EXTENSION
// ISSUE: DVCORE-1065
// FIXED BY: Roman Pedchenko
// DATE: 2014.05.08
/**
 * Copyright © 2014 by Data Virtuality GmbH
 */
package org.apache.ddlutils.model;

import java.io.IOException;
import java.util.List;

import org.apache.ddlutils.platform.SqlBuilder;

/**
 * Index expression class.
 *
 * @author Roman Pedchenko
 * @date 2014.05.08
 */
public class IndexExpression extends IndexColumn {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5561579496166406385L;

	private String expression;
	private List<Column> columns;

	/**
	 * @param expression
	 * @param cols
	 */
	public IndexExpression(String expression, List<Column> cols) {
		this.columns = cols;
		this.expression = expression;
	}

	/**
	 * @return the expression
	 */
	public String getExpression() {
		return expression;
	}

	/**
	 * @param expression the expression to set
	 */
	public void setExpression(String expression) {
		this.expression = expression;
	}

	/**
	 * @return the columns
	 */
	public List<Column> getColumns() {
		return columns;
	}

	/**
	 * @param columns the columns to set
	 */
	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	/* (non-Javadoc)
	 * @see org.apache.ddlutils.model.IndexColumn#resolveWithTable(org.apache.ddlutils.model.Table)
	 */
	@Override
	public void resolveWithTable(Table table) {
		for (Column col : getColumns())
			resolveColumn(table, col.getName());
	}

	/* (non-Javadoc)
	 * @see org.apache.ddlutils.model.IndexColumn#getSql(org.apache.ddlutils.platform.SqlBuilder)
	 */
	@Override
	public String getSql(SqlBuilder sqlBuilder) throws IOException {
		if (getColumns() == null || getColumns().size() < 1)
			return expression;
		Object[] colsArr = new Object[getColumns().size()];
		for (int i = 0; i < getColumns().size(); i++)
			colsArr[i] = sqlBuilder.getDelimitedIdentifier(sqlBuilder.getColumnName(getColumns().get(i)));
		return String.format(expression, colsArr);
	}
}
// END EXTENSION
