/* Copyright (c) 2003-2006 Ricebridge. All Rights Reserved. */


package com.ricebridge.csvman;


import org.jostraca.util.Internal;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;


/** Implementation of {@link java.sql.ResultSetMetaData} to present CSV header data.
 *    <p>This <code>ResultSetMetaData</code> implementation provides much the same interface as a normal <code>ResultSetMetaData</code>.
 *  It simply provides the headers of the CSV file via {@link #getColumnName}. 
 *  Apart from methods relating to column names, all other methods return default values which assume 
 *  that all data is in the form of <code>String</code>s.</p>
 *    <p>The <b><a href="CsvResultSetMetaData.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 */
public class CsvResultSetMetaData implements ResultSetMetaData {

  /** Number of columns of data. */
  protected int      iColumnCount = 0;

  /** Column names */
  protected String[] iColumnName  = null;

  /** Map from column name to index. */
  protected HashMap  iColumnMap   = new HashMap();


  /** Initialised with a <code>List</code> of <code>String[]</code> containing column headers. 
   *  @param pHeaders header names
   */
  public CsvResultSetMetaData( String[] pHeaders ) {
    String[] headers = (String[]) Internal.null_arg(pHeaders);

    iColumnCount = headers.length;
    iColumnName  = new String[ iColumnCount + 1 ];
    for( int cI = 1; cI <= iColumnCount; cI++ ) {
      iColumnName[cI] = headers[cI-1];
      if( null == iColumnName[cI] || "".equals( iColumnName[cI] ) ) {
        iColumnName[cI] = "Column "+cI;
      }
      iColumnMap.put( iColumnName[cI], new Integer(cI) );
    }
  }


    

  /** Find the index (starting at one, not zero) of a named column.
   *  @param pColumnName name of column
   */
  public int findColumn( String pColumnName ) {
    if( iColumnMap.containsKey( pColumnName ) ) {
      return ((Integer) iColumnMap.get(pColumnName)).intValue();
    }
    else {
      throw new CsvManagerException( CsvManagerException.CODE_unknown_column, pColumnName );
    }
  }


  /** Get the number of columns. */
  public int getColumnCount() throws SQLException {
    return iColumnCount;
  }


  /** Get the name of a column.
   *  @param pColumn index of column (from one, not zero)
   */
  public String getColumnLabel( int pColumn ) throws SQLException {
    return getColumnName( pColumn );
  }


  /** Get the name of a column.
   *  @param pColumn index of column (from one, not zero)
   */
  public String getColumnName( int pColumn ) throws SQLException {
    return iColumnName[pColumn];
  }


  /** No action &mdash; see {@link ResultSetMetaData#isAutoIncrement}. */ public boolean 
    isAutoIncrement( int pColumn ) throws SQLException { return true;  }
  /** No action &mdash; see {@link ResultSetMetaData#isCaseSensitive}. */ public boolean 
    isCaseSensitive(int pColumn) throws SQLException { return true; }
  /** No action &mdash; see {@link ResultSetMetaData#isSearchable}. */ public boolean 
    isSearchable(int pColumn) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSetMetaData#isCurrency}. */ public boolean 
    isCurrency(int pColumn) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSetMetaData#isNullable}. */ public int     
    isNullable(int pColumn) throws SQLException { return columnNoNulls; }
  /** No action &mdash; see {@link ResultSetMetaData#isSigned}. */ public boolean 
    isSigned(int pColumn) throws SQLException  { return true; }
  /** No action &mdash; see {@link ResultSetMetaData#getColumnDisplaySize}. */ public int     
    getColumnDisplaySize(int pColumn) throws SQLException { return 255; }
  /** No action &mdash; see {@link ResultSetMetaData#getSchemaName}. */ public String  
    getSchemaName(int pColumn) throws SQLException { return ""; }
  /** No action &mdash; see {@link ResultSetMetaData#getPrecision}. */ public int     
    getPrecision(int pColumn) throws SQLException { return 0; }
  /** No action &mdash; see {@link ResultSetMetaData#getScale}. */ public int     
    getScale(int pColumn) throws SQLException { return 0; }	
  /** No action &mdash; see {@link ResultSetMetaData#getTableName}. */ public String  
    getTableName(int pColumn) throws SQLException { return ""; }
  /** No action &mdash; see {@link ResultSetMetaData#getCatalogName}. */ public String  
    getCatalogName(int pColumn) throws SQLException { return ""; }
  /** No action &mdash; see {@link ResultSetMetaData#getColumnType}. */ public int     
    getColumnType(int pColumn) throws SQLException { return Types.VARCHAR; }
  /** No action &mdash; see {@link ResultSetMetaData#getColumnTypeName}. */ public String  
    getColumnTypeName(int pColumn) throws SQLException { return "VARCHAR"; }
  /** No action &mdash; see {@link ResultSetMetaData#isReadOnly}. */ public boolean 
    isReadOnly(int pColumn) throws SQLException { return true; }
  /** No action &mdash; see {@link ResultSetMetaData#isWritable}. */ public boolean 
    isWritable(int pColumn) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSetMetaData#isDefinitelyWritable}. */ public boolean 
    isDefinitelyWritable(int pColumn) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSetMetaData#getColumnClassName}. */ public String  
    getColumnClassName(int pColumn) throws SQLException { return "java.lang.String"; }

}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Saturday, November 04 2006 at 09:45