/*
 * JBoss, Home of Professional Open Source
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * 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,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * (C) 2006,
 * @author JBoss Inc.
 *
 * $Id$
 */

package com.arjuna.ats.internal.jdbc;

import com.arjuna.ats.jdbc.logging.jdbcLogger;

import java.sql.*;
import java.util.Properties;

/**
 * JDBC 3.0 extention to the JDBC 2.0 Connection wrapper
 */
public class ConnectionImpleJDBC3 extends ConnectionImple implements Connection
{
	public ConnectionImpleJDBC3(String dbName, Properties info) throws SQLException {
		super(dbName, info);
	}

	public void setHoldability(int holdability) throws SQLException
	{
		checkTransaction();

		registerDatabase();

		getConnection().setHoldability(holdability);
	}

	public int getHoldability() throws SQLException
	{
		return getConnection().getHoldability();
	}

	// The JDBC 3.0 spec (section 12.4) prohibits calling setSavepoint indide an XA tx.
	// It does not explicitly disallow calling rollback(savepoint) or releaseSavepoint(savepoint)
	// but allowing them does not make a whole lot of sense, so we don't:

	/**
	 * @message com.arjuna.ats.internal.jdbc.setsavepointerror setSavepoint not allowed inside distributed tx.
	 */
	public Savepoint setSavepoint() throws SQLException
	{
		if (transactionRunning())
		{
			throw new SQLException(jdbcLogger.logMesg.getString("com.arjuna.ats.internal.jdbc.setsavepointerror"));
		}
		else
		{
			return getConnection().setSavepoint();
		}
	}

	public Savepoint setSavepoint(String name) throws SQLException
	{
		if (transactionRunning())
		{
			throw new SQLException(jdbcLogger.logMesg.getString("com.arjuna.ats.internal.jdbc.setsavepointerror"));
		}
		else
		{
			return getConnection().setSavepoint(name);
		}
	}

	/**
	 * @message com.arjuna.ats.internal.jdbc.rollbacksavepointerror rollback(Savepoint) not allowed inside distributed tx.
	 */
	public void rollback(Savepoint savepoint) throws SQLException
	{
		if (transactionRunning())
		{
			throw new SQLException(jdbcLogger.logMesg.getString("com.arjuna.ats.internal.jdbc.rollbacksavepointerror"));
		}
		else
		{
			getConnection().rollback(savepoint);
		}
	}

	/**
	 * @message com.arjuna.ats.internal.jdbc.releasesavepointerror rollback(Savepoint) not allowed inside distributed tx.
	 */
	public void releaseSavepoint(Savepoint savepoint) throws SQLException
	{
		if (transactionRunning())
		{
			throw new SQLException(jdbcLogger.logMesg.getString("com.arjuna.ats.internal.jdbc.releasesavepointerror"));
		}
		else
		{
			getConnection().releaseSavepoint(savepoint);
		}
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency,
									 int resultSetHoldability) throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
											  int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
										 int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().prepareStatement(sql, autoGeneratedKeys);
	}

	public PreparedStatement prepareStatement(String sql, int columnIndexes[])
			throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().prepareStatement(sql, columnIndexes);
	}

	public PreparedStatement prepareStatement(String sql, String columnNames[])
			throws SQLException
	{
		checkTransaction();

		registerDatabase();

		return getConnection().prepareStatement(sql, columnNames);
	}
}
