/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.data.oda.adapter.dtp;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

import org.eclipse.datatools.connectivity.oda.IParameterMetaData;
import org.eclipse.datatools.connectivity.oda.IResultSet;
import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
import org.eclipse.datatools.connectivity.oda.IQuery;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.SortSpec;

/**
 * Query is the DTP ODA adapter for the BIRT ODA driver interface IQuery.
 */
public class Query implements IQuery
{
    private org.eclipse.birt.data.oda.IQuery m_birtQuery = null;
    
    private Query()
    {        
    }

	/**
	 * Creates a ParameterRowSet adapter for the specified object.
	 * @param birtQuery			BIRT ODA IParameterRowSet interface implementation.
	 * @throws OdaException		if data source error occurs.
	 */
	public Query( org.eclipse.birt.data.oda.IQuery birtQuery ) throws OdaException
	{
	    m_birtQuery = birtQuery;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#prepare(java.lang.String)
	 */
	public void prepare( String query ) throws OdaException
	{
	    try
        {
            m_birtQuery.prepare( query );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );            
        }
	}

	/* (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setAppContext(java.lang.Object)
	 */
	public void setAppContext( Object context ) throws OdaException
	{
	    // do nothing; ODA 2.0 driver does not support pass-through context
	}	

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setProperty(java.lang.String,
	 *      java.lang.String)
	 */
	public void setProperty( String name, String value ) throws OdaException
	{
		try
        {
            m_birtQuery.setProperty( name, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );            
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#close()
	 */
	public void close() throws OdaException
	{
	    try
        {
            m_birtQuery.close();
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );            
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setMaxRows(int)
	 */
	public void setMaxRows( int max ) throws OdaException
	{
	    try
        {
            m_birtQuery.setMaxRows( max );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );           
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#getMaxRows()
	 */
	public int getMaxRows() throws OdaException
	{
	    try
        {
            return m_birtQuery.getMaxRows();
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#getMetaData()
	 */
	public IResultSetMetaData getMetaData() throws OdaException
	{
	    try
        {
            return new ResultSetMetaData( m_birtQuery.getMetaData() );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#executeQuery()
	 */
	public IResultSet executeQuery() throws OdaException
	{
		try
        {
            return new ResultSet( m_birtQuery.executeQuery() );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(java.lang.String, int)
	 */
	public void setInt( String parameterName, int value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setInt( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(int, int)
	 */
	public void setInt( int parameterId, int value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setInt( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(java.lang.String,
	 *      double)
	 */
	public void setDouble( String parameterName, double value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setDouble( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(int, double)
	 */
	public void setDouble( int parameterId, double value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setDouble( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(java.lang.String,
	 *      java.math.BigDecimal)
	 */
	public void setBigDecimal( String parameterName, BigDecimal value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setBigDecimal( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(int,
	 *      java.math.BigDecimal)
	 */
	public void setBigDecimal( int parameterId, BigDecimal value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setBigDecimal( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(java.lang.String,
	 *      java.lang.String)
	 */
	public void setString( String parameterName, String value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setString( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(int,
	 *      java.lang.String)
	 */
	public void setString( int parameterId, String value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setString( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(java.lang.String,
	 *      java.sql.Date)
	 */
	public void setDate( String parameterName, Date value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setDate( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(int, java.sql.Date)
	 */
	public void setDate( int parameterId, Date value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setDate( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(java.lang.String,
	 *      java.sql.Time)
	 */
	public void setTime( String parameterName, Time value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setTime( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(int, java.sql.Time)
	 */
	public void setTime( int parameterId, Time value ) throws OdaException
	{
	    try
        {
            m_birtQuery.setTime( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(java.lang.String,
	 *      java.sql.Timestamp)
	 */
	public void setTimestamp( String parameterName, Timestamp value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setTimestamp( parameterName, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(int,
	 *      java.sql.Timestamp)
	 */
	public void setTimestamp( int parameterId, Timestamp value )
			throws OdaException
	{
	    try
        {
            m_birtQuery.setTimestamp( parameterId, value );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#findInParameter(java.lang.String)
	 */
	public int findInParameter( String parameterName ) throws OdaException
	{
	    try
        {
            return m_birtQuery.findInParameter( parameterName );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#getParameterMetaData()
	 */
	public IParameterMetaData getParameterMetaData() throws OdaException
	{
	    try
        {
            return new ParameterMetaData( m_birtQuery.getParameterMetaData() );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#clearInParameters()
	 */
	public void clearInParameters() throws OdaException
	{
	    try
        {
            m_birtQuery.clearInParameters();
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#setSortSpec(org.eclipse.birt.data.oda.SortSpec)
	 */
	public void setSortSpec( SortSpec sortBy ) throws OdaException
	{  
        try
        {
            m_birtQuery.setSortSpec( dtpToBirtSortSpec( sortBy ) );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.datatools.connectivity.oda.IQuery#getSortSpec()
	 */
	public SortSpec getSortSpec() throws OdaException
	{
	    try
        {
	        return birtToDtpSortSpec( m_birtQuery.getSortSpec() );
        }
        catch( org.eclipse.birt.data.oda.OdaException e )
        {
            throw new OdaAdapterException( e );
        }
	}

	/**
	 * Returns a DTP SortSpec, converted from that of the BIRT namespace.
	 */
	SortSpec birtToDtpSortSpec( org.eclipse.birt.data.oda.SortSpec birtSortSpec )
	{    
	    if ( birtSortSpec == null )
	        return null;
	    
        SortSpec dtpSortSpec = new SortSpec( birtSortSpec.getSortMode() );
        
        int numBirtSortKeys = birtSortSpec.getSortKeyCount();
        for ( int i = 1; i <= numBirtSortKeys; i++ )
        {
            dtpSortSpec.addSortKey( birtSortSpec.getSortColumn( i ),
                    				birtSortSpec.getSortOrder( i ) );
        }
        
        return dtpSortSpec;
	}
	
	/**
	 * Returns a BIRT SortSpec, converted from that of the DTP namespace.
	 */
	org.eclipse.birt.data.oda.SortSpec dtpToBirtSortSpec( SortSpec dtpSortSpec )
	{
	    if ( dtpSortSpec == null )
	        return null;
	    
	    org.eclipse.birt.data.oda.SortSpec birtSortSpec = 
	        new org.eclipse.birt.data.oda.SortSpec( dtpSortSpec.getSortMode() );
	    
        int numDtpSortKeys = dtpSortSpec.getSortKeyCount();
	    for ( int i = 1; i <= numDtpSortKeys; i++ )
	    {
	        birtSortSpec.addSortKey( dtpSortSpec.getSortColumn( i ), 
	                				 dtpSortSpec.getSortOrder( i ) );
	    }
	    return birtSortSpec;
	}
	
}
