/* Copyright (c) 2003-2006 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */


package com.ricebridge.csvman;


import org.jostraca.util.Internal;

import java.sql.*;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Map;
import java.util.Calendar;
import java.util.ArrayList;


/** Implementation of a {@link java.sql.ResultSet ResultSet} to present CSV data.
 *    <p>This <code>ResultSet</code> provides much the same interface as a database <code>ResultSet</code>.
 *  This implementation assumes that your data is fairly well behaved and attempts direct data type
 *  conversions for methods like {@link #getInt}. Bad data will cause an exception to be thrown.
 *  Refer to {@link java.sql.ResultSet} for method documentation if none is given here.</p>
 *    <p>The <b><a href="CsvResultSet.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 CsvResultSet implements ResultSet {

  // not supported

  
  /** Not supported. */ public Array      
    getArray( String pColName ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Array      
    getArray( int pIndex ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Blob       
    getBlob( String pColName ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Blob       
    getBlob( int pIndex ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Clob       
    getClob( String pColName ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Clob       
    getClob( int pIndex ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Object     
    getObject( String pColName, Map pMap ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Object     
    getObject( int pIndex, Map pMap ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Ref        
    getRef( String pColName ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Ref        
    getRef( int pIndex ) throws SQLException { throw new UnsupportedOperationException(); }

  /** Not supported. */ public Statement  
    getStatement() throws SQLException { throw new UnsupportedOperationException(); }


  // no action
    
  /** No action &mdash; see {@link ResultSet#getWarnings}. */ public SQLWarning
    getWarnings() throws SQLException { return null; }
  /** No action &mdash; see {@link ResultSet#getCursorName}. */ public String
    getCursorName() throws SQLException { return ""; }
  /** No action &mdash; see {@link ResultSet#rowDeleted}. */ public boolean
    rowDeleted( ) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSet#rowInserted}. */ public boolean
    rowInserted( ) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSet#rowUpdated}. */ public boolean
    rowUpdated( ) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSet#wasNull}. */ public boolean
    wasNull( ) throws SQLException { return false; }
  /** No action &mdash; see {@link ResultSet#getFetchDirection}. */ public int
    getFetchDirection( ) throws SQLException { return FETCH_FORWARD; }
  /** No action &mdash; see {@link ResultSet#setFetchDirection}. */ public void
    setFetchDirection( int pDirection ) throws SQLException { }
  /** No action &mdash; see {@link ResultSet#getFetchSize}. */ public int
    getFetchSize( ) throws SQLException { return 0; }
  /** No action &mdash; see {@link ResultSet#setFetchSize}. */ public void
    setFetchSize( int pRows ) throws SQLException { }
  /** No action &mdash; see {@link ResultSet#getConcurrency}. */ public int
    getConcurrency( ) throws SQLException { return CONCUR_READ_ONLY; }
  /** No action &mdash; see {@link ResultSet#getType}. */ public int
    getType( ) throws SQLException { return TYPE_FORWARD_ONLY; }
  /** No action &mdash; see {@link ResultSet#cancelRowUpdates}. */ public void
    cancelRowUpdates( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#clearWarnings}. */ public void
    clearWarnings( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#close}. */ public void
    close( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#deleteRow}. */ public void
    deleteRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#insertRow}. */ public void
    insertRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#moveToCurrentRow}. */ public void
    moveToCurrentRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#moveToInsertRow}. */ public void
    moveToInsertRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#refreshRow}. */ public void
    refreshRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateArray}. */ public void
    updateArray( String pColumnName, Array pArray ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateArray}. */ public void
    updateArray( int pColumnIndex, Array pArray ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateAsciiStream}. */ public void
    updateAsciiStream( String pColumnName, InputStream pInputStream, int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateAsciiStream}. */ public void
    updateAsciiStream( int pColumnIndex, InputStream pInputStream,  int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBigDecimal}. */ public void
    updateBigDecimal( String pColumnName, BigDecimal pBigDecimal ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBigDecimal}. */ public void
    updateBigDecimal( int pColumnIndex, BigDecimal pBigDecimal ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBinaryStream}. */ public void
    updateBinaryStream( String pColumnName, InputStream pInputStream, int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBinaryStream}. */ public void
    updateBinaryStream( int pColumnIndex, InputStream pInputStream,  int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBlob}. */ public void
    updateBlob( String pColumnName, Blob pBlob ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBlob}. */ public void
    updateBlob( int pColumnIndex, Blob pBlob ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBoolean}. */ public void
    updateBoolean( String pColumnName, boolean pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBoolean}. */ public void
    updateBoolean( int pColumnIndex, boolean pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateByte}. */ public void
    updateByte( String pColumnName, byte pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateByte}. */ public void
    updateByte( int pColumnIndex, byte pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBytes}. */ public void
    updateBytes( String pColumnName, byte pBytes[] ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateBytes}. */ public void
    updateBytes( int pColumnIndex, byte pBytes[] ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateCharacterStream}. */ public void
    updateCharacterStream( String pColumnName, Reader pReader, int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateCharacterStream}. */ public void
    updateCharacterStream( int pColumnIndex, Reader pReader, int pLength ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateClob}. */ public void
    updateClob( String pColumnName, Clob pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateClob}. */ public void
    updateClob( int pColumnIndex, Clob pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateDate}. */ public void
    updateDate( String pColumnName, Date pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateDate}. */ public void
    updateDate( int pColumnIndex, Date pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateDouble}. */ public void
    updateDouble( String pColumnName, double pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateDouble}. */ public void
    updateDouble( int pColumnIndex, double pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateFloat}. */ public void
    updateFloat( String pColumnName, float pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateFloat}. */ public void
    updateFloat( int pColumnIndex, float pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateInt}. */ public void
    updateInt( String pColumnName, int pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateInt}. */ public void
    updateInt( int pColumnIndex, int pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateLong}. */ public void
    updateLong( String pColumnName, long pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateLong}. */ public void
    updateLong( int pColumnIndex, long pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateNull}. */ public void
    updateNull( String pColumnName ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateNull}. */ public void
    updateNull( int pColumnIndex ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateObject}. */ public void
    updateObject( String pColumnName, Object pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateObject}. */ public void
    updateObject( String pColumnName, Object pValue, int pScale )  throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateObject}. */ public void
    updateObject( int pColumnIndex, Object pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateObject}. */ public void
    updateObject( int pColumnIndex, Object pValue, int pScale ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateRef}. */ public void
    updateRef( String pColumnName, Ref pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateRef}. */ public void
    updateRef( int pColumnIndex, Ref pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateRow}. */ public void
    updateRow( ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateShort}. */ public void
    updateShort( String pColumnName, short pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateShort}. */ public void
    updateShort( int pColumnIndex, short pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateString}. */ public void
    updateString( String pColumnName, String pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateString}. */ public void
    updateString( int pColumnIndex, String pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateTime}. */ public void
    updateTime( String pColumnName, Time pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateTime}. */ public void
    updateTime( int pColumnIndex, Time pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateTimestamp}. */ public void
    updateTimestamp( String pColumnName, Timestamp pValue ) throws SQLException {}
  /** No action &mdash; see {@link ResultSet#updateTimestamp}. */ public void
    updateTimestamp( int pColumnIndex, Timestamp pValue )  throws SQLException {}




  // supported

  /** Data as a <code>List</code> of <code>String[]</code> arrays. */
  protected ArrayList  iData       = new ArrayList();

  /** Current line of data. */
  protected String[]   iLine       = null;

  /** index of current line. */
  protected long       iLineIndex  = -1;

  /** Default data format for date parsing. */
  protected DateFormat iDateFormat = DateFormat.getDateInstance();

  /** ResultSetMetaData provider */
  protected CsvResultSetMetaData iCsvRSMD = null;


  /** Create a new <code>ResultSet</code> from CSV data.
   *    <p>When <code>pHasHeaders</code> is <code>true</code>, the first line of data is used as the names of the columns 
   *  and {@link #getMetaData} will return these column names.</p>
   *    <p>When <code>pHasHeaders</code> is <code>false</code>, default names of the form <i>Column X</i> are generated.</p>
   *  @param pData <code>List</code> of <code>String[]</code>
   *  @param pHasHeaders first row is headers
   */
  public CsvResultSet( ArrayList pData, boolean pHasHeaders, int pExpectedNumFields ) {
    setData( pData, pHasHeaders, pExpectedNumFields );
  }


  /** Create a new empty <code>ResultSet</code>.
   *    <p>Initialise with {@link #setData}</p>
   */
  public CsvResultSet() {
    // do nothing
  }


  /** Set data for <code>ResultSet</code>.
   *  @param pData       <code>List</code> of <code>String[]</code> arrays containing data
   *  @param pHasHeaders first line of data is headers
   */
  public void setData( ArrayList pData, boolean pHasHeaders, int pExpectedNumFields ) {
    iData = (ArrayList) Internal.null_arg( pData );

    // get rid of column names row
    if( 0 < iData.size() ) {
      if( pHasHeaders ) {
        iCsvRSMD = new CsvResultSetMetaData( (String[]) iData.get(0) );
        iData.remove(0);
      }
      else {
        int numfields = ((String[]) iData.get(0)).length;
        iCsvRSMD = new CsvResultSetMetaData( new String[ numfields ] );
      }
    }
    else {
      iCsvRSMD = new CsvResultSetMetaData( new String[pExpectedNumFields] );
    }
  }



  // ResultSet methods

  /** As per {@link ResultSet#getMetaData}. */
  public ResultSetMetaData getMetaData( ) throws SQLException {
    return iCsvRSMD;
  }


  /** As per {@link ResultSet#findColumn}. */
  public int findColumn( String pColumnName  ) throws SQLException {
    return iCsvRSMD.findColumn( pColumnName );
  }


  /** As per {@link ResultSet#absolute}. */
  public boolean absolute( int pRow  ) throws SQLException {
    if( pRow > -1 ) {
      iLineIndex = pRow - 1;
    }
    else {
      iLineIndex = iData.size() + pRow;
    }
    return moveToLine();
  }


  /** As per {@link ResultSet#first}. */
  public boolean first( ) throws SQLException {
    return absolute(0);
  }


  /** As per {@link ResultSet#last}. */
  public boolean last( ) throws SQLException {
    return absolute(-1);
  }


  /** As per {@link ResultSet#isAfterLast}. */
  public boolean isAfterLast( ) throws SQLException {
    return iLineIndex >= iData.size(); 
  }


  /** As per {@link ResultSet#isBeforeFirst}. */
  public boolean isBeforeFirst( ) throws SQLException {
    return iLineIndex <= -1;
  }


  /** As per {@link ResultSet#isFirst}. */
  public boolean isFirst( ) throws SQLException {
    return 0 == iLineIndex;
  }


  /** As per {@link ResultSet#isLast}. */
  public boolean isLast( ) throws SQLException {
    return iData.size() - 1 == iLineIndex;
  }


  /** As per {@link ResultSet#next}. */
  public boolean next( ) throws SQLException {
    return relative(1);
  }


  /** As per {@link ResultSet#previous}. */
  public boolean previous( ) throws SQLException {
    return relative(-1);
  }


  /** As per {@link ResultSet#relative}. */
  public boolean relative( int pRows  ) throws SQLException {
    iLineIndex += pRows;
    return moveToLine();
  }


  /** As per {@link ResultSet#getRow}. */
  public int getRow( ) throws SQLException {
    return (int) iLineIndex+1;
  }


  /** As per {@link ResultSet#afterLast}. */
  public void afterLast( ) throws SQLException {
    iLineIndex = iData.size();
  }


  /** As per {@link ResultSet#beforeFirst}. */
  public void beforeFirst( ) throws SQLException {
    iLineIndex = -1;
  }


  /** As per {@link ResultSet#getBigDecimal}. */
  public BigDecimal getBigDecimal( String pColumnName  ) throws SQLException {
    return getBigDecimal( findColumn(pColumnName) );
  }


  /** As per {@link ResultSet#getBigDecimal}. */
  public BigDecimal getBigDecimal( String pColumnName, int pScale  ) throws SQLException {
    return getBigDecimal( findColumn(pColumnName), pScale );
  }


  /** As per {@link ResultSet#getBigDecimal}. */
  public BigDecimal getBigDecimal( int pColumnIndex  ) throws SQLException {
    return new BigDecimal( iLine[pColumnIndex-1] );
  }
  

  /** As per {@link ResultSet#getBigDecimal}. */
  public BigDecimal getBigDecimal( int pColumnIndex, int pScale  ) throws SQLException {
    return new BigDecimal( iLine[pColumnIndex-1] ).setScale( pScale );
  }


  /** As per {@link ResultSet#getObject}. */
  public Object getObject( String pColumnName  ) throws SQLException {
    return getObject(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getObject}. */
  public Object getObject( int pColumnIndex  ) throws SQLException {
    return iLine[pColumnIndex-1];
  }


  /** As per {@link ResultSet#getString}. */
  public String getString( String pColumnName  ) throws SQLException {
    return getString(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getString}. */
  public String getString( int pColumnIndex  ) throws SQLException {
    return iLine[pColumnIndex-1];
  }


  /** As per {@link ResultSet#getBoolean}. */
  public boolean getBoolean( String pColumnName  ) throws SQLException {
    return getBoolean( findColumn(pColumnName ) );
  }


  /** As per {@link ResultSet#getBoolean}. */
  public boolean getBoolean( int pColumnIndex  ) throws SQLException {
    return new Boolean( iLine[pColumnIndex-1] ).booleanValue();
  }


  /** As per {@link ResultSet#getByte}. */
  public byte getByte( String pColumnName  ) throws SQLException {
    return getByte( findColumn(pColumnName) );
  }


  /** As per {@link ResultSet#getByte}. */
  public byte getByte( int pColumnIndex  ) throws SQLException {
    return iLine[pColumnIndex-1].getBytes()[0];
  }


  /** As per {@link ResultSet#getBytes}. */
  public byte[] getBytes( String pColumnName  ) throws SQLException {
    return getBytes(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getBytes}. */
  public byte[] getBytes( int pColumnIndex  ) throws SQLException {
    return iLine[pColumnIndex-1].getBytes();
  }


  /** As per {@link ResultSet#getDouble}. */
  public double getDouble( String pColumnName  ) throws SQLException {
    return getDouble(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getDouble}. */
  public double getDouble( int pColumnIndex  ) throws SQLException {
    return Double.parseDouble(iLine[pColumnIndex-1]);
  }


  /** As per {@link ResultSet#getFloat}. */
  public float getFloat( String pColumnName  ) throws SQLException {
    return getFloat(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getFloat}. */
  public float getFloat( int pColumnIndex  ) throws SQLException {
    return Float.parseFloat(iLine[pColumnIndex-1]);
  }


  /** As per {@link ResultSet#getInt}. */
  public int getInt( String pColumnName  ) throws SQLException {
    return getInt(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getInt}. */
  public int getInt( int pColumnIndex  ) throws SQLException {
    return Integer.parseInt(iLine[pColumnIndex-1]);
  }


  /** As per {@link ResultSet#getAsciiStream}. */
  public InputStream getAsciiStream( String pColumnName  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getAsciiStream}. */
  public InputStream getAsciiStream( int pColumnIndex  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getBinaryStream}. */
  public InputStream getBinaryStream( String pColumnName  )  throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getBinaryStream}. */
  public InputStream getBinaryStream( int pColumnIndex  )  throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getUnicodeStream}. */
  public InputStream getUnicodeStream( String pColumnName  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getUnicodeStream}. */
  public InputStream getUnicodeStream( int pColumnIndex  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getCharacterStream}. */
  public Reader getCharacterStream( String pColumnName  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getCharacterStream}. */
  public Reader getCharacterStream( int pColumnIndex  ) throws SQLException {
    return null;
  }


  /** As per {@link ResultSet#getURL}. */
  public URL getURL( String pColumnName  ) throws SQLException {
    return getURL( findColumn(pColumnName) );
  }


  /** As per {@link ResultSet#getURL}. */
  public URL getURL( int pColumnIndex  ) throws SQLException {
    try {
      return new URL( iLine[pColumnIndex-1] );
    }
    catch( Exception e ) {
      throw new SQLException( e.getMessage() );
    }
  }


  /** As per {@link ResultSet#getDate}. */
  public Date getDate( String pColumnName  ) throws SQLException {
    return getDate( findColumn(pColumnName) );
  }


  /** As per {@link ResultSet#getDate}. */
  public Date getDate( String pColumnName, Calendar pCalendar  ) throws SQLException {
    return getDate( findColumn(pColumnName), pCalendar );
  }


  /** As per {@link ResultSet#getDate}. */
  public Date getDate( int pColumnIndex  ) throws SQLException {
    return getDate( pColumnIndex, null );
  }


  /** As per {@link ResultSet#getDate}. Ignores <code>pCalendar</code>. */
  public Date getDate( int pColumnIndex, Calendar pCalendar  ) throws SQLException {
    try {
      java.util.Date d = iDateFormat.parse( iLine[pColumnIndex-1] );
      return new Date( d.getTime() );
    }
    catch( Exception e ) {
      throw new SQLException( e.getMessage() );
    }
  }


  /** As per {@link ResultSet#getTime}. */
  public Time getTime( String pColumnName  ) throws SQLException {
    return getTime(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getTime}. */
  public Time getTime( String pColumnName, Calendar pCalendar  ) throws SQLException {
    return getTime(findColumn(pColumnName), null );
  }


  /** As per {@link ResultSet#getTime}. */
  public Time getTime( int pColumnIndex  ) throws SQLException {
    return getTime( pColumnIndex, null );
  }


  /** As per {@link ResultSet#getTime}. Ignores <code>Calendar</code>. */
  public Time getTime( int pColumnIndex, Calendar pCalendar  ) throws SQLException {
    return new Time( getLong(pColumnIndex) );
  }


  /** As per {@link ResultSet#getTimestamp}. */
  public Timestamp getTimestamp( String pColumnName  ) throws SQLException {
    return getTimestamp(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getTimestamp}. */
  public Timestamp getTimestamp( String pColumnName, Calendar pCalendar  )  throws SQLException {
    return getTimestamp(findColumn(pColumnName),null);
  }


  /** As per {@link ResultSet#getTimestamp}. */
  public Timestamp getTimestamp( int pColumnIndex  ) throws SQLException {
    return getTimestamp(pColumnIndex,null);
  }


  /** As per {@link ResultSet#getTimestamp}. Ignores <code>Calendar</code>. */
  public Timestamp getTimestamp( int pColumnIndex, Calendar pCalendar  )  throws SQLException {
    try {
      java.util.Date d = iDateFormat.parse( iLine[pColumnIndex-1] );
      return new Timestamp( d.getTime() );
    }
    catch( Exception e ) {
      throw new SQLException( e.getMessage() );
    }
  }


  /** As per {@link ResultSet#getLong}. */
  public long getLong( String pColumnName  ) throws SQLException {
    return getLong(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getLong}. */
  public long getLong( int pColumnIndex  ) throws SQLException {
    return Long.parseLong( iLine[pColumnIndex-1] );
  }


  /** As per {@link ResultSet#getShort}. */
  public short getShort( String pColumnName  ) throws SQLException {
    return getShort(findColumn(pColumnName));
  }


  /** As per {@link ResultSet#getShort}. */
  public short getShort( int pColumnIndex  ) throws SQLException {
    return Short.parseShort( iLine[pColumnIndex-1] );
  }



  // additional

  /** Specify custom date format.
   *  @param pDateFormat custom data format
   */
  public void setDateFormat( DateFormat pDateFormat ) {
    iDateFormat = (DateFormat) Internal.null_arg( pDateFormat );
  }


  // protected 

  /** Move to correct data line. */
  protected boolean moveToLine() {
    boolean inside = -1 < iLineIndex && iLineIndex < iData.size(); 
    if( inside ) {
      iLine = (String[]) iData.get( (int) iLineIndex );
    }
    return inside;
  }

}










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