/*
 * 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 java.sql.Types;

class IntegerColumn extends Column
{
	static final int JDBC_TYPE_INT = Types.INTEGER;
	static final int JDBC_TYPE_LONG = Types.BIGINT;
	
	final int precision;
	final boolean longInsteadOfInt;
	final int[] allowedValues;

	IntegerColumn(final Table table, final String id, 
					  final boolean optional, final int precision,
					  final boolean longInsteadOfInt, final int[] allowedValues)
	{
		super(table, id, false, optional, longInsteadOfInt ? JDBC_TYPE_LONG : JDBC_TYPE_INT);
		this.precision = precision;
		this.longInsteadOfInt = longInsteadOfInt;
		this.allowedValues = allowedValues;

		if(allowedValues!=null)
		{
		 	if(allowedValues.length<2)
				throw new RuntimeException(id);
			
			// ensure, that allowedValues are unique and ordered
			int current = Integer.MIN_VALUE;
			for(int i = 0; i<allowedValues.length; i++)
			{
				if(current>=allowedValues[i])
					throw new RuntimeException();
				current = allowedValues[i];
			}
		}
	}

	/**
	 * Creates a primary key column.
	 */	
	IntegerColumn(final Table table)
	{
		// IMPLEMENTATION NOTE
		//
		// In theory, one could specify different column names
		// for the primary key of different tables here, and the framework
		// should work as well. I tried this once (uncomment the line below),
		// and it did pass all tests.
		//
		//super(table, Table.PK_COLUMN_NAME+table.id, true, true, JDBC_TYPE_INT);

		super(table, Table.PK_COLUMN_NAME, true, true, JDBC_TYPE_INT);

		this.precision = ItemColumn.SYNTETIC_PRIMARY_KEY_PRECISION;
		this.longInsteadOfInt = false;
		this.allowedValues = null;
	}
	
	final String getDatabaseType()
	{
		return table.database.getIntegerType(precision); 
	}

	final String getCheckConstraintIfNotNull()
	{
		if(allowedValues!=null)
		{
			final StringBuffer bf = new StringBuffer();
			bf.append(protectedID + " IN (");

			for(int j = 0; j<allowedValues.length; j++)
			{
				if(j>0)
					bf.append(',');
				bf.append(allowedValues[j]);
			}
			bf.append(')');
			return bf.toString();
		}
		else
			return null;
	}
	
	final void load(final ResultSet resultSet, final int columnIndex, final Row row)
			throws SQLException
	{
		final Object loadedInteger = resultSet.getObject(columnIndex);
		//System.out.println("IntegerColumn.load "+trimmedName+" "+loadedInteger);
		// TODO: should have small numbers (or even bits) in cache instead of full integers if allowedValues!=null
		row.put(this, (loadedInteger!=null) ? convertSQLResult(loadedInteger) : null);
	}

	final String cacheToDatabase(final Object cache)
	{
		if(cache==null)
			return "NULL";
		else
		{
			if(longInsteadOfInt)
				return ((Long)cache).toString();
			else
				return ((Integer)cache).toString();
		}
	}
	
	Object cacheToDatabasePrepared(final Object cache)
	{
		return longInsteadOfInt ? (Long)cache : (Object)(Integer)cache;
	}
	
	final Object getCheckValue()
	{
		return longInsteadOfInt ? (Object)Long.valueOf(1) : Integer.valueOf(1);
	}
	
	private final Number convertSQLResult(final Object sqlInteger)
	{
		// IMPLEMENTATION NOTE for Oracle
		// Whether the returned object is an Integer or a BigDecimal,
		// depends on whether OracleStatement.defineColumnType is used or not,
		// so we support both here.
		if(sqlInteger instanceof BigDecimal)
		{
			// TODO SOON remove this branch
			if (longInsteadOfInt)
				return Long.valueOf(((BigDecimal)sqlInteger).longValue());
			else
				return Integer.valueOf(((BigDecimal)sqlInteger).intValue());
		}
		else
		{
			if (longInsteadOfInt)
			{
				if(sqlInteger instanceof Long)
					return (Long)sqlInteger;
				else
					return Long.valueOf(((Number)sqlInteger).longValue()); // for SumAggregate
			}
			else
			{
				if(sqlInteger instanceof Integer)
					return (Integer)sqlInteger;
				else
					return Integer.valueOf(((Number)sqlInteger).intValue()); // for SumAggregate
			}
		}
	}

}

@@ -826,37 +826,8 @@
 e;%0A%0A
-import java.math.BigDecimal;%0A
 impo
@@ -4104,255 +4104,149 @@
 .%0A%09%09
-if(sqlInteger instanceof BigDecimal)%0A%09%09%7B
+//%0A%09%09// IMPLEMENTATION NOTE for MySQL
 %0A%09%09
-%09
 // 
-TODO SOON remove this branch%0A%09%09%09if (long
+A SumAggregate across an 
 In
-s
 te
-adOfInt)%0A%09%09%09%09return Long.valueOf(((BigDecimal)sqlInteger).longValue());%0A%09%09%09else%0A%09%09%09%09return Integer.valueOf(((BigDecimal)sqlInteger).intValue());%0A%09%09%7D%0A%09%09else
+gerFunction may return Longs or Doubles,%0A%09%09// so we support all here.
 %0A%09%09%7B
@@ -4409,36 +4409,16 @@
 alue());
- // for SumAggregate
 %0A%09%09%09%7D%0A%09%09
@@ -4500,32 +4500,32 @@
 teger;%0A%09%09%09%09else%0A
+
 %09%09%09%09%09return Inte
@@ -4573,28 +4573,8 @@
 ());
- // for SumAggregate
 %0A%09%09%09

remove redundant extra handling for BigDecimal