package com.exedio.cope.lib;

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

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

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

	/**
	 * Creates a primary key column.
	 */	
	IntegerColumn(final Table table) 
	{
		super(table, "PK", 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 getAllowedValuesConstraintID()
	{
		if(allowedValues==null)
			throw new RuntimeException(id);

		return table.database.trimName(table.id + "_" + id+ "_Val");
	}
	
	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);
		if(loadedInteger!=null)
		{
			row.load(this, convertSQLResult(loadedInteger));
		}
	}

	final Object cacheToDatabase(final Object cache)
	{
		if(cache==null)
			return "NULL";
		else
		{
			if(longInsteadOfInt)
				return ((Long)cache).toString();
			else
				return ((Integer)cache).toString();
		}
	}
	
	final long 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)
			return ((BigDecimal)sqlInteger).intValue();
		else
		{
			if (longInsteadOfInt)
				return ((Long)sqlInteger).longValue();
			else
				return ((Integer)sqlInteger).longValue();
		}
	}

}

@@ -845,12 +845,586 @@
 ble)
- 
 %0A%09%7B
+%0A%09%09// IMPLEMENTATION NOTE%0A%09%09//%0A%09%09// The following line specifies the column name for the generated%0A%09%09// primary key of all cope tables to be %22this%22. This prevents%0A%09%09// name collisions with columns for cope attributes, since %22this%22%0A%09%09// is a reserved java keyword, which cannot be used for java attributes.%0A%09%09//%0A%09%09// It's a string literal, since the string is not used anywhere else%0A%09%09// in the framework. In theory, one could specify different column names%0A%09%09// for the primary key of different tables here, and the framework%0A%09%09// should work as well. But I never tried this.
 %0A%09%09s
@@ -1440,10 +1440,12 @@
 e, %22
-PK
+this
 %22, t
@@ -1463,32 +1463,33 @@
 JDBC_TYPE_INT);%0A
+%0A
 %09%09this.precision

use this for primary key column