package ModSQL;
import java.sql.SQLException;

/* $Id: ResultSetMetaData.java,v 1.5 2003/05/29 07:19:40 cvs Exp $
 *
 * Copyright (c) 2003 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/**
 * <p>A ResultSetMetaData object can be used to find out about the types 
 * and properties of the columns in a ResultSet.
 *
 * @author chris.studholme@utoronto.ca
 */
public class ResultSetMetaData implements java.sql.ResultSetMetaData {

  /** Query that generated the ResultSet. */
  private Select query;

  /** 
   * Constructor.
   *
   * @param query query that generated the ResultSet
   */
  protected ResultSetMetaData(Select query) {
    this.query=query;
  }

  /**
   * What's the number of columns in the ResultSet?
   *
   * @return the number
   * @throws SQLException if a database-access error occurs.
   */
  public int getColumnCount() throws SQLException {
    return query.getColumnCount();
  }

  /**
   * Is the column automatically numbered, thus read-only?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isAutoIncrement(int column) throws SQLException {
    return false;
  }
  
  /**
   * Does a column's case matter?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isCaseSensitive(int column) throws SQLException {
    return true;
  }
  
  /**
   * Can the column be used in a where clause?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isSearchable(int column) throws SQLException {
    return true;
  }
  
  /**
   * Is the column a cash value?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isCurrency(int column) throws SQLException {
    return false;
  }
  
  /**
   * Can you put a NULL in this column?		
   *
   * @param column the first column is 1, the second is 2, ...
   * @return columnNoNulls, columnNullable or columnNullableUnknown
   * @throws SQLException if a database-access error occurs.
   */
  public int isNullable(int column) throws SQLException {
    return columnNullableUnknown;
  }
  
  /**
   * Is the column a signed number?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isSigned(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's the column's normal max width in chars?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return max width
   * @throws SQLException if a database-access error occurs.
   */
  public int getColumnDisplaySize(int column) throws SQLException {
    return query.getMaxResultSize(column-1);
  }
  
  /**
   * What's the suggested column title for use in printouts and
   * displays?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so 
   * @throws SQLException if a database-access error occurs.
   */
  public String getColumnLabel(int column) throws SQLException {
    String result = query.getColumnName(column-1);
    if (result==null)
      return "Column"+column;
    // remove "tablename." prefix
    int lastdot = result.lastIndexOf('.');
    if (lastdot>0)
      result = result.substring(lastdot+1);
    return result;
  }
  
  /**
   * What's a column's name?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return column name
   * @throws SQLException if a database-access error occurs.
   */
  public String getColumnName(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's table's schema?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return schema name or "" if not applicable
   * @throws SQLException if a database-access error occurs.
   */
  public String getSchemaName(int column) throws SQLException { 
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's number of decimal digits?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return precision
   * @throws SQLException if a database-access error occurs.
   */
  public int getPrecision(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's number of digits to right of the decimal point?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return scale
   * @throws SQLException if a database-access error occurs.
   */
  public int getScale(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's table name? 
   *
   * @return table name or "" if not applicable
   * @throws SQLException if a database-access error occurs.
   */
  public String getTableName(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's table's catalog name?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return column name or "" if not applicable.
   * @throws SQLException if a database-access error occurs.
   */
  public String getCatalogName(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * What's a column's SQL type?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return SQL type
   * @throws SQLException if a database-access error occurs.
   * @see java.sql.Types
   */
  public int getColumnType(int column) throws SQLException {
    return query.getSQLType(column-1);
  }

  /**
   * What's a column's data source specific type name?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return type name
   * @throws SQLException if a database-access error occurs.
   */
  public String getColumnTypeName(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }
  
  /**
   * Is a column definitely not writable?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isReadOnly(int column) throws SQLException {
    return true;
  }

  /**
   * Is it possible for a write on the column to succeed?
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isWritable(int column) throws SQLException {
    return false;
  }
  
  /**
   * Will a write on the column definitely succeed?	
   *
   * @param column the first column is 1, the second is 2, ...
   * @return true if so
   * @throws SQLException if a database-access error occurs.
   */
  public boolean isDefinitelyWritable(int column) throws SQLException {
    return false;
  }

  /**
   * <p>Return the fully qualified name of the Java class whose instances
   * are manufactured if ResultSet.getObject() is called to retrieve a value
   * from the column.  ResultSet.getObject() may return a subClass of the
   * class returned by this method.
   */
  public String getColumnClassName(int column) throws SQLException {
    throw new SQLException("not implemented");    
  }


}
