All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitbucket.bradleysmithllc.etlunit.feature.database.JDBCClientImpl Maven / Gradle / Ivy

There is a newer version: 4.6.0-eu
Show newest version
package org.bitbucket.bradleysmithllc.etlunit.feature.database;

import org.bitbucket.bradleysmithllc.etlunit.TestExecutionError;

import javax.inject.Inject;
import java.sql.*;

public class JDBCClientImpl implements JDBCClient
{
	private DatabaseFeatureModule databaseFeatureModule;

	@Inject
	public void setDatabaseFeatureModule(DatabaseFeatureModule databaseFeatureModule)
	{
		this.databaseFeatureModule = databaseFeatureModule;
	}

	public void useConnection(DatabaseConnection connection, String mode, ConnectionClient client) throws TestExecutionError
	{
		useConnection(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void useConnection(DatabaseConnection connection, String mode, ConnectionClient client, int id) throws TestExecutionError
	{
		DatabaseImplementation dbConn = databaseFeatureModule.getImplementation(connection.getId());

		try
		{
			Connection sqlConn = dbConn.getConnection(connection, mode, id);

			try
			{
				client.connection(sqlConn, connection, mode, id);
			}
			finally
			{
				dbConn.returnConnection(sqlConn, connection, mode, id);
			}
		}
		catch (SQLException exc)
		{
			throw new TestExecutionError("", DatabaseConstants.ERR_SQL_FAILURE, exc);
		}
		catch (TestExecutionError exc)
		{
			throw exc;
		}
		catch (Exception exc)
		{
			throw new TestExecutionError("", exc);
		}
	}

	public void useStatement(DatabaseConnection connection, String mode, StatementClient client) throws TestExecutionError
	{
		useStatement(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void useStatement(DatabaseConnection connection, String mode, final StatementClient client, int id) throws TestExecutionError
	{
		useConnection(connection, mode, new ConnectionClient()
		{
			public void connection(Connection conn, DatabaseConnection connection, String mode, int id) throws Exception
			{
				Statement st = conn.createStatement();

				try
				{
					client.connection(conn, st, connection, mode, id);
				}
				finally
				{
					st.close();
				}
			}
		}, id);
	}

	public void usePreparedStatement(DatabaseConnection connection, String mode, PreparedStatementClient client) throws TestExecutionError
	{
		usePreparedStatement(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}

	public void usePreparedStatement(DatabaseConnection connection, String mode, final PreparedStatementClient client, int id) throws TestExecutionError
	{
		useConnection(connection, mode, new ConnectionClient()
		{
			public void connection(Connection conn, DatabaseConnection connection, String mode, int id) throws Exception
			{
				PreparedStatement st = conn.prepareStatement(client.prepareText());

				try
				{
					client.connection(conn, st, connection, mode, id);
				}
				finally
				{
					st.close();
				}
			}
		}, id);
	}

	@Override
	public void useCallableStatement(final DatabaseConnection connection, final String mode, final CallableStatementClient client, int id) throws TestExecutionError
	{
		useConnection(connection, mode, new ConnectionClient()
		{
			public void connection(Connection conn, DatabaseConnection connection, String mode, int id) throws Exception
			{
				DatabaseMetaData metaData = conn.getMetaData();
				if (!metaData.supportsStoredProcedures())
				{
					throw new IllegalStateException("Database [" + metaData.getDatabaseProductName() + "] does not support stored procedures");
				}

				CallableStatement st = conn.prepareCall(client.callText());

				try
				{
					client.connection(conn, st, connection, mode, id);
				}
				finally
				{
					st.close();
				}
			}
		}, id);
	}

	@Override
	public void useCallableStatement(DatabaseConnection connection, String mode, CallableStatementClient client) throws TestExecutionError
	{
		useCallableStatement(connection, mode, client, DatabaseImplementation.DEFAULT_ID);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy