/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.exedio.cope.function.PlusView;
import com.exedio.cope.search.GreaterCondition;
import com.exedio.cope.search.GreaterEqualCondition;
import com.exedio.cope.search.LessCondition;
import com.exedio.cope.search.LessEqualCondition;

public abstract class IntegerView
	extends StaticView<Integer>
	implements IntegerFunction
{
	public IntegerView(
			final Function<? extends Object>[] sources,
			final String[] sqlFragments,
			final String functionName)
	{
		super(sources, sqlFragments, functionName, IntegerColumn.JDBC_TYPE_INT);
	}

	final Object load(final ResultSet resultSet, final int columnIndex)
	throws SQLException
	{
		final Object loadedInteger = resultSet.getObject(columnIndex);
		//System.out.println("IntegerView.load "+functionName+" "+loadedInteger+" "+(loadedInteger==null?"null":loadedInteger.getClass().getName()));
		if(loadedInteger!=null)
		{
			if(loadedInteger instanceof BigDecimal)
				return Integer.valueOf(((BigDecimal)loadedInteger).intValue());
			else if(loadedInteger instanceof Long)
				return Integer.valueOf(((Long)loadedInteger).intValue());
			else
				return (Integer)loadedInteger;
		}
		else
			return null;
	}

	final String surface2Database(final Object value)
	{
		if(value==null)
			return "NULL";
		else
			return ((Integer)value).toString();
	}
	
	final void surface2DatabasePrepared(final Statement bf, final Object value)
	{
		bf.appendParameter(((Integer)value).intValue());
	}
	
	public final Integer get(final Item item)
	{
		return (Integer)getObject(item);
	}
	
	public final EqualCondition equal(final int value)
	{
		return new EqualCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final NotEqualCondition notEqual(final int value)
	{
		return new NotEqualCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final LessCondition less(final int value)
	{
		return new LessCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final LessEqualCondition lessOrEqual(final int value)
	{
		return new LessEqualCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final GreaterCondition greater(final int value)
	{
		return new GreaterCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final GreaterEqualCondition greaterOrEqual(final int value)
	{
		return new GreaterEqualCondition<Integer>(this, Integer.valueOf(value));
	}
	
	public final PlusView plus(final IntegerFunction other)
	{
		return new PlusView(new IntegerFunction[]{this, other});
	}
	
	/**
	 * @deprecated use {@link #plus(IntegerFunction)}.
	 */
	@Deprecated
	public final PlusView sum(final IntegerFunction other)
	{
		return sum(other);
	}
}

@@ -3522,19 +3522,20 @@
 %09return 
-sum
+plus
 (other);

fix infinite recursion in deprecated code