package eu.dnetlib.dlms.jdbc;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;

/**
 * MetaData describing an instance of class SimpleDOLResultSet.
 * 
 * @author lexis
 */
public class DOLResultSetMetaData implements ResultSetMetaData {

	/** ResultSet described by this metadata. */
	private DOLResultSet resultSet;

	/**
	 * Constructor.
	 * 
	 * @param resultSet
	 *            SimpleDOLResultSet instance described by this metadata
	 */
	public DOLResultSetMetaData(final DOLResultSet resultSet) {
		this.resultSet = resultSet;
	}

	public DOLResultSet getResultSet() {
		return this.resultSet;
	}

	public void setResultSet(final DOLResultSet resultSet) {
		this.resultSet = resultSet;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getCatalogName(int)
	 */
	public String getCatalogName(final int column) throws SQLException {
		return "";
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnClassName(int)
	 */
	public String getColumnClassName(final int column) throws SQLException {
		return this.resultSet.getColumnInfos()[column].getClass().getName();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnCount()
	 */
	public int getColumnCount() throws SQLException {
		return this.resultSet.getColumnInfos().length;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnDisplaySize(int)
	 */
	public int getColumnDisplaySize(final int column) throws SQLException {
		return 0;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnLabel(int)
	 */
	public String getColumnLabel(final int column) throws SQLException {
		return this.getColumnName(column);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnName(int)
	 */
	public String getColumnName(final int column) throws SQLException {
		return this.resultSet.getColumnInfos()[column].getName();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnType(int)
	 */
	public int getColumnType(final int column) throws SQLException {
		return this.resultSet.getColumnInfos()[column].getType().getTypeCode();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getColumnTypeName(int)
	 */
	public String getColumnTypeName(final int column) throws SQLException {
		return this.resultSet.getColumnInfos()[column].getType().name();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getPrecision(int)
	 */
	public int getPrecision(final int column) throws SQLException {
		//precision not applicable --> return 0
		return 0;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getScale(int)
	 */
	public int getScale(final int column) throws SQLException {
		//scale not applicable --> return 0
		return 0;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getSchemaName(int)
	 */
	public String getSchemaName(final int column) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#getTableName(int)
	 */
	public String getTableName(final int column) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isAutoIncrement(int)
	 */
	public boolean isAutoIncrement(final int column) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isCaseSensitive(int)
	 */
	public boolean isCaseSensitive(final int column) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isCurrency(int)
	 */
	public boolean isCurrency(final int column) throws SQLException {
		//we do not consider currency at all --> for sure the column is not a currency!
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isDefinitelyWritable(int)
	 */
	public boolean isDefinitelyWritable(final int column) throws SQLException {
		//Writes on column not allowed!
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isNullable(int)
	 */
	public int isNullable(final int column) throws SQLException {
		return columnNullableUnknown;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isReadOnly(int)
	 */
	public boolean isReadOnly(final int column) throws SQLException {
		return true;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isSearchable(int)
	 */
	public boolean isSearchable(final int column) throws SQLException {
		//We do not have where clause...
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isSigned(int)
	 */
	public boolean isSigned(final int column) throws SQLException {
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.ResultSetMetaData#isWritable(int)
	 */
	public boolean isWritable(final int column) throws SQLException {
		return false;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
	 */
	public boolean isWrapperFor(final Class<?> iface) throws SQLException {
		throw new SQLException("Do I need to be a wrapper of class " + iface + " ?");
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Wrapper#unwrap(java.lang.Class)
	 */
	public <T> T unwrap(final Class<T> iface) throws SQLException {
		throw new SQLException("Do I need to call unwrap with parameter class " + iface + " ?");
	}

}
