
package org.eclipse.birt.tests.data.engine.acceptance;

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IPreparedQuery;
import org.eclipse.birt.data.engine.api.IQueryResults;
import org.eclipse.birt.data.engine.api.IResultIterator;
import org.eclipse.birt.data.engine.api.querydefn.BaseExpression;
import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ParameterDefinition;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

public class ParameterInt extends APITestCase
{

	public BaseExpression expressions;

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData.TableName" ), ConfigText
				.getString( "Api.TestData.TableSQL" ), ConfigText
				.getString( "Api.TestData.TestDataFileName" ) );
	}

	/**
	 * filter on date time with not between operator
	 * 
	 * @throws Exception
	 */
	public void testFilterNotBetween( ) throws Exception
	{
		String[] bindingNameRow = new String[2];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_SALE_DATE";

		IBaseExpression[] bindingExprRow = new IBaseExpression[]{
				new ScriptExpression( "dataSetRow.COUNTRY", 0 ),
				new ScriptExpression( "dataSetRow.SALE_DATE", 6 ),};

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_SALE_DATE";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression( "dataSetRow.SALE_DATE" );
		FilterDefinition[] filters = new FilterDefinition[]{new FilterDefinition(
				new ConditionalExpression( "row.FILTER_SALE_DATE",
						ConditionalExpression.OP_NOT_BETWEEN, "'05/01/2004'",
						"'06/01/2004'" ) )};

		createAndRunQuery( null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow );
		checkOutputFile( );
	}

	/**
	 * parameter with greater than operator
	 * 
	 * @throws Exception
	 */
	public void testParamGreater( ) throws Exception
	{
		String testSql = "select COUNTRY,AMOUNT from " + getTestTableName( )
				+ " where amount > ?";
		( (OdaDataSetDesign) this.dataSet ).setQueryText( testSql );

		ParameterDefinition paramDefn = new ParameterDefinition( );
		paramDefn.setInputMode( true );
		paramDefn.setName( "param1" );
		paramDefn.setPosition( 1 );
		paramDefn.setDefaultInputValue( "1000" );
		paramDefn.setType( 2 );
		( (OdaDataSetDesign) this.dataSet ).addParameter( paramDefn );
		String[] bindingNameRow = new String[2];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_SALE_DATE";

		IBaseExpression[] bindingExprRow = new IBaseExpression[]{
				new ScriptExpression( "dataSetRow.COUNTRY", 0 ),
				new ScriptExpression( "dataSetRow.AMOUNT", 0 ),};

		createAndRunQuery( null, null, null, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );
		checkOutputFile( );

	}

	/**
	 * filter on date time with not between operator
	 * 
	 * @throws Exception
	 */
	public void testFilterInvalidExpre( ) throws Exception
	{ // TODO:Need modify
		BaseExpression[] expressions = new BaseExpression[]{
				new ScriptExpression( "row.COUNTRY", 0 ),
				new ScriptExpression( "row.SALE_DATE", 6 )};
		FilterDefinition filterDefn = new FilterDefinition(
				new ConditionalExpression( "row.SALE_DATE",
						ConditionalExpression.OP_NOT_BETWEEN, "'05/01/2004'",
						"abcd" ) );

		QueryDefinition queryDefn = new QueryDefinition( );

		queryDefn.setDataSetName( this.dataSet.getName( ) );

		queryDefn.addFilter( filterDefn );

		for ( int i = 0; i < expressions.length; i++ )
		{
			queryDefn.addResultSetExpression( expressions[i].toString( ),
					expressions[i] );
		}
		IPreparedQuery preparedQuery = dataEngine.prepare( queryDefn );
		try
		{
			IQueryResults queryResults = preparedQuery.execute( null );
			IResultIterator resultIt = queryResults.getResultIterator( );
			fail( "should throw out exception" );
		}
		catch ( Exception e )
		{

		}

	}

}
